OpenDaylight Controller functional modules.
[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.IOFInventoryService;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import org.opendaylight.controller.sal.core.ConstructionException;
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.TimeStamp;
36 import org.opendaylight.controller.sal.core.Tables;
37 import org.opendaylight.controller.sal.core.Actions;
38 import org.opendaylight.controller.sal.core.Buffers;
39 import org.opendaylight.controller.sal.core.Capabilities;
40 import org.opendaylight.controller.sal.core.UpdateType;
41 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
42 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
43 import org.opendaylight.controller.sal.utils.GlobalConstants;
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, IOFInventoryService {
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 default container only
61     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in default 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             e.printStackTrace();
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, entry
211                             .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("NodeConnector id " + nodeConnector.getID()
223                 + " type " + nodeConnector.getType() + " "
224                 + type.getName() + " for Node id "
225                 + nodeConnector.getNode().getID());
226
227         if (nodeConnectorProps == null)
228             return;
229
230         synchronized (nodeConnectorProps) {
231             Map<String, Property> propMap = nodeConnectorProps
232                     .get(nodeConnector);
233             switch (type) {
234             case ADDED:
235             case CHANGED:
236                 if (propMap == null)
237                     propMap = new HashMap<String, Property>();
238
239                 if (props != null) {
240                     for (Property prop : props) {
241                         propMap.put(prop.getName(), prop);
242                     }
243                 }
244                 nodeConnectorProps.put(nodeConnector, propMap);
245                 break;
246             case REMOVED:
247                 nodeConnectorProps.remove(nodeConnector);
248                 break;
249             default:
250                 return;
251             }
252         }
253
254         // update sal and discovery
255         synchronized (pluginOutInventoryServices) {
256             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
257                 service.updateNodeConnector(nodeConnector, type, props);
258             }
259         }
260     }
261
262     private void addNode(Node node, Set<Property> props) {
263         logger.trace("{} added", node);
264         if (nodeProps == null)
265             return;
266
267         // update local cache
268         Map<String, Property> propMap = new HashMap<String, Property>();
269         for (Property prop : props) {
270             propMap.put(prop.getName(), prop);
271         }
272         nodeProps.put(node, propMap);
273
274         // update sal
275         synchronized (pluginOutInventoryServices) {
276             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
277                 service.updateNode(node, UpdateType.ADDED, props);
278             }
279         }
280     }
281
282     private void removeNode(Node node) {
283         logger.trace("{} removed", node);
284         if (nodeProps == null)
285             return;
286
287         // update local cache
288         nodeProps.remove(node);
289
290         Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
291         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
292             if (nodeConnector.getNode().equals(node)) {
293                 removeSet.add(nodeConnector);
294             }
295         }
296         for (NodeConnector nodeConnector : removeSet) {
297             nodeConnectorProps.remove(nodeConnector);
298         }
299
300         // update sal
301         synchronized (pluginOutInventoryServices) {
302             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
303                 service.updateNode(node, UpdateType.REMOVED, null);
304             }
305         }
306     }
307
308     /*
309      * Function called by other protocol plugin modules to notify Inventory Service
310      * that a property has changed for the specified switch
311      */
312     @Override
313     public void updateSwitchProperty(Long switchId, Set<Property> propSet) {
314         // update local cache
315         Node node = OFSwitchToNode(controller.getSwitches().get(switchId));
316         Map<String, Property> propMap = nodeProps.get(node);
317         if (propMap == null)
318             propMap = new HashMap<String, Property>();
319         for (Property prop : propSet) {
320             propMap.put(prop.getName(), prop);
321         }
322         nodeProps.put(node, propMap);
323
324         // update sal
325         synchronized (pluginOutInventoryServices) {
326             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
327                 service.updateNode(node, UpdateType.CHANGED, propSet);
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         default:
342             break;
343         }
344     }
345 }