Michal Mach | ee4cf92 | 2017-06-21 16:00:58 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Michal Mach | ee4cf92 | 2017-06-21 16:00:58 +0200 | [diff] [blame] | 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.cisco.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.device.DefaultPortStatistics; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.device.PortStatistics; |
| 29 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.net.driver.DriverHandler; |
| 32 | import org.onosproject.protocol.rest.RestSBController; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Collection; |
| 38 | import java.util.List; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Discovers port statistics from a Cisco NXOS device. |
| 45 | */ |
| 46 | public class CiscoNxosPortStatistics extends AbstractHandlerBehaviour implements PortStatisticsDiscovery { |
| 47 | |
| 48 | private static final String SHOW_INTERFACE_CMD = "show interface"; |
| 49 | private static final String ROW_INTERFACE = "ROW_interface"; |
| 50 | private static final String RESULT = "result"; |
| 51 | private static final String ETH_INPKTS = "eth_inpkts"; |
| 52 | private static final String ETH_OUTPKTS = "eth_outpkts"; |
| 53 | private static final String ETH_INBYTES = "eth_inbytes"; |
| 54 | private static final String ETH_OUTBYTES = "eth_outbytes"; |
| 55 | private static final String ETH_INERR = "eth_inerr"; |
| 56 | private static final String ETH_OUTERR = "eth_outerr"; |
| 57 | private static final String ETH_INERR1 = "eth_inerr"; |
| 58 | private static final String ETH_OUTERR1 = "eth_outerr"; |
| 59 | private static final String WHITE_SPACE_FORMAT = "%s %s"; |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | @Override |
| 64 | public Collection<PortStatistics> discoverPortStatistics() { |
| 65 | DriverHandler handler = handler(); |
| 66 | RestSBController controller = checkNotNull(handler.get(RestSBController.class)); |
| 67 | DeviceId deviceId = handler.data().deviceId(); |
| 68 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 69 | List<Port> ports = deviceService.getPorts(deviceId); |
| 70 | Collection<PortStatistics> portStatistics = Lists.newArrayList(); |
| 71 | |
| 72 | ports.stream() |
| 73 | .filter(Port::isEnabled) |
| 74 | .forEach(port -> portStatistics.add(discoverSpecifiedPortStatistics(port, controller, deviceId))); |
| 75 | |
| 76 | return ImmutableList.copyOf(portStatistics); |
| 77 | } |
| 78 | |
| 79 | private PortStatistics discoverSpecifiedPortStatistics(Port port, |
| 80 | RestSBController controller, |
| 81 | DeviceId deviceId) { |
| 82 | String portName = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 83 | ArrayList<String> cmd = Lists.newArrayList(); |
| 84 | cmd.add(String.format(WHITE_SPACE_FORMAT, SHOW_INTERFACE_CMD, portName)); |
| 85 | |
| 86 | String request = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI); |
| 87 | String response = NxApiRequest.post(controller, deviceId, request); |
| 88 | |
| 89 | return buildStatisticsFromResponse(response, port.number(), deviceId); |
| 90 | } |
| 91 | |
| 92 | private DefaultPortStatistics buildStatisticsFromResponse(String response, |
| 93 | PortNumber portNumber, |
| 94 | DeviceId deviceId) { |
| 95 | DefaultPortStatistics defaultPortStatistics = null; |
| 96 | |
| 97 | try { |
| 98 | ObjectMapper om = new ObjectMapper(); |
| 99 | JsonNode json = om.readTree(response); |
| 100 | JsonNode res = json.get(RESULT); |
| 101 | JsonNode deviceInterface = res.findValue(ROW_INTERFACE); |
| 102 | long packetsReceived = deviceInterface.get(ETH_INPKTS).asLong(); |
| 103 | long packetsSent = deviceInterface.get(ETH_OUTPKTS).asLong(); |
| 104 | long bytesReceived = deviceInterface.get(ETH_INBYTES).asLong(); |
| 105 | long bytesSent = deviceInterface.get(ETH_OUTBYTES).asLong(); |
| 106 | long packetsRxDropped = deviceInterface.get(ETH_INERR).asLong(); |
| 107 | long packetsTxDropped = deviceInterface.get(ETH_OUTERR).asLong(); |
| 108 | long packetsRxErrors = deviceInterface.get(ETH_INERR1).asLong(); |
| 109 | long packetsTxErrors = deviceInterface.get(ETH_OUTERR1).asLong(); |
| 110 | |
| 111 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
Ray Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame^] | 112 | defaultPortStatistics = builder.setPort(portNumber) |
Michal Mach | ee4cf92 | 2017-06-21 16:00:58 +0200 | [diff] [blame] | 113 | .setPacketsReceived(packetsReceived) |
| 114 | .setPacketsSent(packetsSent) |
| 115 | .setBytesReceived(bytesReceived) |
| 116 | .setBytesSent(bytesSent) |
| 117 | .setPacketsRxDropped(packetsRxDropped) |
| 118 | .setPacketsTxDropped(packetsTxDropped) |
| 119 | .setPacketsRxErrors(packetsRxErrors) |
| 120 | .setPacketsTxErrors(packetsTxErrors) |
| 121 | .setDeviceId(deviceId) |
| 122 | .build(); |
| 123 | |
| 124 | } catch (IOException e) { |
| 125 | log.error("Cannot read or process NxApi response: {}", e.toString()); |
| 126 | } |
| 127 | |
| 128 | log.debug("Port statistics: {}", defaultPortStatistics); |
| 129 | return defaultPortStatistics; |
| 130 | } |
| 131 | } |