blob: ff94ab03a11c31683b31e8a7574f13197e891642 [file] [log] [blame]
Carmelo Cascone0831efb2016-05-31 14:50:19 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.provider.bmv2.device.impl;
18
19import com.google.common.collect.Lists;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onosproject.bmv2.api.runtime.Bmv2Action;
22import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent;
23import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
24import org.onosproject.net.Port;
25import org.onosproject.net.device.DefaultPortStatistics;
26import org.onosproject.net.device.PortStatistics;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Collection;
31import java.util.List;
32
33/**
34 * Utility class to read port statistics from a BMv2 device.
35 */
36final class Bmv2PortStatisticsGetter {
37
38 // TODO: make counters configuration dependent
39
40 private static final String TABLE_NAME = "port_count_table";
41 private static final String ACTION_NAME = "count_packet";
42 private static final String EGRESS_COUNTER = "egress_port_counter";
43 private static final String INGRESS_COUNTER = "ingress_port_counter";
44
45 private static final Logger log = LoggerFactory.getLogger(Bmv2PortStatisticsGetter.class);
46
47 private Bmv2PortStatisticsGetter() {
48 // ban constructor.
49 }
50
51 /**
52 * Returns a collection of port statistics for given ports using the given BMv2 device agent.
53 *
54 * @param deviceAgent a device agent
55 * @param ports a collection of ports
56 * @return a collection of port statistics
57 */
58 static Collection<PortStatistics> getPortStatistics(Bmv2DeviceAgent deviceAgent, Collection<Port> ports) {
59
60 List<PortStatistics> ps = Lists.newArrayList();
61
62 for (Port port : ports) {
63 int portNumber = (int) port.number().toLong();
64 try {
65 Pair<Long, Long> egressCounter = deviceAgent.readCounter(EGRESS_COUNTER, portNumber);
66 Pair<Long, Long> ingressCounter = deviceAgent.readCounter(INGRESS_COUNTER, portNumber);
67 ps.add(DefaultPortStatistics.builder()
68 .setPort(portNumber)
69 .setBytesSent(egressCounter.getLeft())
70 .setPacketsSent(egressCounter.getRight())
71 .setBytesReceived(ingressCounter.getLeft())
72 .setPacketsReceived(ingressCounter.getRight())
73 .build());
74 } catch (Bmv2RuntimeException e) {
75 log.info("Unable to read port statistics from {}: {}", port, e.explain());
76 }
77 }
78
79 return ps;
80 }
81
82 /**
83 * Initialize port counters on the given device agent.
84 *
85 * @param deviceAgent a device agent.
86 */
87 static void initCounters(Bmv2DeviceAgent deviceAgent) {
88 try {
89 deviceAgent.setTableDefaultAction(TABLE_NAME, Bmv2Action.builder().withName(ACTION_NAME).build());
90 } catch (Bmv2RuntimeException e) {
91 log.debug("Failed to provision counters on {}: {}", deviceAgent.deviceId(), e.explain());
92 }
93 }
94}
95