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