Deepa Vaddireddy
Committed by Thomas Vachuska

Fix for JIRA ONOS-4620. Whenever networkconfiguration is deleted even the queued will be removed

Change-Id: I8d7f1a873af90cf86ea34f1a2b1585ef4c3e46e4
......@@ -177,4 +177,19 @@ public interface NetworkConfigService
* @param <S> type of subject
*/
<S> void removeConfig(String subjectClassKey, S subject, String configKey);
/**
* Clears the configuration including queued based on the subject.
* If does not exists this call has no effect.
*
* @param subject configuration subject
*/
<S> void removeConfig(S subject);
/**
* Clears the complete configuration including queued.
* If does not exists this call has no effect.
*
*/
<S> void removeConfig();
}
......
......@@ -151,4 +151,19 @@ public interface NetworkConfigStore extends Store<NetworkConfigEvent, NetworkCon
*/
<S> void clearQueuedConfig(S subject, String configKey);
/**
* Clears the configuration based on the subject including queued.
* If does not exists this call has no effect.
*
* @param subject configuration subject
*/
<S> void clearConfig(S subject);
/**
* Clears the complete configuration including queued.
* If does not exists this call has no effect.
*
*/
<S> void clearConfig();
}
......
......@@ -95,4 +95,12 @@ public class NetworkConfigServiceAdapter implements NetworkConfigService {
@Override
public void removeListener(NetworkConfigListener listener) {
}
@Override
public <S> void removeConfig(S subject) {
}
@Override
public <S> void removeConfig() {
}
}
......
......@@ -261,6 +261,18 @@ public class NetworkConfigManager
}
}
@Override
public <S> void removeConfig(S subject) {
checkPermission(CONFIG_WRITE);
store.clearConfig(subject);
}
@Override
public <S> void removeConfig() {
checkPermission(CONFIG_WRITE);
store.clearConfig();
}
// Auxiliary store delegate to receive notification about changes in
// the network configuration store state - by the store itself.
private class InternalStoreDelegate implements NetworkConfigStoreDelegate {
......
......@@ -239,4 +239,43 @@ public class NetworkConfigManagerTest {
assertThat(configService.getSubjectFactory(String.class), notNullValue());
assertThat(configService.getSubjectFactory("key1"), notNullValue());
}
/**
* Tests creation, query and removal of a configuration including queued.
*/
@Test
public void testRemoveConfig() {
assertThat(configService.getSubjectFactory(String.class), nullValue());
assertThat(configService.getSubjectFactory("key"), nullValue());
registry.registerConfigFactory(config1Factory);
registry.registerConfigFactory(config2Factory);
configService.applyConfig("configKey", BasicConfig1.class, new ObjectMapper().createObjectNode());
configService.applyConfig("key1", "key", "config1", new ObjectMapper().createObjectNode());
configService.applyConfig("key1", "keyxx", "config3", new ObjectMapper().createObjectNode());
configService.applyConfig("key2", "key1", "config4", new ObjectMapper().createObjectNode());
configService.removeConfig();
Set<String> subjects = configService.getSubjects(factory1.subjectClass());
assertThat(subjects.size(), is(0));
Set<String> subjects2 = configService.getSubjects(factory2.subjectClass());
assertThat(subjects2.size(), is(0));
configService.applyConfig("key1", "key", "config1", new ObjectMapper().createObjectNode());
configService.applyConfig("key1", "keyxx", "config3", new ObjectMapper().createObjectNode());
configService.applyConfig("key1", "key1", "config4", new ObjectMapper().createObjectNode());
@SuppressWarnings("unchecked")
Set<String> configs = configService.getSubjects(
configService.getSubjectFactory("key1").subjectClass());
configs.forEach(c -> configService.removeConfig(c));
Set<String> newConfig1 = configService.getSubjects(factory1.subjectClass());
assertThat(newConfig1, notNullValue());
}
}
......
......@@ -267,6 +267,24 @@ public class DistributedNetworkConfigStore
configs.remove(key(subject, configKey));
}
@Override
public <S> void clearConfig(S subject) {
ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
if (Objects.equals(subject, k.subject) && delegate != null) {
configs.remove(k);
}
});
}
@Override
public <S> void clearConfig() {
ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
if (delegate != null) {
configs.remove(k);
}
});
}
/**
* Produces a config from the specified subject, config class and raw JSON.
*
......
......@@ -31,6 +31,9 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.Set;
public class DistributedNetworkConfigStoreTest {
private DistributedNetworkConfigStore configStore;
......@@ -203,4 +206,33 @@ public class DistributedNetworkConfigStoreTest {
assertThat(configStore.getSubjects(Integer.class, BasicIntConfig.class), hasSize(1));
assertThat(configStore.getSubjects(Integer.class), hasSize(1));
}
/**
* Tests removal of config including queued.
*/
@Test
public void testRemoveConfig() {
configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1"));
configStore.queueConfig("subject", "config2", new ObjectMapper().createObjectNode());
configStore.queueConfig(123, "config2", new ObjectMapper().createObjectNode());
configStore.applyConfig("subject1", BasicConfig.class, new ObjectMapper().createObjectNode());
configStore.clearConfig();
Set<String> subjects = configStore.getSubjects(String.class);
assertThat(subjects.size(), is(0));
configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1"));
configStore.queueConfig("subject", "config3", new ObjectMapper().createObjectNode());
configStore.queueConfig(123, "config3", new ObjectMapper().createObjectNode());
configStore.applyConfig("subject1", BasicConfig.class, new ObjectMapper().createObjectNode());
Set<String> configs = configStore.getSubjects(String.class);
configs.forEach(c -> configStore.clearConfig(c));
Set<String> newConfig1 = configStore.getSubjects(String.class);
assertThat(newConfig1, notNullValue());
}
}
......
......@@ -294,28 +294,22 @@ public class NetworkConfigWebResource extends AbstractWebResource {
@SuppressWarnings("unchecked")
public Response delete() {
NetworkConfigService service = get(NetworkConfigService.class);
service.getSubjectClasses()
.forEach(subjectClass -> service.getSubjects(subjectClass)
.forEach(subject -> service.getConfigs(subject)
.forEach(config -> service.removeConfig(subject, config.getClass()))));
return Response.noContent().build();
service.removeConfig();
return Response.ok().build();
}
/**
* Clear all network configurations for a subject class.
*
* @param subjectClassKey subject class key
* @return 204 NO CONTENT
*/
@DELETE
@Path("{subjectClassKey}")
@SuppressWarnings("unchecked")
public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
public void delete(@PathParam("subjectClassKey") String subjectClassKey) {
NetworkConfigService service = get(NetworkConfigService.class);
service.getSubjects(service.getSubjectFactory(subjectClassKey).subjectClass())
.forEach(subject -> service.getConfigs(subject)
.forEach(config -> service.removeConfig(subject, config.getClass())));
return Response.noContent().build();
.forEach(subject -> service.removeConfig(subject));
}
/**
......@@ -323,17 +317,14 @@ public class NetworkConfigWebResource extends AbstractWebResource {
*
* @param subjectClassKey subjectKey class key
* @param subjectKey subjectKey key
* @return 204 NO CONTENT
*/
@DELETE
@Path("{subjectClassKey}/{subjectKey}")
@SuppressWarnings("unchecked")
public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
public void delete(@PathParam("subjectClassKey") String subjectClassKey,
@PathParam("subjectKey") String subjectKey) {
NetworkConfigService service = get(NetworkConfigService.class);
Object s = service.getSubjectFactory(subjectClassKey).createSubject(subjectKey);
service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
return Response.noContent().build();
service.removeConfig(subjectKey);
}
/**
......@@ -342,18 +333,17 @@ public class NetworkConfigWebResource extends AbstractWebResource {
* @param subjectClassKey subjectKey class key
* @param subjectKey subjectKey key
* @param configKey configuration class key
* @return 204 NO CONTENT
*/
@DELETE
@Path("{subjectClassKey}/{subjectKey}/{configKey}")
@SuppressWarnings("unchecked")
public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
public void delete(@PathParam("subjectClassKey") String subjectClassKey,
@PathParam("subjectKey") String subjectKey,
@PathParam("configKey") String configKey) {
NetworkConfigService service = get(NetworkConfigService.class);
service.removeConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
service.getConfigClass(subjectClassKey, configKey));
return Response.noContent().build();
service.removeConfig(subjectClassKey,
service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
configKey);
}
}
......