blob: 0f272fb66a1c19cb002f8584ded10cb140a09506 [file] [log] [blame]
Michele Santuari208b1672016-08-04 17:33:12 +02001/*
2 * Copyright 2016-present Open Networking Laboratory
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.provider.linkdiscovery.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.osgi.ComponentContextAdapter;
22import org.onlab.packet.ChassisId;
23import org.onosproject.cfg.ComponentConfigAdapter;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.mastership.MastershipService;
28import org.onosproject.mastership.MastershipServiceAdapter;
29import org.onosproject.net.AbstractProjectableModel;
30import org.onosproject.net.AnnotationKeys;
31import org.onosproject.net.Annotations;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DefaultDevice;
35import org.onosproject.net.DefaultLink;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Link;
39import org.onosproject.net.LinkKey;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.LinkDiscovery;
42import org.onosproject.net.device.DeviceListener;
43import org.onosproject.net.device.DeviceServiceAdapter;
44import org.onosproject.net.driver.AbstractHandlerBehaviour;
45import org.onosproject.net.driver.Behaviour;
46import org.onosproject.net.driver.Driver;
47import org.onosproject.net.driver.DriverAdapter;
48import org.onosproject.net.driver.DriverHandler;
49import org.onosproject.net.driver.DriverServiceAdapter;
50import org.onosproject.net.link.DefaultLinkDescription;
51import org.onosproject.net.link.LinkDescription;
52import org.onosproject.net.link.LinkProviderRegistryAdapter;
53import org.onosproject.net.link.LinkProviderServiceAdapter;
54import org.onosproject.net.link.LinkServiceAdapter;
55import org.onosproject.net.provider.ProviderId;
56
57import java.util.Dictionary;
58import java.util.HashSet;
59import java.util.Hashtable;
60import java.util.Set;
61
62import static org.easymock.EasyMock.*;
63import static org.junit.Assert.*;
64import static org.onlab.junit.TestTools.assertAfter;
65import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.APP_NAME;
66import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.SCHEME_NAME;
67
68/**
69 * Test for polling mechanism of the NetconfLinkProvider.
70 */
71public class LinkDiscoveryProviderTest {
72
73 private static final ComponentContextAdapter CONTEXT =
74 new ComponentContextAdapter() {
75 @Override
76 public Dictionary getProperties() {
77 Hashtable<String, Integer> props = new Hashtable<>();
78 props.put("linkPollFrequencySeconds", 2);
79 props.put("linkPollDelaySeconds", 1);
80 return props;
81 }
82 };
83 // Network Mocks
84 private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("netconf:1.1.1.1");
85 private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("netconf:1.1.1.2");
86 private static final ConnectPoint CP11 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(1));
87 private static final ConnectPoint CP12 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(2));
88 private static final ConnectPoint CP13 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(3));
89 private static final ConnectPoint CP14 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(4));
90 private static final ConnectPoint CP21 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(1));
91 private static final ConnectPoint CP22 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(2));
92 private static final ConnectPoint CP23 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(3));
93 private static final ConnectPoint CP24 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(4));
94 private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder()
95 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
96 private static final DefaultAnnotations LINK_ANNOTATIONS = DefaultAnnotations.builder()
97 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
98 private static final LinkKey LINKKEY1 = LinkKey.linkKey(CP11, CP21);
99 private static final LinkDescription LINK1 = new DefaultLinkDescription(CP11, CP21,
100 Link.Type.DIRECT,
101 LINK_ANNOTATIONS);
102 private static final LinkKey LINKKEY2 = LinkKey.linkKey(CP12, CP22);
103 private static final LinkDescription LINK2 = new DefaultLinkDescription(CP12, CP22,
104 Link.Type.DIRECT,
105 LINK_ANNOTATIONS);
106 private static final LinkKey LINKKEY3 = LinkKey.linkKey(CP13, CP23);
107 private static final LinkDescription LINK3 = new DefaultLinkDescription(CP13, CP23,
108 Link.Type.DIRECT,
109 DefaultAnnotations.builder()
110 .build());
111 private static final LinkKey LINKKEY4 = LinkKey.linkKey(CP14, CP24);
112 private static final LinkDescription LINK4 = new DefaultLinkDescription(CP14, CP24,
113 Link.Type.DIRECT,
114 DefaultAnnotations.builder().build());
115 //Service Mocks
116 private final MockDeviceService deviceService = new MockDeviceService();
117 private final LinkProviderRegistryAdapter linkRegistry = new LinkProviderRegistryAdapter();
118 private final MastershipService mastershipService = new MockMastershipService();
119 private final MockLinkService linkService = new MockLinkService();
120 private final Driver driver = new MockDriver();
121 //Provider related classes
122 private LinkProviderServiceAdapter providerService;
123 private CoreService coreService;
124 private LinkDiscoveryProvider provider = new LinkDiscoveryProvider();
125 private final Device device1 = new MockDevice(provider.id(), DEVICE_ID_1, Device.Type.SWITCH,
126 "foo.inc", "0", "0", "0", new ChassisId(),
127 DEVICE_ANNOTATIONS);
128 private Set<DeviceListener> deviceListeners = new HashSet<>();
129 private ApplicationId appId =
130 new DefaultApplicationId(100, APP_NAME);
131 private TestLink testLink = new TestLink();
132
133 @Before
134 public void setUp() {
135 coreService = createMock(CoreService.class);
136 expect(coreService.registerApplication(appId.name()))
137 .andReturn(appId).anyTimes();
138 replay(coreService);
139 provider.coreService = coreService;
140 provider.providerRegistry = linkRegistry;
141 provider.deviceService = deviceService;
142 provider.mastershipService = mastershipService;
143 provider.linkService = linkService;
144 provider.cfgService = new ComponentConfigAdapter();
145 AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter());
146 provider.activate(null);
147 providerService = linkRegistry.registeredProvider();
148 }
149
150 @Test
151 public void activate() throws Exception {
152 assertFalse("Provider should be registered", linkRegistry.getProviders().contains(provider));
153 assertEquals("Device service should be registered", provider.deviceService, deviceService);
154 assertEquals("Device listener should be added", 1, deviceListeners.size());
155 assertNotNull("Registration expected", providerService);
156 assertEquals("Incorrect provider", provider, providerService.provider());
157 assertFalse("Executor should be running", provider.executor.isShutdown());
158 assertFalse("Executor should be running", provider.executor.isTerminated());
159 assertEquals("Incorrect polling frequency, should be default", 10,
160 provider.linkPollFrequencySeconds);
161 assertEquals("Incorrect polling delay , should be default", 20,
162 provider.linkPollDelaySeconds);
163 }
164
165 @Test
166 public void modified() throws Exception {
167 provider.modified(CONTEXT);
168 assertEquals("Incorrect polling frequency, should be default", 2,
169 provider.linkPollFrequencySeconds);
170 assertEquals("Incorrect polling delay , should be default", 1,
171 provider.linkPollDelaySeconds);
172
173 }
174
175
176 @Test
177 public void deactivate() throws Exception {
178 provider.deactivate();
179 assertEquals("Device listener should be removed", 0, deviceListeners.size());
180 assertFalse("Provider should not be registered", linkRegistry.getProviders().contains(provider));
181 assertTrue(provider.executor.isShutdown());
182 assertNull(provider.providerService);
183 }
184
185
186 @Test
187 public void linksTestForStoredDevice() {
188 provider.modified(CONTEXT);
189 providerService.discoveredLinkDescriptions().put(LINKKEY1, LINK1);
190 providerService.discoveredLinkDescriptions().put(LINKKEY2, LINK2);
191 providerService.discoveredLinkDescriptions().put(LINKKEY4, LINK4);
192 testLink.addLinkDesc(LINK2);
193 testLink.addLinkDesc(LINK3);
194 assertAfter(1100, () -> {
195 assertEquals("Total number of link must be 3", 3, providerService.discoveredLinkDescriptions().size());
196 assertFalse("Link1 should be removed",
197 providerService.discoveredLinkDescriptions().containsKey(LINKKEY1));
198 assertTrue("Link2 should be present",
199 providerService.discoveredLinkDescriptions().containsKey(LINKKEY2));
200 assertTrue("Link3 should be added",
201 providerService.discoveredLinkDescriptions().containsKey(LINKKEY3));
202 assertEquals("Link3 should be annotated", SCHEME_NAME.toUpperCase(),
203 providerService.discoveredLinkDescriptions()
204 .get(LINKKEY3).annotations().value(AnnotationKeys.PROTOCOL));
205 assertTrue("Link4 should be present because it is not related to the LinkDiscovery",
206 providerService.discoveredLinkDescriptions().containsKey(LINKKEY4));
207
208 });
209 clear();
210 }
211
212 private void clear() {
213 testLink.clearLinkDesc();
214 providerService.discoveredLinkDescriptions().clear();
215 providerService.discoveredLinks().clear();
216 }
217
218 private class MockDeviceService extends DeviceServiceAdapter {
219
220 @Override
221 public Iterable<Device> getAvailableDevices() {
222 return ImmutableSet.of(device1);
223 }
224
225 @Override
226 public void addListener(DeviceListener listener) {
227 deviceListeners.add(listener);
228 }
229
230 @Override
231 public void removeListener(DeviceListener listener) {
232 deviceListeners.remove(listener);
233 }
234 }
235
236 private class MockMastershipService extends MastershipServiceAdapter {
237
238 @Override
239 public boolean isLocalMaster(DeviceId deviceId) {
240 return true;
241 }
242 }
243
244 private class MockLinkService extends LinkServiceAdapter {
245 @Override
246 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
247 Set<Link> links = new HashSet<>();
248 providerService.discoveredLinkDescriptions().values()
249 .forEach(x -> links.add(DefaultLink.builder()
250 .providerId(provider.id())
251 .src(x.src())
252 .dst(x.dst())
253 .type(x.type())
254 .isExpected(x.isExpected())
255 .annotations(x.annotations()).build()));
256 return ImmutableSet.copyOf(links);
257 }
258 }
259
260 private class MockDevice extends DefaultDevice {
261
262 public MockDevice(ProviderId providerId, DeviceId id, Type type,
263 String manufacturer, String hwVersion, String swVersion,
264 String serialNumber, ChassisId chassisId, Annotations... annotations) {
265 super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
266 chassisId, annotations);
267 }
268
269 @Override
270 protected Driver locateDriver() {
271 return driver;
272 }
273
274 @Override
275 public Driver driver() {
276 return driver;
277 }
278 }
279
280 private class MockDriver extends DriverAdapter {
281 @Override
282 public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) {
283
284 return (T) testLink;
285 }
286 }
287
288 private class TestLink extends AbstractHandlerBehaviour implements LinkDiscovery {
289 Set<LinkDescription> linkDescriptions = new HashSet<>();
290
291 @Override
292 public Set<LinkDescription> getLinks() {
293 return ImmutableSet.copyOf(linkDescriptions);
294 }
295
296 private void addLinkDesc(LinkDescription link) {
297 linkDescriptions.add(link);
298 }
299
300 private void clearLinkDesc() {
301 linkDescriptions.clear();
302 }
303
304 }
305}