blob: 391b2fd72133a6ac9e05a8c2c2b306c91ae65e15 [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;
56import static org.onosproject.net.link.LinkEvent.Type.*;
tom0d395262014-09-07 16:53:40 -070057
58/**
59 * Test codifying the link service & link provider service contracts.
60 */
tom35c0dc32014-09-19 10:00:58 -070061public class LinkManagerTest {
tom0d395262014-09-07 16:53:40 -070062
tom7e02cda2014-09-18 12:05:46 -070063 private static final ProviderId PID = new ProviderId("of", "foo");
tom0d395262014-09-07 16:53:40 -070064 private static final DeviceId DID1 = deviceId("of:foo");
65 private static final DeviceId DID2 = deviceId("of:bar");
tomd176fc42014-09-08 00:12:30 -070066 private static final DeviceId DID3 = deviceId("of:goo");
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080067 private static final Device DEV1 = new DefaultDevice(
68 PID, DID1, Device.Type.SWITCH, "", "", "", "", null);
69 private static final Device DEV2 = new DefaultDevice(
70 PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
71 private static final Device DEV3 = new DefaultDevice(
72 PID, DID2, Device.Type.SWITCH, "", "", "", "", null);
tom0d395262014-09-07 16:53:40 -070073
74 private static final PortNumber P1 = PortNumber.portNumber(1);
75 private static final PortNumber P2 = PortNumber.portNumber(2);
76 private static final PortNumber P3 = PortNumber.portNumber(3);
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080077 private static final Map<DeviceId, Device> DEVICEIDMAP = new HashMap<>();
tom0d395262014-09-07 16:53:40 -070078
tom35c0dc32014-09-19 10:00:58 -070079 private LinkManager mgr;
tom0d395262014-09-07 16:53:40 -070080
81 protected LinkService service;
82 protected LinkAdminService admin;
83 protected LinkProviderRegistry registry;
84 protected LinkProviderService providerService;
85 protected TestProvider provider;
86 protected TestListener listener = new TestListener();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070087 protected DeviceManager devmgr = new TestDeviceManager();
tom0d395262014-09-07 16:53:40 -070088
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080089
Sahil Lele3a0cdd52015-07-21 14:16:31 -070090
tom0d395262014-09-07 16:53:40 -070091 @Before
92 public void setUp() {
tom35c0dc32014-09-19 10:00:58 -070093 mgr = new LinkManager();
tom0d395262014-09-07 16:53:40 -070094 service = mgr;
95 admin = mgr;
96 registry = mgr;
tom35c0dc32014-09-19 10:00:58 -070097 mgr.store = new SimpleLinkStore();
tom0d395262014-09-07 16:53:40 -070098 mgr.eventDispatcher = new TestEventDispatcher();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070099 mgr.deviceService = devmgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700100 mgr.networkConfigService = new TestNetworkConfigService();
tom0d395262014-09-07 16:53:40 -0700101 mgr.activate();
102
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800103 DEVICEIDMAP.put(DID1, DEV1);
104 DEVICEIDMAP.put(DID2, DEV2);
105 DEVICEIDMAP.put(DID3, DEV3);
106
tom0d395262014-09-07 16:53:40 -0700107 service.addListener(listener);
108
109 provider = new TestProvider();
110 providerService = registry.register(provider);
111 assertTrue("provider should be registered",
112 registry.getProviders().contains(provider.id()));
113 }
114
115 @After
116 public void tearDown() {
117 registry.unregister(provider);
118 assertFalse("provider should not be registered",
119 registry.getProviders().contains(provider.id()));
120 service.removeListener(listener);
121 mgr.deactivate();
122 }
123
tombb58c202014-09-07 22:51:50 -0700124 @Test
125 public void createLink() {
tomd176fc42014-09-08 00:12:30 -0700126 addLink(DID1, P1, DID2, P2, DIRECT);
127 addLink(DID2, P2, DID1, P1, DIRECT);
128 assertEquals("incorrect link count", 2, service.getLinkCount());
129
130 Iterator<Link> it = service.getLinks().iterator();
131 it.next();
132 it.next();
133 assertFalse("incorrect link count", it.hasNext());
134 }
135
136 @Test
137 public void updateLink() {
138 addLink(DID1, P1, DID2, P2, DIRECT);
139 addLink(DID2, P2, DID1, P1, INDIRECT);
140 assertEquals("incorrect link count", 2, service.getLinkCount());
141
142 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
143 validateEvents(LINK_UPDATED);
144 assertEquals("incorrect link count", 2, service.getLinkCount());
145
146 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), INDIRECT));
147 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
148 assertEquals("no events expected", 0, listener.events.size());
149 }
150
151 @Test
152 public void removeLink() {
153 addLink(DID1, P1, DID2, P2, DIRECT);
154 addLink(DID2, P2, DID1, P1, DIRECT);
155 assertEquals("incorrect link count", 2, service.getLinkCount());
156
157 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
158 validateEvents(LINK_REMOVED);
159 assertEquals("incorrect link count", 1, service.getLinkCount());
160 assertNull("link should not be found", service.getLink(cp(DID1, P1), cp(DID2, P2)));
161 assertNotNull("link should be found", service.getLink(cp(DID2, P2), cp(DID1, P1)));
162
163 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
164 assertEquals("no events expected", 0, listener.events.size());
165 }
166
167 @Test
168 public void removeLinksByConnectionPoint() {
169 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
170 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
171 addLink(DID3, P3, DID2, P1, DIRECT);
172 addLink(DID2, P1, DID3, P3, DIRECT);
173 assertEquals("incorrect link count", 4, service.getLinkCount());
174
175 providerService.linksVanished(cp(DID1, P1));
176 assertEquals("incorrect link count", 2, service.getLinkCount());
177 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
178 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
179 }
180
181 @Test
182 public void removeLinksByDevice() {
183 addLink(DID1, P1, DID2, P2, DIRECT);
184 addLink(DID2, P2, DID1, P1, DIRECT);
185 addLink(DID3, P3, DID2, P1, DIRECT);
186 addLink(DID2, P1, DID3, P3, DIRECT);
187 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
188 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
189 assertEquals("incorrect link count", 6, service.getLinkCount());
190
191 providerService.linksVanished(DID2);
192 assertEquals("incorrect link count", 2, service.getLinkCount());
193 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
194 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
195 }
196
197 @Test
198 public void removeLinksAsAdminByConnectionPoint() {
199 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
200 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
201 addLink(DID3, P3, DID2, P1, DIRECT);
202 addLink(DID2, P1, DID3, P3, DIRECT);
203 assertEquals("incorrect link count", 4, service.getLinkCount());
204
205 admin.removeLinks(cp(DID1, P1));
206 assertEquals("incorrect link count", 2, service.getLinkCount());
207 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
208 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
209 }
210
211 @Test
212 public void removeLinksAsAdminByDevice() {
213 addLink(DID1, P1, DID2, P2, DIRECT);
214 addLink(DID2, P2, DID1, P1, DIRECT);
215 addLink(DID3, P3, DID2, P1, DIRECT);
216 addLink(DID2, P1, DID3, P3, DIRECT);
217 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
218 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
219 assertEquals("incorrect link count", 6, service.getLinkCount());
220
221 admin.removeLinks(DID2);
222 assertEquals("incorrect link count", 2, service.getLinkCount());
223 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
224 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
225 }
226
227 @Test
228 public void getLinks() {
229 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
230 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
231 Link l3 = addLink(DID3, P3, DID2, P1, DIRECT);
232 Link l4 = addLink(DID2, P1, DID3, P3, DIRECT);
233 assertEquals("incorrect link count", 4, service.getLinkCount());
234
235 Set<Link> links = service.getLinks(cp(DID1, P1));
236 assertEquals("incorrect links", ImmutableSet.of(l1, l2), links);
237 links = service.getEgressLinks(cp(DID1, P1));
238 assertEquals("incorrect links", ImmutableSet.of(l1), links);
239 links = service.getIngressLinks(cp(DID1, P1));
240 assertEquals("incorrect links", ImmutableSet.of(l2), links);
241
242 links = service.getDeviceLinks(DID2);
243 assertEquals("incorrect links", ImmutableSet.of(l1, l2, l3, l4), links);
244 links = service.getDeviceLinks(DID3);
245 assertEquals("incorrect links", ImmutableSet.of(l3, l4), links);
246
247 links = service.getDeviceEgressLinks(DID2);
248 assertEquals("incorrect links", ImmutableSet.of(l2, l4), links);
249 links = service.getDeviceIngressLinks(DID2);
250 assertEquals("incorrect links", ImmutableSet.of(l1, l3), links);
251 }
252
253
254 private Link addLink(DeviceId sd, PortNumber sp, DeviceId dd, PortNumber dp,
255 Link.Type type) {
256 providerService.linkDetected(new DefaultLinkDescription(cp(sd, sp), cp(dd, dp), type));
257 Link link = listener.events.get(0).subject();
258 validateEvents(LINK_ADDED);
259 return link;
260 }
261
262 private ConnectPoint cp(DeviceId id, PortNumber portNumber) {
263 return new ConnectPoint(id, portNumber);
tombb58c202014-09-07 22:51:50 -0700264 }
265
tom0d395262014-09-07 16:53:40 -0700266 protected void validateEvents(Enum... types) {
267 int i = 0;
268 assertEquals("wrong events received", types.length, listener.events.size());
269 for (Event event : listener.events) {
270 assertEquals("incorrect event type", types[i], event.type());
271 i++;
272 }
273 listener.events.clear();
274 }
275
276
277 private class TestProvider extends AbstractProvider implements LinkProvider {
278 private Device deviceReceived;
279 private MastershipRole roleReceived;
280
281 public TestProvider() {
282 super(PID);
283 }
284 }
285
286 private static class TestListener implements LinkListener {
287 final List<LinkEvent> events = new ArrayList<>();
288
289 @Override
290 public void event(LinkEvent event) {
291 events.add(event);
292 }
293 }
294
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700295 private static class TestDeviceManager extends DeviceManager {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800296
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700297 @Override
298 public MastershipRole getRole(DeviceId deviceId) {
299 return MastershipRole.MASTER;
300 }
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800301
302 @Override
303 public Device getDevice(DeviceId deviceId) {
304 return DEVICEIDMAP.get(deviceId);
305 }
306
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700307 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700308 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
309 }
tom0d395262014-09-07 16:53:40 -0700310}