Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / adsal / sal / implementation / src / main / java / org / opendaylight / controller / sal / implementation / internal / Inventory.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.implementation.internal;
11
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.CopyOnWriteArrayList;
19
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.core.Property;
23 import org.opendaylight.controller.sal.core.UpdateType;
24 import org.opendaylight.controller.sal.inventory.IInventoryService;
25 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
26 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
27 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The class describes SAL service to bridge inventory protocol plugin and upper
33  * applications. One instance per container of the network.
34  */
35 public class Inventory implements IPluginOutInventoryService, IInventoryService {
36     protected static final Logger logger = LoggerFactory
37             .getLogger(Inventory.class);
38     private List<IListenInventoryUpdates> updateService = new CopyOnWriteArrayList<IListenInventoryUpdates>();
39     private List<IPluginInInventoryService> pluginService = new CopyOnWriteArrayList<IPluginInInventoryService>();
40
41     /**
42      * Function called by the dependency manager when all the required
43      * dependencies are satisfied
44      *
45      */
46     void init() {
47         logger.trace("INIT called!");
48     }
49
50     /**
51      * Function called by the dependency manager when at least one
52      * dependency become unsatisfied or when the component is shutting
53      * down because for example bundle is being stopped.
54      *
55      */
56     void destroy() {
57         logger.trace("DESTROY called!");
58     }
59
60     /**
61      * Function called by dependency manager after "init ()" is called
62      * and after the services provided by the class are registered in
63      * the service registry
64      *
65      */
66     void start() {
67         logger.trace("START called!");
68
69         if (pluginService == null) {
70             logger.debug("plugin service not avaiable");
71             return;
72         }
73     }
74
75     /**
76      * Function called by the dependency manager before the services
77      * exported by the component are unregistered, this will be
78      * followed by a "destroy ()" calls
79      *
80      */
81     void stop() {
82         logger.trace("STOP called!");
83     }
84
85     public void setPluginService(IPluginInInventoryService service) {
86         logger.trace("Got plugin service set request {}", service);
87         this.pluginService.add(service);
88     }
89
90     public void unsetPluginService(IPluginInInventoryService service) {
91         logger.trace("Got plugin service UNset request");
92         this.pluginService.remove(service);
93     }
94
95     public void setUpdateService(IListenInventoryUpdates service) {
96         logger.trace("Got update service set request {}", service);
97         this.updateService.add(service);
98     }
99
100     public void unsetUpdateService(IListenInventoryUpdates service) {
101         logger.trace("Got a service UNset request");
102         this.updateService.remove(service);
103     }
104
105     @Override
106     public void updateNode(Node node, UpdateType type, Set<Property> props) {
107         if (type == null) {
108             logger.trace("Input type is null");
109             return;
110         }
111
112         logger.trace("{} {}", node, type);
113
114         for (IListenInventoryUpdates s : this.updateService) {
115             s.updateNode(node, type, props);
116         }
117     }
118
119     @Override
120     public void updateNodeConnector(NodeConnector nodeConnector,
121             UpdateType type, Set<Property> props) {
122         if (type == null) {
123             logger.trace("Input type is null");
124             return;
125         }
126
127         logger.trace("{} {}", nodeConnector, type);
128
129         for (IListenInventoryUpdates s : this.updateService) {
130             s.updateNodeConnector(nodeConnector, type, props);
131         }
132     }
133
134     @Override
135     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
136         ConcurrentMap<Node, Map<String, Property>> nodeProps =
137             new ConcurrentHashMap<Node, Map<String, Property>>(), rv;
138
139         for (IPluginInInventoryService s : this.pluginService) {
140             rv = s.getNodeProps();
141             if (rv != null) {
142                 nodeProps.putAll(rv);
143             }
144         }
145
146         return nodeProps;
147     }
148
149     @Override
150     public Set<Node> getConfiguredNotConnectedNodes() {
151         Set<Node> configuredNotConnected = new HashSet<Node>(), rv;
152         for (IPluginInInventoryService s : this.pluginService) {
153             rv = s.getConfiguredNotConnectedNodes();
154             if (rv != null) {
155                 configuredNotConnected.addAll(rv);
156             }
157         }
158         return configuredNotConnected;
159     }
160
161     @Override
162     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps() {
163         ConcurrentMap<NodeConnector, Map<String, Property>> ncProps =
164             new ConcurrentHashMap<NodeConnector, Map<String, Property>>(), rv;
165
166         for (IPluginInInventoryService s : this.pluginService) {
167             rv = s.getNodeConnectorProps(true);
168             if (rv != null) {
169                 ncProps.putAll(rv);
170             }
171         }
172
173         return ncProps;
174     }
175 }