blob: 61de7dda1b28f5b921f4f5569878413579dff484 [file] [log] [blame]
David K. Bainbridge96cf8542018-05-08 10:46:35 -07001/*
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 */
16package org.onosproject.drivers.ciena.c5170.netconf;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertTrue;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.util.Collection;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.Optional;
27import java.util.Set;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.onosproject.drivers.ciena.c5170.Ciena5170DriversLoader;
32import org.onosproject.drivers.netconf.MockCoreService;
33import org.onosproject.drivers.netconf.MockDevice;
34import org.onosproject.drivers.netconf.MockDeviceService;
35import org.onosproject.drivers.netconf.MockDriverHandler;
36import org.onosproject.drivers.netconf.MockTemplateRequestDriver;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.device.DeviceDescription;
39import org.onosproject.net.device.PortDescription;
40import org.onosproject.net.device.PortStatistics;
41import org.onosproject.net.link.LinkDescription;
42import org.onosproject.netconf.NetconfController;
43import org.slf4j.Logger;
44
45public class DeviceDiscoveryTest {
46
47 Map<DeviceId, MockDriverHandler> mockDriverHandlers = new HashMap<DeviceId, MockDriverHandler>();
48 MockTemplateRequestDriver mockRequestDriver = new MockTemplateRequestDriver();
49 MockDeviceService deviceService = new MockDeviceService();
50
51 Logger log = getLogger(DeviceDiscoveryTest.class);
52
53 @Before
54 public void setup() {
55 MockCoreService coreService = new MockCoreService(101, "org.onosproject.drivers.netconf",
56 "org.onosproject.linkdiscovery", "org.onosproject.drivers.ciena.c5170");
57
58 // Load the mock responses for mock device "netconf:1.2.3.4:830"
59 DeviceId mId = DeviceId.deviceId("netconf:1.2.3.4:830");
60 mockRequestDriver.load(DeviceDiscoveryTest.class, "/templates/responses/device_1_2_3_4/%s.j2", mId,
61 "systemInfo", "softwareVersion", "logicalPorts", "link-info", "port-stats", "chassis-mac");
62 MockDriverHandler mockDriverHandler = new MockDriverHandler(Ciena5170DriversLoader.class,
63 "/ciena-5170-drivers.xml", mId, coreService, deviceService);
64 NetconfController controller = mockDriverHandler.get(NetconfController.class);
65 mockDriverHandlers.put(mId, mockDriverHandler);
66 mockRequestDriver.setDeviceMap(controller.getDevicesMap());
67
68 // Load the mock responses for mock device "netconf:5.6.7.8:830"
69 mId = DeviceId.deviceId("netconf:5.6.7.8:830");
70 mockRequestDriver.load(DeviceDiscoveryTest.class, "/templates/responses/device_5_6_7_8/%s.j2", mId,
71 "systemInfo", "softwareVersion", "logicalPorts", "link-info", "chassis-mac");
72 mockDriverHandler = new MockDriverHandler(Ciena5170DriversLoader.class, "/ciena-5170-drivers.xml", mId,
73 coreService, deviceService);
74 controller = mockDriverHandler.get(NetconfController.class);
75 mockDriverHandlers.put(mId, mockDriverHandler);
76
77 mockRequestDriver.setDeviceMap(controller.getDevicesMap());
78 }
79
80 @Test
81 public void deviceDescriptionTest() {
82 Ciena5170DeviceDescription targetUnderTest = new Ciena5170DeviceDescription();
83 Ciena5170DeviceDescription.TEMPLATE_MANAGER.setRequestDriver(mockRequestDriver);
84 targetUnderTest.setHandler(mockDriverHandlers.get(DeviceId.deviceId("netconf:1.2.3.4:830")));
85
86 DeviceDescription desc = targetUnderTest.discoverDeviceDetails();
87 assertEquals("Chassis ID", "1C1161D18800", desc.chassisId().toString().toUpperCase());
88 assertEquals("Manufacturer", "Ciena", desc.manufacturer());
89 assertEquals("HW Version", "CN5170", desc.hwVersion());
90 assertEquals("Serial Number", "1C1161D18800", desc.serialNumber());
91 assertEquals("SW Version", "saos-01-01-00-0025", desc.swVersion());
92 }
93
94 @Test
95 public void discoverPortDetailsTest() {
96 Ciena5170DeviceDescription targetUnderTest = new Ciena5170DeviceDescription();
97 Ciena5170DeviceDescription.TEMPLATE_MANAGER.setRequestDriver(mockRequestDriver);
98 targetUnderTest.setHandler(mockDriverHandlers.get(DeviceId.deviceId("netconf:1.2.3.4:830")));
99 List<PortDescription> ports = targetUnderTest.discoverPortDetails();
100 assertEquals(44, ports.size());
101 Map<Long, Integer> speedCounts = new HashMap<Long, Integer>();
102 for (PortDescription port : ports) {
103 if (port.portNumber().toLong() >= 41L && port.portNumber().toLong() <= 44L) {
104 assertEquals(String.format("PORT %s enable status", port.portNumber()), true, port.isEnabled());
105 } else {
106 assertEquals(String.format("PORT %s enable status", port.portNumber()), false, port.isEnabled());
107
108 }
109 Long speed = port.portSpeed();
110 Integer count = speedCounts.get(speed);
111 if (count == null) {
112 speedCounts.put(speed, 1);
113 } else {
114 speedCounts.put(speed, count + 1);
115 }
116 }
117
118 assertEquals("PORT count with speed 1G", new Integer(40), speedCounts.get(10000L));
119 assertEquals("PORT count with speed 10G", new Integer(4), speedCounts.get(100000L));
120 }
121
122 @Test
123 public void discoverPortStatisticsTest() {
124 Ciena5170DeviceDescription targetUnderTest = new Ciena5170DeviceDescription();
125 Ciena5170DeviceDescription.TEMPLATE_MANAGER.setRequestDriver(mockRequestDriver);
126 targetUnderTest.setHandler(mockDriverHandlers.get(DeviceId.deviceId("netconf:1.2.3.4:830")));
127 Collection<PortStatistics> stats = targetUnderTest.discoverPortStatistics();
128 assertEquals("PORT STAT COUNT", 44, stats.size());
129 Optional<PortStatistics> stat = stats.stream().filter(s -> s.portNumber().toLong() == 44L).findFirst();
130 assertTrue("Port 44 stats exist", stat.isPresent());
131 assertEquals("Port 44 pkt out", 4587L, stat.get().packetsSent());
132 assertEquals("Port 44 pkt in", 411155469L, stat.get().packetsReceived());
133 assertEquals("Port 44 bytes out", 524744L, stat.get().bytesSent());
134 assertEquals("Port 44 bytes in", 105255079746L, stat.get().bytesReceived());
135 assertEquals("Port 44 errors out", 0L, stat.get().packetsTxErrors());
136 assertEquals("Port 44 errors in", 0L, stat.get().packetsRxErrors());
137 }
138
139 @Test
140 public void getLinksTest() {
141 Ciena5170DeviceDescription targetUnderTest1 = new Ciena5170DeviceDescription();
142 Ciena5170DeviceDescription targetUnderTest2 = new Ciena5170DeviceDescription();
143 Ciena5170DeviceDescription.TEMPLATE_MANAGER.setRequestDriver(mockRequestDriver);
144
145 DeviceId mId = DeviceId.deviceId("netconf:1.2.3.4:830");
146 targetUnderTest1.setHandler(mockDriverHandlers.get(mId));
147 deviceService.addDevice(new MockDevice(mId, targetUnderTest1.discoverDeviceDetails()));
148
149 mId = DeviceId.deviceId("netconf:5.6.7.8:830");
150 targetUnderTest2.setHandler(mockDriverHandlers.get(mId));
151 deviceService.addDevice(new MockDevice(mId, targetUnderTest2.discoverDeviceDetails()));
152
153 Set<LinkDescription> links1 = targetUnderTest1.getLinks();
154 Set<LinkDescription> links2 = targetUnderTest2.getLinks();
155
156 assertEquals("A to B link count", 1, links1.size());
157 assertEquals("B to A link count", 1, links2.size());
158 LinkDescription a2b = links1.toArray(new LinkDescription[0])[0];
159 LinkDescription b2a = links2.toArray(new LinkDescription[0])[0];
160 assertEquals("A to B src and dest", a2b.src(), b2a.dst());
161 assertEquals("B to A src and dest", b2a.src(), a2b.dst());
162 }
163}