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