Merge "Added support to update flows for induvidual security rule add/remove , after...
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / NodeCacheManagerImpl.java
index 9813b215bad7f24ab3aa94baa9b30a58d6b851e4..7e21101e004a9e3cff4cab2f7adeacd8f6240e48 100644 (file)
@@ -23,7 +23,7 @@ import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.osgi.framework.BundleContext;
+
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,21 +36,20 @@ import com.google.common.collect.Maps;
  * @author Sam Hague (shague@redhat.com)
  */
 public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheManager, ConfigInterface {
-    private static final Logger logger = LoggerFactory.getLogger(NodeCacheManagerImpl.class);
-    private final Object nodeCacheLock = new Object();
+    private static final Logger LOG = LoggerFactory.getLogger(NodeCacheManagerImpl.class);
     private Map<NodeId, Node> nodeCache = new ConcurrentHashMap<>();
     private Map<Long, NodeCacheListener> handlers = Maps.newHashMap();
     private volatile Southbound southbound;
 
     @Override
     public void nodeAdded(Node node) {
-        logger.debug("nodeAdded: {}", node);
+        LOG.debug("nodeAdded: {}", node);
         enqueueEvent(new NodeCacheManagerEvent(node, Action.UPDATE));
     }
 
     @Override
     public void nodeRemoved(Node node) {
-        logger.debug("nodeRemoved: {}", node);
+        LOG.debug("nodeRemoved: {}", node);
         enqueueEvent(new NodeCacheManagerEvent(node, Action.DELETE));
     }
 
@@ -67,7 +66,7 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
         }
         nodeCache.put(nodeId, node);
 
-        logger.debug("processNodeUpdate: {} Node type {} {}: {}",
+        LOG.debug("processNodeUpdate: {} Node type {} {}: {}",
                 nodeCache.size(),
                 southbound.getBridge(node) != null ? "BridgeNode" : "OvsdbNode",
                 action == Action.ADD ? "ADD" : "UPDATE",
@@ -77,10 +76,10 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
             try {
                 handler.notifyNode(node, action);
             } catch (Exception e) {
-                logger.error("Failed notifying node add event", e);
+                LOG.error("Failed notifying node add event", e);
             }
         }
-        logger.debug("processNodeUpdate returns");
+        LOG.debug("processNodeUpdate returns");
     }
 
     private void processNodeRemoved(Node node) {
@@ -89,10 +88,10 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
             try {
                 handler.notifyNode(node, Action.DELETE);
             } catch (Exception e) {
-                logger.error("Failed notifying node remove event", e);
+                LOG.error("Failed notifying node remove event", e);
             }
         }
-        logger.warn("processNodeRemoved returns");
+        LOG.warn("processNodeRemoved returns");
     }
 
     /**
@@ -104,11 +103,11 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
     @Override
     public void processEvent(AbstractEvent abstractEvent) {
         if (!(abstractEvent instanceof NodeCacheManagerEvent)) {
-            logger.error("Unable to process abstract event " + abstractEvent);
+            LOG.error("Unable to process abstract event {}", abstractEvent);
             return;
         }
         NodeCacheManagerEvent ev = (NodeCacheManagerEvent) abstractEvent;
-        logger.debug("NodeCacheManagerImpl: dequeue: {}", ev);
+        LOG.debug("NodeCacheManagerImpl: dequeue: {}", ev);
         switch (ev.getAction()) {
             case DELETE:
                 processNodeRemoved(ev.getNode());
@@ -117,7 +116,7 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
                 processNodeUpdate(ev.getNode());
                 break;
             default:
-                logger.warn("Unable to process event action " + ev.getAction());
+                LOG.warn("Unable to process event action {}", ev.getAction());
                 break;
         }
     }
@@ -125,18 +124,18 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
     public void cacheListenerAdded(final ServiceReference ref, NodeCacheListener handler){
         Long pid = (Long) ref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
         handlers.put(pid, handler);
-        logger.info("Node cache listener registered, pid {} {}", pid, handler.getClass().getName());
+        LOG.info("Node cache listener registered, pid {} {}", pid, handler.getClass().getName());
     }
 
     public void cacheListenerRemoved(final ServiceReference ref){
         Long pid = (Long) ref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
         handlers.remove(pid);
-        logger.debug("Node cache listener unregistered, pid {}", pid);
+        LOG.debug("Node cache listener unregistered, pid {}", pid);
     }
 
     @Override
     public Map<NodeId,Node> getOvsdbNodes() {
-        Map<NodeId,Node> ovsdbNodesMap = new ConcurrentHashMap<NodeId,Node>();
+        Map<NodeId,Node> ovsdbNodesMap = new ConcurrentHashMap<>();
         for (Map.Entry<NodeId, Node> ovsdbNodeEntry : nodeCache.entrySet()) {
             if (southbound.extractOvsdbNode(ovsdbNodeEntry.getValue()) != null) {
                 ovsdbNodesMap.put(ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
@@ -165,14 +164,27 @@ public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheMa
         return nodes;
     }
 
+    private void populateNodeCache() {
+        LOG.debug("populateNodeCache : Populating the node cache");
+        List<Node> nodes = southbound.readOvsdbTopologyNodes();
+        for(Node ovsdbNode : nodes) {
+            this.nodeCache.put(ovsdbNode.getNodeId(), ovsdbNode);
+        }
+        nodes = southbound.readOvsdbTopologyBridgeNodes();
+        for(Node bridgeNode : nodes) {
+            this.nodeCache.put(bridgeNode.getNodeId(), bridgeNode);
+        }
+        LOG.debug("populateNodeCache : Node cache population is done. Total nodes : {}",this.nodeCache.size());
+    }
+
     @Override
-    public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
+    public void setDependencies(ServiceReference serviceReference) {
         southbound =
                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
         eventDispatcher =
                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
-        eventDispatcher.eventHandlerAdded(
-                bundleContext.getServiceReference(NodeCacheManager.class.getName()), this);
+        eventDispatcher.eventHandlerAdded(serviceReference, this);
+        populateNodeCache();
     }
 
     @Override