rpcRegistration =
this.rpcRegistry.addRpcImplementation(EndpointService.class, this);
LOG.debug("Added RPC Implementation Correctly");
- } else
+ } else {
rpcRegistration = null;
+ }
if (dataProvider != null) {
InstanceIdentifier<Endpoints> iid =
<packaging>bundle</packaging>
<properties>
+ <powermock.version>1.5.2</powermock.version>
</properties>
<dependencies>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-core</artifactId>
+ <version>${powermock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>${powermock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>${powermock.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
@Override
public java.lang.AutoCloseable createInstance() {
- return new IovisorRenderer(getDataBrokerDependency());
+ return new IovisorRenderer(getDataBrokerDependency(), getRpcRegistryDependency());
}
}
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;
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
endPointManager.close();
}
}
-
}
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<DataChangeListener> registerListener;
+ private DataBroker dataBroker;
+
public EndpointListener(DataBroker dataBroker) {
+ this.dataBroker = dataBroker;
this.registerListener = dataBroker.registerDataChangeListener(
LogicalDatastoreType.OPERATIONAL,
IidFactory.endpointsIidWildcard().child(Endpoint.class),
private void created(Map<InstanceIdentifier<?>, DataObject> created) {
for (Entry<InstanceIdentifier<?>, 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
+ }
}
}
private void updated(Map<InstanceIdentifier<?>, DataObject> updated) {
for (Entry<InstanceIdentifier<?>, 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
+
}
}
private void removed(AsyncDataChangeEvent<InstanceIdentifier<?>, 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
}
}
* 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);
}
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
if (endpointListener != null) {
endpointListener.close();
}
+ if (endpointRpcRegistry != null) {
+ endpointRpcRegistry.close();
+ }
+ EndpointRpcRegistry.unregister(this);
+ }
+
+ @Override
+ public Entry<Class<? extends Augmentation<Endpoint>>, Augmentation<Endpoint>> buildEndpointAugmentation(
+ RegisterEndpointInput input) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Entry<Class<? extends Augmentation<EndpointL3>>, Augmentation<EndpointL3>> buildEndpointL3Augmentation(
+ RegisterEndpointInput input) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Entry<Class<? extends Augmentation<EndpointL3Prefix>>, Augmentation<EndpointL3Prefix>> buildL3PrefixEndpointAugmentation(
+ RegisterL3PrefixEndpointInput input) {
+ // TODO Auto-generated method stub
+ return null;
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * 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<IovisorModuleInstances> IOVISOR_MODULE_INSTANCES_IID = InstanceIdentifier.builder(IovisorModuleInstances.class).build();
+
+ /**
+ * @return The {@link InstanceIdentifier} of the {@link IovisorModuleInstances}
+ */
+ public static InstanceIdentifier<IovisorModuleInstances> 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<IovisorModuleInstance> iovisorModuleInstanceIid(IovisorModuleInstanceKey iovisorModuleInstanceKey) {
+ return IOVISOR_MODULE_INSTANCES_IID.child(IovisorModuleInstance.class, iovisorModuleInstanceKey);
+ }
+}
--- /dev/null
+/*
+ * 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 <code>true</code> if validated, else, <code>false</code>
+ */
+ public static boolean validateIovisorModuleInstance(DataBroker dataBroker, Uri iovisorModuleUri) {
+ Optional<IovisorModuleInstances> 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;
+ }
+}
"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;
}
}
--- /dev/null
+/*
+ * 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<DataChangeListener> registerListener;
+
+ @Mock AsyncDataChangeEvent<InstanceIdentifier<?>, 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<InstanceIdentifier<?>, 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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<IovisorModuleInstance> identifier = IovisorIidFactory.iovisorModuleInstanceIid(iovisorModuleInstanceKey);
+ Assert.assertEquals(iovisorModuleInstanceKey, identifier.firstKeyOf(IovisorModuleInstance.class));
+ }
+}
--- /dev/null
+/*
+ * 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<IovisorModuleInstances> 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<IovisorModuleInstance> 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));
+ }
+}