- 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 / InventoryService.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.Collections;
12 import java.util.Date;
13 import java.util.Dictionary;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.apache.felix.dm.Component;
22 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
23 import org.opendaylight.controller.protocol_plugin.openflow.IStatisticsListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
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.Description;
31 import org.opendaylight.controller.sal.core.Node;
32 import org.opendaylight.controller.sal.core.Node.NodeIDType;
33 import org.opendaylight.controller.sal.core.NodeConnector;
34 import org.opendaylight.controller.sal.core.Property;
35 import org.opendaylight.controller.sal.core.Tables;
36 import org.opendaylight.controller.sal.core.TimeStamp;
37 import org.opendaylight.controller.sal.core.UpdateType;
38 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
39 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
40 import org.opendaylight.controller.sal.utils.GlobalConstants;
41 import org.openflow.protocol.statistics.OFDescriptionStatistics;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The class describes inventory service protocol plugin. One instance per
47  * container of the network. Each instance gets container specific inventory
48  * events from InventoryServiceShim. It interacts with SAL to pass inventory
49  * data to the upper application.
50  * 
51  * 
52  */
53 public class InventoryService implements IInventoryShimInternalListener,
54         IPluginInInventoryService, IStatisticsListener {
55     protected static final Logger logger = LoggerFactory
56             .getLogger(InventoryService.class);
57     private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
58             .synchronizedSet(new HashSet<IPluginOutInventoryService>());
59     private IController controller = null;
60     private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
61     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
62     private boolean isDefaultContainer = false;
63
64     void setController(IController s) {
65         this.controller = s;
66     }
67
68     void unsetController(IController s) {
69         if (this.controller == s) {
70             this.controller = null;
71         }
72     }
73
74     /**
75      * Function called by the dependency manager when all the required
76      * dependencies are satisfied
77      * 
78      */
79     @SuppressWarnings("rawtypes")
80     void init(Component c) {
81         logger.trace("INIT called!");
82
83         Dictionary props = c.getServiceProperties();
84         if (props != null) {
85             String containerName = (String) props.get("containerName");
86             isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
87                     .toString());
88         }
89
90         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
91         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
92     }
93
94     /**
95      * Function called by the dependency manager when at least one dependency
96      * become unsatisfied or when the component is shutting down because for
97      * example bundle is being stopped.
98      * 
99      */
100     void destroy() {
101         logger.trace("DESTROY called!");
102     }
103
104     /**
105      * Function called by dependency manager after "init ()" is called and after
106      * the services provided by the class are registered in the service registry
107      * 
108      */
109     void start() {
110         logger.trace("START called!");
111     }
112
113     /**
114      * Function called by the dependency manager before the services exported by
115      * the component are unregistered, this will be followed by a "destroy ()"
116      * calls
117      * 
118      */
119     void stop() {
120         logger.trace("STOP called!");
121     }
122
123     public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
124         logger.trace("Got a service set request {}", service);
125         if (this.pluginOutInventoryServices != null) {
126             this.pluginOutInventoryServices.add(service);
127         }
128     }
129
130     public void unsetPluginOutInventoryServices(
131             IPluginOutInventoryService service) {
132         logger.trace("Got a service UNset request");
133         if (this.pluginOutInventoryServices != null) {
134             this.pluginOutInventoryServices.remove(service);
135         }
136     }
137
138     protected Node OFSwitchToNode(ISwitch sw) {
139         Node node = null;
140         Object id = sw.getId();
141
142         try {
143             node = new Node(NodeIDType.OPENFLOW, id);
144         } catch (ConstructionException e) {
145             logger.error("",e);
146         }
147
148         return node;
149     }
150
151     /**
152      * Retrieve nodes from openflow
153      */
154     @Override
155     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
156         if (nodeProps == null)
157             return null;
158         Map<Long, ISwitch> switches = controller.getSwitches();
159         for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
160             ISwitch sw = entry.getValue();
161             Node node = OFSwitchToNode(sw);
162             Map<String, Property> propMap = null;
163             if (isDefaultContainer) {
164                 propMap = new HashMap<String, Property>();
165                 byte tables = sw.getTables();
166                 Tables t = new Tables(tables);
167                 if (t != null) {
168                     propMap.put(Tables.TablesPropName, t);
169                 }
170                 int cap = sw.getCapabilities();
171                 Capabilities c = new Capabilities(cap);
172                 if (c != null) {
173                     propMap.put(Capabilities.CapabilitiesPropName, c);
174                 }
175                 int act = sw.getActions();
176                 Actions a = new Actions(act);
177                 if (a != null) {
178                     propMap.put(Actions.ActionsPropName, a);
179                 }
180                 int buffers = sw.getBuffers();
181                 Buffers b = new Buffers(buffers);
182                 if (b != null) {
183                     propMap.put(Buffers.BuffersPropName, b);
184                 }
185                 Date connectedSince = sw.getConnectedDate();
186                 Long connectedSinceTime = (connectedSince == null) ? 0
187                         : connectedSince.getTime();
188                 TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
189                         "connectedSince");
190                 propMap.put(TimeStamp.TimeStampPropName, timeStamp);
191                 nodeProps.put(node, propMap);
192             }
193         }
194         return nodeProps;
195     }
196
197     @Override
198     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
199             Boolean refresh) {
200         if (nodeConnectorProps == null)
201             return null;
202
203         if (isDefaultContainer && refresh) {
204             Map<Long, ISwitch> switches = controller.getSwitches();
205             for (ISwitch sw : switches.values()) {
206                 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
207                         .OFSwitchToProps(sw);
208                 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
209                         .entrySet()) {
210                     updateNodeConnector(entry.getKey(), UpdateType.ADDED,
211                             entry.getValue());
212                 }
213             }
214         }
215
216         return nodeConnectorProps;
217     }
218
219     @Override
220     public void updateNodeConnector(NodeConnector nodeConnector,
221             UpdateType type, Set<Property> props) {
222         logger.trace("updateNodeConnector {} type {}", nodeConnector,
223                 type.getName());
224         if (nodeConnectorProps == null) {
225             logger.trace("nodeConnectorProps is null");
226             return;
227         }
228
229         Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
230         switch (type) {
231         case ADDED:
232         case CHANGED:
233             if (propMap == null)
234                 propMap = new HashMap<String, Property>();
235
236             if (props != null) {
237                 for (Property prop : props) {
238                     propMap.put(prop.getName(), prop);
239                 }
240             }
241             nodeConnectorProps.put(nodeConnector, propMap);
242             break;
243         case REMOVED:
244             nodeConnectorProps.remove(nodeConnector);
245             break;
246         default:
247             return;
248         }
249
250         // update sal and discovery
251         synchronized (pluginOutInventoryServices) {
252             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
253                 service.updateNodeConnector(nodeConnector, type, props);
254             }
255         }
256     }
257
258     private void addNode(Node node, Set<Property> props) {
259         logger.trace("{} added", node);
260         if (nodeProps == null)
261             return;
262
263         // update local cache
264         Map<String, Property> propMap = new HashMap<String, Property>();
265         for (Property prop : props) {
266             propMap.put(prop.getName(), prop);
267         }
268         nodeProps.put(node, propMap);
269
270         // update sal
271         synchronized (pluginOutInventoryServices) {
272             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
273                 service.updateNode(node, UpdateType.ADDED, props);
274             }
275         }
276     }
277
278     private void removeNode(Node node) {
279         logger.trace("{} removed", node);
280         if (nodeProps == null)
281             return;
282
283         // update local cache
284         nodeProps.remove(node);
285
286         Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
287         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
288             if (nodeConnector.getNode().equals(node)) {
289                 removeSet.add(nodeConnector);
290             }
291         }
292         for (NodeConnector nodeConnector : removeSet) {
293             nodeConnectorProps.remove(nodeConnector);
294         }
295
296         // update sal
297         synchronized (pluginOutInventoryServices) {
298             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
299                 service.updateNode(node, UpdateType.REMOVED, null);
300             }
301         }
302     }
303
304     private void updateSwitchProperty(Long switchId, Set<Property> propSet) {
305         // update local cache
306         Node node = OFSwitchToNode(controller.getSwitch(switchId));
307         Map<String, Property> propMap = nodeProps.get(node);
308         if (propMap == null) {
309             propMap = new HashMap<String, Property>();
310         }
311
312         boolean change = false;
313         for (Property prop : propSet) {
314             String propertyName = prop.getName();
315             Property currentProp = propMap.get(propertyName);
316             if (!prop.equals(currentProp)) {
317                 change = true;
318                 propMap.put(propertyName, prop);
319             }
320         }
321         nodeProps.put(node, propMap);
322
323         // Update sal if any of the properties has changed
324         if (change) {
325             synchronized (pluginOutInventoryServices) {
326                 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
327                     service.updateNode(node, UpdateType.CHANGED, propSet);
328                 }
329             }
330         }
331     }
332
333     @Override
334     public void updateNode(Node node, UpdateType type, Set<Property> props) {
335         switch (type) {
336         case ADDED:
337             addNode(node, props);
338             break;
339         case REMOVED:
340             removeNode(node);
341             break;
342         default:
343             break;
344         }
345     }
346
347     @Override
348     public void descriptionRefreshed(Long switchId,
349             OFDescriptionStatistics descriptionStats) {
350
351         Set<Property> propSet = new HashSet<Property>(1);
352         Description desc = new Description(
353                 descriptionStats.getDatapathDescription());
354         propSet.add(desc);
355         this.updateSwitchProperty(switchId, propSet);
356     }
357 }