Fix Connection manager to retrieve inventory when is up
[controller.git] / opendaylight / protocol_plugins / stub / src / main / java / org / opendaylight / controller / protocol_plugins / stub / internal / InventoryService.java
1 package org.opendaylight.controller.protocol_plugins.stub.internal;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.Dictionary;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.CopyOnWriteArraySet;
16
17 import org.apache.felix.dm.Component;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.opendaylight.controller.sal.core.Actions;
21 import org.opendaylight.controller.sal.core.Bandwidth;
22 import org.opendaylight.controller.sal.core.Buffers;
23 import org.opendaylight.controller.sal.core.Capabilities;
24 import org.opendaylight.controller.sal.core.Capabilities.CapabilitiesType;
25 import org.opendaylight.controller.sal.core.ConstructionException;
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.State;
30 import org.opendaylight.controller.sal.core.Tables;
31 import org.opendaylight.controller.sal.core.TimeStamp;
32 import org.opendaylight.controller.sal.core.UpdateType;
33 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
34 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
35 import org.opendaylight.controller.sal.utils.NodeCreator;
36 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
37
38 /**
39  * Stub Implementation for IPluginInReadService used by SAL
40  *
41  *
42  */
43 public class InventoryService implements IPluginInInventoryService {
44     private static final Logger logger = LoggerFactory
45             .getLogger(InventoryService.class);
46
47     private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties
48                                                                   // are
49                                                                   // maintained
50                                                                   // in global
51                                                                   // container
52                                                                   // only
53     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties
54                                                                                     // are
55                                                                                     // maintained
56                                                                                     // in
57                                                                                     // global
58                                                                                     // container
59                                                                                     // only
60     private final Set<IPluginOutInventoryService> pluginOutInventoryServices =
61             new CopyOnWriteArraySet<IPluginOutInventoryService>();
62
63     public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
64         logger.trace("Got a service set request {}", service);
65         if (this.pluginOutInventoryServices != null) {
66             this.pluginOutInventoryServices.add(service);
67         }
68     }
69
70     public void unsetPluginOutInventoryServices(IPluginOutInventoryService service) {
71         logger.trace("Got a service UNset request");
72         if (this.pluginOutInventoryServices != null) {
73             this.pluginOutInventoryServices.remove(service);
74         }
75     }
76
77     /**
78      * Function called by the dependency manager when all the required
79      * dependencies are satisfied
80      *
81      */
82     void init() {
83         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
84         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
85         Node.NodeIDType.registerIDType("STUB", Integer.class);
86         NodeConnector.NodeConnectorIDType.registerIDType("STUB", Integer.class,
87                 "STUB");
88
89         setupNodeProps();
90         setupNodeConnectorProps();
91     }
92
93     private void setupNodeConnectorProps() {
94         Map<String, Property> ncPropMap = new HashMap<String, Property>();
95         Capabilities cap = new Capabilities(
96                 CapabilitiesType.FLOW_STATS_CAPABILITY.getValue());
97         ncPropMap.put(Capabilities.CapabilitiesPropName, cap);
98         Bandwidth bw = new Bandwidth(Bandwidth.BW1Gbps);
99         ncPropMap.put(Bandwidth.BandwidthPropName, bw);
100         State st = new State(State.EDGE_UP);
101         ncPropMap.put(State.StatePropName, st);
102
103         // setup property map for all node connectors
104         NodeConnector nc;
105         Node node;
106         try {
107             node = new Node("STUB", new Integer(0xCAFE));
108             nc = new NodeConnector("STUB", 0xCAFE, node);
109         } catch (ConstructionException e) {
110             nc = null;
111             node = null;
112         }
113         nodeConnectorProps.put(nc, ncPropMap);
114
115         try {
116             node = new Node("STUB", 3366);
117             nc = new NodeConnector("STUB", 12, node);
118         } catch (ConstructionException e) {
119             nc = null;
120             node = null;
121         }
122         nodeConnectorProps.put(nc, ncPropMap);
123
124         try {
125             node = new Node("STUB", 4477);
126             nc = new NodeConnector("STUB", 34, node);
127         } catch (ConstructionException e) {
128             nc = null;
129             node = null;
130         }
131         nodeConnectorProps.put(nc, ncPropMap);
132
133     }
134
135     private void setupNodeProps() {
136         Map<String, Property> propMap = new HashMap<String, Property>();
137
138         Tables t = new Tables((byte) 1);
139         propMap.put(Tables.TablesPropName, t);
140         Capabilities c = new Capabilities((int) 3);
141         propMap.put(Capabilities.CapabilitiesPropName, c);
142         Actions a = new Actions((int) 2);
143         propMap.put(Actions.ActionsPropName, a);
144         Buffers b = new Buffers((int) 1);
145         propMap.put(Buffers.BuffersPropName, b);
146         Long connectedSinceTime = 100000L;
147         TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
148                 "connectedSince");
149         propMap.put(TimeStamp.TimeStampPropName, timeStamp);
150
151         // setup property map for all nodes
152         Node node;
153         try {
154             node = new Node("STUB", new Integer(0xCAFE));
155         } catch (ConstructionException e) {
156             node = null;
157         }
158
159         nodeProps.put(node, propMap);
160
161         try {
162             node = new Node("STUB", 3366);
163         } catch (ConstructionException e) {
164             node = null;
165         }
166         nodeProps.put(node, propMap);
167
168         try {
169             node = new Node("STUB", 4477);
170         } catch (ConstructionException e) {
171             node = null;
172         }
173         nodeProps.put(node, propMap);
174
175     }
176
177     /**
178      * Function called by the dependency manager when at least one dependency
179      * become unsatisfied or when the component is shutting down because for
180      * example bundle is being stopped.
181      *
182      */
183     void destroy() {
184     }
185
186     /**
187      * Function called by dependency manager after "init ()" is called and after
188      * the services provided by the class are registered in the service registry
189      *
190      */
191     void start() {
192     }
193
194     /**
195      * Method called when the plugin has exposed it's services, this will be
196      * used to publish the updates so connection manager can think the
197      * connection is local
198      */
199     void started() {
200         // update sal and discovery
201         for (IPluginOutInventoryService service : pluginOutInventoryServices) {
202             for (Node node : nodeProps.keySet()) {
203                 Set<Property> props = new HashSet<Property>(nodeProps.get(node)
204                         .values());
205                 service.updateNode(node, UpdateType.ADDED, props);
206                 logger.trace("Adding Node {} with props {}", node, props);
207             }
208             for (NodeConnector nc : nodeConnectorProps.keySet()) {
209                 Set<Property> props = new HashSet<Property>(nodeConnectorProps.get(nc)
210                         .values());
211                 service.updateNodeConnector(nc, UpdateType.ADDED, props);
212                 logger.trace("Adding NodeConnectors {} with props {}", nc, props);
213             }
214         }
215     }
216
217     /**
218      * Function called by the dependency manager before the services exported by
219      * the component are unregistered, this will be followed by a "destroy ()"
220      * calls
221      *
222      */
223     void stop() {
224         pluginOutInventoryServices.clear();
225     }
226
227     /**
228      * Retrieve nodes from openflow
229      */
230     @Override
231     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
232         return nodeProps;
233     }
234
235     /**
236      * Retrieve nodeConnectors from openflow
237      */
238     @Override
239     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
240             Boolean refresh) {
241         return nodeConnectorProps;
242     }
243
244 }