blob: 224204462adc8e289c291fd5dbe777ae73ce8113 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.link.impl;
tom0d395262014-09-07 16:53:40 -070017
tomd176fc42014-09-08 00:12:30 -070018import com.google.common.collect.ImmutableSet;
tom0d395262014-09-07 16:53:40 -070019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.event.Event;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070023import org.onosproject.incubator.net.config.NetworkConfigServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultDevice;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Link;
29import org.onosproject.net.MastershipRole;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.link.DefaultLinkDescription;
32import org.onosproject.net.link.LinkAdminService;
33import org.onosproject.net.link.LinkEvent;
34import org.onosproject.net.link.LinkListener;
35import org.onosproject.net.link.LinkProvider;
36import org.onosproject.net.link.LinkProviderRegistry;
37import org.onosproject.net.link.LinkProviderService;
38import org.onosproject.net.link.LinkService;
39import org.onosproject.net.provider.AbstractProvider;
40import org.onosproject.net.provider.ProviderId;
Thomas Vachuska36002e62015-05-19 16:12:29 -070041import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.device.impl.DeviceManager;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070043import org.onosproject.store.trivial.SimpleLinkStore;
tom0d395262014-09-07 16:53:40 -070044
45import java.util.ArrayList;
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080046import java.util.HashMap;
tomd176fc42014-09-08 00:12:30 -070047import java.util.Iterator;
tom0d395262014-09-07 16:53:40 -070048import java.util.List;
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080049import java.util.Map;
tomd176fc42014-09-08 00:12:30 -070050import java.util.Set;
tom0d395262014-09-07 16:53:40 -070051
52import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import static org.onosproject.net.DeviceId.deviceId;
54import static org.onosproject.net.Link.Type.DIRECT;
55import static org.onosproject.net.Link.Type.INDIRECT;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import static org.onosproject.net.link.LinkEvent.Type.*;
tom0d395262014-09-07 16:53:40 -070058
59/**
60 * Test codifying the link service & link provider service contracts.
61 */
tom35c0dc32014-09-19 10:00:58 -070062public class LinkManagerTest {
tom0d395262014-09-07 16:53:40 -070063
tom7e02cda2014-09-18 12:05:46 -070064 private static final ProviderId PID = new ProviderId("of", "foo");
tom0d395262014-09-07 16:53:40 -070065 private static final DeviceId DID1 = deviceId("of:foo");
66 private static final DeviceId DID2 = deviceId("of:bar");
tomd176fc42014-09-08 00:12:30 -070067 private static final DeviceId DID3 = deviceId("of:goo");
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080068 private static final Device DEV1 = new DefaultDevice(
69 PID, DID1, Device.Type.SWITCH, "", "", "", "", null);
70 private static final Device DEV2 = new DefaultDevice(
71 PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
72 private static final Device DEV3 = new DefaultDevice(
73 PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
tom0d395262014-09-07 16:53:40 -070074
75 private static final PortNumber P1 = PortNumber.portNumber(1);
76 private static final PortNumber P2 = PortNumber.portNumber(2);
77 private static final PortNumber P3 = PortNumber.portNumber(3);
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080078 private static final Map<DeviceId, Device> DEVICEIDMAP = new HashMap<>();
tom0d395262014-09-07 16:53:40 -070079
tom35c0dc32014-09-19 10:00:58 -070080 private LinkManager mgr;
tom0d395262014-09-07 16:53:40 -070081
82 protected LinkService service;
83 protected LinkAdminService admin;
84 protected LinkProviderRegistry registry;
85 protected LinkProviderService providerService;
86 protected TestProvider provider;
87 protected TestListener listener = new TestListener();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070088 protected DeviceManager devmgr = new TestDeviceManager();
tom0d395262014-09-07 16:53:40 -070089
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080090
Sahil Lele3a0cdd52015-07-21 14:16:31 -070091
tom0d395262014-09-07 16:53:40 -070092 @Before
93 public void setUp() {
tom35c0dc32014-09-19 10:00:58 -070094 mgr = new LinkManager();
tom0d395262014-09-07 16:53:40 -070095 service = mgr;
96 admin = mgr;
97 registry = mgr;
tom35c0dc32014-09-19 10:00:58 -070098 mgr.store = new SimpleLinkStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070099 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700100 mgr.deviceService = devmgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700101 mgr.networkConfigService = new TestNetworkConfigService();
tom0d395262014-09-07 16:53:40 -0700102 mgr.activate();
103
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800104 DEVICEIDMAP.put(DID1, DEV1);
105 DEVICEIDMAP.put(DID2, DEV2);
106 DEVICEIDMAP.put(DID3, DEV3);
107
tom0d395262014-09-07 16:53:40 -0700108 service.addListener(listener);
109
110 provider = new TestProvider();
111 providerService = registry.register(provider);
112 assertTrue("provider should be registered",
113 registry.getProviders().contains(provider.id()));
114 }
115
116 @After
117 public void tearDown() {
118 registry.unregister(provider);
119 assertFalse("provider should not be registered",
120 registry.getProviders().contains(provider.id()));
121 service.removeListener(listener);
122 mgr.deactivate();
123 }
124
tombb58c202014-09-07 22:51:50 -0700125 @Test
126 public void createLink() {
tomd176fc42014-09-08 00:12:30 -0700127 addLink(DID1, P1, DID2, P2, DIRECT);
128 addLink(DID2, P2, DID1, P1, DIRECT);
129 assertEquals("incorrect link count", 2, service.getLinkCount());
130
131 Iterator<Link> it = service.getLinks().iterator();
132 it.next();
133 it.next();
134 assertFalse("incorrect link count", it.hasNext());
135 }
136
137 @Test
138 public void updateLink() {
139 addLink(DID1, P1, DID2, P2, DIRECT);
140 addLink(DID2, P2, DID1, P1, INDIRECT);
141 assertEquals("incorrect link count", 2, service.getLinkCount());
142
143 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
144 validateEvents(LINK_UPDATED);
145 assertEquals("incorrect link count", 2, service.getLinkCount());
146
147 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), INDIRECT));
148 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
149 assertEquals("no events expected", 0, listener.events.size());
150 }
151
152 @Test
153 public void removeLink() {
154 addLink(DID1, P1, DID2, P2, DIRECT);
155 addLink(DID2, P2, DID1, P1, DIRECT);
156 assertEquals("incorrect link count", 2, service.getLinkCount());
157
158 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
159 validateEvents(LINK_REMOVED);
160 assertEquals("incorrect link count", 1, service.getLinkCount());
161 assertNull("link should not be found", service.getLink(cp(DID1, P1), cp(DID2, P2)));
162 assertNotNull("link should be found", service.getLink(cp(DID2, P2), cp(DID1, P1)));
163
164 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
165 assertEquals("no events expected", 0, listener.events.size());
166 }
167
168 @Test
169 public void removeLinksByConnectionPoint() {
170 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
171 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
172 addLink(DID3, P3, DID2, P1, DIRECT);
173 addLink(DID2, P1, DID3, P3, DIRECT);
174 assertEquals("incorrect link count", 4, service.getLinkCount());
175
176 providerService.linksVanished(cp(DID1, P1));
177 assertEquals("incorrect link count", 2, service.getLinkCount());
178 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
179 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
180 }
181
182 @Test
183 public void removeLinksByDevice() {
184 addLink(DID1, P1, DID2, P2, DIRECT);
185 addLink(DID2, P2, DID1, P1, DIRECT);
186 addLink(DID3, P3, DID2, P1, DIRECT);
187 addLink(DID2, P1, DID3, P3, DIRECT);
188 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
189 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
190 assertEquals("incorrect link count", 6, service.getLinkCount());
191
192 providerService.linksVanished(DID2);
193 assertEquals("incorrect link count", 2, service.getLinkCount());
194 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
195 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
196 }
197
198 @Test
199 public void removeLinksAsAdminByConnectionPoint() {
200 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
201 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
202 addLink(DID3, P3, DID2, P1, DIRECT);
203 addLink(DID2, P1, DID3, P3, DIRECT);
204 assertEquals("incorrect link count", 4, service.getLinkCount());
205
206 admin.removeLinks(cp(DID1, P1));
207 assertEquals("incorrect link count", 2, service.getLinkCount());
208 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
209 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
210 }
211
212 @Test
213 public void removeLinksAsAdminByDevice() {
214 addLink(DID1, P1, DID2, P2, DIRECT);
215 addLink(DID2, P2, DID1, P1, DIRECT);
216 addLink(DID3, P3, DID2, P1, DIRECT);
217 addLink(DID2, P1, DID3, P3, DIRECT);
218 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
219 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
220 assertEquals("incorrect link count", 6, service.getLinkCount());
221
222 admin.removeLinks(DID2);
223 assertEquals("incorrect link count", 2, service.getLinkCount());
224 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
225 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
226 }
227
228 @Test
229 public void getLinks() {
230 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
231 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
232 Link l3 = addLink(DID3, P3, DID2, P1, DIRECT);
233 Link l4 = addLink(DID2, P1, DID3, P3, DIRECT);
234 assertEquals("incorrect link count", 4, service.getLinkCount());
235
236 Set<Link> links = service.getLinks(cp(DID1, P1));
237 assertEquals("incorrect links", ImmutableSet.of(l1, l2), links);
238 links = service.getEgressLinks(cp(DID1, P1));
239 assertEquals("incorrect links", ImmutableSet.of(l1), links);
240 links = service.getIngressLinks(cp(DID1, P1));
241 assertEquals("incorrect links", ImmutableSet.of(l2), links);
242
243 links = service.getDeviceLinks(DID2);
244 assertEquals("incorrect links", ImmutableSet.of(l1, l2, l3, l4), links);
245 links = service.getDeviceLinks(DID3);
246 assertEquals("incorrect links", ImmutableSet.of(l3, l4), links);
247
248 links = service.getDeviceEgressLinks(DID2);
249 assertEquals("incorrect links", ImmutableSet.of(l2, l4), links);
250 links = service.getDeviceIngressLinks(DID2);
251 assertEquals("incorrect links", ImmutableSet.of(l1, l3), links);
252 }
253
254
255 private Link addLink(DeviceId sd, PortNumber sp, DeviceId dd, PortNumber dp,
256 Link.Type type) {
257 providerService.linkDetected(new DefaultLinkDescription(cp(sd, sp), cp(dd, dp), type));
258 Link link = listener.events.get(0).subject();
259 validateEvents(LINK_ADDED);
260 return link;
261 }
262
263 private ConnectPoint cp(DeviceId id, PortNumber portNumber) {
264 return new ConnectPoint(id, portNumber);
tombb58c202014-09-07 22:51:50 -0700265 }
266
tom0d395262014-09-07 16:53:40 -0700267 protected void validateEvents(Enum... types) {
268 int i = 0;
269 assertEquals("wrong events received", types.length, listener.events.size());
270 for (Event event : listener.events) {
271 assertEquals("incorrect event type", types[i], event.type());
272 i++;
273 }
274 listener.events.clear();
275 }
276
277
278 private class TestProvider extends AbstractProvider implements LinkProvider {
279 private Device deviceReceived;
280 private MastershipRole roleReceived;
281
282 public TestProvider() {
283 super(PID);
284 }
285 }
286
287 private static class TestListener implements LinkListener {
288 final List<LinkEvent> events = new ArrayList<>();
289
290 @Override
291 public void event(LinkEvent event) {
292 events.add(event);
293 }
294 }
295
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700296 private static class TestDeviceManager extends DeviceManager {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800297
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700298 @Override
299 public MastershipRole getRole(DeviceId deviceId) {
300 return MastershipRole.MASTER;
301 }
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800302
303 @Override
304 public Device getDevice(DeviceId deviceId) {
305 return DEVICEIDMAP.get(deviceId);
306 }
307
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700308 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700309 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
310 }
tom0d395262014-09-07 16:53:40 -0700311}