Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.drivers.gnmi; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 20 | import com.google.common.util.concurrent.Futures; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 21 | import gnmi.Gnmi; |
| 22 | import gnmi.Gnmi.GetRequest; |
| 23 | import gnmi.Gnmi.GetResponse; |
| 24 | import gnmi.Gnmi.Path; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 25 | import org.onosproject.gnmi.api.GnmiClient; |
| 26 | import org.onosproject.gnmi.api.GnmiController; |
| 27 | import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.device.DefaultPortStatistics; |
| 31 | import org.onosproject.net.device.PortStatistics; |
| 32 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 33 | |
| 34 | import java.time.Duration; |
| 35 | import java.time.temporal.ChronoUnit; |
| 36 | import java.util.Collection; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | /** |
| 43 | * Behaviour to get port statistics from device via gNMI. |
| 44 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 45 | public class OpenConfigGnmiPortStatisticsDiscovery |
| 46 | extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController> |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 47 | implements PortStatisticsDiscovery { |
| 48 | |
pierventre | 50696a7 | 2021-06-01 12:26:36 +0200 | [diff] [blame^] | 49 | private static final String LAST_CHANGE = "last-change"; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 50 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 51 | public OpenConfigGnmiPortStatisticsDiscovery() { |
| 52 | super(GnmiController.class); |
| 53 | } |
| 54 | |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 55 | @Override |
| 56 | public Collection<PortStatistics> discoverPortStatistics() { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 57 | if (!setupBehaviour("discoverPortStatistics()")) { |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 58 | return Collections.emptyList(); |
| 59 | } |
| 60 | |
| 61 | Map<String, PortNumber> ifacePortNumberMapping = Maps.newHashMap(); |
| 62 | List<Port> ports = deviceService.getPorts(deviceId); |
| 63 | GetRequest.Builder getRequest = GetRequest.newBuilder(); |
| 64 | getRequest.setEncoding(Gnmi.Encoding.PROTO); |
| 65 | |
| 66 | // Use this path to get all counters from specific interface(port) |
| 67 | // /interfaces/interface[port-name]/state/counters/[counter name] |
| 68 | ports.forEach(port -> { |
| 69 | String portName = port.number().name(); |
| 70 | Path path = interfaceCounterPath(portName); |
| 71 | getRequest.addPath(path); |
| 72 | ifacePortNumberMapping.put(portName, port.number()); |
| 73 | }); |
| 74 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 75 | GetResponse getResponse = Futures.getUnchecked(client.get(getRequest.build())); |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 76 | |
| 77 | Map<String, Long> inPkts = Maps.newHashMap(); |
| 78 | Map<String, Long> outPkts = Maps.newHashMap(); |
| 79 | Map<String, Long> inBytes = Maps.newHashMap(); |
| 80 | Map<String, Long> outBytes = Maps.newHashMap(); |
| 81 | Map<String, Long> inDropped = Maps.newHashMap(); |
| 82 | Map<String, Long> outDropped = Maps.newHashMap(); |
| 83 | Map<String, Long> inErrors = Maps.newHashMap(); |
| 84 | Map<String, Long> outErrors = Maps.newHashMap(); |
| 85 | Map<String, Duration> timestamps = Maps.newHashMap(); |
| 86 | |
| 87 | // Collect responses and sum {in,out,dropped} packets |
| 88 | getResponse.getNotificationList().forEach(notification -> { |
| 89 | notification.getUpdateList().forEach(update -> { |
| 90 | Path path = update.getPath(); |
| 91 | String ifName = interfaceNameFromPath(path); |
| 92 | timestamps.putIfAbsent(ifName, Duration.ofNanos(notification.getTimestamp())); |
| 93 | |
| 94 | // Last element is the counter name |
| 95 | String counterName = path.getElem(path.getElemCount() - 1).getName(); |
| 96 | long counterValue = update.getVal().getUintVal(); |
| 97 | |
| 98 | |
| 99 | switch (counterName) { |
| 100 | case "in-octets": |
| 101 | inBytes.put(ifName, counterValue); |
| 102 | break; |
| 103 | case "out-octets": |
| 104 | outBytes.put(ifName, counterValue); |
| 105 | break; |
| 106 | case "in-discards": |
| 107 | case "in-fcs-errors": |
| 108 | inDropped.compute(ifName, (k, v) -> v == null ? counterValue : v + counterValue); |
| 109 | break; |
| 110 | case "out-discards": |
| 111 | outDropped.put(ifName, counterValue); |
| 112 | break; |
| 113 | case "in-errors": |
| 114 | inErrors.put(ifName, counterValue); |
| 115 | break; |
| 116 | case "out-errors": |
| 117 | outErrors.put(ifName, counterValue); |
| 118 | break; |
| 119 | case "in-unicast-pkts": |
| 120 | case "in-broadcast-pkts": |
| 121 | case "in-multicast-pkts": |
| 122 | case "in-unknown-protos": |
| 123 | inPkts.compute(ifName, (k, v) -> v == null ? counterValue : v + counterValue); |
| 124 | break; |
| 125 | case "out-unicast-pkts": |
| 126 | case "out-broadcast-pkts": |
| 127 | case "out-multicast-pkts": |
| 128 | outPkts.compute(ifName, (k, v) -> v == null ? counterValue : v + counterValue); |
| 129 | break; |
| 130 | default: |
| 131 | log.warn("Unsupported counter name {}, ignored", counterName); |
| 132 | break; |
| 133 | } |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | // Build ONOS port stats map |
| 138 | return ifacePortNumberMapping.entrySet().stream() |
| 139 | .map(e -> { |
| 140 | String ifName = e.getKey(); |
| 141 | PortNumber portNumber = e.getValue(); |
| 142 | Duration portActive = getDurationActive(portNumber, timestamps.get(ifName)); |
| 143 | return DefaultPortStatistics.builder() |
| 144 | .setDeviceId(deviceId) |
| 145 | .setPort(portNumber) |
| 146 | .setDurationSec(portActive.getSeconds()) |
| 147 | .setDurationNano(portActive.getNano()) |
| 148 | .setPacketsSent(outPkts.getOrDefault(ifName, 0L)) |
| 149 | .setPacketsReceived(inPkts.getOrDefault(ifName, 0L)) |
| 150 | .setPacketsTxDropped(outDropped.getOrDefault(ifName, 0L)) |
| 151 | .setPacketsRxDropped(inDropped.getOrDefault(ifName, 0L)) |
| 152 | .setBytesSent(outBytes.getOrDefault(ifName, 0L)) |
| 153 | .setBytesReceived(inBytes.getOrDefault(ifName, 0L)) |
| 154 | .setPacketsTxErrors(outErrors.getOrDefault(ifName, 0L)) |
| 155 | .setPacketsRxErrors(inErrors.getOrDefault(ifName, 0L)) |
| 156 | .build(); |
| 157 | }) |
| 158 | .collect(Collectors.toList()); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | private String interfaceNameFromPath(Path path) { |
| 163 | // /interfaces/interface[name=iface-name] |
| 164 | return path.getElem(1).getKeyOrDefault("name", null); |
| 165 | } |
| 166 | |
| 167 | private Path interfaceCounterPath(String portName) { |
| 168 | // /interfaces/interface[name=port-name]/state/counters |
| 169 | return Path.newBuilder() |
| 170 | .addElem(Gnmi.PathElem.newBuilder().setName("interfaces").build()) |
| 171 | .addElem(Gnmi.PathElem.newBuilder().setName("interface") |
| 172 | .putKey("name", portName).build()) |
| 173 | .addElem(Gnmi.PathElem.newBuilder().setName("state").build()) |
| 174 | .addElem(Gnmi.PathElem.newBuilder().setName("counters").build()) |
| 175 | .build(); |
| 176 | } |
| 177 | |
| 178 | private Duration getDurationActive(PortNumber portNumber, Duration timestamp) { |
| 179 | Port port = deviceService.getPort(deviceId, portNumber); |
| 180 | if (port == null || !port.isEnabled()) { |
| 181 | //FIXME log |
| 182 | return Duration.ZERO; |
| 183 | } |
pierventre | 50696a7 | 2021-06-01 12:26:36 +0200 | [diff] [blame^] | 184 | |
| 185 | // Set duration 0 for devices that do not support reporting last-change |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 186 | String lastChangedStr = port.annotations().value(LAST_CHANGE); |
| 187 | if (lastChangedStr == null) { |
pierventre | 50696a7 | 2021-06-01 12:26:36 +0200 | [diff] [blame^] | 188 | return Duration.ZERO; |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | try { |
| 192 | long lastChanged = Long.parseLong(lastChangedStr); |
pierventre | 50696a7 | 2021-06-01 12:26:36 +0200 | [diff] [blame^] | 193 | return lastChanged == 0 ? Duration.ZERO : timestamp.minus(lastChanged, ChronoUnit.NANOS); |
Yi Tseng | 59d5f3e | 2018-11-27 23:09:41 -0800 | [diff] [blame] | 194 | } catch (NullPointerException | NumberFormatException ex) { |
| 195 | //FIXME log |
| 196 | return Duration.ZERO; |
| 197 | } |
| 198 | } |
| 199 | } |