Fix for maintaining properties for non-default 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 p, UpdateType t) {
259         logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { p, t, containerName });
260         Node node = p.getNode();
261         Set<String> ncContainers = this.nodeConnectorContainerMap.get(p);
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(p, 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(p);
287                 } else {
288                     this.nodeConnectorContainerMap.put(p, ncContainers);
289                 }
290             }
291             boolean nodeContainerUpdate = true;
292             for (NodeConnector nc : nodeConnectorContainerMap.keySet()) {
293                 if ((nc.getNode().equals(node)) && (nodeConnectorContainerMap.get(nc).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> ncProp = nodeConnectorProps.get(p);
313         // notify InventoryService
314         notifyInventoryShimInternalListener(containerName, p, t, ncProp);
315
316         if (notifyNodeUpdate) {
317             Set<Property> nodeProp = nodeProps.get(node);
318             notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
319         }
320     }
321
322     private void notifyInventoryShimExternalListener(Node node,
323             UpdateType type, Set<Property> props) {
324         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
325             s.updateNode(node, type, props);
326         }
327     }
328
329     private void notifyInventoryShimExternalListener(
330             NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
331         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
332             s.updateNodeConnector(nodeConnector, type, props);
333         }
334     }
335
336     private void notifyInventoryShimInternalListener(String container,
337             NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
338         IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
339                 .get(container);
340         if (inventoryShimInternalListener != null) {
341             inventoryShimInternalListener.updateNodeConnector(nodeConnector,
342                     type, props);
343             logger.trace(
344                     "notifyInventoryShimInternalListener {} type {} for container {}",
345                     new Object[] { nodeConnector, type, container });
346         }
347     }
348
349     /*
350      * Notify all internal and external listeners
351      */
352     private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
353         // notify other containers
354         Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
355                 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
356         containers.add(GlobalConstants.DEFAULT.toString());
357         for (String container : containers) {
358             notifyInventoryShimInternalListener(container, nodeConnector, type, props);
359         }
360
361         // Notify DiscoveryService
362         notifyInventoryShimExternalListener(nodeConnector, type, props);
363     }
364
365     /*
366      * Notify all internal and external listeners
367      */
368     private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
369         // Now notify other containers
370         Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
371                 nodeContainerMap.get(node));
372         containers.add(GlobalConstants.DEFAULT.toString());
373         for (String container : containers) {
374             notifyInventoryShimInternalListener(container, node, type, props);
375         }
376
377         // Notify external listener
378         notifyInventoryShimExternalListener(node, type, props);
379     }
380
381     private void notifyInventoryShimInternalListener(String container,
382             Node node, UpdateType type, Set<Property> props) {
383         IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
384                 .get(container);
385         if (inventoryShimInternalListener != null) {
386             inventoryShimInternalListener.updateNode(node, type, props);
387             logger.trace(
388                     "notifyInventoryShimInternalListener {} type {} for container {}",
389                     new Object[] { node, type, container });
390         }
391     }
392
393     private void addNode(ISwitch sw) {
394         Node node = NodeCreator.createOFNode(sw.getId());
395         UpdateType type = UpdateType.ADDED;
396
397         Set<Property> props = new HashSet<Property>();
398         Long sid = (Long) node.getID();
399
400         Date connectedSince = controller.getSwitches().get(sid)
401                 .getConnectedDate();
402         Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
403                 .getTime();
404         props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
405         props.add(new MacAddress(deriveMacAddress(sid)));
406
407         byte tables = sw.getTables();
408         Tables t = new Tables(tables);
409         if (t != null) {
410             props.add(t);
411         }
412         int cap = sw.getCapabilities();
413         Capabilities c = new Capabilities(cap);
414         if (c != null) {
415             props.add(c);
416         }
417         int act = sw.getActions();
418         Actions a = new Actions(act);
419         if (a != null) {
420             props.add(a);
421         }
422         int buffers = sw.getBuffers();
423         Buffers b = new Buffers(buffers);
424         if (b != null) {
425             props.add(b);
426         }
427
428         nodeProps.put(node, props);
429         // Notify all internal and external listeners
430         notifyInventoryShimListener(node, type, props);
431     }
432
433     private void removeNode(ISwitch sw) {
434         Node node = NodeCreator.createOFNode(sw.getId());
435         if(node == null) {
436             return;
437         }
438         removeNodeConnectorProps(node);
439         nodeProps.remove(node);
440         UpdateType type = UpdateType.REMOVED;
441         // Notify all internal and external listeners
442         notifyInventoryShimListener(node, type, null);
443     }
444
445     private void startService() {
446         // Get a snapshot of all the existing switches
447         Map<Long, ISwitch> switches = this.controller.getSwitches();
448         for (ISwitch sw : switches.values()) {
449             switchAdded(sw);
450         }
451     }
452
453     private void removeNodeConnectorProps(Node node) {
454         List<NodeConnector> ncList = new ArrayList<NodeConnector>();
455         for (NodeConnector nc : nodeConnectorProps.keySet()) {
456             if (nc.getNode().equals(node)) {
457                 ncList.add(nc);
458             }
459         }
460         for (NodeConnector nc : ncList) {
461             nodeConnectorProps.remove(nc);
462         }
463     }
464
465     @Override
466     public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
467         Node node = NodeCreator.createOFNode(switchId);
468         Set<Property> properties = new HashSet<Property>(1);
469         OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
470         Description desc = new Description(ofDesc.getDatapathDescription());
471         properties.add(desc);
472
473         // Notify all internal and external listeners
474         notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
475     }
476
477     private byte[] deriveMacAddress(long dpid) {
478         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
479
480         for (short i = 0; i < 6; i++) {
481             mac[5 - i] = (byte) dpid;
482             dpid >>= 8;
483         }
484
485         return mac;
486     }
487
488     @Override
489     public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
490         // Nothing to do
491     }
492
493     @Override
494     public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
495         // Nothing to do
496     }
497
498     @Override
499     public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {
500         // Nothing to do
501     }
502 }