blob: de7ec9a01aa2fa5ff787a13cf796b937833e0f5b [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;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070057import org.onosproject.ui.UiTopoLayoutService;
Simon Hunt642bc452016-05-04 19:34:45 -070058import org.onosproject.ui.impl.AbstractUiImplTest;
59import org.onosproject.ui.model.ServiceBundle;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070060import org.onosproject.ui.model.topo.UiTopoLayout;
61import org.onosproject.ui.model.topo.UiTopoLayoutId;
Simon Hunt642bc452016-05-04 19:34:45 -070062
Simon Huntc0f20c12016-05-09 09:30:20 -070063import java.util.ArrayList;
64import java.util.Collections;
Simon Hunt642bc452016-05-04 19:34:45 -070065import java.util.HashMap;
Simon Huntc0f20c12016-05-09 09:30:20 -070066import java.util.HashSet;
67import java.util.List;
Simon Hunt642bc452016-05-04 19:34:45 -070068import java.util.Map;
69import java.util.Set;
70
Simon Huntc0f20c12016-05-09 09:30:20 -070071import static org.onosproject.cluster.NodeId.nodeId;
Simon Hunt642bc452016-05-04 19:34:45 -070072import static org.onosproject.net.DeviceId.deviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070073import static org.onosproject.net.HostId.hostId;
74import static org.onosproject.net.PortNumber.portNumber;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070075import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId;
Simon Hunt642bc452016-05-04 19:34:45 -070076
77/**
78 * Base class for model test classes.
79 */
80abstract class AbstractTopoModelTest extends AbstractUiImplTest {
81
Simon Huntc0f20c12016-05-09 09:30:20 -070082 /*
83 Our mock environment:
84
85 Three controllers: C1, C2, C3
86
87 Nine devices: D1 .. D9
88
89 D4 ---+ +--- D7
90 | |
91 D5 --- D1 --- D2 --- D3 --- D8
92 | |
93 D6 ---+ +--- D9
94
95 Twelve hosts (two per D4 ... D9) H4a, H4b, H5a, H5b, ...
96
Simon Hunt4f4ffc32016-08-03 18:30:47 -070097 Layouts:
98 LROOT : (default)
99 +-- L1 : R1
100 +-- L2 : R2
101 +-- L3 : R3
102
Simon Huntc0f20c12016-05-09 09:30:20 -0700103 Regions:
104 R1 : D1, D2, D3
105 R2 : D4, D5, D6
106 R3 : D7, D8, D9
107
108 Mastership:
109 C1 : D1, D2, D3
110 C2 : D4, D5, D6
111 C3 : D7, D8, D9
112
113 Roles: (backups)
114 C1 -> C2, C3
115 C2 -> C1, C3
116 C3 -> C1, C2
117 */
118
119 protected static final String C1 = "C1";
120 protected static final String C2 = "C2";
121 protected static final String C3 = "C3";
122
123 protected static final NodeId CNID_1 = nodeId(C1);
124 protected static final NodeId CNID_2 = nodeId(C2);
125 protected static final NodeId CNID_3 = nodeId(C3);
126
127 protected static final ControllerNode CNODE_1 = cnode(CNID_1, "10.0.0.1");
128 protected static final ControllerNode CNODE_2 = cnode(CNID_2, "10.0.0.2");
129 protected static final ControllerNode CNODE_3 = cnode(CNID_3, "10.0.0.3");
130
131 protected static final String R1 = "R1";
132 protected static final String R2 = "R2";
133 protected static final String R3 = "R3";
134
135 protected static final Set<NodeId> SET_C1 = ImmutableSet.of(CNID_1);
136 protected static final Set<NodeId> SET_C2 = ImmutableSet.of(CNID_2);
137 protected static final Set<NodeId> SET_C3 = ImmutableSet.of(CNID_3);
138
139 protected static final Region REGION_1 =
140 region(R1, Region.Type.METRO, ImmutableList.of(SET_C1, SET_C2));
141 protected static final Region REGION_2 =
142 region(R2, Region.Type.CAMPUS, ImmutableList.of(SET_C2, SET_C1));
143 protected static final Region REGION_3 =
144 region(R3, Region.Type.CAMPUS, ImmutableList.of(SET_C3, SET_C1));
145
146 protected static final Set<Region> REGION_SET =
147 ImmutableSet.of(REGION_1, REGION_2, REGION_3);
148
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700149 protected static final String LROOT = "LROOT";
150 protected static final String L1 = "L1";
151 protected static final String L2 = "L2";
152 protected static final String L3 = "L3";
153
154 protected static final UiTopoLayout LAYOUT_ROOT = layout(LROOT, null, null);
155 protected static final UiTopoLayout LAYOUT_1 = layout(L1, REGION_1, LROOT);
156 protected static final UiTopoLayout LAYOUT_2 = layout(L2, REGION_2, LROOT);
157 protected static final UiTopoLayout LAYOUT_3 = layout(L3, REGION_3, LROOT);
158
159 protected static final Set<UiTopoLayout> LAYOUT_SET =
160 ImmutableSet.of(LAYOUT_ROOT, LAYOUT_1, LAYOUT_2, LAYOUT_3);
161 protected static final Set<UiTopoLayout> ROOT_KIDS =
162 ImmutableSet.of(LAYOUT_1, LAYOUT_2, LAYOUT_3);
163 protected static final Set<UiTopoLayout> PEERS_OF_1 =
164 ImmutableSet.of(LAYOUT_2, LAYOUT_3);
165 protected static final Set<UiTopoLayout> PEERS_OF_2 =
166 ImmutableSet.of(LAYOUT_1, LAYOUT_3);
167 protected static final Set<UiTopoLayout> PEERS_OF_3 =
168 ImmutableSet.of(LAYOUT_1, LAYOUT_2);
169
Simon Hunt58a0dd02016-05-17 11:54:23 -0700170 protected static final String D1 = "d1";
171 protected static final String D2 = "d2";
172 protected static final String D3 = "d3";
173 protected static final String D4 = "d4";
174 protected static final String D5 = "d5";
175 protected static final String D6 = "d6";
176 protected static final String D7 = "d7";
177 protected static final String D8 = "d8";
178 protected static final String D9 = "d9";
Simon Huntc0f20c12016-05-09 09:30:20 -0700179
180 protected static final String MFR = "Mfr";
181 protected static final String HW = "h/w";
182 protected static final String SW = "s/w";
183 protected static final String SERIAL = "ser123";
184
185 protected static final DeviceId DEVID_1 = deviceId(D1);
186 protected static final DeviceId DEVID_2 = deviceId(D2);
187 protected static final DeviceId DEVID_3 = deviceId(D3);
188 protected static final DeviceId DEVID_4 = deviceId(D4);
189 protected static final DeviceId DEVID_5 = deviceId(D5);
190 protected static final DeviceId DEVID_6 = deviceId(D6);
191 protected static final DeviceId DEVID_7 = deviceId(D7);
192 protected static final DeviceId DEVID_8 = deviceId(D8);
193 protected static final DeviceId DEVID_9 = deviceId(D9);
194
195 protected static final Device DEV_1 = device(D1);
196 protected static final Device DEV_2 = device(D2);
197 protected static final Device DEV_3 = device(D3);
198 protected static final Device DEV_4 = device(D4);
199 protected static final Device DEV_5 = device(D5);
200 protected static final Device DEV_6 = device(D6);
201 protected static final Device DEV_7 = device(D7);
202 protected static final Device DEV_8 = device(D8);
203 protected static final Device DEV_9 = device(D9);
204
205 protected static final List<Device> ALL_DEVS =
206 ImmutableList.of(
207 DEV_1, DEV_2, DEV_3,
208 DEV_4, DEV_5, DEV_6,
209 DEV_7, DEV_8, DEV_9
210 );
211
212 private static final Set<DeviceId> DEVS_TRUNK =
213 ImmutableSet.of(DEVID_1, DEVID_2, DEVID_3);
214
215 private static final Set<DeviceId> DEVS_LEFT =
216 ImmutableSet.of(DEVID_4, DEVID_5, DEVID_6);
217
218 private static final Set<DeviceId> DEVS_RIGHT =
219 ImmutableSet.of(DEVID_7, DEVID_8, DEVID_9);
220
221 private static final String[][] LINK_CONNECT_DATA = {
222 {D1, "12", D2, "21"},
223 {D2, "23", D3, "32"},
224 {D4, "41", D1, "14"},
225 {D5, "51", D1, "15"},
226 {D6, "61", D1, "16"},
227 {D7, "73", D3, "37"},
228 {D8, "83", D3, "38"},
229 {D9, "93", D3, "39"},
230 };
231
232 private static final String HOST_MAC_PREFIX = "aa:00:00:00:00:";
233
234 /**
235 * Returns IP address instance for given string.
236 *
237 * @param s string
238 * @return IP address
239 */
240 protected static IpAddress ip(String s) {
241 return IpAddress.valueOf(s);
242 }
243
244 /**
245 * Returns controller node instance for given ID and IP.
246 *
247 * @param id identifier
248 * @param ip IP address
249 * @return controller node instance
250 */
251 protected static ControllerNode cnode(NodeId id, String ip) {
252 return new DefaultControllerNode(id, ip(ip));
253 }
254
255 /**
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700256 * Returns UI topology layout instance with the specified parameters.
257 *
258 * @param layoutId the layout ID
259 * @param region the backing region
260 * @param parentId the parent layout ID
261 * @return layout instance
262 */
263 protected static UiTopoLayout layout(String layoutId, Region region,
264 String parentId) {
265 UiTopoLayoutId pid = parentId == null
266 ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId);
267 return new UiTopoLayout(layoutId(layoutId), region, pid);
268 }
269
270 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700271 * Returns a region instance with specified parameters.
272 *
273 * @param id region id
274 * @param type region type
275 * @param masters ordered list of master sets
276 * @return region instance
277 */
278 protected static Region region(String id, Region.Type type,
279 List<Set<NodeId>> masters) {
280 return new DefaultRegion(RegionId.regionId(id), "Region-" + id,
281 type, masters);
282 }
283
284 /**
285 * Returns device with given ID.
286 *
287 * @param id device ID
288 * @return device instance
289 */
290 protected static Device device(String id) {
291 return new DefaultDevice(ProviderId.NONE, deviceId(id),
292 Device.Type.SWITCH, MFR, HW, SW, SERIAL, null);
293 }
294
Simon Hunt642bc452016-05-04 19:34:45 -0700295 /**
296 * Returns canned results.
Simon Huntc0f20c12016-05-09 09:30:20 -0700297 * <p>
Simon Hunt642bc452016-05-04 19:34:45 -0700298 * At some future point, we may make this "programmable", so that
Simon Huntc0f20c12016-05-09 09:30:20 -0700299 * its state can be changed over the course of a unit test.
Simon Hunt642bc452016-05-04 19:34:45 -0700300 */
301 protected static final ServiceBundle MOCK_SERVICES =
302 new ServiceBundle() {
303 @Override
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700304 public UiTopoLayoutService layout() {
305 return MOCK_LAYOUT;
306 }
307
308 @Override
Simon Hunt642bc452016-05-04 19:34:45 -0700309 public ClusterService cluster() {
310 return MOCK_CLUSTER;
311 }
312
313 @Override
314 public MastershipService mastership() {
315 return MOCK_MASTER;
316 }
317
318 @Override
319 public RegionService region() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700320 return MOCK_REGION;
Simon Hunt642bc452016-05-04 19:34:45 -0700321 }
322
323 @Override
324 public DeviceService device() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700325 return MOCK_DEVICE;
Simon Hunt642bc452016-05-04 19:34:45 -0700326 }
327
328 @Override
329 public LinkService link() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700330 return MOCK_LINK;
Simon Hunt642bc452016-05-04 19:34:45 -0700331 }
332
333 @Override
334 public HostService host() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700335 return MOCK_HOST;
Simon Hunt642bc452016-05-04 19:34:45 -0700336 }
337
338 @Override
339 public IntentService intent() {
340 return null;
341 }
342
343 @Override
344 public FlowRuleService flow() {
345 return null;
346 }
347 };
348
349 private static final ClusterService MOCK_CLUSTER = new MockClusterService();
350 private static final MastershipService MOCK_MASTER = new MockMasterService();
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700351 private static final UiTopoLayoutService MOCK_LAYOUT = new MockLayoutService();
Simon Huntc0f20c12016-05-09 09:30:20 -0700352 private static final RegionService MOCK_REGION = new MockRegionService();
353 private static final DeviceService MOCK_DEVICE = new MockDeviceService();
354 private static final LinkService MOCK_LINK = new MockLinkService();
355 private static final HostService MOCK_HOST = new MockHostService();
Simon Hunt642bc452016-05-04 19:34:45 -0700356
Simon Hunt642bc452016-05-04 19:34:45 -0700357
Simon Hunt642bc452016-05-04 19:34:45 -0700358 private static class MockClusterService extends ClusterServiceAdapter {
Simon Huntc0f20c12016-05-09 09:30:20 -0700359 private final Map<NodeId, ControllerNode> nodes = new HashMap<>();
Simon Hunt642bc452016-05-04 19:34:45 -0700360 private final Map<NodeId, ControllerNode.State> states = new HashMap<>();
361
Simon Huntc0f20c12016-05-09 09:30:20 -0700362 MockClusterService() {
363 nodes.put(CNODE_1.id(), CNODE_1);
364 nodes.put(CNODE_2.id(), CNODE_2);
365 nodes.put(CNODE_3.id(), CNODE_3);
366
367 states.put(CNODE_1.id(), ControllerNode.State.READY);
368 states.put(CNODE_2.id(), ControllerNode.State.ACTIVE);
369 states.put(CNODE_3.id(), ControllerNode.State.ACTIVE);
370 }
371
372 @Override
373 public Set<ControllerNode> getNodes() {
374 return ImmutableSet.copyOf(nodes.values());
375 }
376
377 @Override
378 public ControllerNode getNode(NodeId nodeId) {
379 return nodes.get(nodeId);
380 }
Simon Hunt642bc452016-05-04 19:34:45 -0700381
382 @Override
383 public ControllerNode.State getState(NodeId nodeId) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700384 return states.get(nodeId);
Simon Hunt642bc452016-05-04 19:34:45 -0700385 }
386 }
387
Simon Hunt642bc452016-05-04 19:34:45 -0700388
389 private static class MockMasterService extends MastershipServiceAdapter {
Simon Huntc0f20c12016-05-09 09:30:20 -0700390 private final Map<NodeId, Set<DeviceId>> masterOf = new HashMap<>();
391
392 MockMasterService() {
393 masterOf.put(CNODE_1.id(), DEVS_TRUNK);
394 masterOf.put(CNODE_2.id(), DEVS_LEFT);
395 masterOf.put(CNODE_3.id(), DEVS_RIGHT);
396 }
397
398 @Override
399 public NodeId getMasterFor(DeviceId deviceId) {
400 if (DEVS_TRUNK.contains(deviceId)) {
401 return CNID_1;
402 }
403 if (DEVS_LEFT.contains(deviceId)) {
404 return CNID_2;
405 }
406 if (DEVS_RIGHT.contains(deviceId)) {
407 return CNID_3;
408 }
409 return null;
410 }
411
Simon Hunt642bc452016-05-04 19:34:45 -0700412 @Override
413 public Set<DeviceId> getDevicesOf(NodeId nodeId) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700414 return masterOf.get(nodeId);
Simon Hunt642bc452016-05-04 19:34:45 -0700415 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700416
417 @Override
418 public RoleInfo getNodesFor(DeviceId deviceId) {
419 NodeId master = null;
420 List<NodeId> backups = new ArrayList<>();
421
422 if (DEVS_TRUNK.contains(deviceId)) {
423 master = CNID_1;
424 backups.add(CNID_2);
425 backups.add(CNID_3);
426 } else if (DEVS_LEFT.contains(deviceId)) {
427 master = CNID_2;
428 backups.add(CNID_1);
429 backups.add(CNID_3);
430 } else if (DEVS_RIGHT.contains(deviceId)) {
431 master = CNID_3;
432 backups.add(CNID_1);
433 backups.add(CNID_2);
434 }
435 return new RoleInfo(master, backups);
436 }
437 }
438
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700439 // TODO: consider implementing UiTopoLayoutServiceAdapter and extending that here
440 private static class MockLayoutService implements UiTopoLayoutService {
441 private final Map<UiTopoLayoutId, UiTopoLayout> map = new HashMap<>();
442 private final Map<UiTopoLayoutId, Set<UiTopoLayout>> peers = new HashMap<>();
443 private final Map<RegionId, UiTopoLayout> byRegion = new HashMap<>();
444
445 MockLayoutService() {
446 map.put(LAYOUT_ROOT.id(), LAYOUT_ROOT);
447 map.put(LAYOUT_1.id(), LAYOUT_1);
448 map.put(LAYOUT_2.id(), LAYOUT_2);
449 map.put(LAYOUT_3.id(), LAYOUT_3);
450
451 peers.put(LAYOUT_ROOT.id(), ImmutableSet.of());
452 peers.put(LAYOUT_1.id(), ImmutableSet.of(LAYOUT_2, LAYOUT_3));
453 peers.put(LAYOUT_2.id(), ImmutableSet.of(LAYOUT_1, LAYOUT_3));
454 peers.put(LAYOUT_3.id(), ImmutableSet.of(LAYOUT_1, LAYOUT_2));
455
456 byRegion.put(REGION_1.id(), LAYOUT_1);
457 byRegion.put(REGION_2.id(), LAYOUT_2);
458 byRegion.put(REGION_3.id(), LAYOUT_3);
459 }
460
461 @Override
462 public UiTopoLayout getRootLayout() {
463 return LAYOUT_ROOT;
464 }
465
466 @Override
467 public Set<UiTopoLayout> getLayouts() {
468 return LAYOUT_SET;
469 }
470
471 @Override
472 public boolean addLayout(UiTopoLayout layout) {
473 return false;
474 }
475
476 @Override
477 public UiTopoLayout getLayout(UiTopoLayoutId layoutId) {
478 return map.get(layoutId);
479 }
480
481 @Override
482 public UiTopoLayout getLayout(RegionId regionId) {
483 return byRegion.get(regionId);
484 }
485
486 @Override
487 public Set<UiTopoLayout> getPeerLayouts(UiTopoLayoutId layoutId) {
488 return peers.get(layoutId);
489 }
490
491 @Override
492 public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) {
493 return LAYOUT_ROOT.id().equals(layoutId)
494 ? ROOT_KIDS
495 : Collections.emptySet();
496 }
497
498 @Override
499 public boolean removeLayout(UiTopoLayout layout) {
500 return false;
501 }
502 }
503
Simon Huntc0f20c12016-05-09 09:30:20 -0700504 // TODO: consider implementing RegionServiceAdapter and extending that here
505 private static class MockRegionService implements RegionService {
506
507 private final Map<RegionId, Region> lookup = new HashMap<>();
508
509 MockRegionService() {
510 lookup.put(REGION_1.id(), REGION_1);
511 lookup.put(REGION_2.id(), REGION_2);
512 lookup.put(REGION_3.id(), REGION_3);
513 }
514
515 @Override
516 public Set<Region> getRegions() {
517 return REGION_SET;
518 }
519
520 @Override
521 public Region getRegion(RegionId regionId) {
522 return lookup.get(regionId);
523 }
524
525 @Override
526 public Region getRegionForDevice(DeviceId deviceId) {
527 if (DEVS_TRUNK.contains(deviceId)) {
528 return REGION_1;
529 }
530 if (DEVS_LEFT.contains(deviceId)) {
531 return REGION_2;
532 }
533 if (DEVS_RIGHT.contains(deviceId)) {
534 return REGION_3;
535 }
536 return null;
537 }
538
539 @Override
540 public Set<DeviceId> getRegionDevices(RegionId regionId) {
541 if (REGION_1.id().equals(regionId)) {
542 return DEVS_TRUNK;
543 }
544 if (REGION_2.id().equals(regionId)) {
545 return DEVS_LEFT;
546 }
547 if (REGION_3.id().equals(regionId)) {
548 return DEVS_RIGHT;
549 }
550 return Collections.emptySet();
551 }
552
553 @Override
554 public void addListener(RegionListener listener) {
555 }
556
557 @Override
558 public void removeListener(RegionListener listener) {
559 }
560 }
561
562
563 private static class MockDeviceService extends DeviceServiceAdapter {
564 private final Map<DeviceId, Device> devices = new HashMap<>();
565
566 MockDeviceService() {
567 for (Device dev : ALL_DEVS) {
568 devices.put(dev.id(), dev);
569 }
570 }
571
572 @Override
573 public int getDeviceCount() {
574 return devices.size();
575 }
576
577 @Override
578 public Iterable<Device> getDevices() {
579 return ImmutableList.copyOf(devices.values());
580 }
581
582 @Override
583 public Device getDevice(DeviceId deviceId) {
584 return devices.get(deviceId);
585 }
586
587 }
588
Simon Hunt58a0dd02016-05-17 11:54:23 -0700589 /**
590 * Synthesizes a pair of unidirectional links between two devices. The
591 * string array should be of the form:
592 * <pre>
593 * { "device-A-id", "device-A-port", "device-B-id", "device-B-port" }
594 * </pre>
595 *
596 * @param linkPairData device ids and ports
597 * @return pair of synthesized links
598 */
599 protected static List<Link> makeLinkPair(String[] linkPairData) {
600 DeviceId devA = deviceId(linkPairData[0]);
601 PortNumber portA = portNumber(Long.valueOf(linkPairData[1]));
602 DeviceId devB = deviceId(linkPairData[2]);
603 PortNumber portB = portNumber(Long.valueOf(linkPairData[3]));
604
605 Link linkA = DefaultLink.builder()
606 .providerId(ProviderId.NONE)
607 .type(Link.Type.DIRECT)
608 .src(new ConnectPoint(devA, portA))
609 .dst(new ConnectPoint(devB, portB))
610 .build();
611
612 Link linkB = DefaultLink.builder()
613 .providerId(ProviderId.NONE)
614 .type(Link.Type.DIRECT)
615 .src(new ConnectPoint(devB, portB))
616 .dst(new ConnectPoint(devA, portA))
617 .build();
618
619 return ImmutableList.of(linkA, linkB);
620 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700621
622 private static class MockLinkService extends LinkServiceAdapter {
623 private final Set<Link> links = new HashSet<>();
624
625 MockLinkService() {
626 for (String[] linkPair : LINK_CONNECT_DATA) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700627 links.addAll(makeLinkPair(linkPair));
Simon Huntc0f20c12016-05-09 09:30:20 -0700628 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700629 }
630
631 @Override
632 public int getLinkCount() {
633 return links.size();
634 }
635
636 @Override
637 public Iterable<Link> getLinks() {
638 return ImmutableSet.copyOf(links);
639 }
640
641 // TODO: possibly fill out other methods if we find the model uses them
642 }
643
644
Simon Hunteae81ee2016-05-19 12:33:22 -0700645 /**
646 * Creates a default host connected at the given edge device and port. Note
647 * that an identifying hex character ("a" - "f") should be supplied. This
648 * will be included in the MAC address of the host (and equivalent value
649 * as last byte in IP address).
650 *
651 * @param device edge device
652 * @param port port number
653 * @param hexChar identifying hex character
654 * @return host connected at that location
655 */
656 protected static Host createHost(Device device, int port, String hexChar) {
657 DeviceId deviceId = device.id();
658 String devNum = deviceId.toString().substring(1);
659
660 MacAddress mac = MacAddress.valueOf(HOST_MAC_PREFIX + devNum + hexChar);
661 HostId hostId = hostId(String.format("%s/-1", mac));
662
663 int ipByte = Integer.valueOf(hexChar, 16);
664 if (ipByte < 10 || ipByte > 15) {
665 throw new IllegalArgumentException("hexChar must be a-f");
666 }
667 HostLocation loc = new HostLocation(deviceId, portNumber(port), 0);
668
669 IpAddress ip = ip("10." + devNum + ".0." + ipByte);
670
671 return new DefaultHost(ProviderId.NONE, hostId, mac, VlanId.NONE,
672 loc, ImmutableSet.of(ip));
673 }
674
675 /**
676 * Creates a pair of hosts connected to the specified device.
677 *
678 * @param d edge device
679 * @return pair of hosts
680 */
681 protected static List<Host> createHostPair(Device d) {
682 List<Host> hosts = new ArrayList<>();
683 hosts.add(createHost(d, 101, "a"));
684 hosts.add(createHost(d, 102, "b"));
685 return hosts;
686 }
687
Simon Huntc0f20c12016-05-09 09:30:20 -0700688 private static class MockHostService extends HostServiceAdapter {
689 private final Map<HostId, Host> hosts = new HashMap<>();
690
691 MockHostService() {
692 for (Device d : ALL_DEVS) {
Simon Hunteae81ee2016-05-19 12:33:22 -0700693 for (Host h : createHostPair(d)) {
694 hosts.put(h.id(), h);
695 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700696 }
697 }
698
Simon Huntc0f20c12016-05-09 09:30:20 -0700699 @Override
700 public int getHostCount() {
701 return hosts.size();
702 }
703
704 @Override
705 public Iterable<Host> getHosts() {
706 return ImmutableSet.copyOf(hosts.values());
707 }
708
709 @Override
710 public Host getHost(HostId hostId) {
711 return hosts.get(hostId);
712 }
713
714 // TODO: possibly fill out other methods, should the model require them
Simon Hunt642bc452016-05-04 19:34:45 -0700715 }
716
717}