blob: 18c56791fc395ea4f2fd51d3dedf6ae0fd19d21e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
alshabib7911a052014-10-16 17:49:37 -070018import org.onlab.packet.ChassisId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070019import org.onlab.packet.IpAddress;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070020import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.event.EventDeliveryService;
23import org.onosproject.net.provider.ProviderId;
tomca90c462014-09-22 11:40:58 -070024
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070025import java.lang.reflect.Field;
tomca90c462014-09-22 11:40:58 -070026import java.util.ArrayList;
27import java.util.HashSet;
28import java.util.List;
29
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080030import static org.junit.Assert.assertEquals;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070031import static org.onlab.packet.MacAddress.valueOf;
32import static org.onlab.packet.VlanId.vlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import static org.onosproject.net.DeviceId.deviceId;
34import static org.onosproject.net.HostId.hostId;
35import static org.onosproject.net.PortNumber.portNumber;
tomca90c462014-09-22 11:40:58 -070036
37/**
38 * Miscellaneous tools for testing core related to the network model.
39 */
40public final class NetTestTools {
41
42 private NetTestTools() {
43 }
44
45 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080046 public static final ApplicationId APP_ID = new TestApplicationId("foo");
tomca90c462014-09-22 11:40:58 -070047
48 // Short-hand for producing a device id from a string
49 public static DeviceId did(String id) {
50 return deviceId("of:" + id);
51 }
52
53
54 // Short-hand for producing a host id from a string
55 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070056 return hostId(id);
tomca90c462014-09-22 11:40:58 -070057 }
58
59 // Crates a new device with the specified id
60 public static Device device(String id) {
61 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070062 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070063 }
64
65 // Crates a new host with the specified id
66 public static Host host(String id, String did) {
67 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
68 new HostLocation(did(did), portNumber(1), 321),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070069 new HashSet<IpAddress>());
tomca90c462014-09-22 11:40:58 -070070 }
71
Ray Milkeye6684082014-10-16 16:59:47 -070072 // Short-hand for creating a connection point.
73 public static ConnectPoint connectPoint(String id, int port) {
74 return new ConnectPoint(did(id), portNumber(port));
75 }
76
tomca90c462014-09-22 11:40:58 -070077 // Short-hand for creating a link.
78 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070079 return new DefaultLink(PID,
80 connectPoint(src, sp),
81 connectPoint(dst, dp),
tomca90c462014-09-22 11:40:58 -070082 Link.Type.DIRECT);
83 }
84
85 // Creates a path that leads through the given devices.
86 public static Path createPath(String... ids) {
87 List<Link> links = new ArrayList<>();
88 for (int i = 0; i < ids.length - 1; i++) {
89 links.add(link(ids[i], i, ids[i + 1], i));
90 }
91 return new DefaultPath(PID, links, ids.length);
92 }
93
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070094 // Creates OCh signal
95 public static OchSignal createLambda() {
96 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
97 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080098
99 /**
100 * Verifies that Annotations created by merging {@code annotations} is
101 * equal to actual Annotations.
102 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700103 * @param actual annotations to check
104 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800105 */
106 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
107 DefaultAnnotations expected = DefaultAnnotations.builder().build();
108 for (SparseAnnotations a : annotations) {
109 expected = DefaultAnnotations.merge(expected, a);
110 }
111 assertEquals(expected.keys(), actual.keys());
112 for (String key : expected.keys()) {
113 assertEquals(expected.value(key), actual.value(key));
114 }
115 }
116
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700117 /**
118 * Injects the given event delivery service into the specified manager
119 * component.
120 *
121 * @param manager manager component
122 * @param svc service reference to be injected
123 */
124 public static void injectEventDispatcher(Object manager, EventDeliveryService svc) {
125 Class mc = manager.getClass();
126 for (Field f : mc.getSuperclass().getDeclaredFields()) {
127 if (f.getType().equals(EventDeliveryService.class)) {
128 try {
129 f.setAccessible(true);
130 f.set(manager, svc);
131 } catch (IllegalAccessException e) {
132 throw new IllegalArgumentException("Unable to inject reference", e);
133 }
134 }
135 }
136 }
137
tomca90c462014-09-22 11:40:58 -0700138}