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