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