blob: 2cab8fe4ee30f9bd79541f6bbfe52062f7a8fa33 [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 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.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;
22import org.onlab.onos.event.Event;
tombb58c202014-09-07 22:51:50 -070023import org.onlab.onos.net.ConnectPoint;
tom0d395262014-09-07 16:53:40 -070024import org.onlab.onos.net.Device;
25import org.onlab.onos.net.DeviceId;
tombb58c202014-09-07 22:51:50 -070026import org.onlab.onos.net.Link;
tom0d395262014-09-07 16:53:40 -070027import org.onlab.onos.net.MastershipRole;
tom0d395262014-09-07 16:53:40 -070028import org.onlab.onos.net.PortNumber;
tombb58c202014-09-07 22:51:50 -070029import org.onlab.onos.net.link.DefaultLinkDescription;
tom0d395262014-09-07 16:53:40 -070030import org.onlab.onos.net.link.LinkAdminService;
31import org.onlab.onos.net.link.LinkEvent;
32import org.onlab.onos.net.link.LinkListener;
33import org.onlab.onos.net.link.LinkProvider;
34import org.onlab.onos.net.link.LinkProviderRegistry;
35import org.onlab.onos.net.link.LinkProviderService;
36import org.onlab.onos.net.link.LinkService;
37import org.onlab.onos.net.provider.AbstractProvider;
38import org.onlab.onos.net.provider.ProviderId;
tom8bf2e6b2014-09-10 20:53:54 -070039import org.onlab.onos.event.impl.TestEventDispatcher;
tombe988312014-09-19 18:38:47 -070040import org.onlab.onos.net.device.impl.DeviceManager;
tomea961ff2014-10-01 12:45:15 -070041import org.onlab.onos.store.trivial.impl.SimpleLinkStore;
tom0d395262014-09-07 16:53:40 -070042
43import java.util.ArrayList;
tomd176fc42014-09-08 00:12:30 -070044import java.util.Iterator;
tom0d395262014-09-07 16:53:40 -070045import java.util.List;
tomd176fc42014-09-08 00:12:30 -070046import java.util.Set;
tom0d395262014-09-07 16:53:40 -070047
48import static org.junit.Assert.*;
tom0d395262014-09-07 16:53:40 -070049import static org.onlab.onos.net.DeviceId.deviceId;
tomd176fc42014-09-08 00:12:30 -070050import static org.onlab.onos.net.Link.Type.DIRECT;
51import static org.onlab.onos.net.Link.Type.INDIRECT;
52import static org.onlab.onos.net.link.LinkEvent.Type.*;
tom0d395262014-09-07 16:53:40 -070053
54/**
55 * Test codifying the link service & link provider service contracts.
56 */
tom35c0dc32014-09-19 10:00:58 -070057public class LinkManagerTest {
tom0d395262014-09-07 16:53:40 -070058
tom7e02cda2014-09-18 12:05:46 -070059 private static final ProviderId PID = new ProviderId("of", "foo");
tom0d395262014-09-07 16:53:40 -070060 private static final DeviceId DID1 = deviceId("of:foo");
61 private static final DeviceId DID2 = deviceId("of:bar");
tomd176fc42014-09-08 00:12:30 -070062 private static final DeviceId DID3 = deviceId("of:goo");
tom0d395262014-09-07 16:53:40 -070063
64 private static final PortNumber P1 = PortNumber.portNumber(1);
65 private static final PortNumber P2 = PortNumber.portNumber(2);
66 private static final PortNumber P3 = PortNumber.portNumber(3);
67
68
tom35c0dc32014-09-19 10:00:58 -070069 private LinkManager mgr;
tom0d395262014-09-07 16:53:40 -070070
71 protected LinkService service;
72 protected LinkAdminService admin;
73 protected LinkProviderRegistry registry;
74 protected LinkProviderService providerService;
75 protected TestProvider provider;
76 protected TestListener listener = new TestListener();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070077 protected DeviceManager devmgr = new TestDeviceManager();
tom0d395262014-09-07 16:53:40 -070078
79 @Before
80 public void setUp() {
tom35c0dc32014-09-19 10:00:58 -070081 mgr = new LinkManager();
tom0d395262014-09-07 16:53:40 -070082 service = mgr;
83 admin = mgr;
84 registry = mgr;
tom35c0dc32014-09-19 10:00:58 -070085 mgr.store = new SimpleLinkStore();
tom0d395262014-09-07 16:53:40 -070086 mgr.eventDispatcher = new TestEventDispatcher();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070087 mgr.deviceService = devmgr;
tom0d395262014-09-07 16:53:40 -070088 mgr.activate();
89
90 service.addListener(listener);
91
92 provider = new TestProvider();
93 providerService = registry.register(provider);
94 assertTrue("provider should be registered",
95 registry.getProviders().contains(provider.id()));
96 }
97
98 @After
99 public void tearDown() {
100 registry.unregister(provider);
101 assertFalse("provider should not be registered",
102 registry.getProviders().contains(provider.id()));
103 service.removeListener(listener);
104 mgr.deactivate();
105 }
106
tombb58c202014-09-07 22:51:50 -0700107 @Test
108 public void createLink() {
tomd176fc42014-09-08 00:12:30 -0700109 addLink(DID1, P1, DID2, P2, DIRECT);
110 addLink(DID2, P2, DID1, P1, DIRECT);
111 assertEquals("incorrect link count", 2, service.getLinkCount());
112
113 Iterator<Link> it = service.getLinks().iterator();
114 it.next();
115 it.next();
116 assertFalse("incorrect link count", it.hasNext());
117 }
118
119 @Test
120 public void updateLink() {
121 addLink(DID1, P1, DID2, P2, DIRECT);
122 addLink(DID2, P2, DID1, P1, INDIRECT);
123 assertEquals("incorrect link count", 2, service.getLinkCount());
124
125 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
126 validateEvents(LINK_UPDATED);
127 assertEquals("incorrect link count", 2, service.getLinkCount());
128
129 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), INDIRECT));
130 providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
131 assertEquals("no events expected", 0, listener.events.size());
132 }
133
134 @Test
135 public void removeLink() {
136 addLink(DID1, P1, DID2, P2, DIRECT);
137 addLink(DID2, P2, DID1, P1, DIRECT);
138 assertEquals("incorrect link count", 2, service.getLinkCount());
139
140 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
141 validateEvents(LINK_REMOVED);
142 assertEquals("incorrect link count", 1, service.getLinkCount());
143 assertNull("link should not be found", service.getLink(cp(DID1, P1), cp(DID2, P2)));
144 assertNotNull("link should be found", service.getLink(cp(DID2, P2), cp(DID1, P1)));
145
146 providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
147 assertEquals("no events expected", 0, listener.events.size());
148 }
149
150 @Test
151 public void removeLinksByConnectionPoint() {
152 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
153 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
154 addLink(DID3, P3, DID2, P1, DIRECT);
155 addLink(DID2, P1, DID3, P3, DIRECT);
156 assertEquals("incorrect link count", 4, service.getLinkCount());
157
158 providerService.linksVanished(cp(DID1, P1));
159 assertEquals("incorrect link count", 2, service.getLinkCount());
160 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
161 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
162 }
163
164 @Test
165 public void removeLinksByDevice() {
166 addLink(DID1, P1, DID2, P2, DIRECT);
167 addLink(DID2, P2, DID1, P1, DIRECT);
168 addLink(DID3, P3, DID2, P1, DIRECT);
169 addLink(DID2, P1, DID3, P3, DIRECT);
170 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
171 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
172 assertEquals("incorrect link count", 6, service.getLinkCount());
173
174 providerService.linksVanished(DID2);
175 assertEquals("incorrect link count", 2, service.getLinkCount());
176 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
177 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
178 }
179
180 @Test
181 public void removeLinksAsAdminByConnectionPoint() {
182 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
183 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
184 addLink(DID3, P3, DID2, P1, DIRECT);
185 addLink(DID2, P1, DID3, P3, DIRECT);
186 assertEquals("incorrect link count", 4, service.getLinkCount());
187
188 admin.removeLinks(cp(DID1, P1));
189 assertEquals("incorrect link count", 2, service.getLinkCount());
190 assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
191 assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
192 }
193
194 @Test
195 public void removeLinksAsAdminByDevice() {
196 addLink(DID1, P1, DID2, P2, DIRECT);
197 addLink(DID2, P2, DID1, P1, DIRECT);
198 addLink(DID3, P3, DID2, P1, DIRECT);
199 addLink(DID2, P1, DID3, P3, DIRECT);
200 Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
201 Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
202 assertEquals("incorrect link count", 6, service.getLinkCount());
203
204 admin.removeLinks(DID2);
205 assertEquals("incorrect link count", 2, service.getLinkCount());
206 assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
207 assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
208 }
209
210 @Test
211 public void getLinks() {
212 Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
213 Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
214 Link l3 = addLink(DID3, P3, DID2, P1, DIRECT);
215 Link l4 = addLink(DID2, P1, DID3, P3, DIRECT);
216 assertEquals("incorrect link count", 4, service.getLinkCount());
217
218 Set<Link> links = service.getLinks(cp(DID1, P1));
219 assertEquals("incorrect links", ImmutableSet.of(l1, l2), links);
220 links = service.getEgressLinks(cp(DID1, P1));
221 assertEquals("incorrect links", ImmutableSet.of(l1), links);
222 links = service.getIngressLinks(cp(DID1, P1));
223 assertEquals("incorrect links", ImmutableSet.of(l2), links);
224
225 links = service.getDeviceLinks(DID2);
226 assertEquals("incorrect links", ImmutableSet.of(l1, l2, l3, l4), links);
227 links = service.getDeviceLinks(DID3);
228 assertEquals("incorrect links", ImmutableSet.of(l3, l4), links);
229
230 links = service.getDeviceEgressLinks(DID2);
231 assertEquals("incorrect links", ImmutableSet.of(l2, l4), links);
232 links = service.getDeviceIngressLinks(DID2);
233 assertEquals("incorrect links", ImmutableSet.of(l1, l3), links);
234 }
235
236
237 private Link addLink(DeviceId sd, PortNumber sp, DeviceId dd, PortNumber dp,
238 Link.Type type) {
239 providerService.linkDetected(new DefaultLinkDescription(cp(sd, sp), cp(dd, dp), type));
240 Link link = listener.events.get(0).subject();
241 validateEvents(LINK_ADDED);
242 return link;
243 }
244
245 private ConnectPoint cp(DeviceId id, PortNumber portNumber) {
246 return new ConnectPoint(id, portNumber);
tombb58c202014-09-07 22:51:50 -0700247 }
248
tom0d395262014-09-07 16:53:40 -0700249 protected void validateEvents(Enum... types) {
250 int i = 0;
251 assertEquals("wrong events received", types.length, listener.events.size());
252 for (Event event : listener.events) {
253 assertEquals("incorrect event type", types[i], event.type());
254 i++;
255 }
256 listener.events.clear();
257 }
258
259
260 private class TestProvider extends AbstractProvider implements LinkProvider {
261 private Device deviceReceived;
262 private MastershipRole roleReceived;
263
264 public TestProvider() {
265 super(PID);
266 }
267 }
268
269 private static class TestListener implements LinkListener {
270 final List<LinkEvent> events = new ArrayList<>();
271
272 @Override
273 public void event(LinkEvent event) {
274 events.add(event);
275 }
276 }
277
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700278 private static class TestDeviceManager extends DeviceManager {
279 @Override
280 public MastershipRole getRole(DeviceId deviceId) {
281 return MastershipRole.MASTER;
282 }
283 }
284
tom0d395262014-09-07 16:53:40 -0700285}