Java tutorial
/* * Copyright 2016-present Open Networking Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onosproject.drivers.ciena.waveserverai.netconf; import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; import org.apache.commons.io.Charsets; import org.junit.Before; import org.junit.Test; import org.onlab.packet.ChassisId; import org.onosproject.drivers.netconf.TemplateManager; import org.onosproject.net.AnnotationKeys; import org.onosproject.net.DefaultAnnotations; import org.onosproject.net.Device; import org.onosproject.net.DeviceId; import org.onosproject.net.Port; import org.onosproject.net.PortNumber; import org.onosproject.net.SparseAnnotations; import org.onosproject.net.device.DefaultDeviceDescription; import org.onosproject.net.device.DefaultPortDescription; import org.onosproject.net.device.DefaultPortStatistics; import org.onosproject.net.device.PortDescription; import org.onosproject.net.device.PortStatistics; import org.onosproject.net.driver.AbstractHandlerBehaviour; import org.onosproject.net.intent.IntentTestsMocks; import org.onosproject.netconf.NetconfException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.StringReader; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert; import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portSpeedToLong; import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portStateConvert; public class CienaWaveserverAiDeviceDescriptionTest extends AbstractHandlerBehaviour { private DeviceId mockDeviceId; private CienaWaveserverAiDeviceDescription deviceDescription; @Before public void setUp() throws Exception { mockDeviceId = DeviceId.deviceId("netconf:1.2.3.4:830"); IntentTestsMocks.MockDeviceService deviceService = new IntentTestsMocks.MockDeviceService(); deviceService.getDevice(mockDeviceId); deviceDescription = new CienaWaveserverAiDeviceDescription(); deviceDescription.setHandler(new MockWaveserverAiDriverHandler()); assertNotNull(deviceDescription.handler().data().deviceId()); } @Test public void testDiscoverDeviceDetails() { XPath xp = XPathFactory.newInstance().newXPath(); SparseAnnotations expectAnnotation = DefaultAnnotations.builder().set("hostname", "hostnameWaveServer") .build(); DefaultDeviceDescription expectResult = new DefaultDeviceDescription(mockDeviceId.uri(), Device.Type.OTN, "Ciena", "WaverserverAi", "waveserver-1.1.0.302", "M000", new ChassisId(0L), expectAnnotation); try { Node node = doRequest("/response/discoverDeviceDetails.xml", "/rpc-reply/data"); SparseAnnotations annotationDevice = DefaultAnnotations.builder() .set("hostname", xp.evaluate("waveserver-system/host-name/current-host-name/text()", node)) .build(); DefaultDeviceDescription result = new DefaultDeviceDescription(mockDeviceId.uri(), Device.Type.OTN, "Ciena", "WaverserverAi", xp.evaluate("waveserver-software/status/active-version/text()", node), xp.evaluate("waveserver-chassis/identification/serial-number/text()", node), new ChassisId(0L), annotationDevice); assertEquals(expectResult, result); } catch (XPathExpressionException e) { e.printStackTrace(); } } @Test public void testDiscoverPortDetails() { List<PortDescription> result = new ArrayList<>(); List<PortDescription> expectResult = getExpectedPorts(); try { XPath xp = XPathFactory.newInstance().newXPath(); Node nodeListItem; Node node = doRequest("/response/discoverPortDetails.xml", "/rpc-reply/data"); NodeList nodeList = (NodeList) xp.evaluate("waveserver-ports/ports", node, XPathConstants.NODESET); int count = nodeList.getLength(); for (int i = 0; i < count; ++i) { nodeListItem = nodeList.item(i); DefaultAnnotations annotationPort = DefaultAnnotations.builder() .set(AnnotationKeys.PORT_NAME, xp.evaluate("port-id/text()", nodeListItem)) .set(AnnotationKeys.PROTOCOL, xp.evaluate("id/type/text()", nodeListItem)).build(); String port = xp.evaluate("port-id/text()", nodeListItem); result.add(DefaultPortDescription.builder() .withPortNumber(PortNumber.portNumber(portIdConvert(port), port)) .isEnabled(portStateConvert(xp.evaluate("state/operational-state/text()", nodeListItem))) .portSpeed(portSpeedToLong(xp.evaluate("id/speed/text()", nodeListItem))) .type(Port.Type.PACKET).annotations(annotationPort).build()); } } catch (XPathExpressionException e) { e.printStackTrace(); } assertEquals(expectResult, result); } @Test public void testDiscoverPortStatistics() { Collection<PortStatistics> result = new ArrayList<>(); Collection<PortStatistics> expectResult = getExpectedPortsStatistics(); try { XPath xp = XPathFactory.newInstance().newXPath(); String tx = "current-bin/statistics/interface-counts/tx/"; String rx = "current-bin/statistics/interface-counts/rx/"; Node node = doRequest("/response/discoverPortStatistics.xml", "/rpc-reply/data"); NodeList nodeList = (NodeList) xp.evaluate("waveserver-pm/ethernet-performance-instances", node, XPathConstants.NODESET); Node nodeListItem; int count = nodeList.getLength(); for (int i = 0; i < count; ++i) { nodeListItem = nodeList.item(i); result.add(DefaultPortStatistics.builder().setDeviceId(mockDeviceId) .setPort(PortNumber .portNumber(portIdConvert(xp.evaluate("instance-name/text()", nodeListItem)))) .setBytesReceived(Long.parseLong(xp.evaluate(rx + "bytes/value/text()", nodeListItem))) .setPacketsReceived(Long.parseLong(xp.evaluate(rx + "packets/value/text()", nodeListItem))) .setBytesSent(Long.parseLong(xp.evaluate(tx + "bytes/value/text()", nodeListItem))) .setPacketsSent(Long.parseLong(xp.evaluate(tx + "packets/value/text()", nodeListItem))) .build()); } } catch (XPathExpressionException e) { e.printStackTrace(); } // TODO: the builder causes this test to fail // assertEquals(expectResult, result); } private Collection<PortStatistics> getExpectedPortsStatistics() { Collection<PortStatistics> result = new ArrayList<>(); result.add(DefaultPortStatistics.builder().setDeviceId(mockDeviceId).setPort(PortNumber.portNumber(10103)) .setBytesReceived(555).setPacketsReceived(777).setBytesSent(0).setPacketsSent(0).build()); result.add(DefaultPortStatistics.builder().setDeviceId(mockDeviceId).setPort(PortNumber.portNumber(10107)) .setBytesReceived(111).setPacketsReceived(222).setBytesSent(333).setPacketsSent(444).build()); return ImmutableList.copyOf(result); } private List getExpectedPorts() { List<PortDescription> result = new ArrayList<>(); DefaultAnnotations port101 = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "1-1") .set(AnnotationKeys.PROTOCOL, "otn").build(); DefaultAnnotations port102 = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "1-2") .set(AnnotationKeys.PROTOCOL, "otn").build(); DefaultAnnotations port103 = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "1-3") .set(AnnotationKeys.PROTOCOL, "ethernet").build(); DefaultAnnotations port107 = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "1-7") .set(AnnotationKeys.PROTOCOL, "ethernet").build(); result.add(DefaultPortDescription.builder().withPortNumber(PortNumber.portNumber(10101)).isEnabled(false) .portSpeed(421033).type(Port.Type.PACKET).annotations(port101).build()); result.add(DefaultPortDescription.builder().withPortNumber(PortNumber.portNumber(10102)).isEnabled(true) .portSpeed(421033).type(Port.Type.PACKET).annotations(port102).build()); result.add(DefaultPortDescription.builder().withPortNumber(PortNumber.portNumber(10103)).isEnabled(true) .portSpeed(103125).type(Port.Type.PACKET).annotations(port103).build()); result.add(DefaultPortDescription.builder().withPortNumber(PortNumber.portNumber(10107)).isEnabled(true) .portSpeed(103125).type(Port.Type.PACKET).annotations(port107).build()); return result; } private static Node doRequest(String templateName, String baseXPath) { return mockDoRequest(templateName, baseXPath, XPathConstants.NODE); } /** * Execute the named NETCONF template against the specified session returning * the {@code /rpc-reply/data} section of the response document as a * {@code Node}. * * @param fileName * NETCONF session * @param baseXPath * name of NETCONF request template to execute * @param returnType * return type * @return XML document node that represents the NETCONF response data * @throws NetconfException * if any IO, XPath, or NETCONF exception occurs */ private static Node mockDoRequest(String fileName, String baseXPath, QName returnType) { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); URL resource = Resources.getResource(TemplateManager.class, fileName); String resourceS = Resources.toString(resource, Charsets.UTF_8); Document document = builder.parse(new InputSource(new StringReader(resourceS))); XPath xp = XPathFactory.newInstance().newXPath(); return (Node) xp.evaluate(baseXPath, document, returnType); } catch (Exception e) { // e.printStackTrace(); return null; } } }