3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.statisticsmanager.internal;
12 import java.util.ArrayList;
13 import java.util.EnumSet;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
23 import org.opendaylight.controller.clustering.services.CacheConfigException;
24 import org.opendaylight.controller.clustering.services.CacheExistException;
25 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
26 import org.opendaylight.controller.clustering.services.IClusterServices;
27 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
28 import org.opendaylight.controller.sal.core.IContainer;
29 import org.opendaylight.controller.sal.core.Node;
30 import org.opendaylight.controller.sal.core.NodeConnector;
31 import org.opendaylight.controller.sal.core.NodeTable;
32 import org.opendaylight.controller.sal.core.Property;
33 import org.opendaylight.controller.sal.core.UpdateType;
34 import org.opendaylight.controller.sal.flowprogrammer.Flow;
35 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
36 import org.opendaylight.controller.sal.reader.FlowOnNode;
37 import org.opendaylight.controller.sal.reader.IReadService;
38 import org.opendaylight.controller.sal.reader.IReadServiceListener;
39 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
40 import org.opendaylight.controller.sal.reader.NodeDescription;
41 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
42 import org.opendaylight.controller.sal.utils.ServiceHelper;
43 import org.opendaylight.controller.statisticsmanager.IStatisticsManager;
44 import org.opendaylight.controller.switchmanager.ISwitchManager;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The class caches latest network nodes statistics as notified by reader
50 * services and provides API to retrieve them.
52 public class StatisticsManager implements IStatisticsManager, IReadServiceListener, IListenInventoryUpdates {
53 private static final Logger log = LoggerFactory.getLogger(StatisticsManager.class);
54 private IContainer container;
55 private IClusterContainerServices clusterContainerService;
56 private IReadService reader;
58 private ConcurrentMap<Node, List<FlowOnNode>> flowStatistics;
59 private ConcurrentMap<Node, List<NodeConnectorStatistics>> nodeConnectorStatistics;
60 private ConcurrentMap<Node, List<NodeTableStatistics>> tableStatistics;
61 private ConcurrentMap<Node, NodeDescription> descriptionStatistics;
63 private void nonClusterObjectCreate() {
64 flowStatistics = new ConcurrentHashMap<Node, List<FlowOnNode>>();
65 nodeConnectorStatistics = new ConcurrentHashMap<Node, List<NodeConnectorStatistics>>();
66 tableStatistics = new ConcurrentHashMap<Node, List<NodeTableStatistics>>();
67 descriptionStatistics = new ConcurrentHashMap<Node, NodeDescription>();
70 @SuppressWarnings("deprecation")
71 private void allocateCaches() {
72 if (clusterContainerService == null) {
73 nonClusterObjectCreate();
74 log.error("Clustering service unavailable. Allocated non-cluster statistics manager cache.");
79 clusterContainerService.createCache("statisticsmanager.flowStatistics",
80 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
81 clusterContainerService.createCache("statisticsmanager.nodeConnectorStatistics",
82 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
83 clusterContainerService.createCache("statisticsmanager.tableStatistics",
84 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
85 clusterContainerService.createCache("statisticsmanager.descriptionStatistics",
86 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
88 } catch (CacheConfigException cce) {
89 log.error("Statistics cache configuration invalid - check cache mode");
90 } catch (CacheExistException ce) {
91 log.debug("Skipping statistics cache creation - already present");
94 @SuppressWarnings({ "unchecked", "deprecation" })
95 private void retrieveCaches() {
96 ConcurrentMap<?, ?> map;
98 if (this.clusterContainerService == null) {
99 log.warn("Can't retrieve statistics manager cache, Clustering service unavailable.");
103 log.debug("Statistics Manager - retrieveCaches for Container {}", container);
105 map = clusterContainerService.getCache("statisticsmanager.flowStatistics");
107 this.flowStatistics = (ConcurrentMap<Node, List<FlowOnNode>>) map;
109 log.error("Cache allocation failed for statisticsmanager.flowStatistics in container {}", container.getName());
112 map = clusterContainerService.getCache("statisticsmanager.nodeConnectorStatistics");
114 this.nodeConnectorStatistics = (ConcurrentMap<Node, List<NodeConnectorStatistics>>) map;
116 log.error("Cache allocation failed for statisticsmanager.nodeConnectorStatistics in container {}", container.getName());
119 map = clusterContainerService.getCache("statisticsmanager.tableStatistics");
121 this.tableStatistics = (ConcurrentMap<Node, List<NodeTableStatistics>>) map;
123 log.error("Cache allocation failed for statisticsmanager.tableStatistics in container {}", container.getName());
126 map = clusterContainerService.getCache("statisticsmanager.descriptionStatistics");
128 this.descriptionStatistics = (ConcurrentMap<Node, NodeDescription>) map;
130 log.error("Cache allocation failed for statisticsmanager.descriptionStatistics in container {}", container.getName());
135 * Function called by the dependency manager when all the required
136 * dependencies are satisfied
140 log.debug("INIT called!");
147 * Function called by the dependency manager when at least one
148 * dependency become unsatisfied or when the component is shutting
149 * down because for example bundle is being stopped.
153 log.debug("DESTROY called!");
157 * Function called by dependency manager after "init ()" is called
158 * and after the services provided by the class are registered in
159 * the service registry
163 log.debug("START called!");
167 * Function called after registering the service in OSGi service registry.
170 //retrieve current statistics so we don't have to wait for next refresh
171 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(
172 ISwitchManager.class, container.getName(), this);
173 if (reader != null && switchManager != null) {
174 Set<Node> nodeSet = switchManager.getNodes();
175 for (Node node : nodeSet) {
176 flowStatistics.put(node, reader.readAllFlows(node));
177 descriptionStatistics.put(node, reader.readDescription(node));
178 tableStatistics.put(node, reader.readNodeTable(node));
179 nodeConnectorStatistics.put(node, reader.readNodeConnectors(node));
183 log.warn("Failed to retrieve current statistics. Statistics will not be immidiately available!");
188 * Function called by the dependency manager before the services
189 * exported by the component are unregistered, this will be
190 * followed by a "destroy ()" calls
194 log.debug("STOP called!");
197 void setClusterContainerService(IClusterContainerServices s) {
198 log.debug("Cluster Service set for Statistics Mgr");
199 this.clusterContainerService = s;
202 void unsetClusterContainerService(IClusterContainerServices s) {
203 if (this.clusterContainerService == s) {
204 log.debug("Cluster Service removed for Statistics Mgr!");
205 this.clusterContainerService = null;
208 void setIContainer(IContainer c){
211 public void unsetIContainer(IContainer s) {
212 if (this.container == s) {
213 this.container = null;
217 public void setReaderService(IReadService service) {
218 log.debug("Got inventory service set request {}", service);
219 this.reader = service;
222 public void unsetReaderService(IReadService service) {
223 log.debug("Got a service UNset request {}", service);
228 public List<FlowOnNode> getFlows(Node node) {
233 List<FlowOnNode> flowList = new ArrayList<FlowOnNode>();
234 List<FlowOnNode> cachedList = flowStatistics.get(node);
235 if (cachedList != null){
236 flowList.addAll(cachedList);
242 public Map<Node, List<FlowOnNode>> getFlowStatisticsForFlowList(List<FlowEntry> flowList) {
243 Map<Node, List<FlowOnNode>> statMapOutput = new HashMap<Node, List<FlowOnNode>>();
245 if (flowList == null || flowList.isEmpty()){
246 return statMapOutput;
250 //index FlowEntries' flows by node so we don't traverse entire flow list for each flowEntry
251 Map<Node, Set<Flow>> index = new HashMap<Node, Set<Flow>>();
252 for (FlowEntry flowEntry : flowList) {
253 node = flowEntry.getNode();
254 Set<Flow> set = (index.containsKey(node) ? index.get(node) : new HashSet<Flow>());
255 set.add(flowEntry.getFlow());
256 index.put(node, set);
259 //iterate over flows per indexed node and add to output
260 for (Entry<Node, Set<Flow>> indexEntry : index.entrySet()) {
261 node = indexEntry.getKey();
262 List<FlowOnNode> flowsPerNode = flowStatistics.get(node);
264 if (flowsPerNode != null && !flowsPerNode.isEmpty()){
265 List<FlowOnNode> filteredFlows = statMapOutput.containsKey(node) ?
266 statMapOutput.get(node) : new ArrayList<FlowOnNode>();
268 for (FlowOnNode flowOnNode : flowsPerNode) {
269 if (indexEntry.getValue().contains(flowOnNode.getFlow())) {
270 filteredFlows.add(flowOnNode);
273 statMapOutput.put(node, filteredFlows);
276 return statMapOutput;
280 public int getFlowsNumber(Node node) {
282 if (node == null || (l = flowStatistics.get(node)) == null){
289 public NodeDescription getNodeDescription(Node node) {
293 NodeDescription nd = descriptionStatistics.get(node);
294 return nd != null? nd.clone() : null;
298 public NodeConnectorStatistics getNodeConnectorStatistics(NodeConnector nodeConnector) {
299 if (nodeConnector == null){
303 List<NodeConnectorStatistics> statList = nodeConnectorStatistics.get(nodeConnector.getNode());
304 if (statList != null){
305 for (NodeConnectorStatistics stat : statList) {
306 if (stat.getNodeConnector().equals(nodeConnector)){
315 public List<NodeConnectorStatistics> getNodeConnectorStatistics(Node node) {
320 List<NodeConnectorStatistics> statList = new ArrayList<NodeConnectorStatistics>();
321 List<NodeConnectorStatistics> cachedList = nodeConnectorStatistics.get(node);
322 if (cachedList != null) {
323 statList.addAll(cachedList);
329 public NodeTableStatistics getNodeTableStatistics(NodeTable nodeTable) {
330 if (nodeTable == null){
333 List<NodeTableStatistics> statList = tableStatistics.get(nodeTable.getNode());
334 if (statList != null){
335 for (NodeTableStatistics stat : statList) {
336 if (stat.getNodeTable().getID().equals(nodeTable.getID())){
345 public List<NodeTableStatistics> getNodeTableStatistics(Node node){
349 List<NodeTableStatistics> statList = new ArrayList<NodeTableStatistics>();
350 List<NodeTableStatistics> cachedList = tableStatistics.get(node);
351 if (cachedList != null) {
352 statList.addAll(cachedList);
358 public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
359 this.flowStatistics.put(node, flowStatsList);
363 public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
364 this.nodeConnectorStatistics.put(node, ncStatsList);
368 public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
369 this.tableStatistics.put(node, tableStatsList);
373 public void descriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
374 this.descriptionStatistics.put(node, nodeDescription);
378 public void updateNode(Node node, UpdateType type, Set<Property> props) {
379 //if node is removed, remove stats mappings
380 if (type == UpdateType.REMOVED) {
381 flowStatistics.remove(node);
382 nodeConnectorStatistics.remove(node);
383 tableStatistics.remove(node);
384 descriptionStatistics.remove(node);
389 public void updateNodeConnector(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
390 // not interested in this update