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