blob: a925aa32c3e34a60c05ef209ef25a09684608989 [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;
Gamze Abaka641fc072018-09-04 09:16:27 +000041import org.opencord.sadis.BandwidthProfileInformation;
42import org.opencord.sadis.BaseInformationService;
Matteo Scandolo962a6ad2018-12-11 15:39:42 -080043import org.opencord.sadis.SubscriberAndDeviceInformation;
Gamze Abaka641fc072018-09-04 09:16:27 +000044import org.opencord.sadis.SadisService;
Matteo Scandolo962a6ad2018-12-11 15:39:42 -080045import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
48public class OltTest {
49 private final Logger log = LoggerFactory.getLogger(getClass());
50 private Olt olt;
51
52 private static final VlanId CLIENT_C_TAG = VlanId.vlanId((short) 999);
53 private static final VlanId CLIENT_S_TAG = VlanId.vlanId((short) 111);
54 private static final String CLIENT_NAS_PORT_ID = "PON 1/1";
55 private static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1";
56
57 private static final String OLT_DEV_ID = "of:00000000000000aa";
58 private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID);
59 private static final String SCHEME_NAME = "olt";
60 private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder()
61 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
62
63 @Before
64 public void setUp() {
65 olt = new Olt();
66 olt.deviceService = new MockDeviceService();
Gamze Abaka641fc072018-09-04 09:16:27 +000067 olt.sadisService = new MockSadisService();
68 olt.subsService = olt.sadisService.getSubscriberInfoService();
69
Matteo Scandolo962a6ad2018-12-11 15:39:42 -080070 }
71
72 /**
73 * Tests that the getSubscriber method does throw a NullPointerException with a meaningful message.
74 */
75 @Test
76 public void testGetSubscriberError() {
77 ConnectPoint cp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1);
78 try {
79 olt.getSubscriber(cp);
80 } catch (NullPointerException e) {
81 assertEquals(e.getMessage(), "Invalid connect point");
82 }
83 }
84
85 /**
86 * Tests that the getSubscriber method returns Subscriber informations.
87 */
88 @Test
89 public void testGetSubscriber() {
90 ConnectPoint cp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 2);
91
92 SubscriberAndDeviceInformation s = olt.getSubscriber(cp);
93
94 assertEquals(s.circuitId(), CLIENT_CIRCUIT_ID);
95 assertEquals(s.cTag(), CLIENT_C_TAG);
96 assertEquals(s.sTag(), CLIENT_S_TAG);
97 assertEquals(s.nasPortId(), CLIENT_NAS_PORT_ID);
98 }
99
100 private class MockDevice extends DefaultDevice {
101
102 public MockDevice(ProviderId providerId, DeviceId id, Type type,
103 String manufacturer, String hwVersion, String swVersion,
104 String serialNumber, ChassisId chassisId, Annotations... annotations) {
105 super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
106 chassisId, annotations);
107 }
108 }
109
110 private class MockDeviceService extends DeviceServiceAdapter {
111
112 private ProviderId providerId = new ProviderId("of", "foo");
113 private final Device device1 = new MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH,
114 "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(),
115 DEVICE_ANNOTATIONS);
116
117 @Override
118 public Device getDevice(DeviceId devId) {
119 return device1;
120
121 }
122
123 @Override
124 public Port getPort(ConnectPoint cp) {
125 log.info("Looking up port {}", cp.port().toString());
126 if (cp.port().toString().equals("1")) {
127 return null;
128 }
129 return new MockPort();
130 }
131 }
132
133 private class MockPort implements Port {
134
135 @Override
136 public boolean isEnabled() {
137 return true;
138 }
139 @Override
140 public long portSpeed() {
141 return 1000;
142 }
143 @Override
144 public Element element() {
145 return null;
146 }
147 @Override
148 public PortNumber number() {
149 return null;
150 }
151 @Override
152 public Annotations annotations() {
153 return new MockAnnotations();
154 }
155 @Override
156 public Type type() {
157 return Port.Type.FIBER;
158 }
159
160 private class MockAnnotations implements Annotations {
161
162 @Override
163 public String value(String val) {
164 return "BRCM12345678";
165 }
166 @Override
167 public Set<String> keys() {
168 return null;
169 }
170 }
171 }
172
Gamze Abaka641fc072018-09-04 09:16:27 +0000173 private class MockSadisService implements SadisService {
174
175
176 @Override
177 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
178 return new MockSubService();
179 }
180
181 @Override
182 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
183 return null;
184 }
185 }
186
187 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
Matteo Scandolo962a6ad2018-12-11 15:39:42 -0800188 MockSubscriberAndDeviceInformation sub =
189 new MockSubscriberAndDeviceInformation(CLIENT_NAS_PORT_ID, CLIENT_C_TAG,
190 CLIENT_S_TAG, CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null);
191 @Override
192 public SubscriberAndDeviceInformation get(String id) {
193 return sub;
194 }
195
196 @Override
197 public void invalidateAll() {}
198 @Override
199 public void invalidateId(String id) {}
200 @Override
201 public SubscriberAndDeviceInformation getfromCache(String id) {
202 return null;
203 }
204 }
205
206 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
207
208 MockSubscriberAndDeviceInformation(String id, VlanId ctag,
209 VlanId stag, String nasPortId,
210 String circuitId, MacAddress hardId,
211 Ip4Address ipAddress) {
212 this.setCTag(ctag);
213 this.setHardwareIdentifier(hardId);
214 this.setId(id);
215 this.setIPAddress(ipAddress);
216 this.setSTag(stag);
217 this.setNasPortId(nasPortId);
218 this.setCircuitId(circuitId);
219 }
220 }
221
222}