blob: 010c2cd2a6b5c5b8431c379561347d1ce60507be [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
tomca90c462014-09-22 11:40:58 -070017
Ray Milkey5b94aea2015-08-05 11:00:55 -070018import org.onlab.junit.TestUtils;
alshabib7911a052014-10-16 17:49:37 -070019import org.onlab.packet.ChassisId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070020import org.onosproject.TestApplicationId;
Madan Jampani6f8b7022015-12-07 16:59:59 -080021import org.onosproject.cluster.NodeId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070022import org.onosproject.core.ApplicationId;
23import org.onosproject.event.EventDeliveryService;
24import org.onosproject.net.provider.ProviderId;
tomca90c462014-09-22 11:40:58 -070025
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070026import java.lang.reflect.Field;
tomca90c462014-09-22 11:40:58 -070027import java.util.ArrayList;
28import java.util.HashSet;
29import java.util.List;
30
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080031import static org.junit.Assert.assertEquals;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070032import static org.onlab.packet.MacAddress.valueOf;
33import static org.onlab.packet.VlanId.vlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DeviceId.deviceId;
35import static org.onosproject.net.HostId.hostId;
36import static org.onosproject.net.PortNumber.portNumber;
tomca90c462014-09-22 11:40:58 -070037
38/**
39 * Miscellaneous tools for testing core related to the network model.
40 */
41public final class NetTestTools {
42
43 private NetTestTools() {
44 }
45
46 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080047 public static final ApplicationId APP_ID = new TestApplicationId("foo");
Madan Jampani6f8b7022015-12-07 16:59:59 -080048 public static final NodeId NODE_ID = new NodeId("node1");
tomca90c462014-09-22 11:40:58 -070049
50 // Short-hand for producing a device id from a string
51 public static DeviceId did(String id) {
52 return deviceId("of:" + id);
53 }
54
55
56 // Short-hand for producing a host id from a string
57 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070058 return hostId(id);
tomca90c462014-09-22 11:40:58 -070059 }
60
61 // Crates a new device with the specified id
62 public static Device device(String id) {
63 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070064 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070065 }
66
67 // Crates a new host with the specified id
68 public static Host host(String id, String did) {
69 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
70 new HostLocation(did(did), portNumber(1), 321),
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070071 new HashSet<>());
tomca90c462014-09-22 11:40:58 -070072 }
73
Ray Milkeye6684082014-10-16 16:59:47 -070074 // Short-hand for creating a connection point.
75 public static ConnectPoint connectPoint(String id, int port) {
76 return new ConnectPoint(did(id), portNumber(port));
77 }
78
tomca90c462014-09-22 11:40:58 -070079 // Short-hand for creating a link.
80 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070081 return new DefaultLink(PID,
82 connectPoint(src, sp),
83 connectPoint(dst, dp),
tomca90c462014-09-22 11:40:58 -070084 Link.Type.DIRECT);
85 }
86
87 // Creates a path that leads through the given devices.
88 public static Path createPath(String... ids) {
89 List<Link> links = new ArrayList<>();
90 for (int i = 0; i < ids.length - 1; i++) {
91 links.add(link(ids[i], i, ids[i + 1], i));
92 }
93 return new DefaultPath(PID, links, ids.length);
94 }
95
Thomas Vachuskadc91b552016-03-29 14:02:47 -070096 // Creates a path that leads through the given devices.
97 public static Path createPath(boolean srcIsEdge, boolean dstIsEdge, String... ids) {
98 List<Link> links = new ArrayList<>();
99 for (int i = 0; i < ids.length - 1; i++) {
100 if (i == 0 && srcIsEdge) {
101 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1]), true));
102 } else if (i == ids.length - 2 && dstIsEdge) {
103 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i]), false));
104 } else {
105 links.add(link(ids[i], i, ids[i + 1], i));
106 }
107 }
108 return new DefaultPath(PID, links, ids.length);
109 }
110
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111 // Creates OCh signal
112 public static OchSignal createLambda() {
113 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
114 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800115
116 /**
117 * Verifies that Annotations created by merging {@code annotations} is
118 * equal to actual Annotations.
119 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700120 * @param actual annotations to check
121 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800122 */
123 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
124 DefaultAnnotations expected = DefaultAnnotations.builder().build();
125 for (SparseAnnotations a : annotations) {
126 expected = DefaultAnnotations.merge(expected, a);
127 }
128 assertEquals(expected.keys(), actual.keys());
129 for (String key : expected.keys()) {
130 assertEquals(expected.value(key), actual.value(key));
131 }
132 }
133
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700134 /**
135 * Injects the given event delivery service into the specified manager
136 * component.
137 *
138 * @param manager manager component
139 * @param svc service reference to be injected
140 */
141 public static void injectEventDispatcher(Object manager, EventDeliveryService svc) {
142 Class mc = manager.getClass();
143 for (Field f : mc.getSuperclass().getDeclaredFields()) {
144 if (f.getType().equals(EventDeliveryService.class)) {
145 try {
Ray Milkey5b94aea2015-08-05 11:00:55 -0700146 TestUtils.setField(manager, f.getName(), svc);
147 } catch (TestUtils.TestUtilsException e) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700148 throw new IllegalArgumentException("Unable to inject reference", e);
149 }
Ray Milkey5b94aea2015-08-05 11:00:55 -0700150 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700151 }
152 }
153 }
154
tomca90c462014-09-22 11:40:58 -0700155}