blob: 98fc07d12cf28ceec8728052004e1db4a8935650 [file] [log] [blame]
Simon Hunt642bc452016-05-04 19:34:45 -07001/*
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 */
16
17package org.onosproject.ui.impl.topo.model;
18
Simon Huntc0f20c12016-05-09 09:30:20 -070019import com.google.common.collect.ImmutableList;
Simon Hunt642bc452016-05-04 19:34:45 -070020import com.google.common.collect.ImmutableSet;
Simon Huntc0f20c12016-05-09 09:30:20 -070021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Simon Hunt642bc452016-05-04 19:34:45 -070024import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ClusterServiceAdapter;
26import org.onosproject.cluster.ControllerNode;
Simon Huntc0f20c12016-05-09 09:30:20 -070027import org.onosproject.cluster.DefaultControllerNode;
Simon Hunt642bc452016-05-04 19:34:45 -070028import org.onosproject.cluster.NodeId;
Simon Huntc0f20c12016-05-09 09:30:20 -070029import org.onosproject.cluster.RoleInfo;
Simon Hunt642bc452016-05-04 19:34:45 -070030import org.onosproject.mastership.MastershipService;
31import org.onosproject.mastership.MastershipServiceAdapter;
Simon Huntc0f20c12016-05-09 09:30:20 -070032import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultDevice;
34import org.onosproject.net.DefaultHost;
35import org.onosproject.net.DefaultLink;
36import org.onosproject.net.Device;
Simon Hunt642bc452016-05-04 19:34:45 -070037import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070038import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
40import org.onosproject.net.HostLocation;
41import org.onosproject.net.Link;
42import org.onosproject.net.PortNumber;
Simon Hunt642bc452016-05-04 19:34:45 -070043import org.onosproject.net.device.DeviceService;
Simon Huntc0f20c12016-05-09 09:30:20 -070044import org.onosproject.net.device.DeviceServiceAdapter;
Simon Hunt642bc452016-05-04 19:34:45 -070045import org.onosproject.net.flow.FlowRuleService;
46import org.onosproject.net.host.HostService;
Simon Huntc0f20c12016-05-09 09:30:20 -070047import org.onosproject.net.host.HostServiceAdapter;
Simon Hunt642bc452016-05-04 19:34:45 -070048import org.onosproject.net.intent.IntentService;
49import org.onosproject.net.link.LinkService;
Simon Huntc0f20c12016-05-09 09:30:20 -070050import org.onosproject.net.link.LinkServiceAdapter;
51import org.onosproject.net.provider.ProviderId;
52import org.onosproject.net.region.DefaultRegion;
53import org.onosproject.net.region.Region;
54import org.onosproject.net.region.RegionId;
55import org.onosproject.net.region.RegionListener;
Simon Hunt642bc452016-05-04 19:34:45 -070056import org.onosproject.net.region.RegionService;
57import org.onosproject.ui.impl.AbstractUiImplTest;
58import org.onosproject.ui.model.ServiceBundle;
59
Simon Huntc0f20c12016-05-09 09:30:20 -070060import java.util.ArrayList;
61import java.util.Collections;
Simon Hunt642bc452016-05-04 19:34:45 -070062import java.util.HashMap;
Simon Huntc0f20c12016-05-09 09:30:20 -070063import java.util.HashSet;
64import java.util.List;
Simon Hunt642bc452016-05-04 19:34:45 -070065import java.util.Map;
66import java.util.Set;
67
Simon Huntc0f20c12016-05-09 09:30:20 -070068import static org.onosproject.cluster.NodeId.nodeId;
Simon Hunt642bc452016-05-04 19:34:45 -070069import static org.onosproject.net.DeviceId.deviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070070import static org.onosproject.net.HostId.hostId;
71import static org.onosproject.net.PortNumber.portNumber;
Simon Hunt642bc452016-05-04 19:34:45 -070072
73/**
74 * Base class for model test classes.
75 */
76abstract class AbstractTopoModelTest extends AbstractUiImplTest {
77
Simon Huntc0f20c12016-05-09 09:30:20 -070078 /*
79 Our mock environment:
80
81 Three controllers: C1, C2, C3
82
83 Nine devices: D1 .. D9
84
85 D4 ---+ +--- D7
86 | |
87 D5 --- D1 --- D2 --- D3 --- D8
88 | |
89 D6 ---+ +--- D9
90
91 Twelve hosts (two per D4 ... D9) H4a, H4b, H5a, H5b, ...
92
93 Regions:
94 R1 : D1, D2, D3
95 R2 : D4, D5, D6
96 R3 : D7, D8, D9
97
98 Mastership:
99 C1 : D1, D2, D3
100 C2 : D4, D5, D6
101 C3 : D7, D8, D9
102
103 Roles: (backups)
104 C1 -> C2, C3
105 C2 -> C1, C3
106 C3 -> C1, C2
107 */
108
109 protected static final String C1 = "C1";
110 protected static final String C2 = "C2";
111 protected static final String C3 = "C3";
112
113 protected static final NodeId CNID_1 = nodeId(C1);
114 protected static final NodeId CNID_2 = nodeId(C2);
115 protected static final NodeId CNID_3 = nodeId(C3);
116
117 protected static final ControllerNode CNODE_1 = cnode(CNID_1, "10.0.0.1");
118 protected static final ControllerNode CNODE_2 = cnode(CNID_2, "10.0.0.2");
119 protected static final ControllerNode CNODE_3 = cnode(CNID_3, "10.0.0.3");
120
121 protected static final String R1 = "R1";
122 protected static final String R2 = "R2";
123 protected static final String R3 = "R3";
124
125 protected static final Set<NodeId> SET_C1 = ImmutableSet.of(CNID_1);
126 protected static final Set<NodeId> SET_C2 = ImmutableSet.of(CNID_2);
127 protected static final Set<NodeId> SET_C3 = ImmutableSet.of(CNID_3);
128
129 protected static final Region REGION_1 =
130 region(R1, Region.Type.METRO, ImmutableList.of(SET_C1, SET_C2));
131 protected static final Region REGION_2 =
132 region(R2, Region.Type.CAMPUS, ImmutableList.of(SET_C2, SET_C1));
133 protected static final Region REGION_3 =
134 region(R3, Region.Type.CAMPUS, ImmutableList.of(SET_C3, SET_C1));
135
136 protected static final Set<Region> REGION_SET =
137 ImmutableSet.of(REGION_1, REGION_2, REGION_3);
138
139 protected static final String D1 = "D1";
140 protected static final String D2 = "D2";
141 protected static final String D3 = "D3";
142 protected static final String D4 = "D4";
143 protected static final String D5 = "D5";
144 protected static final String D6 = "D6";
145 protected static final String D7 = "D7";
146 protected static final String D8 = "D8";
147 protected static final String D9 = "D9";
148
149 protected static final String MFR = "Mfr";
150 protected static final String HW = "h/w";
151 protected static final String SW = "s/w";
152 protected static final String SERIAL = "ser123";
153
154 protected static final DeviceId DEVID_1 = deviceId(D1);
155 protected static final DeviceId DEVID_2 = deviceId(D2);
156 protected static final DeviceId DEVID_3 = deviceId(D3);
157 protected static final DeviceId DEVID_4 = deviceId(D4);
158 protected static final DeviceId DEVID_5 = deviceId(D5);
159 protected static final DeviceId DEVID_6 = deviceId(D6);
160 protected static final DeviceId DEVID_7 = deviceId(D7);
161 protected static final DeviceId DEVID_8 = deviceId(D8);
162 protected static final DeviceId DEVID_9 = deviceId(D9);
163
164 protected static final Device DEV_1 = device(D1);
165 protected static final Device DEV_2 = device(D2);
166 protected static final Device DEV_3 = device(D3);
167 protected static final Device DEV_4 = device(D4);
168 protected static final Device DEV_5 = device(D5);
169 protected static final Device DEV_6 = device(D6);
170 protected static final Device DEV_7 = device(D7);
171 protected static final Device DEV_8 = device(D8);
172 protected static final Device DEV_9 = device(D9);
173
174 protected static final List<Device> ALL_DEVS =
175 ImmutableList.of(
176 DEV_1, DEV_2, DEV_3,
177 DEV_4, DEV_5, DEV_6,
178 DEV_7, DEV_8, DEV_9
179 );
180
181 private static final Set<DeviceId> DEVS_TRUNK =
182 ImmutableSet.of(DEVID_1, DEVID_2, DEVID_3);
183
184 private static final Set<DeviceId> DEVS_LEFT =
185 ImmutableSet.of(DEVID_4, DEVID_5, DEVID_6);
186
187 private static final Set<DeviceId> DEVS_RIGHT =
188 ImmutableSet.of(DEVID_7, DEVID_8, DEVID_9);
189
190 private static final String[][] LINK_CONNECT_DATA = {
191 {D1, "12", D2, "21"},
192 {D2, "23", D3, "32"},
193 {D4, "41", D1, "14"},
194 {D5, "51", D1, "15"},
195 {D6, "61", D1, "16"},
196 {D7, "73", D3, "37"},
197 {D8, "83", D3, "38"},
198 {D9, "93", D3, "39"},
199 };
200
201 private static final String HOST_MAC_PREFIX = "aa:00:00:00:00:";
202
203 /**
204 * Returns IP address instance for given string.
205 *
206 * @param s string
207 * @return IP address
208 */
209 protected static IpAddress ip(String s) {
210 return IpAddress.valueOf(s);
211 }
212
213 /**
214 * Returns controller node instance for given ID and IP.
215 *
216 * @param id identifier
217 * @param ip IP address
218 * @return controller node instance
219 */
220 protected static ControllerNode cnode(NodeId id, String ip) {
221 return new DefaultControllerNode(id, ip(ip));
222 }
223
224 /**
225 * Returns a region instance with specified parameters.
226 *
227 * @param id region id
228 * @param type region type
229 * @param masters ordered list of master sets
230 * @return region instance
231 */
232 protected static Region region(String id, Region.Type type,
233 List<Set<NodeId>> masters) {
234 return new DefaultRegion(RegionId.regionId(id), "Region-" + id,
235 type, masters);
236 }
237
238 /**
239 * Returns device with given ID.
240 *
241 * @param id device ID
242 * @return device instance
243 */
244 protected static Device device(String id) {
245 return new DefaultDevice(ProviderId.NONE, deviceId(id),
246 Device.Type.SWITCH, MFR, HW, SW, SERIAL, null);
247 }
248
Simon Hunt642bc452016-05-04 19:34:45 -0700249 /**
250 * Returns canned results.
Simon Huntc0f20c12016-05-09 09:30:20 -0700251 * <p>
Simon Hunt642bc452016-05-04 19:34:45 -0700252 * At some future point, we may make this "programmable", so that
Simon Huntc0f20c12016-05-09 09:30:20 -0700253 * its state can be changed over the course of a unit test.
Simon Hunt642bc452016-05-04 19:34:45 -0700254 */
255 protected static final ServiceBundle MOCK_SERVICES =
256 new ServiceBundle() {
257 @Override
258 public ClusterService cluster() {
259 return MOCK_CLUSTER;
260 }
261
262 @Override
263 public MastershipService mastership() {
264 return MOCK_MASTER;
265 }
266
267 @Override
268 public RegionService region() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700269 return MOCK_REGION;
Simon Hunt642bc452016-05-04 19:34:45 -0700270 }
271
272 @Override
273 public DeviceService device() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700274 return MOCK_DEVICE;
Simon Hunt642bc452016-05-04 19:34:45 -0700275 }
276
277 @Override
278 public LinkService link() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700279 return MOCK_LINK;
Simon Hunt642bc452016-05-04 19:34:45 -0700280 }
281
282 @Override
283 public HostService host() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700284 return MOCK_HOST;
Simon Hunt642bc452016-05-04 19:34:45 -0700285 }
286
287 @Override
288 public IntentService intent() {
289 return null;
290 }
291
292 @Override
293 public FlowRuleService flow() {
294 return null;
295 }
296 };
297
298 private static final ClusterService MOCK_CLUSTER = new MockClusterService();
299 private static final MastershipService MOCK_MASTER = new MockMasterService();
Simon Huntc0f20c12016-05-09 09:30:20 -0700300 private static final RegionService MOCK_REGION = new MockRegionService();
301 private static final DeviceService MOCK_DEVICE = new MockDeviceService();
302 private static final LinkService MOCK_LINK = new MockLinkService();
303 private static final HostService MOCK_HOST = new MockHostService();
Simon Hunt642bc452016-05-04 19:34:45 -0700304
Simon Hunt642bc452016-05-04 19:34:45 -0700305
306
307 private static class MockClusterService extends ClusterServiceAdapter {
Simon Huntc0f20c12016-05-09 09:30:20 -0700308 private final Map<NodeId, ControllerNode> nodes = new HashMap<>();
Simon Hunt642bc452016-05-04 19:34:45 -0700309 private final Map<NodeId, ControllerNode.State> states = new HashMap<>();
310
Simon Huntc0f20c12016-05-09 09:30:20 -0700311 MockClusterService() {
312 nodes.put(CNODE_1.id(), CNODE_1);
313 nodes.put(CNODE_2.id(), CNODE_2);
314 nodes.put(CNODE_3.id(), CNODE_3);
315
316 states.put(CNODE_1.id(), ControllerNode.State.READY);
317 states.put(CNODE_2.id(), ControllerNode.State.ACTIVE);
318 states.put(CNODE_3.id(), ControllerNode.State.ACTIVE);
319 }
320
321 @Override
322 public Set<ControllerNode> getNodes() {
323 return ImmutableSet.copyOf(nodes.values());
324 }
325
326 @Override
327 public ControllerNode getNode(NodeId nodeId) {
328 return nodes.get(nodeId);
329 }
Simon Hunt642bc452016-05-04 19:34:45 -0700330
331 @Override
332 public ControllerNode.State getState(NodeId nodeId) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700333 return states.get(nodeId);
Simon Hunt642bc452016-05-04 19:34:45 -0700334 }
335 }
336
Simon Hunt642bc452016-05-04 19:34:45 -0700337
338 private static class MockMasterService extends MastershipServiceAdapter {
Simon Huntc0f20c12016-05-09 09:30:20 -0700339 private final Map<NodeId, Set<DeviceId>> masterOf = new HashMap<>();
340
341 MockMasterService() {
342 masterOf.put(CNODE_1.id(), DEVS_TRUNK);
343 masterOf.put(CNODE_2.id(), DEVS_LEFT);
344 masterOf.put(CNODE_3.id(), DEVS_RIGHT);
345 }
346
347 @Override
348 public NodeId getMasterFor(DeviceId deviceId) {
349 if (DEVS_TRUNK.contains(deviceId)) {
350 return CNID_1;
351 }
352 if (DEVS_LEFT.contains(deviceId)) {
353 return CNID_2;
354 }
355 if (DEVS_RIGHT.contains(deviceId)) {
356 return CNID_3;
357 }
358 return null;
359 }
360
Simon Hunt642bc452016-05-04 19:34:45 -0700361 @Override
362 public Set<DeviceId> getDevicesOf(NodeId nodeId) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700363 return masterOf.get(nodeId);
Simon Hunt642bc452016-05-04 19:34:45 -0700364 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700365
366 @Override
367 public RoleInfo getNodesFor(DeviceId deviceId) {
368 NodeId master = null;
369 List<NodeId> backups = new ArrayList<>();
370
371 if (DEVS_TRUNK.contains(deviceId)) {
372 master = CNID_1;
373 backups.add(CNID_2);
374 backups.add(CNID_3);
375 } else if (DEVS_LEFT.contains(deviceId)) {
376 master = CNID_2;
377 backups.add(CNID_1);
378 backups.add(CNID_3);
379 } else if (DEVS_RIGHT.contains(deviceId)) {
380 master = CNID_3;
381 backups.add(CNID_1);
382 backups.add(CNID_2);
383 }
384 return new RoleInfo(master, backups);
385 }
386 }
387
388 // TODO: consider implementing RegionServiceAdapter and extending that here
389 private static class MockRegionService implements RegionService {
390
391 private final Map<RegionId, Region> lookup = new HashMap<>();
392
393 MockRegionService() {
394 lookup.put(REGION_1.id(), REGION_1);
395 lookup.put(REGION_2.id(), REGION_2);
396 lookup.put(REGION_3.id(), REGION_3);
397 }
398
399 @Override
400 public Set<Region> getRegions() {
401 return REGION_SET;
402 }
403
404 @Override
405 public Region getRegion(RegionId regionId) {
406 return lookup.get(regionId);
407 }
408
409 @Override
410 public Region getRegionForDevice(DeviceId deviceId) {
411 if (DEVS_TRUNK.contains(deviceId)) {
412 return REGION_1;
413 }
414 if (DEVS_LEFT.contains(deviceId)) {
415 return REGION_2;
416 }
417 if (DEVS_RIGHT.contains(deviceId)) {
418 return REGION_3;
419 }
420 return null;
421 }
422
423 @Override
424 public Set<DeviceId> getRegionDevices(RegionId regionId) {
425 if (REGION_1.id().equals(regionId)) {
426 return DEVS_TRUNK;
427 }
428 if (REGION_2.id().equals(regionId)) {
429 return DEVS_LEFT;
430 }
431 if (REGION_3.id().equals(regionId)) {
432 return DEVS_RIGHT;
433 }
434 return Collections.emptySet();
435 }
436
437 @Override
438 public void addListener(RegionListener listener) {
439 }
440
441 @Override
442 public void removeListener(RegionListener listener) {
443 }
444 }
445
446
447 private static class MockDeviceService extends DeviceServiceAdapter {
448 private final Map<DeviceId, Device> devices = new HashMap<>();
449
450 MockDeviceService() {
451 for (Device dev : ALL_DEVS) {
452 devices.put(dev.id(), dev);
453 }
454 }
455
456 @Override
457 public int getDeviceCount() {
458 return devices.size();
459 }
460
461 @Override
462 public Iterable<Device> getDevices() {
463 return ImmutableList.copyOf(devices.values());
464 }
465
466 @Override
467 public Device getDevice(DeviceId deviceId) {
468 return devices.get(deviceId);
469 }
470
471 }
472
473
474 private static class MockLinkService extends LinkServiceAdapter {
475 private final Set<Link> links = new HashSet<>();
476
477 MockLinkService() {
478 for (String[] linkPair : LINK_CONNECT_DATA) {
479 links.addAll(makeLinks(linkPair));
480 }
481
482 }
483
484 private Set<Link> makeLinks(String[] linkPair) {
485 DeviceId devA = deviceId(linkPair[0]);
486 PortNumber portA = portNumber(Long.valueOf(linkPair[1]));
487 DeviceId devB = deviceId(linkPair[2]);
488 PortNumber portB = portNumber(Long.valueOf(linkPair[3]));
489
490 Link linkA = DefaultLink.builder()
491 .providerId(ProviderId.NONE)
492 .type(Link.Type.DIRECT)
493 .src(new ConnectPoint(devA, portA))
494 .dst(new ConnectPoint(devB, portB))
495 .build();
496
497 Link linkB = DefaultLink.builder()
498 .providerId(ProviderId.NONE)
499 .type(Link.Type.DIRECT)
500 .src(new ConnectPoint(devB, portB))
501 .dst(new ConnectPoint(devA, portA))
502 .build();
503
504 return ImmutableSet.of(linkA, linkB);
505 }
506
507 @Override
508 public int getLinkCount() {
509 return links.size();
510 }
511
512 @Override
513 public Iterable<Link> getLinks() {
514 return ImmutableSet.copyOf(links);
515 }
516
517 // TODO: possibly fill out other methods if we find the model uses them
518 }
519
520
521 private static class MockHostService extends HostServiceAdapter {
522 private final Map<HostId, Host> hosts = new HashMap<>();
523
524 MockHostService() {
525 for (Device d : ALL_DEVS) {
526 // two hosts per device
527 createHosts(hosts, d);
528 }
529 }
530
531 private void createHosts(Map<HostId, Host> hosts, Device d) {
532 DeviceId deviceId = d.id();
533 String devNum = deviceId.toString().substring(1);
534
535 String ha = devNum + "a";
536 String hb = devNum + "b";
537
538 MacAddress macA = MacAddress.valueOf(HOST_MAC_PREFIX + ha);
539 MacAddress macB = MacAddress.valueOf(HOST_MAC_PREFIX + hb);
540
541 HostId hostA = hostId(String.format("%s/-1", macA));
542 HostId hostB = hostId(String.format("%s/-1", macB));
543
544 PortNumber portA = portNumber(101);
545 PortNumber portB = portNumber(102);
546
547 HostLocation locA = new HostLocation(deviceId, portA, 0);
548 HostLocation locB = new HostLocation(deviceId, portB, 0);
549
550 IpAddress ipA = ip("10." + devNum + ".0.1");
551 IpAddress ipB = ip("10." + devNum + ".0.2");
552
553 Host host = new DefaultHost(ProviderId.NONE,
554 hostA, macA, VlanId.NONE, locA,
555 ImmutableSet.of(ipA));
556 hosts.put(hostA, host);
557
558 host = new DefaultHost(ProviderId.NONE,
559 hostB, macB, VlanId.NONE, locB,
560 ImmutableSet.of(ipB));
561 hosts.put(hostB, host);
562 }
563
564 @Override
565 public int getHostCount() {
566 return hosts.size();
567 }
568
569 @Override
570 public Iterable<Host> getHosts() {
571 return ImmutableSet.copyOf(hosts.values());
572 }
573
574 @Override
575 public Host getHost(HostId hostId) {
576 return hosts.get(hostId);
577 }
578
579 // TODO: possibly fill out other methods, should the model require them
Simon Hunt642bc452016-05-04 19:34:45 -0700580 }
581
582}