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