blob: 2a10f5639c8ff2b802c55892ed05545715064d3a [file] [log] [blame]
Charles Chan82ab1932016-01-30 23:22:37 -08001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan82ab1932016-01-30 23:22:37 -08003 *
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.segmentrouting.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.collect.ImmutableSet;
22import org.junit.Before;
23import org.junit.Test;
Andrea Campanella60ce2222018-04-30 11:48:55 +020024import org.onlab.packet.IpPrefix;
Charles Chan82ab1932016-01-30 23:22:37 -080025import org.onlab.packet.MacAddress;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.TestApplicationId;
Charles Chan57bd98c2016-02-22 19:27:29 -080028import org.onosproject.net.ConnectPoint;
Charles Chan43547ca2016-02-10 20:46:58 -080029import org.onosproject.net.DeviceId;
Charles Chan82ab1932016-01-30 23:22:37 -080030import org.onosproject.net.config.Config;
31import org.onosproject.net.config.ConfigApplyDelegate;
Charles Chan1963f4f2016-02-18 14:22:42 -080032import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan57bd98c2016-02-22 19:27:29 -080033
Charles Chan636e3472016-02-22 23:02:41 -080034import java.io.InputStream;
Charles Chan82ab1932016-01-30 23:22:37 -080035import java.util.Set;
36
37import static org.hamcrest.Matchers.is;
Charles Chan57bd98c2016-02-22 19:27:29 -080038import static org.junit.Assert.*;
Charles Chan82ab1932016-01-30 23:22:37 -080039
40/**
41 * Tests for class {@link SegmentRoutingAppConfig}.
42 */
43public class SegmentRoutingAppConfigTest {
Charles Chan82ab1932016-01-30 23:22:37 -080044 private SegmentRoutingAppConfig config;
Charles Chan43547ca2016-02-10 20:46:58 -080045 private SegmentRoutingAppConfig invalidConfig;
Pier Ventre7a78de22016-10-31 15:00:01 -070046 private SegmentRoutingAppConfig mplsEcmpConfig;
Charles Chan636e3472016-02-22 23:02:41 -080047
Charles Chan43547ca2016-02-10 20:46:58 -080048 private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01");
49 private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02");
50 private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03");
Charles Chan57bd98c2016-02-22 19:27:29 -080051 private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1");
52 private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2");
53 private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3");
Charles Chan43547ca2016-02-10 20:46:58 -080054 private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1");
55 private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2");
Charles Chan3bf64b92016-05-20 10:55:40 -070056 private static final String PROVIDER_1 = "org.onosproject.provider.host";
57 private static final String PROVIDER_2 = "org.onosproject.netcfghost";
58 private static final String PROVIDER_3 = "org.onosproject.anotherprovider";
Andrea Campanella60ce2222018-04-30 11:48:55 +020059 private static final IpPrefix BLACKHOLE_IP = IpPrefix.valueOf("10.0.0.0/8");
60 private static final IpPrefix BLACKHOLE_IP_2 = IpPrefix.valueOf("20.0.0.0/8");
Charles Chan82ab1932016-01-30 23:22:37 -080061
62 /**
63 * Initialize test related variables.
64 *
65 * @throws Exception
66 */
67 @Before
68 public void setUp() throws Exception {
Charles Chan636e3472016-02-22 23:02:41 -080069 InputStream jsonStream = SegmentRoutingAppConfigTest.class
Charles Chan82f19972016-05-17 13:13:55 -070070 .getResourceAsStream("/app.json");
Charles Chan636e3472016-02-22 23:02:41 -080071 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
Charles Chan82f19972016-05-17 13:13:55 -070072 .getResourceAsStream("/app-invalid.json");
Pier Ventre7a78de22016-10-31 15:00:01 -070073 InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class
74 .getResourceAsStream("/app-ecmp.json");
Charles Chan636e3472016-02-22 23:02:41 -080075
Charles Chan46fdfaf2016-11-09 20:51:44 -080076 String key = SegmentRoutingManager.APP_NAME;
Charles Chan82f19972016-05-17 13:13:55 -070077 ApplicationId subject = new TestApplicationId(key);
Charles Chan82ab1932016-01-30 23:22:37 -080078 ObjectMapper mapper = new ObjectMapper();
Charles Chan636e3472016-02-22 23:02:41 -080079 JsonNode jsonNode = mapper.readTree(jsonStream);
80 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Pier Ventre7a78de22016-10-31 15:00:01 -070081 JsonNode mplsEcmpJsonNode = mapper.readTree(mplsEcmpJsonStream);
Charles Chan82ab1932016-01-30 23:22:37 -080082 ConfigApplyDelegate delegate = new MockDelegate();
83
84 config = new SegmentRoutingAppConfig();
85 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chan43547ca2016-02-10 20:46:58 -080086 invalidConfig = new SegmentRoutingAppConfig();
87 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Pier Ventre7a78de22016-10-31 15:00:01 -070088 mplsEcmpConfig = new SegmentRoutingAppConfig();
89 mplsEcmpConfig.init(subject, key, mplsEcmpJsonNode, mapper, delegate);
Charles Chan82ab1932016-01-30 23:22:37 -080090 }
91
92 /**
Charles Chan43547ca2016-02-10 20:46:58 -080093 * Tests config validity.
94 *
95 * @throws Exception
96 */
97 @Test
98 public void testIsValid() throws Exception {
99 assertTrue(config.isValid());
100 assertFalse(invalidConfig.isValid());
Pier Ventre7a78de22016-10-31 15:00:01 -0700101 assertTrue(mplsEcmpConfig.isValid());
102 }
103
104 /**
105 * Test MPLS-ECMP default getter. By-default
106 * MPLS-ECMPS is false.
107 */
108 @Test
109 public void testDefaultMplsEcmp() {
110 boolean mplsEcmp = config.mplsEcmp();
111 assertThat(mplsEcmp, is(false));
112 }
113
114 /**
115 * Test MPLS-ECMP getter.
116 */
117 @Test
118 public void testMplsEcmp() {
119 boolean mplsEcmp = mplsEcmpConfig.mplsEcmp();
120 assertThat(mplsEcmp, is(true));
121 }
122
123 /**
124 * Test MPLS-ECMP setter.
125 */
126 @Test
127 public void testSetMplsEcmp() {
128 /*
129 * In the config the value is not set.
130 */
131 boolean mplsEcmp = config.mplsEcmp();
132 assertThat(mplsEcmp, is(false));
133 config.setMplsEcmp(true);
134 mplsEcmp = config.mplsEcmp();
135 assertThat(mplsEcmp, is(true));
136 /*
137 * In the mplsEcmpConfig the value is true,
138 */
139 mplsEcmp = mplsEcmpConfig.mplsEcmp();
140 assertThat(mplsEcmp, is(true));
141 config.setMplsEcmp(false);
142 mplsEcmp = config.mplsEcmp();
143 assertThat(mplsEcmp, is(false));
Charles Chan43547ca2016-02-10 20:46:58 -0800144 }
145
146 /**
147 * Tests vRouterMacs getter.
Charles Chan82ab1932016-01-30 23:22:37 -0800148 *
149 * @throws Exception
150 */
151 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800152 public void testVRouterMacs() throws Exception {
153 Set<MacAddress> vRouterMacs = config.vRouterMacs();
154 assertNotNull("vRouterMacs should not be null", vRouterMacs);
155 assertThat(vRouterMacs.size(), is(2));
156 assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
157 assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
Charles Chan82ab1932016-01-30 23:22:37 -0800158 }
159
160 /**
Charles Chan43547ca2016-02-10 20:46:58 -0800161 * Tests vRouterMacs setter.
Charles Chan82ab1932016-01-30 23:22:37 -0800162 *
163 * @throws Exception
164 */
165 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800166 public void testSetVRouterMacs() throws Exception {
Charles Chan82ab1932016-01-30 23:22:37 -0800167 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chan43547ca2016-02-10 20:46:58 -0800168 builder.add(ROUTER_MAC_3);
Charles Chan82ab1932016-01-30 23:22:37 -0800169 config.setVRouterMacs(builder.build());
170
Charles Chan57bd98c2016-02-22 19:27:29 -0800171 Set<MacAddress> vRouterMacs = config.vRouterMacs();
172 assertThat(vRouterMacs.size(), is(1));
173 assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
Charles Chan43547ca2016-02-10 20:46:58 -0800174 }
175
176 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800177 * Tests suppressSubnet getter.
Charles Chan43547ca2016-02-10 20:46:58 -0800178 *
179 * @throws Exception
180 */
181 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800182 public void testSuppressSubnet() throws Exception {
183 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
184 assertNotNull("suppressSubnet should not be null", suppressSubnet);
185 assertThat(suppressSubnet.size(), is(2));
186 assertTrue(suppressSubnet.contains(PORT_1));
187 assertTrue(suppressSubnet.contains(PORT_2));
Charles Chan43547ca2016-02-10 20:46:58 -0800188 }
189
190 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800191 * Tests suppressSubnet setter.
Charles Chan43547ca2016-02-10 20:46:58 -0800192 *
193 * @throws Exception
194 */
195 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800196 public void testSetSuppressSubnet() throws Exception {
197 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
198 builder.add(PORT_3);
199 config.setSuppressSubnet(builder.build());
Charles Chan43547ca2016-02-10 20:46:58 -0800200
Charles Chan57bd98c2016-02-22 19:27:29 -0800201 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
202 assertNotNull("suppressSubnet should not be null", suppressSubnet);
203 assertThat(suppressSubnet.size(), is(1));
204 assertTrue(suppressSubnet.contains(PORT_3));
205 }
206
207 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700208 * Tests suppressHostByPort getter.
Charles Chan57bd98c2016-02-22 19:27:29 -0800209 *
210 * @throws Exception
211 */
212 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700213 public void testSuppressHostByPort() throws Exception {
214 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
215 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
216 assertThat(suppressHostByPort.size(), is(2));
217 assertTrue(suppressHostByPort.contains(PORT_1));
218 assertTrue(suppressHostByPort.contains(PORT_2));
Charles Chan57bd98c2016-02-22 19:27:29 -0800219 }
220
221 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700222 * Tests suppressHostByPort setter.
Charles Chan57bd98c2016-02-22 19:27:29 -0800223 *
224 * @throws Exception
225 */
226 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700227 public void testSetSuppressHostByPort() throws Exception {
Charles Chan57bd98c2016-02-22 19:27:29 -0800228 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
229 builder.add(PORT_3);
Charles Chan3bf64b92016-05-20 10:55:40 -0700230 config.setSuppressHostByPort(builder.build());
Charles Chan57bd98c2016-02-22 19:27:29 -0800231
Charles Chan3bf64b92016-05-20 10:55:40 -0700232 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
233 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
234 assertThat(suppressHostByPort.size(), is(1));
235 assertTrue(suppressHostByPort.contains(PORT_3));
Charles Chan82ab1932016-01-30 23:22:37 -0800236 }
237
Charles Chan370a65b2016-05-10 17:29:47 -0700238 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700239 * Tests suppressHostByProvider getter.
Charles Chan370a65b2016-05-10 17:29:47 -0700240 *
241 * @throws Exception
242 */
243 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700244 public void testSuppressHostByProvider() throws Exception {
245 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
246 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
247 assertThat(supprsuppressHostByProvider.size(), is(2));
248 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1));
249 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2));
Charles Chan370a65b2016-05-10 17:29:47 -0700250 }
251
252 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700253 * Tests suppressHostByProvider setter.
Charles Chan370a65b2016-05-10 17:29:47 -0700254 *
255 * @throws Exception
256 */
257 @Test
258 public void testSetHostLearning() throws Exception {
Charles Chan3bf64b92016-05-20 10:55:40 -0700259 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
260 builder.add(PROVIDER_3);
261 config.setSuppressHostByProvider(builder.build());
262
263 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
264 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
265 assertThat(supprsuppressHostByProvider.size(), is(1));
266 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3));
Charles Chan370a65b2016-05-10 17:29:47 -0700267 }
268
Andrea Campanella60ce2222018-04-30 11:48:55 +0200269 /**
270 * Tests BlackHoleIps getter.
271 *
272 * @throws Exception
273 */
274 @Test
275 public void testBlackHoleIps() throws Exception {
276 Set<IpPrefix> blackHoleIps = config.blackholeIPs();
277 assertNotNull("BlackHoleIps should not be null", blackHoleIps);
278 assertThat(blackHoleIps.size(), is(1));
279 assertTrue(blackHoleIps.contains(BLACKHOLE_IP));
280 }
281
282 /**
283 * Tests BlackHoleIps setter.
284 *
285 * @throws Exception
286 */
287 @Test
288 public void testSetBlackHoleIps() throws Exception {
289
290 config.setBalckholeIps(ImmutableSet.of(BLACKHOLE_IP_2));
291
292 Set<IpPrefix> blackHoleIps = config.blackholeIPs();
293 assertThat(blackHoleIps.size(), is(1));
294 assertTrue(blackHoleIps.contains(BLACKHOLE_IP_2));
295 }
296
Charles Chan82ab1932016-01-30 23:22:37 -0800297 private class MockDelegate implements ConfigApplyDelegate {
298 @Override
299 public void onApply(Config config) {
300 }
301 }
Pier Ventre7a78de22016-10-31 15:00:01 -0700302}