- SAL Inventory needs to be modified to be able to take multiple IPluginInInventorySe...
[controller.git] / opendaylight / 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.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.CopyOnWriteArrayList;
18
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.UpdateType;
23 import org.opendaylight.controller.sal.inventory.IInventoryService;
24 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
25 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
26 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The class describes SAL service to bridge inventory protocol plugin and upper
32  * applications. One instance per container of the network.
33  */
34 public class Inventory implements IPluginOutInventoryService, IInventoryService {
35     protected static final Logger logger = LoggerFactory
36             .getLogger(Inventory.class);
37     private List<IListenInventoryUpdates> updateService = new CopyOnWriteArrayList<IListenInventoryUpdates>();
38     private List<IPluginInInventoryService> pluginService = new CopyOnWriteArrayList<IPluginInInventoryService>();
39
40     /**
41      * Function called by the dependency manager when all the required
42      * dependencies are satisfied
43      *
44      */
45     void init() {
46         logger.trace("INIT called!");
47     }
48
49     /**
50      * Function called by the dependency manager when at least one
51      * dependency become unsatisfied or when the component is shutting
52      * down because for example bundle is being stopped.
53      *
54      */
55     void destroy() {
56         logger.trace("DESTROY called!");
57     }
58
59     /**
60      * Function called by dependency manager after "init ()" is called
61      * and after the services provided by the class are registered in
62      * the service registry
63      *
64      */
65     void start() {
66         logger.trace("START called!");
67
68         if (pluginService == null) {
69             logger.debug("plugin service not avaiable");
70             return;
71         }
72     }
73
74     /**
75      * Function called by the dependency manager before the services
76      * exported by the component are unregistered, this will be
77      * followed by a "destroy ()" calls
78      *
79      */
80     void stop() {
81         logger.trace("STOP called!");
82     }
83
84     public void setPluginService(IPluginInInventoryService service) {
85         logger.trace("Got plugin service set request {}", service);
86         this.pluginService.add(service);
87     }
88
89     public void unsetPluginService(IPluginInInventoryService service) {
90         logger.trace("Got plugin service UNset request");
91         this.pluginService.remove(service);
92     }
93
94     public void setUpdateService(IListenInventoryUpdates service) {
95         logger.trace("Got update service set request {}", service);
96         this.updateService.add(service);
97     }
98
99     public void unsetUpdateService(IListenInventoryUpdates service) {
100         logger.trace("Got a service UNset request");
101         this.updateService.remove(service);
102     }
103
104     @Override
105     public void updateNode(Node node, UpdateType type, Set<Property> props) {
106         if (type == null) {
107             logger.trace("Input type is null");
108             return;
109         }
110
111         logger.trace("{} {}", node, type);
112
113         for (IListenInventoryUpdates s : this.updateService) {
114             s.updateNode(node, type, props);
115         }
116     }
117
118     @Override
119     public void updateNodeConnector(NodeConnector nodeConnector,
120             UpdateType type, Set<Property> props) {
121         if (type == null) {
122             logger.trace("Input type is null");
123             return;
124         }
125
126         logger.trace("{} {}", nodeConnector, type);
127
128         for (IListenInventoryUpdates s : this.updateService) {
129             s.updateNodeConnector(nodeConnector, type, props);
130         }
131     }
132
133     @Override
134     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
135         ConcurrentMap<Node, Map<String, Property>> nodeProps =
136             new ConcurrentHashMap<Node, Map<String, Property>>(), rv;
137
138         for (IPluginInInventoryService s : this.pluginService) {
139             rv = s.getNodeProps();
140             if (rv != null) {
141                 nodeProps.putAll(rv);
142             }
143         }
144
145         return nodeProps;
146     }
147
148     @Override
149     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps() {
150         ConcurrentMap<NodeConnector, Map<String, Property>> ncProps =
151             new ConcurrentHashMap<NodeConnector, Map<String, Property>>(), rv;
152
153         for (IPluginInInventoryService s : this.pluginService) {
154             rv = s.getNodeConnectorProps(true);
155             if (rv != null) {
156                 ncProps.putAll(rv);
157             }
158         }
159
160         return ncProps;
161     }
162 }