Clean all unused and redundant imports in controller.
[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     /**
69      * Function called by the dependency manager when all the required
70      * dependencies are satisfied
71      *
72      */
73     void init() {
74         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
75         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
76         Node.NodeIDType.registerIDType("STUB", Integer.class);
77         NodeConnector.NodeConnectorIDType.registerIDType("STUB", Integer.class,
78                 "STUB");
79
80         setupNodeProps();
81         setupNodeConnectorProps();
82     }
83
84     private void setupNodeConnectorProps() {
85         Map<String, Property> ncPropMap = new HashMap<String, Property>();
86         Capabilities cap = new Capabilities(
87                 CapabilitiesType.FLOW_STATS_CAPABILITY.getValue());
88         ncPropMap.put(Capabilities.CapabilitiesPropName, cap);
89         Bandwidth bw = new Bandwidth(Bandwidth.BW1Gbps);
90         ncPropMap.put(Bandwidth.BandwidthPropName, bw);
91         State st = new State(State.EDGE_UP);
92         ncPropMap.put(State.StatePropName, st);
93
94         // setup property map for all node connectors
95         NodeConnector nc;
96         Node node;
97         try {
98             node = new Node("STUB", new Integer(0xCAFE));
99             nc = new NodeConnector("STUB", 0xCAFE, node);
100         } catch (ConstructionException e) {
101             nc = null;
102             node = null;
103         }
104         nodeConnectorProps.put(nc, ncPropMap);
105
106         try {
107             node = new Node("STUB", 3366);
108             nc = new NodeConnector("STUB", 12, 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", 4477);
117             nc = new NodeConnector("STUB", 34, node);
118         } catch (ConstructionException e) {
119             nc = null;
120             node = null;
121         }
122         nodeConnectorProps.put(nc, ncPropMap);
123
124     }
125
126     private void setupNodeProps() {
127         Map<String, Property> propMap = new HashMap<String, Property>();
128
129         Tables t = new Tables((byte) 1);
130         propMap.put(Tables.TablesPropName, t);
131         Capabilities c = new Capabilities((int) 3);
132         propMap.put(Capabilities.CapabilitiesPropName, c);
133         Actions a = new Actions((int) 2);
134         propMap.put(Actions.ActionsPropName, a);
135         Buffers b = new Buffers((int) 1);
136         propMap.put(Buffers.BuffersPropName, b);
137         Long connectedSinceTime = 100000L;
138         TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
139                 "connectedSince");
140         propMap.put(TimeStamp.TimeStampPropName, timeStamp);
141
142         // setup property map for all nodes
143         Node node;
144         try {
145             node = new Node("STUB", new Integer(0xCAFE));
146         } catch (ConstructionException e) {
147             node = null;
148         }
149
150         nodeProps.put(node, propMap);
151
152         try {
153             node = new Node("STUB", 3366);
154         } catch (ConstructionException e) {
155             node = null;
156         }
157         nodeProps.put(node, propMap);
158
159         try {
160             node = new Node("STUB", 4477);
161         } catch (ConstructionException e) {
162             node = null;
163         }
164         nodeProps.put(node, propMap);
165
166     }
167
168     /**
169      * Function called by the dependency manager when at least one dependency
170      * become unsatisfied or when the component is shutting down because for
171      * example bundle is being stopped.
172      *
173      */
174     void destroy() {
175     }
176
177     /**
178      * Function called by dependency manager after "init ()" is called and after
179      * the services provided by the class are registered in the service registry
180      *
181      */
182     void start() {
183     }
184
185     /**
186      * Method called when the plugin has exposed it's services, this will be
187      * used to publish the updates so connection manager can think the
188      * connection is local
189      */
190     void started() {
191         // update sal and discovery
192         for (IPluginOutInventoryService service : pluginOutInventoryServices) {
193             for (Node node : nodeProps.keySet()) {
194                 Set<Property> props = new HashSet<Property>(nodeProps.get(node)
195                         .values());
196                 service.updateNode(node, UpdateType.ADDED, props);
197                 logger.trace("Adding Node {} with props {}", node, props);
198             }
199             for (NodeConnector nc : nodeConnectorProps.keySet()) {
200                 Set<Property> props = new HashSet<Property>(nodeConnectorProps.get(nc)
201                         .values());
202                 service.updateNodeConnector(nc, UpdateType.ADDED, props);
203                 logger.trace("Adding NodeConnectors {} with props {}", nc, props);
204             }
205         }
206     }
207
208     /**
209      * Function called by the dependency manager before the services exported by
210      * the component are unregistered, this will be followed by a "destroy ()"
211      * calls
212      *
213      */
214     void stop() {
215         pluginOutInventoryServices.clear();
216     }
217
218     /**
219      * Retrieve nodes from openflow
220      */
221     @Override
222     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
223         return nodeProps;
224     }
225
226     /**
227      * Retrieve nodeConnectors from openflow
228      */
229     @Override
230     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
231             Boolean refresh) {
232         return nodeConnectorProps;
233     }
234
235 }