David Bainbridge | 7526c45 | 2018-04-20 14:14:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.c5170.netconf; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.xml.xpath.XPath; |
| 30 | import javax.xml.xpath.XPathConstants; |
| 31 | import javax.xml.xpath.XPathExpressionException; |
| 32 | import javax.xml.xpath.XPathFactory; |
| 33 | |
| 34 | import org.onlab.packet.ChassisId; |
| 35 | import org.onosproject.drivers.netconf.TemplateManager; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.Port; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.behaviour.LinkDiscovery; |
| 43 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 44 | import org.onosproject.net.device.DefaultPortDescription; |
| 45 | import org.onosproject.net.device.DefaultPortStatistics; |
| 46 | import org.onosproject.net.device.DeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 48 | import org.onosproject.net.device.DeviceService; |
| 49 | import org.onosproject.net.device.PortDescription; |
| 50 | import org.onosproject.net.device.PortStatistics; |
| 51 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 52 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 53 | import org.onosproject.net.link.DefaultLinkDescription; |
| 54 | import org.onosproject.net.link.LinkDescription; |
| 55 | import org.onosproject.netconf.NetconfController; |
| 56 | import org.onosproject.netconf.NetconfException; |
| 57 | import org.onosproject.netconf.NetconfSession; |
| 58 | import org.slf4j.Logger; |
| 59 | import org.w3c.dom.Node; |
| 60 | import org.w3c.dom.NodeList; |
| 61 | |
| 62 | /** |
| 63 | * Discovers the ports from a Ciena WaveServer Rest device. |
| 64 | */ |
| 65 | public class Ciena5170DeviceDescription extends AbstractHandlerBehaviour |
| 66 | implements DeviceDescriptionDiscovery, PortStatisticsDiscovery, LinkDiscovery { |
| 67 | private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager(); |
| 68 | |
| 69 | static { |
| 70 | TEMPLATE_MANAGER.load(Ciena5170DeviceDescription.class, "/templates/requests/%s.j2", "systemInfo", |
| 71 | "chassis-mac", "softwareVersion", "logicalPorts", "port-stats", "link-info"); |
| 72 | } |
| 73 | |
| 74 | private static final Logger log = getLogger(Ciena5170DeviceDescription.class); |
| 75 | |
| 76 | @Override |
| 77 | public DeviceDescription discoverDeviceDetails() { |
| 78 | |
| 79 | DeviceId deviceId = handler().data().deviceId(); |
| 80 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 81 | NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession(); |
| 82 | try { |
| 83 | Node systemInfo = TEMPLATE_MANAGER.doRequest(session, "systemInfo"); |
| 84 | Node chassisMac = TEMPLATE_MANAGER.doRequest(session, "chassis-mac"); |
| 85 | Node softwareVersion = TEMPLATE_MANAGER.doRequest(session, "softwareVersion"); |
| 86 | XPath xp = XPathFactory.newInstance().newXPath(); |
| 87 | String mac = xp.evaluate("lldp-global-operational/chassis-id/text()", chassisMac).toUpperCase(); |
| 88 | return new DefaultDeviceDescription(deviceId.uri(), Device.Type.SWITCH, "Ciena", |
| 89 | xp.evaluate("components/component/name/text()", systemInfo), |
| 90 | xp.evaluate("software-state/running-package/package-version/text()", softwareVersion), mac, |
| 91 | new ChassisId(Long.valueOf(mac, 16))); |
| 92 | |
| 93 | } catch (XPathExpressionException | NetconfException ne) { |
| 94 | log.error("failed to query system info from device {} : {}", handler().data().deviceId(), ne.getMessage(), |
| 95 | ne); |
| 96 | } |
| 97 | |
| 98 | return new DefaultDeviceDescription(deviceId.uri(), Device.Type.SWITCH, "Ciena", "5170", "Unknown", "Unknown", |
| 99 | new ChassisId()); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Convert the specification of port speed in the of of #unit, i.e. {@10G} to MB |
| 104 | * as represented by a Long. |
| 105 | * |
| 106 | * @param ps |
| 107 | * specification of port speed |
| 108 | * @return port speed as MBs |
| 109 | */ |
| 110 | private Long portSpeedToLong(String ps) { |
| 111 | String value = ps.trim(); |
| 112 | StringBuilder digits = new StringBuilder(); |
| 113 | String unit = ""; |
| 114 | for (int i = 0; i < value.length(); i += 1) { |
| 115 | final char c = value.charAt(i); |
| 116 | if (Character.isDigit(c)) { |
| 117 | digits.append(c); |
| 118 | } else { |
| 119 | unit = value.substring(i).toUpperCase().trim(); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | switch (unit) { |
| 125 | case "G": |
| 126 | case "GB": |
| 127 | return Long.valueOf(digits.toString()) * 1000; |
| 128 | case "M": |
| 129 | case "MB": |
| 130 | default: |
| 131 | return Long.valueOf(digits.toString()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public List<PortDescription> discoverPortDetails() { |
| 137 | List<PortDescription> ports = new ArrayList<PortDescription>(); |
| 138 | DeviceId deviceId = handler().data().deviceId(); |
| 139 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 140 | if (controller == null || controller.getDevicesMap() == null |
| 141 | || controller.getDevicesMap().get(deviceId) == null) { |
| 142 | log.warn("NETCONF session to device {} not yet established, will be retried", deviceId); |
| 143 | return ports; |
| 144 | } |
| 145 | NetconfSession session = controller.getDevicesMap().get(deviceId).getSession(); |
| 146 | |
| 147 | try { |
| 148 | Node logicalPorts = TEMPLATE_MANAGER.doRequest(session, "logicalPorts"); |
| 149 | XPath xp = XPathFactory.newInstance().newXPath(); |
| 150 | NodeList nl = (NodeList) xp.evaluate("interfaces/interface/config", logicalPorts, XPathConstants.NODESET); |
| 151 | int count = nl.getLength(); |
| 152 | Node node; |
| 153 | for (int i = 0; i < count; i += 1) { |
| 154 | node = nl.item(i); |
| 155 | if (xp.evaluate("type/text()", node).equals("ettp")) { |
| 156 | ports.add(DefaultPortDescription.builder() |
| 157 | .withPortNumber(PortNumber.portNumber(xp.evaluate("name/text()", node))) |
| 158 | .isEnabled(Boolean.valueOf(xp.evaluate("admin-status/text()", node))) |
| 159 | .portSpeed(portSpeedToLong(xp.evaluate("port-speed/text()", node))).type(Port.Type.PACKET) |
| 160 | .build()); |
| 161 | } |
| 162 | } |
| 163 | } catch (NetconfException | XPathExpressionException e) { |
| 164 | log.error("Unable to retrieve port information for device {}, {}", deviceId, e); |
| 165 | } |
| 166 | return ports; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Collection<PortStatistics> discoverPortStatistics() { |
| 171 | List<PortStatistics> stats = new ArrayList<PortStatistics>(); |
| 172 | |
| 173 | DeviceId deviceId = handler().data().deviceId(); |
| 174 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 175 | if (controller == null || controller.getDevicesMap() == null |
| 176 | || controller.getDevicesMap().get(deviceId) == null) { |
| 177 | log.warn("NETCONF session to device {} not yet established, will be retried", deviceId); |
| 178 | return stats; |
| 179 | } |
| 180 | NetconfSession session = controller.getDevicesMap().get(deviceId).getSession(); |
| 181 | |
| 182 | try { |
| 183 | Node data = TEMPLATE_MANAGER.doRequest(session, "port-stats"); |
| 184 | XPath xp = XPathFactory.newInstance().newXPath(); |
| 185 | NodeList interfaces = (NodeList) xp.evaluate("interfaces/interface", data, XPathConstants.NODESET); |
| 186 | int count = interfaces.getLength(); |
| 187 | for (int i = 0; i < count; i += 1) { |
| 188 | Node iface = interfaces.item(i); |
| 189 | if (xp.evaluate("config/type/text()", iface).equals("ettp")) { |
| 190 | stats.add(DefaultPortStatistics.builder().setDeviceId(deviceId) |
| 191 | .setPort(PortNumber.portNumber(xp.evaluate("name/text()", iface))) |
| 192 | .setBytesReceived(Long.valueOf(xp.evaluate("state/counters/in-octets/text()", iface))) |
| 193 | .setBytesSent(Long.valueOf(xp.evaluate("state/counters/out-octets/text()", iface))) |
| 194 | .setPacketsReceived(Long.valueOf(xp.evaluate("state/counters/in-pkts/text()", iface))) |
| 195 | .setPacketsSent(Long.valueOf(xp.evaluate("state/counters/out-pkts/text()", iface))) |
| 196 | .setPacketsTxErrors(Long.valueOf(xp.evaluate("state/counters/out-errors/text()", iface))) |
| 197 | .setPacketsRxErrors(Long.valueOf(xp.evaluate("state/counters/in-errors/text()", iface))) |
| 198 | .build()); |
| 199 | } |
| 200 | } |
| 201 | } catch (NetconfException | XPathExpressionException e) { |
| 202 | log.error("Unable to retrieve port statistics for device {}, {}", deviceId, e); |
| 203 | } |
| 204 | |
| 205 | return stats; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<LinkDescription> getLinks() { |
| 210 | log.debug("LINKS CHECKING ..."); |
| 211 | Set<LinkDescription> links = new HashSet<LinkDescription>(); |
| 212 | DeviceId deviceId = handler().data().deviceId(); |
| 213 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 214 | if (controller == null || controller.getDevicesMap() == null |
| 215 | || controller.getDevicesMap().get(deviceId) == null) { |
| 216 | log.warn("NETCONF session to device {} not yet established, cannot load links, will be retried", deviceId); |
| 217 | return links; |
| 218 | } |
| 219 | NetconfSession session = controller.getDevicesMap().get(deviceId).getSession(); |
| 220 | try { |
| 221 | |
| 222 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 223 | |
| 224 | Iterable<Device> devices = deviceService.getAvailableDevices(); |
| 225 | Map<String, Device> lookup = new HashMap<String, Device>(); |
| 226 | for (Device d : devices) { |
| 227 | lookup.put(d.chassisId().toString().toUpperCase(), d); |
| 228 | } |
| 229 | |
| 230 | Node logicalPorts = TEMPLATE_MANAGER.doRequest(session, "link-info"); |
| 231 | XPath xp = XPathFactory.newInstance().newXPath(); |
| 232 | NodeList ifaces = (NodeList) xp.evaluate("interfaces/interface", logicalPorts, XPathConstants.NODESET); |
| 233 | int count = ifaces.getLength(); |
| 234 | Node iface; |
| 235 | Node destChassis; |
| 236 | for (int i = 0; i < count; i += 1) { |
| 237 | iface = ifaces.item(i); |
| 238 | if (xp.evaluate("config/type/text()", iface).equals("ettp")) { |
| 239 | destChassis = (Node) xp.evaluate("state/lldp-remote-port-operational/chassis-id", iface, |
| 240 | XPathConstants.NODE); |
| 241 | |
| 242 | if (destChassis != null) { |
| 243 | Device dest = lookup.get(destChassis.getTextContent().toUpperCase()); |
| 244 | |
| 245 | if (dest != null) { |
| 246 | |
| 247 | links.add(new DefaultLinkDescription( |
| 248 | new ConnectPoint(deviceId, |
| 249 | PortNumber.portNumber(xp.evaluate("name/text()", iface))), |
| 250 | new ConnectPoint(dest.id(), |
| 251 | PortNumber.portNumber(xp.evaluate( |
| 252 | "state/lldp-remote-port-operational/port-id/text()", iface))), |
| 253 | Link.Type.DIRECT, true)); |
| 254 | } else { |
Jeff Groom | 34c28ce | 2018-04-26 19:42:18 -0600 | [diff] [blame] | 255 | log.warn("DEST chassisID not found: chassis {} port {}", |
| 256 | destChassis.getTextContent().toUpperCase(), xp.evaluate("name/text()", iface)); |
David Bainbridge | 7526c45 | 2018-04-20 14:14:37 -0700 | [diff] [blame] | 257 | } |
| 258 | } else { |
| 259 | log.debug("NO LINK for {}", xp.evaluate("name/text()", iface)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } catch (NetconfException | XPathExpressionException e) { |
| 264 | log.error("Unable to retrieve links for device {}, {}", deviceId, e); |
| 265 | } |
| 266 | |
| 267 | return links; |
| 268 | } |
| 269 | |
| 270 | } |