+++ /dev/null
-/*
- * Copyright (C) 2015 Red Hat, Inc.
- *
- * 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
- *
- * Authors : Sam Hague
- */
-package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
-
-import com.google.common.collect.Lists;
-import java.util.List;
-import java.util.Map;
-
-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.AsyncDataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
-import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
-import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
-import org.opendaylight.ovsdb.utils.mdsal.node.NodeUtils;
-import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
-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;
-
-public class FlowCapableNodeDataChangeListener implements DataChangeListener, AutoCloseable {
- private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeDataChangeListener.class);
- private ListenerRegistration<DataChangeListener> registration;
- private final Object nodeCacheLock = new Object();
- private List<Node> nodeCache = Lists.newArrayList();
- private PipelineOrchestrator pipelineOrchestrator = null;
- private NodeCacheManager nodeCacheManager = null;
-
- public static final InstanceIdentifier<FlowCapableNode> createFlowCapableNodePath () {
- return InstanceIdentifier.builder(Nodes.class)
- .child(Node.class)
- .augmentation(FlowCapableNode.class)
- .build();
- }
-
- public FlowCapableNodeDataChangeListener (DataBroker dataBroker) {
- LOG.info("Registering FlowCapableNodeChangeListener");
- registration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
- createFlowCapableNodePath(), this, AsyncDataBroker.DataChangeScope.BASE);
- }
-
- @Override
- public void close () throws Exception {
- registration.close();
- }
-
- @Override
- public void onDataChanged (AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
- LOG.debug(">>>> onDataChanged: {}", changes);
- checkMemberInitialization();
-
- for (InstanceIdentifier instanceIdentifier : changes.getRemovedPaths()) {
- DataObject originalDataObject = changes.getOriginalData().get(instanceIdentifier);
- if (originalDataObject instanceof Node) {
- Node node = (Node) originalDataObject;
- String openflowId = node.getId().getValue();
- LOG.info(">>>>> removed iiD: {} - NodeKey: {}", instanceIdentifier, openflowId);
- Node openFlowNode = NodeUtils.getOpenFlowNode(openflowId);
- if (removeNodeFromCache(openFlowNode)) {
- notifyNodeRemoved(openFlowNode);
- }
- }
- }
-
- for (Map.Entry<InstanceIdentifier<?>, DataObject> created : changes.getCreatedData().entrySet()) {
- InstanceIdentifier<?> iID = created.getKey();
- String openflowId = iID.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
- LOG.info(">>>>> created iiD: {} - first: {} - NodeKey: {}",
- iID, iID.firstIdentifierOf(Node.class), openflowId);
- Node openFlowNode = NodeUtils.getOpenFlowNode(openflowId);
- if (addNodeToCache(openFlowNode)) {
- notifyNodeCreated(openFlowNode);
- } else {
- notifyNodeUpdated(openFlowNode);
- }
- }
-
- for (Map.Entry<InstanceIdentifier<?>, DataObject> updated : changes.getUpdatedData().entrySet()) {
- InstanceIdentifier<?> iID = updated.getKey();
- String openflowId = iID.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
- LOG.info(">>>>> updated iiD: {} - first: {} - NodeKey: {}",
- iID, iID.firstIdentifierOf(Node.class), openflowId);
- Node openFlowNode = NodeUtils.getOpenFlowNode(openflowId);
- if (addNodeToCache(openFlowNode)) {
- notifyNodeCreated(openFlowNode);
- } else {
- notifyNodeUpdated(openFlowNode);
- }
- }
- }
-
- /**
- * This method returns the true if node was added to the nodeCache. If param node
- * is already in the cache, this method is expected to return false.
- *
- * @param openFlowNode the node to be added to the cache, if needed
- * @return whether new node entry was added to cache
- */
- private Boolean addNodeToCache (Node openFlowNode) {
- synchronized (nodeCacheLock) {
- if (nodeCache.contains(openFlowNode)) {
- return false;
- }
- return nodeCache.add(openFlowNode);
- }
- }
-
- /**
- * This method returns the true if node was removed from the nodeCache. If param node
- * is not in the cache, this method is expected to return false.
- *
- * @param openFlowNode the node to be removed from the cache, if needed
- * @return whether new node entry was removed from cache
- */
- private Boolean removeNodeFromCache (Node openFlowNode) {
- synchronized (nodeCacheLock) {
- return nodeCache.remove(openFlowNode);
- }
- }
-
- private void notifyNodeUpdated (Node openFlowNode) {
- final String openflowId = openFlowNode.getId().getValue();
- LOG.debug("notifyNodeUpdated: Node {} from Controller's inventory Service", openflowId);
-
- // TODO: will do something amazing here, someday
- }
-
- private void notifyNodeCreated (Node openFlowNode) {
- final String openflowId = openFlowNode.getId().getValue();
- LOG.info("notifyNodeCreated: Node {} from Controller's inventory Service", openflowId);
-
- if (pipelineOrchestrator != null) {
- //pipelineOrchestrator.enqueue(openflowId);
- }
- if (nodeCacheManager != null) {
- //nodeCacheManager.nodeAdded(openflowId);
- }
- }
-
- private void notifyNodeRemoved (Node openFlowNode) {
- LOG.info("notifyNodeRemoved: Node {} from Controller's inventory Service",
- openFlowNode.getId().getValue());
-
- if (nodeCacheManager != null) {
- //nodeCacheManager.nodeRemoved(openFlowNode);
- }
- }
-
- private void checkMemberInitialization () {
- /**
- * Obtain local ref to members, if needed. Having these local saves us from calling getGlobalInstance
- * upon every event.
- */
- if (pipelineOrchestrator == null) {
- pipelineOrchestrator =
- (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
- }
- if (nodeCacheManager == null) {
- nodeCacheManager = (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
- }
- }
-}
private ConsumerContext consumerContext = null;
private DataBroker dataBroker;
private NotificationProviderService notificationService;
- private FlowCapableNodeDataChangeListener flowCapableNodeChangeListener;
static final Logger logger = LoggerFactory.getLogger(MdsalConsumerImpl.class);
}
}
- void start() {
- }
-
- void stop() {
- }
-
@Override
public void onSessionInitialized(ConsumerContext session) {
this.consumerContext = session;
dataBroker = session.getSALService(DataBroker.class);
logger.info("OVSDB Neutron Session Initialized with CONSUMER CONTEXT {}", session.toString());
- //flowCapableNodeChangeListener = new FlowCapableNodeDataChangeListener(dataBroker);
}
@Override
+++ /dev/null
-/*
- * Copyright (c) 2015 Inocybe Technologies. 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.ovsdb.openstack.netvirt.providers.openflow13;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.same;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.lang.reflect.Field;
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.neutron.spi.INeutronPortCRUD;
-import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
-import org.opendaylight.ovsdb.openstack.netvirt.NeutronCacheUtils;
-import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
-import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
-import org.opendaylight.ovsdb.openstack.netvirt.impl.EventDispatcherImpl;
-import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
-
-/**
- * Unit test for {@link FlowCapableNodeDataChangeListener}
- */
-/* TODO SB_MIGRATION */
-@Ignore
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(ServiceHelper.class)
-public class FlowCapableNodeDataChangeListenerTest {
-
- @InjectMocks
- private FlowCapableNodeDataChangeListener nodeListener;
-
- @Mock
- private DataBroker dataBroker;
-
- @Mock
- private ListenerRegistration<DataChangeListener> registration;
-
- @Mock
- private NodeCacheManager nodeCacheManager;
-
- @Mock
- private PipelineOrchestrator orchestrator;
-
- @Before
- public void setUp() {
-
- DataBroker dataBroker = mock(DataBroker.class);
- registration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
- FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
- , nodeListener
- , AsyncDataBroker.DataChangeScope.BASE);
-
- when(dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
- FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
- , nodeListener
- , AsyncDataBroker.DataChangeScope.BASE)).thenReturn(registration);
-
- nodeListener = new FlowCapableNodeDataChangeListener(dataBroker);
- orchestrator = Mockito.mock(PipelineOrchestrator.class);
- nodeCacheManager = Mockito.mock(NodeCacheManager.class);
-
-
- PowerMockito.mockStatic(ServiceHelper.class);
-
- Mockito.when(ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, nodeListener)).thenReturn(orchestrator);
- Mockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, nodeListener)).thenReturn(nodeCacheManager);
-
- }
-
-
- /**
- * Test method {@link FlowCapableNodeDataChangeListener#notifyFlowCapableNodeEventTest(String,Action)}
- */
- /* TODO SB_MIGRATION *//*
- @Test
- public void notifyFlowCapableNodeEventTest() throws Exception{
-
- List<Node> nodeCache = (List<Node>) getClassField(nodeListener, "nodeCache");
-
- nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.ADD);
- nodeListener.notifyFlowCapableNodeEvent("flowid2", Action.ADD);
- assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after an ADD operation is incorrect", 2, nodeCache.size());
- verify(nodeCacheManager,times(1)).nodeAdded("flowid1");
- verify(nodeCacheManager,times(1)).nodeAdded("flowid2");
- verify(orchestrator, times(1)).enqueue("flowid1");
-
- nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.UPDATE);
- assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after an UPDATE operation is incorrect", 2, nodeCache.size());
- verify(nodeCacheManager, times(1)).nodeAdded("flowid1");
- verify(orchestrator, times(1)).enqueue("flowid1");
-
- nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.DELETE);
- assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after a DELETE operation is incorrect", 2, nodeCache.size());
- verify(nodeCacheManager, times(1)).nodeAdded("flowid1");
- verify(nodeCacheManager, times(1)).nodeRemoved("flowid1");
- verify(orchestrator, times(1)).enqueue("flowid1");
-
- }*/
-
- /**
- * Get the specified field from FlowCapableNodeDataChangeListener using reflection
- * @param instance - the class instance
- * @param fieldName - the field to retrieve
- *
- * @return the desired field
- */
- private Object getClassField(FlowCapableNodeDataChangeListener instance, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
- Field field = FlowCapableNodeDataChangeListener.class.getDeclaredField(fieldName);
- field.setAccessible(true);
- return field.get(instance);
- }
-
-}
}
- /**
- * Test method {@link MdsalConsumerImpl#notifyFlowCapableNodeCreateEvent(String, Action)}
- */
- @Test
- public void notifyFlowCapableNodeCreateEventTest() throws Exception{
-
- FlowCapableNodeDataChangeListener nodeChangeListener = (FlowCapableNodeDataChangeListener) getClassField(mdsalConsumer, "flowCapableNodeChangeListener");
-
- //Send a notification
- //mdsalConsumer.notifyFlowCapableNodeCreateEvent("flowId1", Action.ADD);
-
- List<Node> nodeCache = (List<Node>) getClassField(nodeChangeListener, "nodeCache");
- assertEquals("Error, notifyFlowCapableNodeEvent() - MdsalConsumerImpl NodeDataChangeLister inventory size after an ADD operation is incorrect", 1, nodeCache.size());
- }
-
/**
* Get the specified field from MdsalConsumerImpl using reflection
- * @param instancee - the class instance
+ * @param instance - the class instance
* @param fieldName - the field to retrieve
*
* @return the desired field
field.setAccessible(true);
return field.get(instance);
}
-
- /**
- * Get the specified field from FlowCapableNodeDataChangeListener using reflection
- * @param instancee - the class instance
- * @param fieldName - the field to retrieve
- *
- * @return the desired field
- */
- private Object getClassField(FlowCapableNodeDataChangeListener instance, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
- Field field = FlowCapableNodeDataChangeListener.class.getDeclaredField(fieldName);
- field.setAccessible(true);
- return field.get(instance);
- }
-
}