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