OpenFlow Protocol_plugin changes to make use of the Connection Manager infrastructure...
[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.Dictionary;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.CopyOnWriteArraySet;
19
20 import org.apache.felix.dm.Component;
21 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryProvider;
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.connection.IPluginOutConnectionService;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.core.Property;
29 import org.opendaylight.controller.sal.core.UpdateType;
30 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
31 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
32 import org.opendaylight.controller.sal.utils.GlobalConstants;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The class describes inventory service protocol plugin. One instance per
38  * container of the network. Each instance gets container specific inventory
39  * events from InventoryServiceShim. It interacts with SAL to pass inventory
40  * data to the upper application.
41  *
42  *
43  */
44 public class InventoryService implements IInventoryShimInternalListener,
45         IPluginInInventoryService, IInventoryProvider {
46     protected static final Logger logger = LoggerFactory
47             .getLogger(InventoryService.class);
48     private Set<IPluginOutInventoryService> pluginOutInventoryServices;
49     private IController controller = null;
50     private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
51     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
52     private boolean isDefaultContainer = false;
53     private String containerName = null;
54
55     void setController(IController s) {
56         this.controller = s;
57     }
58
59     void unsetController(IController s) {
60         if (this.controller == s) {
61             this.controller = null;
62         }
63     }
64
65     /**
66      * Function called by the dependency manager when all the required
67      * dependencies are satisfied
68      *
69      */
70     @SuppressWarnings("rawtypes")
71     void init(Component c) {
72         logger.trace("INIT called!");
73
74         Dictionary props = c.getServiceProperties();
75         if (props != null) {
76             containerName = (String) props.get("containerName");
77             if (containerName != null) {
78                 isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
79                         .toString());
80             }
81         }
82
83         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
84         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
85         pluginOutInventoryServices = new CopyOnWriteArraySet<IPluginOutInventoryService>();
86     }
87
88     /**
89      * Function called by the dependency manager when at least one dependency
90      * become unsatisfied or when the component is shutting down because for
91      * example bundle is being stopped.
92      *
93      */
94     void destroy() {
95         logger.trace("DESTROY called!");
96     }
97
98     /**
99      * Function called by dependency manager after "init ()" is called and after
100      * the services provided by the class are registered in the service registry
101      *
102      */
103     void start() {
104         logger.trace("START called!");
105     }
106
107     /**
108      * Function called by the dependency manager before the services exported by
109      * the component are unregistered, this will be followed by a "destroy ()"
110      * calls
111      *
112      */
113     void stop() {
114         logger.trace("STOP called!");
115     }
116
117     public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
118         logger.trace("Got a service set request {}", service);
119         if (this.pluginOutInventoryServices != null) {
120             this.pluginOutInventoryServices.add(service);
121         }
122     }
123
124     public void unsetPluginOutInventoryServices(
125             IPluginOutInventoryService service) {
126         logger.trace("Got a service UNset request");
127         if (this.pluginOutInventoryServices != null) {
128             this.pluginOutInventoryServices.remove(service);
129         }
130     }
131
132     /**
133      * Retrieve nodes from openflow
134      */
135     @Override
136     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
137         logger.debug("getNodePros for container {}", containerName);
138         return nodeProps;
139     }
140
141     @Override
142     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
143             Boolean refresh) {
144         if (nodeConnectorProps == null) {
145             return null;
146         }
147
148         if (isDefaultContainer && refresh) {
149             Map<Long, ISwitch> switches = controller.getSwitches();
150             for (ISwitch sw : switches.values()) {
151                 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
152                         .OFSwitchToProps(sw);
153                 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
154                         .entrySet()) {
155                     updateNodeConnector(entry.getKey(), UpdateType.ADDED,
156                             entry.getValue());
157                 }
158             }
159         }
160
161         return nodeConnectorProps;
162     }
163
164     @Override
165     public void updateNodeConnector(NodeConnector nodeConnector,
166             UpdateType type, Set<Property> props) {
167         logger.trace("updateNodeConnector {} type {}", nodeConnector,
168                 type.getName());
169         if (nodeConnectorProps == null) {
170             logger.trace("nodeConnectorProps is null");
171             return;
172         }
173
174         Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
175         switch (type) {
176         case ADDED:
177         case CHANGED:
178             if (propMap == null) {
179                 propMap = new HashMap<String, Property>();
180             }
181             if (props != null) {
182                 for (Property prop : props) {
183                     propMap.put(prop.getName(), prop);
184                 }
185             }
186             nodeConnectorProps.put(nodeConnector, propMap);
187             break;
188         case REMOVED:
189             nodeConnectorProps.remove(nodeConnector);
190             break;
191         default:
192             return;
193         }
194
195         // update sal and discovery
196         for (IPluginOutInventoryService service : pluginOutInventoryServices) {
197             service.updateNodeConnector(nodeConnector, type, props);
198         }
199
200     }
201
202     private void addNode(Node node, Set<Property> props) {
203         if (nodeProps == null) {
204             return;
205         }
206
207         logger.trace("addNode: {} added, props: {} for container {}",
208                 new Object[] { node, props, containerName });
209
210         // update local cache
211         Map<String, Property> propMap = nodeProps.get(node);
212         if (propMap == null) {
213             propMap = new HashMap<String, Property>();
214         }
215
216         if (props != null) {
217             for (Property prop : props) {
218                 propMap.put(prop.getName(), prop);
219             }
220         }
221         nodeProps.put(node, propMap);
222
223         // update sal
224         for (IPluginOutInventoryService service : pluginOutInventoryServices) {
225             service.updateNode(node, UpdateType.ADDED, props);
226         }
227     }
228
229     private void removeNode(Node node) {
230         logger.trace("{} removed", node);
231         if (nodeProps == null)
232             return;
233
234         // update local cache
235         nodeProps.remove(node);
236
237         Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
238         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
239             if (nodeConnector.getNode().equals(node)) {
240                 removeSet.add(nodeConnector);
241             }
242         }
243         for (NodeConnector nodeConnector : removeSet) {
244             nodeConnectorProps.remove(nodeConnector);
245         }
246
247         // update sal
248         for (IPluginOutInventoryService service : pluginOutInventoryServices) {
249             service.updateNode(node, UpdateType.REMOVED, null);
250         }
251     }
252
253     private void updateNode(Node node, Set<Property> properties) {
254         logger.trace("{} updated, props: {}", node, properties);
255         if (nodeProps == null || !nodeProps.containsKey(node) ||
256                 properties == null || properties.isEmpty()) {
257             return;
258         }
259
260         // Update local cache with new properties
261         Set<Property> newProperties = new HashSet<Property>(properties.size());
262         Map<String, Property> propertyMap = nodeProps.get(node);
263         for (Property property : properties) {
264             String name = property.getName();
265             Property currentProperty = propertyMap.get(name);
266             if (!property.equals(currentProperty)) {
267                 propertyMap.put(name, property);
268                 newProperties.add(property);
269             }
270         }
271
272         // Update SAL if we got new properties
273         if (!newProperties.isEmpty()) {
274             for (IPluginOutInventoryService service : pluginOutInventoryServices) {
275                 service.updateNode(node, UpdateType.CHANGED, newProperties);
276             }
277         }
278     }
279
280     @Override
281     public void updateNode(Node node, UpdateType type, Set<Property> props) {
282         switch (type) {
283         case ADDED:
284             addNode(node, props);
285             break;
286         case REMOVED:
287             removeNode(node);
288             break;
289         case CHANGED:
290             updateNode(node, props);
291             break;
292         default:
293             break;
294         }
295     }
296 }