blob: 842c86ddd51c42239c167a31713cd0803306f363 [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
Pier Ventre766995d2016-10-05 22:15:56 -070018import com.google.common.collect.ImmutableList;
Ray Milkey5b94aea2015-08-05 11:00:55 -070019import org.onlab.junit.TestUtils;
alshabib7911a052014-10-16 17:49:37 -070020import org.onlab.packet.ChassisId;
Pier Ventre766995d2016-10-05 22:15:56 -070021import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.MplsLabel;
24import org.onlab.packet.VlanId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070025import org.onosproject.TestApplicationId;
Madan Jampani6f8b7022015-12-07 16:59:59 -080026import org.onosproject.cluster.NodeId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.event.EventDeliveryService;
Pier Ventre766995d2016-10-05 22:15:56 -070029import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Constraint;
34import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070035import org.onosproject.net.provider.ProviderId;
tomca90c462014-09-22 11:40:58 -070036
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070037import java.lang.reflect.Field;
tomca90c462014-09-22 11:40:58 -070038import java.util.ArrayList;
39import java.util.HashSet;
40import java.util.List;
41
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080042import static org.junit.Assert.assertEquals;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070043import static org.onlab.packet.MacAddress.valueOf;
44import static org.onlab.packet.VlanId.vlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.DeviceId.deviceId;
46import static org.onosproject.net.HostId.hostId;
47import static org.onosproject.net.PortNumber.portNumber;
tomca90c462014-09-22 11:40:58 -070048
49/**
50 * Miscellaneous tools for testing core related to the network model.
51 */
52public final class NetTestTools {
53
54 private NetTestTools() {
55 }
56
57 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080058 public static final ApplicationId APP_ID = new TestApplicationId("foo");
Madan Jampani6f8b7022015-12-07 16:59:59 -080059 public static final NodeId NODE_ID = new NodeId("node1");
tomca90c462014-09-22 11:40:58 -070060
61 // Short-hand for producing a device id from a string
62 public static DeviceId did(String id) {
63 return deviceId("of:" + id);
64 }
65
66
67 // Short-hand for producing a host id from a string
68 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070069 return hostId(id);
tomca90c462014-09-22 11:40:58 -070070 }
71
72 // Crates a new device with the specified id
73 public static Device device(String id) {
74 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070075 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070076 }
77
78 // Crates a new host with the specified id
79 public static Host host(String id, String did) {
80 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
81 new HostLocation(did(did), portNumber(1), 321),
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070082 new HashSet<>());
tomca90c462014-09-22 11:40:58 -070083 }
84
Ray Milkeye6684082014-10-16 16:59:47 -070085 // Short-hand for creating a connection point.
86 public static ConnectPoint connectPoint(String id, int port) {
87 return new ConnectPoint(did(id), portNumber(port));
88 }
89
tomca90c462014-09-22 11:40:58 -070090 // Short-hand for creating a link.
91 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070092 return new DefaultLink(PID,
93 connectPoint(src, sp),
94 connectPoint(dst, dp),
Brian Stanke612cebf2016-05-02 10:21:33 -040095 Link.Type.DIRECT, Link.State.ACTIVE);
tomca90c462014-09-22 11:40:58 -070096 }
97
Pier Ventre766995d2016-10-05 22:15:56 -070098 /**
99 * Short-hand for creating a link.
100 *
101 * @param src the src of the link
102 * @param dst the dst of the link
103 * @return a link
104 */
105 public static Link link(ConnectPoint src, ConnectPoint dst) {
106 return new DefaultLink(PID, src, dst, Link.Type.DIRECT, Link.State.ACTIVE);
107 }
108
tomca90c462014-09-22 11:40:58 -0700109 // Creates a path that leads through the given devices.
110 public static Path createPath(String... ids) {
111 List<Link> links = new ArrayList<>();
112 for (int i = 0; i < ids.length - 1; i++) {
113 links.add(link(ids[i], i, ids[i + 1], i));
114 }
115 return new DefaultPath(PID, links, ids.length);
116 }
117
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700118 // Creates a path that leads through the given devices.
119 public static Path createPath(boolean srcIsEdge, boolean dstIsEdge, String... ids) {
120 List<Link> links = new ArrayList<>();
121 for (int i = 0; i < ids.length - 1; i++) {
122 if (i == 0 && srcIsEdge) {
123 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1]), true));
124 } else if (i == ids.length - 2 && dstIsEdge) {
125 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i]), false));
126 } else {
127 links.add(link(ids[i], i, ids[i + 1], i));
128 }
129 }
130 return new DefaultPath(PID, links, ids.length);
131 }
132
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700133 // Creates OCh signal
134 public static OchSignal createLambda() {
135 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
136 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800137
138 /**
139 * Verifies that Annotations created by merging {@code annotations} is
140 * equal to actual Annotations.
141 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700142 * @param actual annotations to check
143 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800144 */
145 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
146 DefaultAnnotations expected = DefaultAnnotations.builder().build();
147 for (SparseAnnotations a : annotations) {
148 expected = DefaultAnnotations.merge(expected, a);
149 }
150 assertEquals(expected.keys(), actual.keys());
151 for (String key : expected.keys()) {
152 assertEquals(expected.value(key), actual.value(key));
153 }
154 }
155
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700156 /**
157 * Injects the given event delivery service into the specified manager
158 * component.
159 *
160 * @param manager manager component
161 * @param svc service reference to be injected
162 */
163 public static void injectEventDispatcher(Object manager, EventDeliveryService svc) {
164 Class mc = manager.getClass();
165 for (Field f : mc.getSuperclass().getDeclaredFields()) {
166 if (f.getType().equals(EventDeliveryService.class)) {
167 try {
Ray Milkey5b94aea2015-08-05 11:00:55 -0700168 TestUtils.setField(manager, f.getName(), svc);
169 } catch (TestUtils.TestUtilsException e) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700170 throw new IllegalArgumentException("Unable to inject reference", e);
171 }
Ray Milkey5b94aea2015-08-05 11:00:55 -0700172 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700173 }
174 }
175 }
176
Pier Ventre766995d2016-10-05 22:15:56 -0700177 /**
178 * Builds an empty selector.
179 *
180 * @return the selector
181 */
182 public static TrafficSelector emptySelector() {
183 return DefaultTrafficSelector.emptySelector();
184 }
185
186 /**
187 * Builds a vlan selector.
188 *
189 * @return the selector
190 */
191 public static TrafficSelector vlanSelector(String vlanId) {
192 return DefaultTrafficSelector.builder()
193 .matchVlanId(VlanId.vlanId(vlanId))
194 .build();
195 }
196
197 /**
198 * Builds a mpls selector.
199 *
200 * @return the selector
201 */
202 public static TrafficSelector mplsSelector(String mplsLabel) {
203 return DefaultTrafficSelector.builder()
204 .matchMplsLabel(MplsLabel.mplsLabel(mplsLabel))
205 .build();
206 }
207
208 /**
209 * Builds an ip prefix dst selector.
210 *
211 * @return the selector
212 */
213 public static TrafficSelector ipPrefixDstSelector(String prefix) {
214 return DefaultTrafficSelector.builder()
215 .matchIPDst(IpPrefix.valueOf(prefix))
216 .build();
217 }
218
219 /**
220 * Builds an empty treatment.
221 *
222 * @return the treatment
223 */
224 public static TrafficTreatment emptyTreatment() {
225 return DefaultTrafficTreatment.emptyTreatment();
226 }
227
228 /**
229 * Builds a mac dst treatment.
230 *
231 * @return the treatment
232 */
233 public static TrafficTreatment macDstTreatment(String mac) {
234 return DefaultTrafficTreatment.builder()
235 .setEthDst(MacAddress.valueOf(mac))
236 .build();
237 }
238
239 /**
240 * Builds a list containing a vlan encapsulation constraint.
241 *
242 * @return the list of constraints
243 */
244 public static List<Constraint> vlanConstraint() {
245 return ImmutableList.of(
246 new EncapsulationConstraint(EncapsulationType.VLAN)
247 );
248 }
249
250 /**
251 * Builds a list containing a mpls encapsulation constraint.
252 *
253 * @return the list of constraints
254 */
255 public static List<Constraint> mplsConstraint() {
256 return ImmutableList.of(
257 new EncapsulationConstraint(EncapsulationType.MPLS)
258 );
259 }
260
tomca90c462014-09-22 11:40:58 -0700261}