From: adetalhouet Date: Thu, 26 Nov 2015 16:08:04 +0000 (-0500) Subject: Rework IOVisor model and validate IovisorModuleInstance on Endpoint created events X-Git-Tag: release/beryllium~61^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=79172809957d79822f8ed59156703daced6b14e3;p=groupbasedpolicy.git Rework IOVisor model and validate IovisorModuleInstance on Endpoint created events Change-Id: Id80f1b58e3b837ce6d17f0ffbf29b999ce1e7b58 Signed-off-by: adetalhouet --- diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistry.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistry.java index e389b1dd3..951f1f8b5 100644 --- a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistry.java +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistry.java @@ -115,8 +115,9 @@ public class EndpointRpcRegistry implements EndpointService, AutoCloseable { rpcRegistration = this.rpcRegistry.addRpcImplementation(EndpointService.class, this); LOG.debug("Added RPC Implementation Correctly"); - } else + } else { rpcRegistration = null; + } if (dataProvider != null) { InstanceIdentifier iid = diff --git a/renderers/iovisor/pom.xml b/renderers/iovisor/pom.xml index 14eedbf0c..e3ecf4d41 100644 --- a/renderers/iovisor/pom.xml +++ b/renderers/iovisor/pom.xml @@ -18,6 +18,7 @@ bundle + 1.5.2 @@ -36,5 +37,23 @@ mockito-all test + + org.powermock + powermock-core + ${powermock.version} + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + diff --git a/renderers/iovisor/src/main/java/org/opendaylight/controller/config/yang/config/iovisor_provider/impl/IovisorProviderModule.java b/renderers/iovisor/src/main/java/org/opendaylight/controller/config/yang/config/iovisor_provider/impl/IovisorProviderModule.java index 5553a1b84..22dd64b26 100644 --- a/renderers/iovisor/src/main/java/org/opendaylight/controller/config/yang/config/iovisor_provider/impl/IovisorProviderModule.java +++ b/renderers/iovisor/src/main/java/org/opendaylight/controller/config/yang/config/iovisor_provider/impl/IovisorProviderModule.java @@ -18,6 +18,6 @@ public class IovisorProviderModule extends org.opendaylight.controller.config.ya @Override public java.lang.AutoCloseable createInstance() { - return new IovisorRenderer(getDataBrokerDependency()); + return new IovisorRenderer(getDataBrokerDependency(), getRpcRegistryDependency()); } } diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/IovisorRenderer.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/IovisorRenderer.java index ddda3f590..1796972db 100644 --- a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/IovisorRenderer.java +++ b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/IovisorRenderer.java @@ -9,6 +9,7 @@ package org.opendaylight.groupbasedpolicy.renderer.iovisor; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; import org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint.EndpointManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,9 +23,9 @@ public class IovisorRenderer implements AutoCloseable { private EndpointManager endPointManager; - public IovisorRenderer(final DataBroker dataBroker) { + public IovisorRenderer(DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) { LOG.info("IOVisor Renderer has Started"); - this.endPointManager = new EndpointManager(dataBroker); + this.endPointManager = new EndpointManager(dataBroker, rpcProviderRegistry); } @Override @@ -33,5 +34,4 @@ public class IovisorRenderer implements AutoCloseable { endPointManager.close(); } } - } diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListener.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListener.java index a48c74110..2e2781e49 100644 --- a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListener.java +++ b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListener.java @@ -16,23 +16,30 @@ import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorModuleUtils; import org.opendaylight.groupbasedpolicy.util.IidFactory; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleAugmentation; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Preconditions; + public class EndpointListener implements DataChangeListener, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(EndpointListener.class); private final ListenerRegistration registerListener; + private DataBroker dataBroker; + public EndpointListener(DataBroker dataBroker) { + this.dataBroker = dataBroker; this.registerListener = dataBroker.registerDataChangeListener( LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class), @@ -56,8 +63,15 @@ public class EndpointListener implements DataChangeListener, AutoCloseable { private void created(Map, DataObject> created) { for (Entry, DataObject> newEndpoint : created.entrySet()) { Endpoint endpoint = fromMd(newEndpoint.getKey(), (Endpoint) newEndpoint.getValue()); - LOG.info("Endpoint CREATED {}", endpoint); - // TODO process created event + LOG.debug("Endpoint CREATED {}", endpoint); + + // Validate the IOVisorModuleInstance + IovisorModuleAugmentation iovisorModuleAugmentation = endpoint.getAugmentation(IovisorModuleAugmentation.class); + Preconditions.checkNotNull(iovisorModuleAugmentation.getUri(), "At this point, the Endpoint should be provided with a IovisorModuleInstance"); + if (IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, iovisorModuleAugmentation.getUri())) { + LOG.debug("This Endpoint {} provides a valid IovisorModuleInstance {}", endpoint, iovisorModuleAugmentation.getUri()); + // TODO process validated endpoint + } } } @@ -70,8 +84,9 @@ public class EndpointListener implements DataChangeListener, AutoCloseable { private void updated(Map, DataObject> updated) { for (Entry, DataObject> updatedEndpoint : updated.entrySet()) { Endpoint endpoint = fromMd(updatedEndpoint.getKey(), (Endpoint) updatedEndpoint.getValue()); - LOG.info("Endpoint UPDATED {}", endpoint); + LOG.debug("Endpoint UPDATED {}", endpoint); //TODO process updated event + } } @@ -84,7 +99,7 @@ public class EndpointListener implements DataChangeListener, AutoCloseable { private void removed(AsyncDataChangeEvent, DataObject> changes) { for (InstanceIdentifier deletedEndpointPath : changes.getRemovedPaths()) { Endpoint endpoint = fromMd(deletedEndpointPath, (Endpoint) changes.getOriginalData().get(deletedEndpointPath)); - LOG.info("Endpoint REMOVED {}", endpoint); + LOG.debug("Endpoint REMOVED {}", endpoint); // TODO process removed event } } @@ -98,11 +113,10 @@ public class EndpointListener implements DataChangeListener, AutoCloseable { * Endpoint from the related event * @return Endpoint constructed from the one gathered in the related event */ - @SuppressWarnings("deprecation") private Endpoint fromMd(InstanceIdentifier iid, Endpoint endpoint) { EndpointBuilder result = new EndpointBuilder(); - final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class, EndpointKey.class); + final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class); if (endpointKey != null) { result.setKey(endpointKey); } diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManager.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManager.java index 1c8ffa0ff..6593852b9 100644 --- a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManager.java +++ b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManager.java @@ -8,22 +8,39 @@ package org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint; +import java.util.Map.Entry; + import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.endpoint.EndpointRpcRegistry; +import org.opendaylight.groupbasedpolicy.endpoint.EpRendererAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix; +import org.opendaylight.yangtools.yang.binding.Augmentation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Preconditions; -public class EndpointManager implements AutoCloseable { +public class EndpointManager implements EpRendererAugmentation, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(EndpointManager.class); + + private EndpointRpcRegistry endpointRpcRegistry; private EndpointListener endpointListener; - public EndpointManager(DataBroker dataBroker) { + public EndpointManager(DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) { LOG.debug("Initialized IOVisor EndpointManager"); - Preconditions.checkNotNull("DataBroker instance must not be null", dataBroker); + Preconditions.checkNotNull(dataBroker, "DataBroker instance must not be null"); + Preconditions.checkNotNull(rpcProviderRegistry, "RpcProviderRegistry instance must not be null"); + this.endpointRpcRegistry = new EndpointRpcRegistry(dataBroker, rpcProviderRegistry); this.endpointListener = new EndpointListener(dataBroker); + + EndpointRpcRegistry.register(this); } @Override @@ -31,5 +48,30 @@ public class EndpointManager implements AutoCloseable { if (endpointListener != null) { endpointListener.close(); } + if (endpointRpcRegistry != null) { + endpointRpcRegistry.close(); + } + EndpointRpcRegistry.unregister(this); + } + + @Override + public Entry>, Augmentation> buildEndpointAugmentation( + RegisterEndpointInput input) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Entry>, Augmentation> buildEndpointL3Augmentation( + RegisterEndpointInput input) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Entry>, Augmentation> buildL3PrefixEndpointAugmentation( + RegisterL3PrefixEndpointInput input) { + // TODO Auto-generated method stub + return null; } } \ No newline at end of file diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactory.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactory.java new file mode 100644 index 000000000..ea21f6a19 --- /dev/null +++ b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactory.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.utils; + +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + + +public class IovisorIidFactory { + + private IovisorIidFactory() { + } + + private static final InstanceIdentifier IOVISOR_MODULE_INSTANCES_IID = InstanceIdentifier.builder(IovisorModuleInstances.class).build(); + + /** + * @return The {@link InstanceIdentifier} of the {@link IovisorModuleInstances} + */ + public static InstanceIdentifier iovisorModuleInstancesIid() { + return IOVISOR_MODULE_INSTANCES_IID; + } + + /** + * Return the InstanceIdentifier for a specific IovisorModuleInstance. + * @param iovisorModuleInstanceKey The key of the {@link IovisorModuleInstance} we want to retrieve. + * @return The {@link InstanceIdentifier} of the {@link IovisorModuleInstance} + */ + public static InstanceIdentifier iovisorModuleInstanceIid(IovisorModuleInstanceKey iovisorModuleInstanceKey) { + return IOVISOR_MODULE_INSTANCES_IID.child(IovisorModuleInstance.class, iovisorModuleInstanceKey); + } +} diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtils.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtils.java new file mode 100644 index 000000000..cfd7e4fb3 --- /dev/null +++ b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtils.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.utils; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.groupbasedpolicy.util.DataStoreHelper; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModule; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance; + +import com.google.common.base.Optional; + +public class IovisorModuleUtils { + + private IovisorModuleUtils() { + } + + /** + * Make sure the specified IOvisor module Uri exists in the datastore. + * @param dataBroker An instance of the {@link DataBroker} + * @param iovisorModuleUri The Uri of the {@link IovisorModule} we want to validate + * @return true if validated, else, false + */ + public static boolean validateIovisorModuleInstance(DataBroker dataBroker, Uri iovisorModuleUri) { + Optional res = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, + IovisorIidFactory.iovisorModuleInstancesIid(), + dataBroker.newReadOnlyTransaction()); + if (res.isPresent()) { + for (IovisorModuleInstance instance : res.get().getIovisorModuleInstance()) { + if (instance.getUri().equals(iovisorModuleUri)) { + return true; + } + } + } + return false; + } +} diff --git a/renderers/iovisor/src/main/yang/iovisor.yang b/renderers/iovisor/src/main/yang/iovisor.yang index b9aa1f5ed..28d612f79 100644 --- a/renderers/iovisor/src/main/yang/iovisor.yang +++ b/renderers/iovisor/src/main/yang/iovisor.yang @@ -24,16 +24,36 @@ module iovisor { "Initial revision."; } - grouping iovisor-module-location { - leaf iom-uri { + typedef iovisor-module-id { + description "Uniquely identifies the IOVisor module"; + type string; + } + + grouping iovisor-module { + leaf uri { type inet:uri; description "IOVisor module uniform resource identifier."; } } - - augment "/endpoint:endpoints" { + + container iovisor-module-instances { + description "IOVisor module inventory."; + list iovisor-module-instance { + key "id"; + config true; + description "This is a list of IOVisor modules."; + + leaf id { + type iovisor-module-id; + } + + uses iovisor-module; + } + } + + augment "/endpoint:endpoints/endpoint:endpoint" { description "Augmentation adding the IOVisor module location"; - ext:augment-identifier "iovior-module-location"; - uses iovisor-module-location; + ext:augment-identifier "iovisor-module-augmentation"; + uses iovisor-module; } } diff --git a/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListenerTest.java b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListenerTest.java new file mode 100644 index 000000000..221393196 --- /dev/null +++ b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointListenerTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; +import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorModuleUtils; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleAugmentation; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.membermodification.MemberMatcher; +import org.powermock.api.support.membermodification.MemberModifier; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({EndpointListener.class, IovisorModuleUtils.class}) +public class EndpointListenerTest { + + @Mock EndpointListener endpointListner; + @Mock DataBroker dataBroker; + @Mock ListenerRegistration registerListener; + + @Mock AsyncDataChangeEvent, DataObject> changes; + + @Before + public void setUp() { + endpointListner = PowerMockito.mock(EndpointListener.class, Mockito.CALLS_REAL_METHODS); + } + + @Test + public void onDataChangedTest() throws Exception { + MemberModifier.suppress(MemberMatcher.method(EndpointListener.class, "created", Map.class)); + MemberModifier.suppress(MemberMatcher.method(EndpointListener.class, "updated", Map.class)); + MemberModifier.suppress(MemberMatcher.method(EndpointListener.class, "removed", AsyncDataChangeEvent.class)); + + endpointListner.onDataChanged(changes); + + PowerMockito.verifyPrivate(endpointListner, times(1)).invoke("created", any(Map.class)); + PowerMockito.verifyPrivate(endpointListner, times(1)).invoke("updated", any(Map.class)); + PowerMockito.verifyPrivate(endpointListner, times(1)).invoke("removed", any(AsyncDataChangeEvent.class)); + } + + @SuppressWarnings("unchecked") + @Test + public void createdTest() throws Exception { + InstanceIdentifier endpointIid = InstanceIdentifier.create(Endpoints.class) + .child(Endpoint.class) + .augmentation(IovisorModuleAugmentation.class); + + IovisorModuleAugmentation aug = mock(IovisorModuleAugmentation.class); + when(aug.getUri()).thenReturn(mock(Uri.class)); + + Endpoint endpoint = mock(Endpoint.class); + when(endpoint.getAugmentation(any(Class.class))).thenReturn(aug); + + PowerMockito.doReturn(endpoint).when(endpointListner, "fromMd", any(InstanceIdentifier.class), any(Endpoint.class)); + + Map, DataObject> created = new HashMap<>(); + created.put(endpointIid, endpoint); + when(changes.getCreatedData()).thenReturn(created); + + PowerMockito.mockStatic(IovisorModuleUtils.class); + PowerMockito.when(IovisorModuleUtils.validateIovisorModuleInstance(any(DataBroker.class), any(Uri.class))).thenReturn(true); + + endpointListner.onDataChanged(changes); + PowerMockito.verifyPrivate(endpointListner, times(1)).invoke("fromMd", any(InstanceIdentifier.class), any(Endpoint.class)); + + // TODO test what happens when the validateIovisorModuleInstance returns + // true or false. (Not yet implemented) + } + + @Test + public void updatedTest() { + // Nothing to test + } + + @Test + public void removedTest() { + // Nothing to test + } +} \ No newline at end of file diff --git a/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManagerTest.java b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManagerTest.java new file mode 100644 index 000000000..40ec52d73 --- /dev/null +++ b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManagerTest.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint; + +public class EndpointManagerTest { + + // Nothing to test +} \ No newline at end of file diff --git a/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactoryTest.java b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactoryTest.java new file mode 100644 index 000000000..d656c3a8b --- /dev/null +++ b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorIidFactoryTest.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.utils; + +import static org.mockito.Mockito.mock; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class IovisorIidFactoryTest { + + @Test + public void iovisorModuleInstanceIidTest() { + IovisorModuleInstanceKey iovisorModuleInstanceKey = mock(IovisorModuleInstanceKey.class); + InstanceIdentifier identifier = IovisorIidFactory.iovisorModuleInstanceIid(iovisorModuleInstanceKey); + Assert.assertEquals(iovisorModuleInstanceKey, identifier.firstKeyOf(IovisorModuleInstance.class)); + } +} diff --git a/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtilsTest.java b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtilsTest.java new file mode 100644 index 000000000..732b5297b --- /dev/null +++ b/renderers/iovisor/src/test/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/utils/IovisorModuleUtilsTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.iovisor.utils; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.groupbasedpolicy.util.DataStoreHelper; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.google.common.base.Optional; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({DataStoreHelper.class}) +public class IovisorModuleUtilsTest { + + @SuppressWarnings("unchecked") + @Test + public void validateIovisorModuleInstanceTest() { + DataBroker dataBroker = Mockito.mock(DataBroker.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(mock(ReadOnlyTransaction.class)); + + PowerMockito.mockStatic(DataStoreHelper.class); + Optional result = mock(Optional.class); + PowerMockito.when(DataStoreHelper.readFromDs(any(LogicalDatastoreType.class), + any(InstanceIdentifier.class), + any(ReadOnlyTransaction.class))) + .thenReturn(result); + + Uri goodUri = mock(Uri.class); + + when(result.isPresent()).thenReturn(false); + Assert.assertFalse(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, goodUri)); + + when(result.isPresent()).thenReturn(true); + IovisorModuleInstance iovisorModuleInstance = mock(IovisorModuleInstance.class); + when(iovisorModuleInstance.getUri()).thenReturn(goodUri); + List iovisorModuleInstanceList = new ArrayList<>(); + iovisorModuleInstanceList.add(iovisorModuleInstance); + IovisorModuleInstances iovisorModuleInstances = mock(IovisorModuleInstances.class); + when(iovisorModuleInstances.getIovisorModuleInstance()).thenReturn(iovisorModuleInstanceList); + when(result.get()).thenReturn(iovisorModuleInstances); + Assert.assertTrue(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, goodUri)); + + Uri wrongUri = mock(Uri.class); + Assert.assertFalse(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, wrongUri)); + } +}