InventoryShim to check node presence, before notifying in container
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryServiceShim.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.protocol_plugin.openflow.internal;
10
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.CopyOnWriteArrayList;
20 import java.util.concurrent.CopyOnWriteArraySet;
21
22 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
23 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsListener;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
27 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
28 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitchStateListener;
29 import org.opendaylight.controller.sal.core.Actions;
30 import org.opendaylight.controller.sal.core.Buffers;
31 import org.opendaylight.controller.sal.core.Capabilities;
32 import org.opendaylight.controller.sal.core.ContainerFlow;
33 import org.opendaylight.controller.sal.core.Description;
34 import org.opendaylight.controller.sal.core.IContainerListener;
35 import org.opendaylight.controller.sal.core.MacAddress;
36 import org.opendaylight.controller.sal.core.Node;
37 import org.opendaylight.controller.sal.core.NodeConnector;
38 import org.opendaylight.controller.sal.core.Property;
39 import org.opendaylight.controller.sal.core.Tables;
40 import org.opendaylight.controller.sal.core.TimeStamp;
41 import org.opendaylight.controller.sal.core.UpdateType;
42 import org.opendaylight.controller.sal.utils.GlobalConstants;
43 import org.opendaylight.controller.sal.utils.NodeCreator;
44 import org.openflow.protocol.OFMessage;
45 import org.openflow.protocol.OFPortStatus;
46 import org.openflow.protocol.OFPortStatus.OFPortReason;
47 import org.openflow.protocol.OFType;
48 import org.openflow.protocol.statistics.OFDescriptionStatistics;
49 import org.openflow.protocol.statistics.OFStatistics;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * The class describes a shim layer that bridges inventory events from Openflow
55  * core to various listeners. The notifications are filtered based on container
56  * configurations.
57  *
58  *
59  */
60 public class InventoryServiceShim implements IContainerListener,
61         IMessageListener, ISwitchStateListener, IOFStatisticsListener {
62     protected static final Logger logger = LoggerFactory
63             .getLogger(InventoryServiceShim.class);
64     private IController controller = null;
65     private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
66     private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
67     private final ConcurrentMap<NodeConnector, Set<String>> nodeConnectorContainerMap = new ConcurrentHashMap<NodeConnector, Set<String>>();
68     private final ConcurrentMap<Node, Set<String>> nodeContainerMap = new ConcurrentHashMap<Node, Set<String>>();
69     private final ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Set<Property>>();
70     private final ConcurrentMap<Node, Set<Property>> nodeProps = new ConcurrentHashMap<Node, Set<Property>>();
71
72     void setController(IController s) {
73         this.controller = s;
74     }
75
76     void unsetController(IController s) {
77         if (this.controller == s) {
78             this.controller = null;
79         }
80     }
81
82     void setInventoryShimInternalListener(Map<?, ?> props,
83             IInventoryShimInternalListener s) {
84         if (props == null) {
85             logger.error("setInventoryShimInternalListener property is null");
86             return;
87         }
88         String containerName = (String) props.get("containerName");
89         if (containerName == null) {
90             logger.error("setInventoryShimInternalListener containerName not supplied");
91             return;
92         }
93         if ((this.inventoryShimInternalListeners != null)
94                 && !this.inventoryShimInternalListeners.containsValue(s)) {
95             this.inventoryShimInternalListeners.put(containerName, s);
96             logger.trace(
97                     "Added inventoryShimInternalListener for container {}",
98                     containerName);
99         }
100     }
101
102     void unsetInventoryShimInternalListener(Map<?, ?> props,
103             IInventoryShimInternalListener s) {
104         if (props == null) {
105             logger.error("unsetInventoryShimInternalListener property is null");
106             return;
107         }
108         String containerName = (String) props.get("containerName");
109         if (containerName == null) {
110             logger.error("unsetInventoryShimInternalListener containerName not supplied");
111             return;
112         }
113         if ((this.inventoryShimInternalListeners != null)
114                 && this.inventoryShimInternalListeners.get(containerName) != null
115                 && this.inventoryShimInternalListeners.get(containerName)
116                         .equals(s)) {
117             this.inventoryShimInternalListeners.remove(containerName);
118             logger.trace(
119                     "Removed inventoryShimInternalListener for container {}",
120                     containerName);
121         }
122     }
123
124     void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
125         logger.trace("Set inventoryShimExternalListener");
126         if ((this.inventoryShimExternalListeners != null)
127                 && !this.inventoryShimExternalListeners.contains(s)) {
128             this.inventoryShimExternalListeners.add(s);
129         }
130     }
131
132     void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
133         if ((this.inventoryShimExternalListeners != null)
134                 && this.inventoryShimExternalListeners.contains(s)) {
135             this.inventoryShimExternalListeners.remove(s);
136         }
137     }
138
139     /**
140      * Function called by the dependency manager when all the required
141      * dependencies are satisfied
142      *
143      */
144     void init() {
145         this.controller.addMessageListener(OFType.PORT_STATUS, this);
146         this.controller.addSwitchStateListener(this);
147     }
148
149     /**
150      * Function called after registering the service in OSGi service registry.
151      */
152     void started() {
153         /* Start with existing switches */
154         startService();
155     }
156
157     /**
158      * Function called by the dependency manager when at least one dependency
159      * become unsatisfied or when the component is shutting down because for
160      * example bundle is being stopped.
161      *
162      */
163     void destroy() {
164         this.controller.removeMessageListener(OFType.PACKET_IN, this);
165         this.controller.removeSwitchStateListener(this);
166
167         this.inventoryShimInternalListeners.clear();
168         this.nodeConnectorContainerMap.clear();
169         this.nodeContainerMap.clear();
170         this.controller = null;
171     }
172
173     @Override
174     public void receive(ISwitch sw, OFMessage msg) {
175         if (msg instanceof OFPortStatus) {
176             handlePortStatusMessage(sw, (OFPortStatus) msg);
177         }
178         return;
179     }
180
181     protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
182         Node node = NodeCreator.createOFNode(sw.getId());
183         NodeConnector nodeConnector = PortConverter.toNodeConnector(
184             m.getDesc().getPortNumber(), node);
185         // get node connector properties
186         Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
187
188         UpdateType type = null;
189         if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
190             type = UpdateType.ADDED;
191             nodeConnectorProps.put(nodeConnector, props);
192         } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
193             type = UpdateType.REMOVED;
194             nodeConnectorProps.remove(nodeConnector);
195         } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
196             type = UpdateType.CHANGED;
197             nodeConnectorProps.put(nodeConnector, props);
198         }
199
200         logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
201
202         if (type != null) {
203             notifyInventoryShimListener(nodeConnector, type, props);
204         }
205     }
206
207     @Override
208     public void switchAdded(ISwitch sw) {
209         if (sw == null) {
210             return;
211         }
212
213         // Add all the nodeConnectors of this switch
214         Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
215                 .OFSwitchToProps(sw);
216         for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
217             Set<Property> props = new HashSet<Property>();
218             Set<Property> prop = entry.getValue();
219             if (prop != null) {
220                 props.addAll(prop);
221             }
222             nodeConnectorProps.put(entry.getKey(), props);
223             notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
224                     entry.getValue());
225         }
226
227         // Add this node
228         addNode(sw);
229     }
230
231     @Override
232     public void switchDeleted(ISwitch sw) {
233         if (sw == null) {
234             return;
235         }
236
237         removeNode(sw);
238     }
239
240     @Override
241     public void containerModeUpdated(UpdateType t) {
242         // do nothing
243     }
244
245     @Override
246     public void tagUpdated(String containerName, Node n, short oldTag,
247             short newTag, UpdateType t) {
248         logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
249                 n, t, containerName });
250     }
251
252     @Override
253     public void containerFlowUpdated(String containerName,
254             ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
255     }
256
257     @Override
258     public void nodeConnectorUpdated(String containerName, NodeConnector nc, UpdateType t) {
259         logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { nc, t, containerName });
260         Node node = nc.getNode();
261         Set<String> ncContainers = this.nodeConnectorContainerMap.get(nc);
262         Set<String> nodeContainers = this.nodeContainerMap.get(node);
263         if (ncContainers == null) {
264             ncContainers = new CopyOnWriteArraySet<String>();
265         }
266         if (nodeContainers == null) {
267             nodeContainers = new CopyOnWriteArraySet<String>();
268         }
269         boolean notifyNodeUpdate = false;
270
271         switch (t) {
272         case ADDED:
273             if (ncContainers.add(containerName)) {
274                 this.nodeConnectorContainerMap.put(nc, ncContainers);
275             }
276             if (nodeContainers.add(containerName)) {
277                 this.nodeContainerMap.put(node, nodeContainers);
278                 notifyNodeUpdate = true;
279             }
280             break;
281         case REMOVED:
282             if (ncContainers.remove(containerName)) {
283                 if (ncContainers.isEmpty()) {
284                     // Do cleanup to reduce memory footprint if no
285                     // elements to be tracked
286                     this.nodeConnectorContainerMap.remove(nc);
287                 } else {
288                     this.nodeConnectorContainerMap.put(nc, ncContainers);
289                 }
290             }
291             boolean nodeContainerUpdate = true;
292             for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
293                 if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).contains(containerName))) {
294                     nodeContainerUpdate = false;
295                     break;
296                 }
297             }
298             if (nodeContainerUpdate) {
299                 nodeContainers.remove(containerName);
300                 notifyNodeUpdate = true;
301                 if (nodeContainers.isEmpty()) {
302                     this.nodeContainerMap.remove(node);
303                 } else {
304                     this.nodeContainerMap.put(node, nodeContainers);
305                 }
306             }
307             break;
308         case CHANGED:
309             break;
310         }
311
312         Set<Property> nodeProp = nodeProps.get(node);
313         if (nodeProp == null) {
314             return;
315         }
316         Set<Property> ncProp = nodeConnectorProps.get(nc);
317         // notify InventoryService
318         notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
319
320         if (notifyNodeUpdate) {
321             notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
322         }
323     }
324
325     private void notifyInventoryShimExternalListener(Node node,
326             UpdateType type, Set<Property> props) {
327         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
328             s.updateNode(node, type, props);
329         }
330     }
331
332     private void notifyInventoryShimExternalListener(
333             NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
334         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
335             s.updateNodeConnector(nodeConnector, type, props);
336         }
337     }
338
339     private void notifyInventoryShimInternalListener(String container,
340             NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
341         IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
342                 .get(container);
343         if (inventoryShimInternalListener != null) {
344             inventoryShimInternalListener.updateNodeConnector(nodeConnector,
345                     type, props);
346             logger.trace(
347                     "notifyInventoryShimInternalListener {} type {} for container {}",
348                     new Object[] { nodeConnector, type, container });
349         }
350     }
351
352     /*
353      * Notify all internal and external listeners
354      */
355     private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
356         // notify other containers
357         Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
358                 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
359         containers.add(GlobalConstants.DEFAULT.toString());
360         for (String container : containers) {
361             notifyInventoryShimInternalListener(container, nodeConnector, type, props);
362         }
363
364         // Notify DiscoveryService
365         notifyInventoryShimExternalListener(nodeConnector, type, props);
366     }
367
368     /*
369      * Notify all internal and external listeners
370      */
371     private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
372         // Now notify other containers
373         Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
374                 nodeContainerMap.get(node));
375         containers.add(GlobalConstants.DEFAULT.toString());
376         for (String container : containers) {
377             notifyInventoryShimInternalListener(container, node, type, props);
378         }
379
380         // Notify external listener
381         notifyInventoryShimExternalListener(node, type, props);
382     }
383
384     private void notifyInventoryShimInternalListener(String container,
385             Node node, UpdateType type, Set<Property> props) {
386         IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
387                 .get(container);
388         if (inventoryShimInternalListener != null) {
389             inventoryShimInternalListener.updateNode(node, type, props);
390             logger.trace(
391                     "notifyInventoryShimInternalListener {} type {} for container {}",
392                     new Object[] { node, type, container });
393         }
394     }
395
396     private void addNode(ISwitch sw) {
397         Node node = NodeCreator.createOFNode(sw.getId());
398         UpdateType type = UpdateType.ADDED;
399
400         Set<Property> props = new HashSet<Property>();
401         Long sid = (Long) node.getID();
402
403         Date connectedSince = controller.getSwitches().get(sid)
404                 .getConnectedDate();
405         Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
406                 .getTime();
407         props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
408         props.add(new MacAddress(deriveMacAddress(sid)));
409
410         byte tables = sw.getTables();
411         Tables t = new Tables(tables);
412         if (t != null) {
413             props.add(t);
414         }
415         int cap = sw.getCapabilities();
416         Capabilities c = new Capabilities(cap);
417         if (c != null) {
418             props.add(c);
419         }
420         int act = sw.getActions();
421         Actions a = new Actions(act);
422         if (a != null) {
423             props.add(a);
424         }
425         int buffers = sw.getBuffers();
426         Buffers b = new Buffers(buffers);
427         if (b != null) {
428             props.add(b);
429         }
430
431         nodeProps.put(node, props);
432         // Notify all internal and external listeners
433         notifyInventoryShimListener(node, type, props);
434     }
435
436     private void removeNode(ISwitch sw) {
437         Node node = NodeCreator.createOFNode(sw.getId());
438         if(node == null) {
439             return;
440         }
441         removeNodeConnectorProps(node);
442         nodeProps.remove(node);
443         UpdateType type = UpdateType.REMOVED;
444         // Notify all internal and external listeners
445         notifyInventoryShimListener(node, type, null);
446     }
447
448     private void startService() {
449         // Get a snapshot of all the existing switches
450         Map<Long, ISwitch> switches = this.controller.getSwitches();
451         for (ISwitch sw : switches.values()) {
452             switchAdded(sw);
453         }
454     }
455
456     private void removeNodeConnectorProps(Node node) {
457         List<NodeConnector> ncList = new ArrayList<NodeConnector>();
458         for (NodeConnector nc : nodeConnectorProps.keySet()) {
459             if (nc.getNode().equals(node)) {
460                 ncList.add(nc);
461             }
462         }
463         for (NodeConnector nc : ncList) {
464             nodeConnectorProps.remove(nc);
465         }
466     }
467
468     @Override
469     public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
470         Node node = NodeCreator.createOFNode(switchId);
471         Set<Property> properties = new HashSet<Property>(1);
472         OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
473         Description desc = new Description(ofDesc.getDatapathDescription());
474         properties.add(desc);
475
476         // Notify all internal and external listeners
477         notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
478     }
479
480     private byte[] deriveMacAddress(long dpid) {
481         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
482
483         for (short i = 0; i < 6; i++) {
484             mac[5 - i] = (byte) dpid;
485             dpid >>= 8;
486         }
487
488         return mac;
489     }
490
491     @Override
492     public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
493         // Nothing to do
494     }
495
496     @Override
497     public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
498         // Nothing to do
499     }
500
501     @Override
502     public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {
503         // Nothing to do
504     }
505 }