blob: 8526573415aad73b9e44185f4cfd0e14e9e6013e [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 Chan5270ed02016-01-30 23:22:37 -080034import java.util.Set;
35
36import static org.hamcrest.Matchers.is;
Charles Chand9681e72016-02-22 19:27:29 -080037import static org.junit.Assert.*;
Charles Chan5270ed02016-01-30 23:22:37 -080038
39/**
40 * Tests for class {@link SegmentRoutingAppConfig}.
41 */
42public class SegmentRoutingAppConfigTest {
Charles Chan5270ed02016-01-30 23:22:37 -080043 private SegmentRoutingAppConfig config;
Charles Chanf2565a92016-02-10 20:46:58 -080044 private SegmentRoutingAppConfig invalidConfig;
Pier Ventre98161782016-10-31 15:00:01 -070045 private SegmentRoutingAppConfig mplsEcmpConfig;
Charles Chana35b02c2016-02-22 23:02:41 -080046
Charles Chanf2565a92016-02-10 20:46:58 -080047 private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01");
48 private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02");
49 private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03");
Charles Chand9681e72016-02-22 19:27:29 -080050 private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1");
51 private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2");
52 private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3");
Charles Chanf2565a92016-02-10 20:46:58 -080053 private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1");
54 private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2");
Charles Chanb3007e12016-05-20 10:55:40 -070055 private static final String PROVIDER_1 = "org.onosproject.provider.host";
56 private static final String PROVIDER_2 = "org.onosproject.netcfghost";
57 private static final String PROVIDER_3 = "org.onosproject.anotherprovider";
Charles Chan5270ed02016-01-30 23:22:37 -080058
59 /**
60 * Initialize test related variables.
61 *
62 * @throws Exception
63 */
64 @Before
65 public void setUp() throws Exception {
Charles Chana35b02c2016-02-22 23:02:41 -080066 InputStream jsonStream = SegmentRoutingAppConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070067 .getResourceAsStream("/app.json");
Charles Chana35b02c2016-02-22 23:02:41 -080068 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070069 .getResourceAsStream("/app-invalid.json");
Pier Ventre98161782016-10-31 15:00:01 -070070 InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class
71 .getResourceAsStream("/app-ecmp.json");
Charles Chana35b02c2016-02-22 23:02:41 -080072
Charles Chan2c15aca2016-11-09 20:51:44 -080073 String key = SegmentRoutingManager.APP_NAME;
Charles Chanfc5c7802016-05-17 13:13:55 -070074 ApplicationId subject = new TestApplicationId(key);
Charles Chan5270ed02016-01-30 23:22:37 -080075 ObjectMapper mapper = new ObjectMapper();
Charles Chana35b02c2016-02-22 23:02:41 -080076 JsonNode jsonNode = mapper.readTree(jsonStream);
77 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Pier Ventre98161782016-10-31 15:00:01 -070078 JsonNode mplsEcmpJsonNode = mapper.readTree(mplsEcmpJsonStream);
Charles Chan5270ed02016-01-30 23:22:37 -080079 ConfigApplyDelegate delegate = new MockDelegate();
80
81 config = new SegmentRoutingAppConfig();
82 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chanf2565a92016-02-10 20:46:58 -080083 invalidConfig = new SegmentRoutingAppConfig();
84 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Pier Ventre98161782016-10-31 15:00:01 -070085 mplsEcmpConfig = new SegmentRoutingAppConfig();
86 mplsEcmpConfig.init(subject, key, mplsEcmpJsonNode, mapper, delegate);
Charles Chan5270ed02016-01-30 23:22:37 -080087 }
88
89 /**
Charles Chanf2565a92016-02-10 20:46:58 -080090 * Tests config validity.
91 *
92 * @throws Exception
93 */
94 @Test
95 public void testIsValid() throws Exception {
96 assertTrue(config.isValid());
97 assertFalse(invalidConfig.isValid());
Pier Ventre98161782016-10-31 15:00:01 -070098 assertTrue(mplsEcmpConfig.isValid());
99 }
100
101 /**
102 * Test MPLS-ECMP default getter. By-default
103 * MPLS-ECMPS is false.
104 */
105 @Test
106 public void testDefaultMplsEcmp() {
107 boolean mplsEcmp = config.mplsEcmp();
108 assertThat(mplsEcmp, is(false));
109 }
110
111 /**
112 * Test MPLS-ECMP getter.
113 */
114 @Test
115 public void testMplsEcmp() {
116 boolean mplsEcmp = mplsEcmpConfig.mplsEcmp();
117 assertThat(mplsEcmp, is(true));
118 }
119
120 /**
121 * Test MPLS-ECMP setter.
122 */
123 @Test
124 public void testSetMplsEcmp() {
125 /*
126 * In the config the value is not set.
127 */
128 boolean mplsEcmp = config.mplsEcmp();
129 assertThat(mplsEcmp, is(false));
130 config.setMplsEcmp(true);
131 mplsEcmp = config.mplsEcmp();
132 assertThat(mplsEcmp, is(true));
133 /*
134 * In the mplsEcmpConfig the value is true,
135 */
136 mplsEcmp = mplsEcmpConfig.mplsEcmp();
137 assertThat(mplsEcmp, is(true));
138 config.setMplsEcmp(false);
139 mplsEcmp = config.mplsEcmp();
140 assertThat(mplsEcmp, is(false));
Charles Chanf2565a92016-02-10 20:46:58 -0800141 }
142
143 /**
144 * Tests vRouterMacs getter.
Charles Chan5270ed02016-01-30 23:22:37 -0800145 *
146 * @throws Exception
147 */
148 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800149 public void testVRouterMacs() throws Exception {
150 Set<MacAddress> vRouterMacs = config.vRouterMacs();
151 assertNotNull("vRouterMacs should not be null", vRouterMacs);
152 assertThat(vRouterMacs.size(), is(2));
153 assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
154 assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
Charles Chan5270ed02016-01-30 23:22:37 -0800155 }
156
157 /**
Charles Chanf2565a92016-02-10 20:46:58 -0800158 * Tests vRouterMacs setter.
Charles Chan5270ed02016-01-30 23:22:37 -0800159 *
160 * @throws Exception
161 */
162 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800163 public void testSetVRouterMacs() throws Exception {
Charles Chan5270ed02016-01-30 23:22:37 -0800164 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chanf2565a92016-02-10 20:46:58 -0800165 builder.add(ROUTER_MAC_3);
Charles Chan5270ed02016-01-30 23:22:37 -0800166 config.setVRouterMacs(builder.build());
167
Charles Chand9681e72016-02-22 19:27:29 -0800168 Set<MacAddress> vRouterMacs = config.vRouterMacs();
169 assertThat(vRouterMacs.size(), is(1));
170 assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
Charles Chanf2565a92016-02-10 20:46:58 -0800171 }
172
173 /**
Charles Chand9681e72016-02-22 19:27:29 -0800174 * Tests suppressSubnet getter.
Charles Chanf2565a92016-02-10 20:46:58 -0800175 *
176 * @throws Exception
177 */
178 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800179 public void testSuppressSubnet() throws Exception {
180 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
181 assertNotNull("suppressSubnet should not be null", suppressSubnet);
182 assertThat(suppressSubnet.size(), is(2));
183 assertTrue(suppressSubnet.contains(PORT_1));
184 assertTrue(suppressSubnet.contains(PORT_2));
Charles Chanf2565a92016-02-10 20:46:58 -0800185 }
186
187 /**
Charles Chand9681e72016-02-22 19:27:29 -0800188 * Tests suppressSubnet setter.
Charles Chanf2565a92016-02-10 20:46:58 -0800189 *
190 * @throws Exception
191 */
192 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800193 public void testSetSuppressSubnet() throws Exception {
194 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
195 builder.add(PORT_3);
196 config.setSuppressSubnet(builder.build());
Charles Chanf2565a92016-02-10 20:46:58 -0800197
Charles Chand9681e72016-02-22 19:27:29 -0800198 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
199 assertNotNull("suppressSubnet should not be null", suppressSubnet);
200 assertThat(suppressSubnet.size(), is(1));
201 assertTrue(suppressSubnet.contains(PORT_3));
202 }
203
204 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700205 * Tests suppressHostByPort getter.
Charles Chand9681e72016-02-22 19:27:29 -0800206 *
207 * @throws Exception
208 */
209 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700210 public void testSuppressHostByPort() throws Exception {
211 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
212 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
213 assertThat(suppressHostByPort.size(), is(2));
214 assertTrue(suppressHostByPort.contains(PORT_1));
215 assertTrue(suppressHostByPort.contains(PORT_2));
Charles Chand9681e72016-02-22 19:27:29 -0800216 }
217
218 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700219 * Tests suppressHostByPort setter.
Charles Chand9681e72016-02-22 19:27:29 -0800220 *
221 * @throws Exception
222 */
223 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700224 public void testSetSuppressHostByPort() throws Exception {
Charles Chand9681e72016-02-22 19:27:29 -0800225 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
226 builder.add(PORT_3);
Charles Chanb3007e12016-05-20 10:55:40 -0700227 config.setSuppressHostByPort(builder.build());
Charles Chand9681e72016-02-22 19:27:29 -0800228
Charles Chanb3007e12016-05-20 10:55:40 -0700229 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
230 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
231 assertThat(suppressHostByPort.size(), is(1));
232 assertTrue(suppressHostByPort.contains(PORT_3));
Charles Chan5270ed02016-01-30 23:22:37 -0800233 }
234
Charles Chan6ea94fc2016-05-10 17:29:47 -0700235 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700236 * Tests suppressHostByProvider getter.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700237 *
238 * @throws Exception
239 */
240 @Test
Charles Chanb3007e12016-05-20 10:55:40 -0700241 public void testSuppressHostByProvider() throws Exception {
242 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
243 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
244 assertThat(supprsuppressHostByProvider.size(), is(2));
245 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1));
246 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2));
Charles Chan6ea94fc2016-05-10 17:29:47 -0700247 }
248
249 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700250 * Tests suppressHostByProvider setter.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700251 *
252 * @throws Exception
253 */
254 @Test
255 public void testSetHostLearning() throws Exception {
Charles Chanb3007e12016-05-20 10:55:40 -0700256 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
257 builder.add(PROVIDER_3);
258 config.setSuppressHostByProvider(builder.build());
259
260 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
261 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
262 assertThat(supprsuppressHostByProvider.size(), is(1));
263 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3));
Charles Chan6ea94fc2016-05-10 17:29:47 -0700264 }
265
Charles Chan5270ed02016-01-30 23:22:37 -0800266 private class MockDelegate implements ConfigApplyDelegate {
267 @Override
268 public void onApply(Config config) {
269 }
270 }
Pier Ventre98161782016-10-31 15:00:01 -0700271}