OpenFlow Protocol_plugin changes to make use of the Connection Manager infrastructure...
[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;
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")
55                 : null;
56         pluginOutReadServices = new CopyOnWriteArraySet<IPluginOutReadService>();
57     }
58
59     /**
60      * Function called by the dependency manager when at least one
61      * dependency become unsatisfied or when the component is shutting
62      * down because for example bundle is being stopped.
63      *
64      */
65     void destroy() {
66     }
67
68     /**
69      * Function called by dependency manager after "init ()" is called
70      * and after the services provided by the class are registered in
71      * the service registry
72      *
73      */
74     void start() {
75     }
76
77     /**
78      * Function called by the dependency manager before the services
79      * exported by the component are unregistered, this will be
80      * followed by a "destroy ()" calls
81      *
82      */
83     void stop() {
84     }
85
86     public void setService(IReadServiceFilter filter) {
87         this.filter = filter;
88     }
89
90     public void unsetService(IReadServiceFilter filter) {
91         this.filter = null;
92     }
93
94     public void setPluginOutReadServices(IPluginOutReadService service) {
95         logger.trace("Got a service set request {}", service);
96         if (this.pluginOutReadServices != null) {
97             this.pluginOutReadServices.add(service);
98         }
99     }
100
101     public void unsetPluginOutReadServices(
102             IPluginOutReadService service) {
103         logger.trace("Got a service UNset request");
104         if (this.pluginOutReadServices != null) {
105             this.pluginOutReadServices.remove(service);
106         }
107     }
108     @Override
109     public FlowOnNode readFlow(Node node, Flow flow, boolean cached) {
110         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
111             logger.error("Invalid node type");
112             return null;
113         }
114
115         if (!connectionOutService.isLocal(node)) {
116             logger.debug("This Controller is not the master for the node : " + node);
117             return null;
118         }
119         return filter.readFlow(containerName, node, flow, cached);
120     }
121
122     @Override
123     public List<FlowOnNode> readAllFlow(Node node, boolean cached) {
124         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
125             logger.error("Invalid node type");
126             return null;
127         }
128
129         if (!connectionOutService.isLocal(node)) {
130             logger.debug("This Controller is not the master for the node : " + node);
131             return null;
132         }
133
134         return filter.readAllFlow(containerName, node, cached);
135     }
136
137     @Override
138     public NodeDescription readDescription(Node node, boolean cached) {
139         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
140             logger.error("Invalid node type");
141             return null;
142         }
143
144         if (!connectionOutService.isLocal(node)) {
145             logger.debug("This Controller is not the master for the node : " + node);
146             return null;
147         }
148
149         return filter.readDescription(node, cached);
150     }
151
152     @Override
153     public NodeConnectorStatistics readNodeConnector(NodeConnector connector,
154             boolean cached) {
155         if (!connector.getNode().getType()
156             .equals(NodeIDType.OPENFLOW)) {
157             logger.error("Invalid node type");
158             return null;
159         }
160
161         if (!connectionOutService.isLocal(connector.getNode())) {
162             logger.debug("This Controller is not the master for connector : "+connector);
163             return null;
164         }
165
166         return filter.readNodeConnector(containerName, connector, cached);
167     }
168
169     @Override
170     public List<NodeConnectorStatistics> readAllNodeConnector(Node node,
171             boolean cached) {
172         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
173             logger.error("Invalid node type");
174             return null;
175         }
176
177         if (!connectionOutService.isLocal(node)) {
178             logger.debug("This Controller is not the master for node : " + node);
179             return null;
180         }
181
182         return filter.readAllNodeConnector(containerName, node, cached);
183     }
184
185     @Override
186     public long getTransmitRate(NodeConnector connector) {
187         if (!connector.getNode().getType()
188             .equals(NodeIDType.OPENFLOW)) {
189             logger.error("Invalid node type");
190             return 0;
191         }
192
193         if (!connectionOutService.isLocal(connector.getNode())) {
194             logger.debug("This Controller is not the master for connector : "+connector);
195             return 0;
196         }
197
198         return filter.getTransmitRate(containerName, connector);
199     }
200
201     @Override
202     public NodeTableStatistics readNodeTable(NodeTable table, boolean cached) {
203         if (!table.getNode().getType()
204                 .equals(NodeIDType.OPENFLOW)) {
205             logger.error("Invalid node type");
206             return null;
207         }
208
209         if (!connectionOutService.isLocal(table.getNode())) {
210             logger.debug("This Controller is not the master for connector : "+table);
211             return null;
212         }
213
214         return filter.readNodeTable(containerName, table, cached);
215     }
216
217     @Override
218     public List<NodeTableStatistics> readAllNodeTable(Node node, boolean cached) {
219         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
220             logger.error("Invalid node type");
221             return null;
222         }
223
224         if (!connectionOutService.isLocal(node)) {
225             logger.debug("This Controller is not the master for node : " + node);
226             return null;
227         }
228
229         return filter.readAllNodeTable(containerName, node, cached);
230     }
231
232     @Override
233     public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
234         if (!connectionOutService.isLocal(node)) {
235             logger.debug("This Controller is not the master for node : " + node);
236             return;
237         }
238         for (IPluginOutReadService service : pluginOutReadServices) {
239             service.nodeFlowStatisticsUpdated(node, flowStatsList);
240         }
241     }
242
243     @Override
244     public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
245         if (!connectionOutService.isLocal(node)) {
246             logger.debug("This Controller is not the master for node : " + node);
247             return;
248         }
249         for (IPluginOutReadService service : pluginOutReadServices) {
250             service.nodeConnectorStatisticsUpdated(node, ncStatsList);
251         }
252     }
253
254     @Override
255     public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
256         if (!connectionOutService.isLocal(node)) {
257             logger.debug("This Controller is not the master for node : " + node);
258             return;
259         }
260         for (IPluginOutReadService service : pluginOutReadServices) {
261             service.nodeTableStatisticsUpdated(node, tableStatsList);
262         }
263     }
264
265     @Override
266     public void nodeDescriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
267         if (!connectionOutService.isLocal(node)) {
268             logger.debug("This Controller is not the master for node : " + node);
269             return;
270         }
271         for (IPluginOutReadService service : pluginOutReadServices) {
272             service.descriptionStatisticsUpdated(node, nodeDescription);
273         }
274     }
275
276     void setIPluginOutConnectionService(IPluginOutConnectionService s) {
277         connectionOutService = s;
278     }
279
280     void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
281         if (connectionOutService == s) {
282             connectionOutService = null;
283         }
284     }
285 }