2 * Copyright (c) 2015 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Maps;
12 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
16 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
17 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
18 import org.opendaylight.ovsdb.openstack.netvirt.NodeCacheManagerEvent;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
24 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * @author Flavio Fernandes (ffernand@redhat.com)
34 * @author Sam Hague (shague@redhat.com)
36 public class NodeCacheManagerImpl extends AbstractHandler implements NodeCacheManager, ConfigInterface {
37 private static final Logger logger = LoggerFactory.getLogger(NodeCacheManagerImpl.class);
38 private final Object nodeCacheLock = new Object();
39 private Map<NodeId, Node> nodeCache = new ConcurrentHashMap<>();
40 private Map<Long, NodeCacheListener> handlers = Maps.newHashMap();
41 private volatile EventDispatcher eventDispatcher;
42 private volatile Southbound southbound;
45 public void nodeAdded(Node node) {
46 logger.debug("nodeAdded: {}", node);
47 enqueueEvent(new NodeCacheManagerEvent(node, Action.UPDATE));
51 public void nodeRemoved(Node node) {
52 logger.debug("nodeRemoved: {}", node);
53 enqueueEvent(new NodeCacheManagerEvent(node, Action.DELETE));
57 // might need to break this into two different events
58 // notifyOvsdbNode, notifyBridgeNode or just make sure the
59 // classes implementing the interface check for ovsdbNode or bridgeNode
60 private void processNodeUpdate(Node node) {
61 Action action = Action.UPDATE;
63 NodeId nodeId = node.getNodeId();
64 if (nodeCache.get(nodeId) == null) {
67 nodeCache.put(nodeId, node);
69 logger.debug("processNodeUpdate: {} Node type {} {}: {}",
71 southbound.getBridge(node) != null ? "BridgeNode" : "OvsdbNode",
72 action == Action.ADD ? "ADD" : "UPDATE",
75 for (NodeCacheListener handler : handlers.values()) {
77 handler.notifyNode(node, action);
78 } catch (Exception e) {
79 logger.error("Failed notifying node add event", e);
82 logger.debug("processNodeUpdate returns");
85 private void processNodeRemoved(Node node) {
86 nodeCache.remove(node);
87 for (NodeCacheListener handler : handlers.values()) {
89 handler.notifyNode(node, Action.DELETE);
90 } catch (Exception e) {
91 logger.error("Failed notifying node remove event", e);
94 logger.warn("processNodeRemoved returns");
100 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
101 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
104 public void processEvent(AbstractEvent abstractEvent) {
105 if (!(abstractEvent instanceof NodeCacheManagerEvent)) {
106 logger.error("Unable to process abstract event " + abstractEvent);
109 NodeCacheManagerEvent ev = (NodeCacheManagerEvent) abstractEvent;
110 logger.debug("NodeCacheManagerImpl: dequeue: {}", ev);
111 switch (ev.getAction()) {
113 processNodeRemoved(ev.getNode());
116 processNodeUpdate(ev.getNode());
119 logger.warn("Unable to process event action " + ev.getAction());
124 public void cacheListenerAdded(final ServiceReference ref, NodeCacheListener handler){
125 Long pid = (Long) ref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
126 handlers.put(pid, handler);
127 logger.info("Node cache listener registered, pid {} {}", pid, handler.getClass().getName());
130 public void cacheListenerRemoved(final ServiceReference ref){
131 Long pid = (Long) ref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
132 handlers.remove(pid);
133 logger.debug("Node cache listener unregistered, pid {}", pid);
137 public Map<NodeId,Node> getOvsdbNodes() {
138 Map<NodeId,Node> ovsdbNodesMap = new ConcurrentHashMap<NodeId,Node>();
139 for (Map.Entry<NodeId, Node> ovsdbNodeEntry : nodeCache.entrySet()) {
140 if (southbound.extractOvsdbNode(ovsdbNodeEntry.getValue()) != null) {
141 ovsdbNodesMap.put(ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
144 return ovsdbNodesMap;
148 public List<Node> getBridgeNodes() {
149 List<Node> nodes = Lists.newArrayList();
150 for (Node node : nodeCache.values()) {
151 if (southbound.getBridge(node) != null) {
159 public List<Node> getNodes() {
160 List<Node> nodes = Lists.newArrayList();
161 for (Node node : nodeCache.values()) {
168 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
170 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
172 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
173 eventDispatcher.eventHandlerAdded(
174 bundleContext.getServiceReference(NodeCacheManager.class.getName()), this);
175 super.setDispatcher(eventDispatcher);
179 public void setDependencies(Object impl) {}