Move stats caching to FM StatisticsManager
[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.core.Node;
21 import org.opendaylight.controller.sal.core.Node.NodeIDType;
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.core.NodeTable;
24 import org.opendaylight.controller.sal.flowprogrammer.Flow;
25 import org.opendaylight.controller.sal.reader.FlowOnNode;
26 import org.opendaylight.controller.sal.reader.IPluginInReadService;
27 import org.opendaylight.controller.sal.reader.IPluginOutReadService;
28 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
29 import org.opendaylight.controller.sal.reader.NodeDescription;
30 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Container Instance of IPluginInReadService implementation class
36  */
37 public class ReadService implements IPluginInReadService, IReadFilterInternalListener {
38     private static final Logger logger = LoggerFactory
39             .getLogger(ReadService.class);
40     private IReadServiceFilter filter;
41     private Set<IPluginOutReadService> pluginOutReadServices;
42     private String containerName;
43
44     /**
45      * Function called by the dependency manager when all the required
46      * dependencies are satisfied
47      *
48      */
49     @SuppressWarnings("unchecked")
50     void init(Component c) {
51         Dictionary<Object, Object> props = c.getServiceProperties();
52         containerName = (props != null) ? (String) props.get("containerName")
53                 : null;
54         pluginOutReadServices = new CopyOnWriteArraySet<IPluginOutReadService>();
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     }
65
66     /**
67      * Function called by dependency manager after "init ()" is called
68      * and after the services provided by the class are registered in
69      * the service registry
70      *
71      */
72     void start() {
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     }
83
84     public void setService(IReadServiceFilter filter) {
85         this.filter = filter;
86     }
87
88     public void unsetService(IReadServiceFilter filter) {
89         this.filter = null;
90     }
91
92     public void setPluginOutReadServices(IPluginOutReadService service) {
93         logger.trace("Got a service set request {}", service);
94         if (this.pluginOutReadServices != null) {
95             this.pluginOutReadServices.add(service);
96         }
97     }
98
99     public void unsetPluginOutReadServices(
100             IPluginOutReadService service) {
101         logger.trace("Got a service UNset request");
102         if (this.pluginOutReadServices != null) {
103             this.pluginOutReadServices.remove(service);
104         }
105     }
106     @Override
107     public FlowOnNode readFlow(Node node, Flow flow, boolean cached) {
108         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
109             logger.error("Invalid node type");
110             return null;
111         }
112
113         return filter.readFlow(containerName, node, flow, cached);
114     }
115
116     @Override
117     public List<FlowOnNode> readAllFlow(Node node, boolean cached) {
118         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
119             logger.error("Invalid node type");
120             return null;
121         }
122
123         return filter.readAllFlow(containerName, node, cached);
124     }
125
126     @Override
127     public NodeDescription readDescription(Node node, boolean cached) {
128         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
129             logger.error("Invalid node type");
130             return null;
131         }
132
133         return filter.readDescription(node, cached);
134     }
135
136     @Override
137     public NodeConnectorStatistics readNodeConnector(NodeConnector connector,
138             boolean cached) {
139         if (!connector.getNode().getType()
140             .equals(NodeIDType.OPENFLOW)) {
141             logger.error("Invalid node type");
142             return null;
143         }
144         return filter.readNodeConnector(containerName, connector, cached);
145     }
146
147     @Override
148     public List<NodeConnectorStatistics> readAllNodeConnector(Node node,
149             boolean cached) {
150         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
151             logger.error("Invalid node type");
152             return null;
153         }
154
155         return filter.readAllNodeConnector(containerName, node, cached);
156     }
157
158     @Override
159     public long getTransmitRate(NodeConnector connector) {
160         if (!connector.getNode().getType()
161             .equals(NodeIDType.OPENFLOW)) {
162             logger.error("Invalid node type");
163             return 0;
164         }
165         return filter.getTransmitRate(containerName, connector);
166     }
167
168     @Override
169     public NodeTableStatistics readNodeTable(NodeTable table, boolean cached) {
170         if (!table.getNode().getType()
171                 .equals(NodeIDType.OPENFLOW)) {
172             logger.error("Invalid node type");
173             return null;
174         }
175         return filter.readNodeTable(containerName, table, cached);
176     }
177
178     @Override
179     public List<NodeTableStatistics> readAllNodeTable(Node node, boolean cached) {
180         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
181             logger.error("Invalid node type");
182             return null;
183         }
184
185         return filter.readAllNodeTable(containerName, node, cached);
186     }
187
188     @Override
189     public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
190         for (IPluginOutReadService service : pluginOutReadServices) {
191             service.nodeFlowStatisticsUpdated(node, flowStatsList);
192         }
193     }
194
195     @Override
196     public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
197         for (IPluginOutReadService service : pluginOutReadServices) {
198             service.nodeConnectorStatisticsUpdated(node, ncStatsList);
199         }
200     }
201
202     @Override
203     public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
204         for (IPluginOutReadService service : pluginOutReadServices) {
205             service.nodeTableStatisticsUpdated(node, tableStatsList);
206         }
207     }
208
209     @Override
210     public void nodeDescriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
211         for (IPluginOutReadService service : pluginOutReadServices) {
212             service.descriptionStatisticsUpdated(node, nodeDescription);
213         }
214     }
215 }