Jeff Groom | 34c28ce | 2018-04-26 19:42:18 -0600 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.drivers.ciena.waveserverai.netconf; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.onosproject.drivers.netconf.TemplateManager; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.device.DefaultPortStatistics; |
| 23 | import org.onosproject.net.device.PortStatistics; |
| 24 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.netconf.NetconfController; |
| 27 | import org.onosproject.netconf.NetconfException; |
| 28 | import org.onosproject.netconf.NetconfSession; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.w3c.dom.Node; |
| 31 | import org.w3c.dom.NodeList; |
| 32 | |
| 33 | import javax.xml.xpath.XPath; |
| 34 | import javax.xml.xpath.XPathConstants; |
| 35 | import javax.xml.xpath.XPathExpressionException; |
| 36 | import javax.xml.xpath.XPathFactory; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collection; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Discovers the device and ports from a Ciena WaveServer Ai Netconf device. |
| 46 | */ |
| 47 | |
| 48 | public 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 | } |