blob: aa8896c6b3fa77f5cb8b23781fb1623600ad8158 [file] [log] [blame]
Charles Chan5270ed02016-01-30 23:22:37 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Charles Chan5270ed02016-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;
24import org.onlab.packet.MacAddress;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.TestApplicationId;
Charles Chand9681e72016-02-22 19:27:29 -080027import org.onosproject.net.ConnectPoint;
Charles Chanf2565a92016-02-10 20:46:58 -080028import org.onosproject.net.DeviceId;
Charles Chan5270ed02016-01-30 23:22:37 -080029import org.onosproject.net.config.Config;
30import org.onosproject.net.config.ConfigApplyDelegate;
Charles Chan116188d2016-02-18 14:22:42 -080031import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chand9681e72016-02-22 19:27:29 -080032
Charles Chana35b02c2016-02-22 23:02:41 -080033import java.io.InputStream;
Charles Chand9681e72016-02-22 19:27:29 -080034import java.util.Optional;
Charles Chan5270ed02016-01-30 23:22:37 -080035import java.util.Set;
36
37import static org.hamcrest.Matchers.is;
Charles Chand9681e72016-02-22 19:27:29 -080038import static org.junit.Assert.*;
Charles Chan5270ed02016-01-30 23:22:37 -080039
40/**
41 * Tests for class {@link SegmentRoutingAppConfig}.
42 */
43public class SegmentRoutingAppConfigTest {
Charles Chan5270ed02016-01-30 23:22:37 -080044 private SegmentRoutingAppConfig config;
Charles Chanf2565a92016-02-10 20:46:58 -080045 private SegmentRoutingAppConfig invalidConfig;
Pier Ventre98161782016-10-31 15:00:01 -070046 private SegmentRoutingAppConfig mplsEcmpConfig;
Charles Chana35b02c2016-02-22 23:02:41 -080047
Charles Chanf2565a92016-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 Chand9681e72016-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 Chanf2565a92016-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 Chanb3007e12016-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";
Charles Chan5270ed02016-01-30 23:22:37 -080059
60 /**
61 * Initialize test related variables.
62 *
63 * @throws Exception
64 */
65 @Before
66 public void setUp() throws Exception {
Charles Chana35b02c2016-02-22 23:02:41 -080067 InputStream jsonStream = SegmentRoutingAppConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070068 .getResourceAsStream("/app.json");
Charles Chana35b02c2016-02-22 23:02:41 -080069 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070070 .getResourceAsStream("/app-invalid.json");
Pier Ventre98161782016-10-31 15:00:01 -070071 InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class
72 .getResourceAsStream("/app-ecmp.json");
Charles Chana35b02c2016-02-22 23:02:41 -080073
Charles Chan2c15aca2016-11-09 20:51:44 -080074 String key = SegmentRoutingManager.APP_NAME;
Charles Chanfc5c7802016-05-17 13:13:55 -070075 ApplicationId subject = new TestApplicationId(key);
Charles Chan5270ed02016-01-30 23:22:37 -080076 ObjectMapper mapper = new ObjectMapper();
Charles Chana35b02c2016-02-22 23:02:41 -080077 JsonNode jsonNode = mapper.readTree(jsonStream);
78 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Pier Ventre98161782016-10-31 15:00:01 -070079 JsonNode mplsEcmpJsonNode = mapper.readTree(mplsEcmpJsonStream);
Charles Chan5270ed02016-01-30 23:22:37 -080080 ConfigApplyDelegate delegate = new MockDelegate();
81
82 config = new SegmentRoutingAppConfig();
83 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chanf2565a92016-02-10 20:46:58 -080084 invalidConfig = new SegmentRoutingAppConfig();
85 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Pier Ventre98161782016-10-31 15:00:01 -070086 mplsEcmpConfig = new SegmentRoutingAppConfig();
87 mplsEcmpConfig.init(subject, key, mplsEcmpJsonNode, mapper, delegate);
Charles Chan5270ed02016-01-30 23:22:37 -080088 }
89
90 /**
Charles Chanf2565a92016-02-10 20:46:58 -080091 * Tests config validity.
92 *
93 * @throws Exception
94 */
95 @Test
96 public void testIsValid() throws Exception {
97 assertTrue(config.isValid());
98 assertFalse(invalidConfig.isValid());
Pier Ventre98161782016-10-31 15:00:01 -070099 assertTrue(mplsEcmpConfig.isValid());
100 }
101
102 /**
103 * Test MPLS-ECMP default getter. By-default
104 * MPLS-ECMPS is false.
105 */
106 @Test
107 public void testDefaultMplsEcmp() {
108 boolean mplsEcmp = config.mplsEcmp();
109 assertThat(mplsEcmp, is(false));
110 }
111
112 /**
113 * Test MPLS-ECMP getter.
114 */
115 @Test
116 public void testMplsEcmp() {
117 boolean mplsEcmp = mplsEcmpConfig.mplsEcmp();
118 assertThat(mplsEcmp, is(true));
119 }
120
121 /**
122 * Test MPLS-ECMP setter.
123 */
124 @Test
125 public void testSetMplsEcmp() {
126 /*
127 * In the config the value is not set.
128 */
129 boolean mplsEcmp = config.mplsEcmp();
130 assertThat(mplsEcmp, is(false));
131 config.setMplsEcmp(true);
132 mplsEcmp = config.mplsEcmp();
133 assertThat(mplsEcmp, is(true));
134 /*
135 * In the mplsEcmpConfig the value is true,
136 */
137 mplsEcmp = mplsEcmpConfig.mplsEcmp();
138 assertThat(mplsEcmp, is(true));
139 config.setMplsEcmp(false);
140 mplsEcmp = config.mplsEcmp();
141 assertThat(mplsEcmp, is(false));
Charles Chanf2565a92016-02-10 20:46:58 -0800142 }
143
144 /**
145 * Tests vRouterMacs getter.
Charles Chan5270ed02016-01-30 23:22:37 -0800146 *
147 * @throws Exception
148 */
149 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800150 public void testVRouterMacs() throws Exception {
151 Set<MacAddress> vRouterMacs = config.vRouterMacs();
152 assertNotNull("vRouterMacs should not be null", vRouterMacs);
153 assertThat(vRouterMacs.size(), is(2));
154 assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
155 assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
Charles Chan5270ed02016-01-30 23:22:37 -0800156 }
157
158 /**
Charles Chanf2565a92016-02-10 20:46:58 -0800159 * Tests vRouterMacs setter.
Charles Chan5270ed02016-01-30 23:22:37 -0800160 *
161 * @throws Exception
162 */
163 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800164 public void testSetVRouterMacs() throws Exception {
Charles Chan5270ed02016-01-30 23:22:37 -0800165 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chanf2565a92016-02-10 20:46:58 -0800166 builder.add(ROUTER_MAC_3);
Charles Chan5270ed02016-01-30 23:22:37 -0800167 config.setVRouterMacs(builder.build());
168
Charles Chand9681e72016-02-22 19:27:29 -0800169 Set<MacAddress> vRouterMacs = config.vRouterMacs();
170 assertThat(vRouterMacs.size(), is(1));
171 assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
Charles Chanf2565a92016-02-10 20:46:58 -0800172 }
173
174 /**
175 * Tests vRouterId getter.
176 *
177 * @throws Exception
178 */
179 @Test
180 public void testVRouterId() throws Exception {
Charles Chand9681e72016-02-22 19:27:29 -0800181 Optional<DeviceId> vRouterId = config.vRouterId();
182 assertTrue(vRouterId.isPresent());
183 assertThat(vRouterId.get(), is(VROUTER_ID_1));
Charles Chanf2565a92016-02-10 20:46:58 -0800184 }
185
186 /**
187 * Tests vRouterId setter.
188 *
189 * @throws Exception
190 */
191 @Test
192 public void testSetVRouterId() throws Exception {
193 config.setVRouterId(VROUTER_ID_2);
Charles Chand9681e72016-02-22 19:27:29 -0800194
195 Optional<DeviceId> vRouterId = config.vRouterId();
196 assertTrue(vRouterId.isPresent());
197 assertThat(vRouterId.get(), is(VROUTER_ID_2));
Charles Chanf2565a92016-02-10 20:46:58 -0800198 }
199
200 /**
Charles Chand9681e72016-02-22 19:27:29 -0800201 * Tests suppressSubnet getter.
Charles Chanf2565a92016-02-10 20:46:58 -0800202 *
203 * @throws Exception
204 */
205 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800206 public void testSuppressSubnet() throws Exception {
207 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
208 assertNotNull("suppressSubnet should not be null", suppressSubnet);
209 assertThat(suppressSubnet.size(), is(2));
210 assertTrue(suppressSubnet.contains(PORT_1));
211 assertTrue(suppressSubnet.contains(PORT_2));
Charles Chanf2565a92016-02-10 20:46:58 -0800212 }
213
214 /**
Charles Chand9681e72016-02-22 19:27:29 -0800215 * Tests suppressSubnet setter.
Charles Chanf2565a92016-02-10 20:46:58 -0800216 *
217 * @throws Exception
218 */
219 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800220 public void testSetSuppressSubnet() throws Exception {
221 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
222 builder.add(PORT_3);
223 config.setSuppressSubnet(builder.build());
Charles Chanf2565a92016-02-10 20:46:58 -0800224
Charles Chand9681e72016-02-22 19:27:29 -0800225 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
226 assertNotNull("suppressSubnet should not be null", suppressSubnet);
227 assertThat(suppressSubnet.size(), is(1));
228 assertTrue(suppressSubnet.contains(PORT_3));
229 }
230
231 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700232 * Tests suppressHostByPort getter.
Charles Chand9681e72016-02-22 19:27:29 -0800233 *
234 * @throws Exception
235 */
236 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700237 public void testSuppressHostByPort() throws Exception {
238 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
239 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
240 assertThat(suppressHostByPort.size(), is(2));
241 assertTrue(suppressHostByPort.contains(PORT_1));
242 assertTrue(suppressHostByPort.contains(PORT_2));
Charles Chand9681e72016-02-22 19:27:29 -0800243 }
244
245 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700246 * Tests suppressHostByPort setter.
Charles Chand9681e72016-02-22 19:27:29 -0800247 *
248 * @throws Exception
249 */
250 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700251 public void testSetSuppressHostByPort() throws Exception {
Charles Chand9681e72016-02-22 19:27:29 -0800252 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
253 builder.add(PORT_3);
Charles Chanb3007e12016-05-20 10:55:40 -0700254 config.setSuppressHostByPort(builder.build());
Charles Chand9681e72016-02-22 19:27:29 -0800255
Charles Chanb3007e12016-05-20 10:55:40 -0700256 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
257 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
258 assertThat(suppressHostByPort.size(), is(1));
259 assertTrue(suppressHostByPort.contains(PORT_3));
Charles Chan5270ed02016-01-30 23:22:37 -0800260 }
261
Charles Chan6ea94fc2016-05-10 17:29:47 -0700262 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700263 * Tests suppressHostByProvider getter.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700264 *
265 * @throws Exception
266 */
267 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700268 public void testSuppressHostByProvider() throws Exception {
269 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
270 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
271 assertThat(supprsuppressHostByProvider.size(), is(2));
272 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1));
273 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2));
Charles Chan6ea94fc2016-05-10 17:29:47 -0700274 }
275
276 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700277 * Tests suppressHostByProvider setter.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700278 *
279 * @throws Exception
280 */
281 @Test
282 public void testSetHostLearning() throws Exception {
Charles Chanb3007e12016-05-20 10:55:40 -0700283 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
284 builder.add(PROVIDER_3);
285 config.setSuppressHostByProvider(builder.build());
286
287 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
288 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
289 assertThat(supprsuppressHostByProvider.size(), is(1));
290 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3));
Charles Chan6ea94fc2016-05-10 17:29:47 -0700291 }
292
Charles Chan5270ed02016-01-30 23:22:37 -0800293 private class MockDelegate implements ConfigApplyDelegate {
294 @Override
295 public void onApply(Config config) {
296 }
297 }
Pier Ventre98161782016-10-31 15:00:01 -0700298}