blob: 94e187521d7d626e04841fbd9520bc6e43dbd793 [file] [log] [blame]
10068695e340ec92019-10-11 07:03:00 +00001/*
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 */
16package org.onosproject.drivers.zte;
17
18import com.google.common.base.Strings;
19import com.google.common.collect.Lists;
20import org.apache.commons.configuration.XMLConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Port;
24import org.onosproject.net.device.DefaultPortStatistics;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.device.PortStatistics;
27import org.onosproject.net.device.PortStatisticsDiscovery;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.netconf.NetconfController;
30import org.onosproject.netconf.NetconfException;
31import org.onosproject.netconf.NetconfSession;
32import org.slf4j.Logger;
33
34import java.util.Collection;
35import java.util.List;
36
37import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME;
38import static org.slf4j.LoggerFactory.getLogger;
39
40public class ZtePortStatisticsDiscovery extends AbstractHandlerBehaviour
41 implements PortStatisticsDiscovery {
42
43 private static final Logger LOG = getLogger(ZtePortStatisticsDiscovery.class);
44
45 @Override
46 public Collection<PortStatistics> discoverPortStatistics() {
47 DeviceId deviceId = handler().data().deviceId();
48 LOG.debug("Discovering ZTE PortStatistics for device {}", deviceId);
49
50 NetconfController controller = handler().get(NetconfController.class);
51
52 if (null == controller) {
53 LOG.error("Cannot find NetconfController");
54 return null;
55 }
56
57 NetconfSession session = controller.getDevicesMap().get(deviceId).getSession();
58
59 if (null == session) {
60 LOG.error("No session available for device {}", deviceId);
61 return null;
62 }
63
64 DeviceService deviceService = this.handler().get(DeviceService.class);
65 List<Port> ports = deviceService.getPorts(deviceId);
66
67 Collection<PortStatistics> portStatistics = Lists.newArrayList();
68
69 ports.stream()
70 .filter(Port::isEnabled)
71 .filter(this::isClientPort)
72 .forEach(port -> portStatistics.add(discoverSpecifiedPortStatistics(session, deviceId, port)));
73
74 return portStatistics;
75 }
76
77 private boolean isClientPort(Port port) {
78 String portName = port.annotations().value(OC_NAME);
79 if (Strings.isNullOrEmpty(portName)) {
80 return false;
81 }
82 String[] portInfos = portName.split("-");
83 return portInfos.length == 4 && portInfos[3].startsWith("C");
84 }
85
86 private String getDataOfRpcReply(String rpcReply) {
87 String data = null;
88 int begin = rpcReply.indexOf("<data>");
89 int end = rpcReply.lastIndexOf("</data>");
90 if (begin != -1 && end != -1) {
91 data = (String) rpcReply.subSequence(begin, end + "</data>".length());
92 } else {
93 data = rpcReply;
94 }
95 return data;
96 }
97
98 private PortStatistics discoverSpecifiedPortStatistics(NetconfSession session, DeviceId deviceId, Port port) {
99
100 String portName = port.annotations().value(OC_NAME);
101 String rpc = buildPortStatisticsRequest("INTERFACE" + portName.substring("PORT".length()));
102
103 try {
104 String reply = session.requestSync(rpc);
105 XMLConfiguration cfg = (XMLConfiguration) XmlConfigParser.loadXmlString(getDataOfRpcReply(reply));
106 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
107
108 builder.setPort(port.number())
109 .setPacketsReceived(getInteger(cfg, "in-pks"))
110 .setPacketsSent(getInteger(cfg, "out-pkts"))
111 .setBytesReceived(getInteger(cfg, "in-octets"))
112 .setBytesSent(getInteger(cfg, "out-octets"))
113 .setPacketsRxDropped(getInteger(cfg, "in-fcs-errors"))
114 .setPacketsTxDropped(getInteger(cfg, "carrier-transitions"))
115 .setPacketsRxErrors(getInteger(cfg, "in-errors"))
116 .setPacketsTxErrors(getInteger(cfg, "out-errors"))
117 .setDeviceId(deviceId);
118
119 return builder.build();
120 } catch (NetconfException e) {
121 LOG.error("ZTE device portStatistic request error.", e);
122 return null;
123 }
124 }
125
126 private String buildPortStatisticsRequest(String portName) {
127 StringBuilder rpc = new StringBuilder();
128 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
129 rpc.append("<get>");
130 rpc.append("<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">");
131 rpc.append("<interfaces xmlns=\"http://openconfig.net/yang/interfaces\">");
132 rpc.append("<interface>");
133 rpc.append("<name>");
134 rpc.append(portName);
135 rpc.append("</name>");
136 rpc.append("<state>");
137 rpc.append("<counters>");
138 rpc.append("</counters>");
139 rpc.append("</state>");
140 rpc.append("</interface>");
141 rpc.append("</interfaces>");
142 rpc.append("</filter>");
143 rpc.append("</get>");
144 rpc.append("</rpc>");
145
146 return rpc.toString();
147 }
148
149 private int getInteger(XMLConfiguration cfg, String item) {
150 String numString = cfg.getString("interfaces.interface.state.counters." + item);
151 if (Strings.isNullOrEmpty(numString)) {
152 LOG.debug("Cannot get port statistic data for {}, set 0 as default.", item);
153 return 0;
154 }
155
156 try {
157 return Integer.parseInt(numString);
158 } catch (NumberFormatException e) {
159 LOG.warn("Cannot convert data for {}", item);
160 return 0;
161 }
162 }
163}