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