ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.cisco; |
| 18 | |
| 19 | |
| 20 | import org.junit.Test; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 21 | import org.onlab.packet.ChassisId; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 24 | import org.onosproject.net.DefaultDevice; |
| 25 | import org.onosproject.net.DeviceId; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.device.DefaultPortDescription; |
| 29 | import org.onosproject.net.device.PortDescription; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 30 | import org.onosproject.net.provider.ProviderId; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 31 | import java.io.InputStream; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 34 | import java.util.Scanner; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 35 | import static org.junit.Assert.assertEquals; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 36 | import static org.onosproject.net.Device.Type.SWITCH; |
| 37 | import static org.onosproject.net.DeviceId.deviceId; |
| 38 | |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Tests the parser for Netconf TextBlock configurations and replies from Cisco devices. |
| 42 | */ |
| 43 | public class TextBlockParserCiscoTest { |
| 44 | |
| 45 | private static final PortNumber INTF1_PORT = PortNumber.portNumber(0); |
| 46 | private static final String INTF1_NAME = "FastEthernet0/0"; |
| 47 | private static final PortNumber INTF2_PORT = PortNumber.portNumber(0); |
| 48 | private static final String INTF2_NAME = "Ethernet1/0"; |
| 49 | private static final PortNumber INTF3_PORT = PortNumber.portNumber(0); |
| 50 | private static final String INTF3_NAME = "GigabitEthernet2/0"; |
| 51 | private static final PortNumber INTF4_PORT = PortNumber.portNumber(0); |
| 52 | private static final String INTF4_NAME = "Serial3/0"; |
| 53 | private static final PortNumber INTF5_PORT = PortNumber.portNumber(0); |
| 54 | private static final String INTF5_NAME = "POS4/0"; |
| 55 | private static final PortNumber INTF6_PORT = PortNumber.portNumber(0); |
| 56 | private static final String INTF6_NAME = "Fddi5/0"; |
| 57 | private static final Port.Type COPPER = Port.Type.COPPER; |
| 58 | private static final Port.Type FIBER = Port.Type.FIBER; |
| 59 | private static final long CONNECTION_SPEED_ETHERNET = 100000; |
| 60 | private static final long CONNECTION_SPEED_SERIAL = 1544; |
| 61 | private static final long CONNECTION_SPEED_POS = 9952000; |
| 62 | private static final long CONNECTION_SPEED_FDDI = 100000; |
| 63 | private static final boolean IS_ENABLED = true; |
| 64 | private static final boolean IS_NOT_ENABLED = false; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 65 | private static final String SHOW_VERSION = "/testShowVersion.xml"; |
| 66 | private static final String SHOW_INTFS = "/testShowInterfaces.xml"; |
| 67 | private static final String SW = "IOS C3560E 15.0(2)EJ"; |
| 68 | private static final String HW = "SM-X-ES3-24-P"; |
| 69 | private static final String MFR = "Cisco"; |
| 70 | private static final String SN = "FOC18401Z3R"; |
| 71 | private static final ProviderId PROVIDERID = new ProviderId("of", "foo"); |
| 72 | private static final DeviceId DEVICE = deviceId("of:foo"); |
| 73 | private static final ChassisId CID = new ChassisId(); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 74 | |
| 75 | @Test |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 76 | public void controllersVersion() { |
| 77 | InputStream streamOrig = getClass().getResourceAsStream(SHOW_VERSION); |
| 78 | String version = new Scanner(streamOrig, "UTF-8").useDelimiter("\\Z").next(); |
| 79 | version = version.substring(version.indexOf('\n') + 1); |
| 80 | String[] actualDetails = TextBlockParserCisco.parseCiscoIosDeviceDetails(version); |
| 81 | |
| 82 | assertEquals("Information could not be retrieved", |
| 83 | getExpectedInfo(), actualInfo(actualDetails)); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void controllersIntfs() { |
| 88 | InputStream streamOrig = getClass().getResourceAsStream(SHOW_INTFS); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 89 | String rpcReply = new Scanner(streamOrig, "UTF-8").useDelimiter("\\Z").next(); |
| 90 | List<PortDescription> actualIntfs = TextBlockParserCisco.parseCiscoIosPorts(rpcReply); |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 91 | assertEquals("Information could not be retrieved", |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 92 | getExpectedIntfs(), actualIntfs); |
| 93 | } |
| 94 | |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 95 | private DefaultDevice getExpectedInfo() { |
| 96 | return new DefaultDevice(PROVIDERID, DEVICE, SWITCH, MFR, HW, SW, SN, CID); |
| 97 | } |
| 98 | |
| 99 | private DefaultDevice actualInfo(String[] actualDetails) { |
| 100 | |
| 101 | return new DefaultDevice(PROVIDERID, DEVICE, SWITCH, actualDetails[0], |
| 102 | actualDetails[1], actualDetails[2], |
| 103 | actualDetails[3], CID); |
| 104 | } |
| 105 | |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 106 | private List<PortDescription> getExpectedIntfs() { |
| 107 | DefaultAnnotations.Builder int1Annotations = DefaultAnnotations.builder() |
| 108 | .set(AnnotationKeys.PORT_NAME, INTF1_NAME); |
| 109 | DefaultAnnotations.Builder int2Annotations = DefaultAnnotations.builder() |
| 110 | .set(AnnotationKeys.PORT_NAME, INTF2_NAME); |
| 111 | DefaultAnnotations.Builder int3Annotations = DefaultAnnotations.builder() |
| 112 | .set(AnnotationKeys.PORT_NAME, INTF3_NAME); |
| 113 | DefaultAnnotations.Builder int4Annotations = DefaultAnnotations.builder() |
| 114 | .set(AnnotationKeys.PORT_NAME, INTF4_NAME); |
| 115 | DefaultAnnotations.Builder int5Annotations = DefaultAnnotations.builder() |
| 116 | .set(AnnotationKeys.PORT_NAME, INTF5_NAME); |
| 117 | DefaultAnnotations.Builder int6Annotations = DefaultAnnotations.builder() |
| 118 | .set(AnnotationKeys.PORT_NAME, INTF6_NAME); |
| 119 | |
| 120 | List<PortDescription> intfs = new ArrayList<>(); |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame^] | 121 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF1_PORT).isEnabled(IS_ENABLED) |
| 122 | .type(COPPER).portSpeed(CONNECTION_SPEED_ETHERNET) |
| 123 | .annotations(int1Annotations.build()).build()); |
| 124 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF2_PORT).isEnabled(IS_NOT_ENABLED) |
| 125 | .type(COPPER).portSpeed(CONNECTION_SPEED_ETHERNET) |
| 126 | .annotations(int2Annotations.build()).build()); |
| 127 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF3_PORT).isEnabled(IS_NOT_ENABLED) |
| 128 | .type(COPPER).portSpeed(CONNECTION_SPEED_ETHERNET) |
| 129 | .annotations(int3Annotations.build()).build()); |
| 130 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF4_PORT).isEnabled(IS_ENABLED) |
| 131 | .type(COPPER).portSpeed(CONNECTION_SPEED_SERIAL) |
| 132 | .annotations(int4Annotations.build()).build()); |
| 133 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF5_PORT).isEnabled(IS_ENABLED) |
| 134 | .type(FIBER).portSpeed(CONNECTION_SPEED_POS) |
| 135 | .annotations(int5Annotations.build()).build()); |
| 136 | intfs.add(DefaultPortDescription.builder().withPortNumber(INTF6_PORT).isEnabled(IS_ENABLED) |
| 137 | .type(FIBER).portSpeed(CONNECTION_SPEED_FDDI) |
| 138 | .annotations(int6Annotations.build()).build()); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 139 | return intfs; |
| 140 | } |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 141 | |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 142 | } |