215216e25f00a8fb3f9538d59902d9daa5e86357
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / ReadService.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.protocol_plugin.openflow.internal;
11
12 import java.util.Dictionary;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.concurrent.CopyOnWriteArraySet;
16
17 import org.apache.felix.dm.Component;
18 import org.opendaylight.controller.protocol_plugin.openflow.IReadFilterInternalListener;
19 import org.opendaylight.controller.protocol_plugin.openflow.IReadServiceFilter;
20 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.core.Node.NodeIDType;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.core.NodeTable;
25 import org.opendaylight.controller.sal.flowprogrammer.Flow;
26 import org.opendaylight.controller.sal.reader.FlowOnNode;
27 import org.opendaylight.controller.sal.reader.IPluginInReadService;
28 import org.opendaylight.controller.sal.reader.IPluginOutReadService;
29 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
30 import org.opendaylight.controller.sal.reader.NodeDescription;
31 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Container Instance of IPluginInReadService implementation class
37  */
38 public class ReadService implements IPluginInReadService, IReadFilterInternalListener {
39     private static final Logger logger = LoggerFactory
40             .getLogger(ReadService.class);
41     private IReadServiceFilter filter;
42     private Set<IPluginOutReadService> pluginOutReadServices = new CopyOnWriteArraySet<IPluginOutReadService>();
43     private String containerName;
44     private IPluginOutConnectionService connectionOutService;
45
46     /**
47      * Function called by the dependency manager when all the required
48      * dependencies are satisfied
49      *
50      */
51     @SuppressWarnings("unchecked")
52     void init(Component c) {
53         Dictionary<Object, Object> props = c.getServiceProperties();
54         containerName = (props != null) ? (String) props.get("containerName") : null;
55     }
56
57     /**
58      * Function called by the dependency manager when at least one
59      * dependency become unsatisfied or when the component is shutting
60      * down because for example bundle is being stopped.
61      *
62      */
63     void destroy() {
64         pluginOutReadServices.clear();
65     }
66
67     /**
68      * Function called by dependency manager after "init ()" is called
69      * and after the services provided by the class are registered in
70      * the service registry
71      *
72      */
73     void start() {
74     }
75
76     /**
77      * Function called by the dependency manager before the services
78      * exported by the component are unregistered, this will be
79      * followed by a "destroy ()" calls
80      *
81      */
82     void stop() {
83     }
84
85     public void setService(IReadServiceFilter filter) {
86         this.filter = filter;
87     }
88
89     public void unsetService(IReadServiceFilter filter) {
90         this.filter = null;
91     }
92
93     public void setPluginOutReadServices(IPluginOutReadService service) {
94         logger.trace("Got a service set request {}", service);
95         if (this.pluginOutReadServices != null) {
96             this.pluginOutReadServices.add(service);
97         }
98     }
99
100     public void unsetPluginOutReadServices(
101             IPluginOutReadService service) {
102         logger.trace("Got a service UNset request");
103         if (this.pluginOutReadServices != null) {
104             this.pluginOutReadServices.remove(service);
105         }
106     }
107     @Override
108     public FlowOnNode readFlow(Node node, Flow flow, boolean cached) {
109         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
110             logger.error("Invalid node type");
111             return null;
112         }
113
114         if (!connectionOutService.isLocal(node)) {
115             logger.debug("This Controller is not the master for the node : " + node);
116             return null;
117         }
118         return filter.readFlow(containerName, node, flow, cached);
119     }
120
121     @Override
122     public List<FlowOnNode> readAllFlow(Node node, boolean cached) {
123         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
124             logger.error("Invalid node type");
125             return null;
126         }
127
128         if (!connectionOutService.isLocal(node)) {
129             logger.debug("This Controller is not the master for the node : " + node);
130             return null;
131         }
132
133         return filter.readAllFlow(containerName, node, cached);
134     }
135
136     @Override
137     public NodeDescription readDescription(Node node, boolean cached) {
138         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
139             logger.error("Invalid node type");
140             return null;
141         }
142
143         if (!connectionOutService.isLocal(node)) {
144             logger.debug("This Controller is not the master for the node : " + node);
145             return null;
146         }
147
148         return filter.readDescription(node, cached);
149     }
150
151     @Override
152     public NodeConnectorStatistics readNodeConnector(NodeConnector connector,
153             boolean cached) {
154         if (!connector.getNode().getType()
155             .equals(NodeIDType.OPENFLOW)) {
156             logger.error("Invalid node type");
157             return null;
158         }
159
160         if (!connectionOutService.isLocal(connector.getNode())) {
161             logger.debug("This Controller is not the master for connector : "+connector);
162             return null;
163         }
164
165         return filter.readNodeConnector(containerName, connector, cached);
166     }
167
168     @Override
169     public List<NodeConnectorStatistics> readAllNodeConnector(Node node,
170             boolean cached) {
171         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
172             logger.error("Invalid node type");
173             return null;
174         }
175
176         if (!connectionOutService.isLocal(node)) {
177             logger.debug("This Controller is not the master for node : " + node);
178             return null;
179         }
180
181         return filter.readAllNodeConnector(containerName, node, cached);
182     }
183
184     @Override
185     public long getTransmitRate(NodeConnector connector) {
186         if (!connector.getNode().getType()
187             .equals(NodeIDType.OPENFLOW)) {
188             logger.error("Invalid node type");
189             return 0;
190         }
191
192         if (!connectionOutService.isLocal(connector.getNode())) {
193             logger.debug("This Controller is not the master for connector : "+connector);
194             return 0;
195         }
196
197         return filter.getTransmitRate(containerName, connector);
198     }
199
200     @Override
201     public NodeTableStatistics readNodeTable(NodeTable table, boolean cached) {
202         if (!table.getNode().getType()
203                 .equals(NodeIDType.OPENFLOW)) {
204             logger.error("Invalid node type");
205             return null;
206         }
207
208         if (!connectionOutService.isLocal(table.getNode())) {
209             logger.debug("This Controller is not the master for connector : "+table);
210             return null;
211         }
212
213         return filter.readNodeTable(containerName, table, cached);
214     }
215
216     @Override
217     public List<NodeTableStatistics> readAllNodeTable(Node node, boolean cached) {
218         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
219             logger.error("Invalid node type");
220             return null;
221         }
222
223         if (!connectionOutService.isLocal(node)) {
224             logger.debug("This Controller is not the master for node : " + node);
225             return null;
226         }
227
228         return filter.readAllNodeTable(containerName, node, cached);
229     }
230
231     @Override
232     public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
233         if (!connectionOutService.isLocal(node)) {
234             logger.debug("This Controller is not the master for node : " + node);
235             return;
236         }
237         for (IPluginOutReadService service : pluginOutReadServices) {
238             service.nodeFlowStatisticsUpdated(node, flowStatsList);
239         }
240     }
241
242     @Override
243     public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
244         if (!connectionOutService.isLocal(node)) {
245             logger.debug("This Controller is not the master for node : " + node);
246             return;
247         }
248         for (IPluginOutReadService service : pluginOutReadServices) {
249             service.nodeConnectorStatisticsUpdated(node, ncStatsList);
250         }
251     }
252
253     @Override
254     public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
255         if (!connectionOutService.isLocal(node)) {
256             logger.debug("This Controller is not the master for node : " + node);
257             return;
258         }
259         for (IPluginOutReadService service : pluginOutReadServices) {
260             service.nodeTableStatisticsUpdated(node, tableStatsList);
261         }
262     }
263
264     @Override
265     public void nodeDescriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
266         if (!connectionOutService.isLocal(node)) {
267             logger.debug("This Controller is not the master for node : " + node);
268             return;
269         }
270         for (IPluginOutReadService service : pluginOutReadServices) {
271             service.descriptionStatisticsUpdated(node, nodeDescription);
272         }
273     }
274
275     void setIPluginOutConnectionService(IPluginOutConnectionService s) {
276         connectionOutService = s;
277     }
278
279     void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
280         if (connectionOutService == s) {
281             connectionOutService = null;
282         }
283     }
284 }