HelloONOS | 120ba47 | 2019-08-02 20:21:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | package org.onosproject.drivers.arista; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.onosproject.net.AnnotationKeys; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.device.DefaultPortStatistics; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.device.PortStatistics; |
| 28 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.Optional; |
| 37 | |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | /** |
| 41 | * Discovers port statistics from a Arista Rest devices. |
| 42 | */ |
| 43 | public class PortStatisticsImpl extends AbstractHandlerBehaviour implements PortStatisticsDiscovery { |
| 44 | |
| 45 | private static final String IN_UCASTPKTS = "inUcastPkts"; |
| 46 | private static final String IN_BRODCASTPKTS = "inBroadcastPkts"; |
| 47 | private static final String IN_MULICASTPKTS = "inMulticastPkts"; |
| 48 | private static final String IN_OCTETS = "inOctets"; |
| 49 | private static final String IN_TOTALERRORS = "totalInErrors"; |
| 50 | private static final String IN_DISCARDS = "inDiscards"; |
| 51 | private static final String OUT_UCASTPKTS = "outUcastPkts"; |
| 52 | private static final String OUT_BRODCASTPKTS = "outBroadcastPkts"; |
| 53 | private static final String OUT_MULICASTPKTS = "outMulticastPkts"; |
| 54 | private static final String OUT_OCTETS = "outOctets"; |
| 55 | private static final String OUT_TOTALERRORS = "totalOutErrors"; |
| 56 | private static final String OUT_DISCARDS = "outDiscards"; |
| 57 | private static final String COUNTER = "counterRefreshTime"; |
| 58 | private static final String INTERFACES = "interfaces"; |
| 59 | private static final String INTERFACE_COUNTERS = "interfaceCounters"; |
| 60 | |
| 61 | private static final String SHOW_INTERFACES = "show interfaces"; |
| 62 | private static final int SEC_TO_NSEC = 1000000000; |
| 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | @Override |
| 67 | public Collection<PortStatistics> discoverPortStatistics() { |
| 68 | Collection<PortStatistics> portStatistics = Lists.newArrayList(); |
| 69 | try { |
| 70 | DeviceId deviceId = handler().data().deviceId(); |
| 71 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 72 | List<Port> ports = deviceService.getPorts(deviceId); |
| 73 | Optional<JsonNode> result = AristaUtils.retrieveCommandResult(handler(), SHOW_INTERFACES); |
| 74 | |
| 75 | if (!result.isPresent()) { |
| 76 | return portStatistics; |
| 77 | } |
| 78 | |
| 79 | JsonNode interfaces = result.get().findValue(INTERFACES); |
| 80 | |
| 81 | if (interfaces == null) { |
| 82 | return portStatistics; |
| 83 | } |
| 84 | |
| 85 | Iterator<Map.Entry<String, JsonNode>> ifIterator = interfaces.fields(); |
| 86 | while (ifIterator.hasNext()) { |
| 87 | Map.Entry<String, JsonNode> intf = ifIterator.next(); |
| 88 | String ifName = intf.getKey(); |
| 89 | JsonNode interfaceNode = intf.getValue(); |
| 90 | JsonNode interfaceCounters = interfaceNode.get(INTERFACE_COUNTERS); |
| 91 | if (interfaceCounters == null) { |
| 92 | continue; |
| 93 | } |
| 94 | ports.stream().filter(Port::isEnabled) |
| 95 | .filter(port -> { |
| 96 | String portName = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 97 | return portName != null && portName.equals(ifName); |
| 98 | }) |
| 99 | .findAny() |
| 100 | .ifPresent(port -> portStatistics.add( |
| 101 | buildStatisticsForPort(interfaceCounters, port.number(), deviceId))); |
| 102 | } |
| 103 | } catch (Exception e) { |
| 104 | log.error("Exception occurred because of", e); |
| 105 | } |
| 106 | return portStatistics; |
| 107 | } |
| 108 | |
| 109 | private DefaultPortStatistics buildStatisticsForPort(JsonNode portResponse, |
| 110 | PortNumber portNumber, |
| 111 | DeviceId deviceId) { |
| 112 | DefaultPortStatistics defaultPortStatistics = null; |
| 113 | |
| 114 | try { |
| 115 | long packetsReceivedUcast = portResponse.get(IN_UCASTPKTS).asLong(); |
| 116 | long packetsReceivedMcast = portResponse.get(IN_MULICASTPKTS).asLong(); |
| 117 | long packetsReceivedBcast = portResponse.get(IN_BRODCASTPKTS).asLong(); |
| 118 | long packetsSentUcast = portResponse.get(OUT_UCASTPKTS).asLong(); |
| 119 | long packetsSentMcast = portResponse.get(OUT_MULICASTPKTS).asLong(); |
| 120 | long packetsSentBcast = portResponse.get(OUT_BRODCASTPKTS).asLong(); |
| 121 | long bytesReceived = portResponse.get(IN_OCTETS).asLong(); |
| 122 | long bytesSent = portResponse.get(OUT_OCTETS).asLong(); |
| 123 | long packetsRxDropped = portResponse.get(IN_DISCARDS).asLong(); |
| 124 | long packetsTxDropped = portResponse.get(OUT_DISCARDS).asLong(); |
| 125 | long packetsRxErrors = portResponse.get(IN_TOTALERRORS).asLong(); |
| 126 | long packetsTxErrors = portResponse.get(OUT_TOTALERRORS).asLong(); |
| 127 | double counter = portResponse.get(COUNTER).asDouble(); |
| 128 | |
| 129 | long packetsSent = packetsSentUcast + packetsSentMcast + packetsSentBcast; |
| 130 | long packetsReceived = packetsReceivedUcast + packetsReceivedMcast + packetsReceivedBcast; |
| 131 | long counterSec = (long) counter; |
| 132 | long counterNano = (long) (counter * SEC_TO_NSEC); |
| 133 | |
| 134 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 135 | defaultPortStatistics = builder.setPort(portNumber) |
| 136 | .setPacketsReceived(packetsReceived) |
| 137 | .setPacketsSent(packetsSent) |
| 138 | .setBytesReceived(bytesReceived) |
| 139 | .setBytesSent(bytesSent) |
| 140 | .setPacketsRxDropped(packetsRxDropped) |
| 141 | .setPacketsTxDropped(packetsTxDropped) |
| 142 | .setPacketsRxErrors(packetsRxErrors) |
| 143 | .setPacketsTxErrors(packetsTxErrors) |
| 144 | .setDeviceId(deviceId) |
| 145 | .setDurationSec(counterSec) |
| 146 | .setDurationNano(counterNano) |
| 147 | .setAnnotations(DefaultAnnotations.builder().build()) |
| 148 | .build(); |
| 149 | |
| 150 | } catch (Exception e) { |
| 151 | log.error("Cannot process port statistics calculation: {}", e.toString()); |
| 152 | } |
| 153 | |
| 154 | log.debug("Port statistics: {}", defaultPortStatistics); |
| 155 | return defaultPortStatistics; |
| 156 | } |
| 157 | } |