blob: 1878c743625b98efa242f38bc5e6868f1d763309 [file] [log] [blame]
Matteo Scandolo962a6ad2018-12-11 15:39:42 -08001/*
2 * Copyright 2016-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.opencord.olt.impl;
17
18import static org.junit.Assert.assertEquals;
19
20import java.util.Set;
21
22import org.junit.Before;
23import org.junit.Test;
24
25import org.onlab.packet.ChassisId;
26import org.onlab.packet.Ip4Address;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.net.AnnotationKeys;
30import org.onosproject.net.Annotations;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.DefaultDevice;
34import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.Element;
37import org.onosproject.net.Port;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.device.DeviceServiceAdapter;
40import org.onosproject.net.provider.ProviderId;
41import org.opencord.sadis.SubscriberAndDeviceInformation;
42import org.opencord.sadis.SubscriberAndDeviceInformationService;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46public class OltTest {
47 private final Logger log = LoggerFactory.getLogger(getClass());
48 private Olt olt;
49
50 private static final VlanId CLIENT_C_TAG = VlanId.vlanId((short) 999);
51 private static final VlanId CLIENT_S_TAG = VlanId.vlanId((short) 111);
52 private static final String CLIENT_NAS_PORT_ID = "PON 1/1";
53 private static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1";
54
55 private static final String OLT_DEV_ID = "of:00000000000000aa";
56 private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID);
57 private static final String SCHEME_NAME = "olt";
58 private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder()
59 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
60
61 @Before
62 public void setUp() {
63 olt = new Olt();
64 olt.deviceService = new MockDeviceService();
65 olt.subsService = new MockSubService();
66 }
67
68 /**
69 * Tests that the getSubscriber method does throw a NullPointerException with a meaningful message.
70 */
71 @Test
72 public void testGetSubscriberError() {
73 ConnectPoint cp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1);
74 try {
75 olt.getSubscriber(cp);
76 } catch (NullPointerException e) {
77 assertEquals(e.getMessage(), "Invalid connect point");
78 }
79 }
80
81 /**
82 * Tests that the getSubscriber method returns Subscriber informations.
83 */
84 @Test
85 public void testGetSubscriber() {
86 ConnectPoint cp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 2);
87
88 SubscriberAndDeviceInformation s = olt.getSubscriber(cp);
89
90 assertEquals(s.circuitId(), CLIENT_CIRCUIT_ID);
91 assertEquals(s.cTag(), CLIENT_C_TAG);
92 assertEquals(s.sTag(), CLIENT_S_TAG);
93 assertEquals(s.nasPortId(), CLIENT_NAS_PORT_ID);
94 }
95
96 private class MockDevice extends DefaultDevice {
97
98 public MockDevice(ProviderId providerId, DeviceId id, Type type,
99 String manufacturer, String hwVersion, String swVersion,
100 String serialNumber, ChassisId chassisId, Annotations... annotations) {
101 super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
102 chassisId, annotations);
103 }
104 }
105
106 private class MockDeviceService extends DeviceServiceAdapter {
107
108 private ProviderId providerId = new ProviderId("of", "foo");
109 private final Device device1 = new MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH,
110 "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(),
111 DEVICE_ANNOTATIONS);
112
113 @Override
114 public Device getDevice(DeviceId devId) {
115 return device1;
116
117 }
118
119 @Override
120 public Port getPort(ConnectPoint cp) {
121 log.info("Looking up port {}", cp.port().toString());
122 if (cp.port().toString().equals("1")) {
123 return null;
124 }
125 return new MockPort();
126 }
127 }
128
129 private class MockPort implements Port {
130
131 @Override
132 public boolean isEnabled() {
133 return true;
134 }
135 @Override
136 public long portSpeed() {
137 return 1000;
138 }
139 @Override
140 public Element element() {
141 return null;
142 }
143 @Override
144 public PortNumber number() {
145 return null;
146 }
147 @Override
148 public Annotations annotations() {
149 return new MockAnnotations();
150 }
151 @Override
152 public Type type() {
153 return Port.Type.FIBER;
154 }
155
156 private class MockAnnotations implements Annotations {
157
158 @Override
159 public String value(String val) {
160 return "BRCM12345678";
161 }
162 @Override
163 public Set<String> keys() {
164 return null;
165 }
166 }
167 }
168
169 private class MockSubService implements SubscriberAndDeviceInformationService {
170 MockSubscriberAndDeviceInformation sub =
171 new MockSubscriberAndDeviceInformation(CLIENT_NAS_PORT_ID, CLIENT_C_TAG,
172 CLIENT_S_TAG, CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null);
173 @Override
174 public SubscriberAndDeviceInformation get(String id) {
175 return sub;
176 }
177
178 @Override
179 public void invalidateAll() {}
180 @Override
181 public void invalidateId(String id) {}
182 @Override
183 public SubscriberAndDeviceInformation getfromCache(String id) {
184 return null;
185 }
186 }
187
188 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
189
190 MockSubscriberAndDeviceInformation(String id, VlanId ctag,
191 VlanId stag, String nasPortId,
192 String circuitId, MacAddress hardId,
193 Ip4Address ipAddress) {
194 this.setCTag(ctag);
195 this.setHardwareIdentifier(hardId);
196 this.setId(id);
197 this.setIPAddress(ipAddress);
198 this.setSTag(stag);
199 this.setNasPortId(nasPortId);
200 this.setCircuitId(circuitId);
201 }
202 }
203
204}