2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.genius.interfacemanager.pmcounters;
10 import com.google.common.util.concurrent.ThreadFactoryBuilder;
11 import java.lang.Thread.UncaughtExceptionHandler;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.ThreadFactory;
22 import java.util.concurrent.TimeUnit;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class NodeConnectorStatsImpl extends AbstractDataChangeListener<Node>{
52 private static final Logger logger = LoggerFactory.getLogger(NodeConnectorStatsImpl.class);
53 private static final String STATS_POLL_FLAG = "interfacemgr.pmcounters.poll";
54 private static final int THREAD_POOL_SIZE = 4;
55 private static final int NO_DELAY = 0;
56 public static final PMAgentForNodeConnectorCounters pmagent = new PMAgentForNodeConnectorCounters();
57 private PortRpcStatisticsListener portStatsListener = new PortRpcStatisticsListener();
58 private FlowRpcStatisticsListener flowTableStatsListener = new FlowRpcStatisticsListener();
59 private List<BigInteger> nodes = new ArrayList<>();
60 Map<String, Map<String, String>> nodeAndNcIdOFPortDurationMap = new ConcurrentHashMap<>();
61 Map<String, Map<String, String>> nodeAndNcIdOFPortReceiveDropMap = new ConcurrentHashMap<>();
62 Map<String, Map<String, String>> nodeAndNcIdOFPortReceiveError = new ConcurrentHashMap<>();
63 Map<String, Map<String, String>> nodeAndNcIdPacketSentMap = new ConcurrentHashMap<>();
64 Map<String, Map<String, String>> nodeAndNcIdPacketReceiveMap = new ConcurrentHashMap<>();
65 Map<String, Map<String, String>> nodeAndNcIdBytesSentMap = new ConcurrentHashMap<>();
66 Map<String, Map<String, String>> nodeAndNcIdBytesReceiveMap = new ConcurrentHashMap<>();
67 Map<String, Map<String, String>> nodeAndEntriesPerOFTableMap = new ConcurrentHashMap<>();
68 private ScheduledFuture<?> scheduledResult;
69 private OpendaylightPortStatisticsService statPortService;
70 private ScheduledExecutorService portStatExecutorService;
71 private OpendaylightFlowTableStatisticsService opendaylightFlowTableStatisticsService;
72 public NodeConnectorStatsImpl(DataBroker db, NotificationService notificationService, OpendaylightPortStatisticsService statPortService, OpendaylightFlowTableStatisticsService opendaylightFlowTableStatisticsService) {
74 this.statPortService = statPortService;
75 this.opendaylightFlowTableStatisticsService = opendaylightFlowTableStatisticsService;
77 portStatExecutorService = Executors.newScheduledThreadPool(THREAD_POOL_SIZE, getThreadFactory("Port Stats Request Task"));
78 notificationService.registerNotificationListener(portStatsListener);
79 notificationService.registerNotificationListener(flowTableStatsListener);
80 pmagent.registerMbean();
83 private void registerListener(final DataBroker db) {
85 db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
86 getWildCardPath(), NodeConnectorStatsImpl.this, AsyncDataBroker.DataChangeScope.SUBTREE);
87 } catch (final Exception e) {
88 logger.error("NodeConnectorStatsImpl: DataChange listener registration fail!", e);
89 throw new IllegalStateException("NodeConnectorStatsImpl: registration Listener failed.", e);
93 private InstanceIdentifier<Node> getWildCardPath() {
94 return InstanceIdentifier.create(Nodes.class).child(Node.class);
98 * PortStat request task is started when first DPN gets connected
100 private void schedulePortStatRequestTask() {
101 if (!Boolean.getBoolean(STATS_POLL_FLAG)) {
102 logger.info("Port statistics is turned off");
105 logger.info("Scheduling port statistics request");
106 PortStatRequestTask portStatRequestTask = new PortStatRequestTask();
107 scheduledResult = portStatExecutorService.scheduleAtFixedRate(portStatRequestTask, NO_DELAY, 10000, TimeUnit.MILLISECONDS);
111 * PortStat request task is stopped when last DPN is removed.
113 private void stopPortStatRequestTask() {
114 if(scheduledResult != null) {
115 logger.info("Stopping port statistics request");
116 scheduledResult.cancel(true);
121 * This task queries for node connector statistics as well as flowtables statistics every 10 secs.
122 * Minimum period which can be configured for PMJob is 10 secs.
124 private class PortStatRequestTask implements Runnable {
128 if(logger.isTraceEnabled()) {
129 logger.trace("Requesting port stats - {}");
131 for (BigInteger node : nodes) {
132 logger.trace("Requesting AllNodeConnectorStatistics for node - {}", node);
133 statPortService.getAllNodeConnectorsStatistics(buildGetAllNodeConnectorStatistics(node));
134 opendaylightFlowTableStatisticsService.getFlowTablesStatistics(buildGetFlowTablesStatistics(node));
138 private GetAllNodeConnectorsStatisticsInput buildGetAllNodeConnectorStatistics(BigInteger dpId) {
139 return new GetAllNodeConnectorsStatisticsInputBuilder()
141 new NodeRef(InstanceIdentifier.builder(Nodes.class)
142 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpId.toString()))).build())).build();
145 private GetFlowTablesStatisticsInput buildGetFlowTablesStatistics(BigInteger dpId) {
146 return new GetFlowTablesStatisticsInputBuilder()
148 new NodeRef(InstanceIdentifier.builder(Nodes.class)
149 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpId.toString()))).build())).build();
154 private ThreadFactory getThreadFactory(String threadNameFormat) {
155 ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
156 builder.setNameFormat(threadNameFormat);
157 builder.setUncaughtExceptionHandler( new UncaughtExceptionHandler() {
159 public void uncaughtException(Thread t, Throwable e) {
160 logger.error("Received Uncaught Exception event in Thread: {}", t.getName(), e);
163 return builder.build();
167 * PortRpcStatisticsListener listens for the NodeConnectorStatisticsUpdate and then update the corresponding counter map
169 class PortRpcStatisticsListener implements OpendaylightPortStatisticsListener {
172 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate ncStats) {
173 Map<String, String> ncIdOFPortDurationMap = new HashMap<>();
174 Map<String, String> ncIdOFPortReceiveDropMap = new HashMap<>();
175 Map<String, String> ncIdOFPortReceiveError = new HashMap<>();
176 Map<String, String> ncIdPacketSentMap = new HashMap<>();
177 Map<String, String> ncIdPacketReceiveMap = new HashMap<>();
178 Map<String, String> ncIdBytesSentMap = new HashMap<>();
179 Map<String, String> ncIdBytesReceiveMap = new HashMap<>();
180 List<NodeConnectorStatisticsAndPortNumberMap> ncStatsAndPortMapList = ncStats.getNodeConnectorStatisticsAndPortNumberMap();
181 NodeId nodeId = ncStats.getId();
182 String node = nodeId.getValue().split(":")[1];
183 for (NodeConnectorStatisticsAndPortNumberMap ncStatsAndPortMap : ncStatsAndPortMapList) {
184 NodeConnectorId nodeConnector = ncStatsAndPortMap.getNodeConnectorId();
185 String port = nodeConnector.getValue().split(":")[2];
186 String nodePortStr = "dpnId_" + node + "_portNum_" + port;
187 ncIdOFPortDurationMap.put("OFPortDuration:" + nodePortStr + "_OFPortDuration", ncStatsAndPortMap.getDuration().getSecond().getValue().toString());
188 ncIdOFPortReceiveDropMap.put("PacketsPerOFPortReceiveDrop:" + nodePortStr + "_PacketsPerOFPortReceiveDrop", ncStatsAndPortMap.getReceiveDrops().toString());
189 ncIdOFPortReceiveError.put("PacketsPerOFPortReceiveError:" + nodePortStr + "_PacketsPerOFPortReceiveError", ncStatsAndPortMap.getReceiveErrors().toString());
190 ncIdPacketSentMap.put("PacketsPerOFPortSent:" + nodePortStr + "_PacketsPerOFPortSent", ncStatsAndPortMap.getPackets().getTransmitted().toString());
191 ncIdPacketReceiveMap.put("PacketsPerOFPortReceive:" + nodePortStr + "_PacketsPerOFPortReceive", ncStatsAndPortMap.getPackets().getReceived().toString());
192 ncIdBytesSentMap.put("BytesPerOFPortSent:" + nodePortStr + "_BytesPerOFPortSent", ncStatsAndPortMap.getBytes().getTransmitted().toString());
193 ncIdBytesReceiveMap.put("BytesPerOFPortReceive:" + nodePortStr + "_BytesPerOFPortReceive", ncStatsAndPortMap.getBytes().getReceived().toString());
195 logger.trace("Port Stats {}", ncStatsAndPortMapList);
196 //Storing allNodeConnectorStats(like ncIdOFPortDurationMap) in a map with key as node for easy removal and addition of allNodeConnectorStats.
197 nodeAndNcIdOFPortDurationMap.put(node, ncIdOFPortDurationMap);
198 nodeAndNcIdOFPortReceiveDropMap.put(node, ncIdOFPortReceiveDropMap);
199 nodeAndNcIdOFPortReceiveError.put(node, ncIdOFPortReceiveError);
200 nodeAndNcIdPacketSentMap.put(node, ncIdPacketSentMap);
201 nodeAndNcIdPacketReceiveMap.put(node, ncIdPacketReceiveMap);
202 nodeAndNcIdBytesSentMap.put(node, ncIdBytesSentMap);
203 nodeAndNcIdBytesReceiveMap.put(node, ncIdBytesReceiveMap);
204 //Combining the stats of all nodeconnectors in all nodes. This Map will be stored under MBean which will be queried as regular intervals.
205 ncIdOFPortDurationMap = combineAllNodesStats(nodeAndNcIdOFPortDurationMap);
206 ncIdOFPortReceiveDropMap = combineAllNodesStats(nodeAndNcIdOFPortReceiveDropMap);
207 ncIdOFPortReceiveError = combineAllNodesStats(nodeAndNcIdOFPortReceiveError);
208 ncIdPacketSentMap = combineAllNodesStats(nodeAndNcIdPacketSentMap);
209 ncIdPacketReceiveMap = combineAllNodesStats(nodeAndNcIdPacketReceiveMap);
210 ncIdBytesSentMap = combineAllNodesStats(nodeAndNcIdBytesSentMap);
211 ncIdBytesReceiveMap = combineAllNodesStats(nodeAndNcIdBytesReceiveMap);
212 pmagent.connectToPMAgent(ncIdOFPortDurationMap, ncIdOFPortReceiveDropMap, ncIdOFPortReceiveError, ncIdPacketSentMap, ncIdPacketReceiveMap, ncIdBytesSentMap, ncIdBytesReceiveMap);
216 * Input allNodesStats contains statistics of all nodeConnectors of all nodes. Key is the node and values contains another map with key as node connector and value as statresult.
217 * Output will be a map with key as nodeconnector and value as the statresult. The key contains nodeconnectors of all the nodes.
221 private Map<String, String> combineAllNodesStats(Map<String, Map<String, String>> allNodesStats) {
222 Map<String, String> allNcsStatsMap = new HashMap<>();
223 for (Map.Entry<String, Map<String, String>> entry : allNodesStats.entrySet()) {
224 Map<String, String> ncStatsMap = entry.getValue();
225 for (Map.Entry<String, String> statResult : ncStatsMap.entrySet()) {
226 allNcsStatsMap.put(statResult.getKey(), statResult.getValue());
229 return allNcsStatsMap;
233 * FlowRpcStatisticsListener listens for the FlowTableStatisticsUpdate and then update the corresponding counter map
235 class FlowRpcStatisticsListener implements OpendaylightFlowTableStatisticsListener {
238 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate flowTableStats) {
239 String node = flowTableStats.getId().getValue().split(":")[1];
240 Map<String, String> entriesPerOFTableMap = new HashMap<>();
241 List<FlowTableAndStatisticsMap> flowTableAndStatisticsMapList = flowTableStats.getFlowTableAndStatisticsMap();
242 for (FlowTableAndStatisticsMap flowTableAndStatisticsMap : flowTableAndStatisticsMapList) {
243 String nodeTableStr = "dpnId_" + node + "_table_" + flowTableAndStatisticsMap.getTableId().getValue().toString();
244 entriesPerOFTableMap.put("EntriesPerOFTable:" + nodeTableStr + "_EntriesPerOFTable", flowTableAndStatisticsMap.getActiveFlows().getValue().toString());
246 nodeAndEntriesPerOFTableMap.put(node, entriesPerOFTableMap);
247 entriesPerOFTableMap = combineAllNodesStats(nodeAndEntriesPerOFTableMap);
248 pmagent.connectToPMAgentAndInvokeEntriesPerOFTable(entriesPerOFTableMap);
254 protected void remove(InstanceIdentifier<Node> identifier, Node node) {
255 NodeId nodeId = node.getId();
256 String nodeVal = nodeId.getValue().split(":")[1];
257 BigInteger dpId = new BigInteger(nodeVal);
258 if (nodes.contains(dpId)) {
260 nodeAndNcIdOFPortDurationMap.remove(nodeVal);
261 nodeAndNcIdOFPortReceiveDropMap.remove(nodeVal);
262 nodeAndNcIdOFPortReceiveError.remove(nodeVal);
263 nodeAndNcIdPacketSentMap.remove(nodeVal);
264 nodeAndNcIdPacketReceiveMap.remove(nodeVal);
265 nodeAndNcIdBytesSentMap.remove(nodeVal);
266 nodeAndNcIdBytesReceiveMap.remove(nodeVal);
267 nodeAndEntriesPerOFTableMap.remove(nodeVal);
269 if (nodes.isEmpty()) {
270 stopPortStatRequestTask();
275 protected void update(InstanceIdentifier<Node> identifier, Node original,
277 // TODO Auto-generated method stub
281 protected void add(InstanceIdentifier<Node> identifier, Node node) {
282 NodeId nodeId = node.getId();
283 BigInteger dpId = new BigInteger(nodeId.getValue().split(":")[1]);
284 if (nodes.contains(dpId)) {
288 if (nodes.size() == 1) {
289 schedulePortStatRequestTask();