blob: 24c7a08b897cbe56c0c71d8390f5a67b52e4fa31 [file] [log] [blame]
Jeff Groom34c28ce2018-04-26 19:42:18 -06001/*
2 * Copyright 2016-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.ciena.waveserverai.netconf;
17
18import com.google.common.collect.ImmutableList;
19import org.onosproject.drivers.netconf.TemplateManager;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.device.DefaultPortStatistics;
23import org.onosproject.net.device.PortStatistics;
24import org.onosproject.net.device.PortStatisticsDiscovery;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.netconf.NetconfController;
27import org.onosproject.netconf.NetconfException;
28import org.onosproject.netconf.NetconfSession;
29import org.slf4j.Logger;
30import org.w3c.dom.Node;
31import org.w3c.dom.NodeList;
32
33import javax.xml.xpath.XPath;
34import javax.xml.xpath.XPathConstants;
35import javax.xml.xpath.XPathExpressionException;
36import javax.xml.xpath.XPathFactory;
37import java.util.ArrayList;
38import java.util.Collection;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Discovers the device and ports from a Ciena WaveServer Ai Netconf device.
46 */
47
48public class CienaWaveserverAiPortStatisticsDiscovery extends AbstractHandlerBehaviour
49 implements PortStatisticsDiscovery {
50 private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
51
52 private final Logger log = getLogger(getClass());
53
54 public CienaWaveserverAiPortStatisticsDiscovery() {
55 log.info("Loaded handler behaviour CienaWaveserverAiPortStatisticsDiscovery.");
56 }
57
58 static {
59 TEMPLATE_MANAGER.load(CienaWaveserverAiPortStatisticsDiscovery.class,
60 "/templates/requests/%s.j2",
61 "discoverPortStatistics");
62 }
63
64 @Override
65 public Collection<PortStatistics> discoverPortStatistics() {
66 log.debug("Calculating port stats for Waveserver Ai device");
67 Collection<PortStatistics> portStats = new ArrayList<>();
68
69 DeviceId deviceId = handler().data().deviceId();
70 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
71 if (controller == null || controller.getDevicesMap() == null
72 || controller.getDevicesMap().get(deviceId) == null) {
73 log.warn("NETCONF session to device {} not yet established, cannot load links, will be retried", deviceId);
74 return portStats;
75 }
76 NetconfSession session = controller.getDevicesMap().get(deviceId).getSession();
77
78 try {
79 XPath xp = XPathFactory.newInstance().newXPath();
80 String tx = "current-bin/statistics/interface-counts/tx/";
81 String rx = "current-bin/statistics/interface-counts/rx/";
82
83 Node node = TEMPLATE_MANAGER.doRequest(session, "discoverPortStatistics");
84 NodeList nodeList = (NodeList) xp.evaluate("waveserver-pm/ethernet-performance-instances",
85 node, XPathConstants.NODESET);
86 Node nodeListItem;
87 int count = nodeList.getLength();
88 for (int i = 0; i < count; ++i) {
89 nodeListItem = nodeList.item(i);
90 portStats.add(DefaultPortStatistics.builder()
91 .setDeviceId(deviceId)
92 .setPort(PortNumber.portNumber(
93 portIdConvert(xp.evaluate("instance-name/text()", nodeListItem))))
94 .setPacketsReceived(Long.parseLong(xp.evaluate(rx + "packets/value/text()", nodeListItem)))
95 .setPacketsSent(Long.parseLong(xp.evaluate(tx + "packets/value/text()", nodeListItem)))
96 .setBytesReceived(Long.parseLong(xp.evaluate(rx + "bytes/value/text()", nodeListItem)))
97 .setBytesSent(Long.parseLong(xp.evaluate(tx + "bytes/value/text()", nodeListItem)))
98// .setPacketsRxDropped(packetsRxDropped)
99// .setPacketsRxErrors(packetsRxErrors)
100// .setPacketsTxDropped(packetsTxDropped)
101// .setPacketsTxErrors(packetsTxErrors)
102 .build());
103 }
104 } catch (NetconfException | XPathExpressionException e) {
105 log.error("Unable to retrieve port stats information for device {}, {}", deviceId, e);
106 }
107
108 return ImmutableList.copyOf(portStats);
109 }
110
111}