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