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