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