blob: e6f491238ce558cf8878605f5006c5c5fb9d807f [file] [log] [blame]
Charles Chan82ab1932016-01-30 23:22:37 -08001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
24import org.onlab.packet.MacAddress;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.TestApplicationId;
Charles Chan57bd98c2016-02-22 19:27:29 -080027import org.onosproject.net.ConnectPoint;
Charles Chan43547ca2016-02-10 20:46:58 -080028import org.onosproject.net.DeviceId;
Charles Chan82ab1932016-01-30 23:22:37 -080029import org.onosproject.net.config.Config;
30import org.onosproject.net.config.ConfigApplyDelegate;
Charles Chan1963f4f2016-02-18 14:22:42 -080031import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan57bd98c2016-02-22 19:27:29 -080032
Charles Chan636e3472016-02-22 23:02:41 -080033import java.io.InputStream;
Charles Chan57bd98c2016-02-22 19:27:29 -080034import java.util.Optional;
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 {
44 private static final ApplicationId APP_ID =
Charles Chan1963f4f2016-02-18 14:22:42 -080045 new TestApplicationId(SegmentRoutingManager.SR_APP_ID);
Charles Chan82ab1932016-01-30 23:22:37 -080046
47 private SegmentRoutingAppConfig config;
Charles Chan43547ca2016-02-10 20:46:58 -080048 private SegmentRoutingAppConfig invalidConfig;
Charles Chan636e3472016-02-22 23:02:41 -080049
Charles Chan43547ca2016-02-10 20:46:58 -080050 private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01");
51 private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02");
52 private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03");
Charles Chan57bd98c2016-02-22 19:27:29 -080053 private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1");
54 private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2");
55 private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3");
Charles Chan43547ca2016-02-10 20:46:58 -080056 private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1");
57 private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2");
Charles Chan3bf64b92016-05-20 10:55:40 -070058 private static final String PROVIDER_1 = "org.onosproject.provider.host";
59 private static final String PROVIDER_2 = "org.onosproject.netcfghost";
60 private static final String PROVIDER_3 = "org.onosproject.anotherprovider";
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
70 .getResourceAsStream("/sr-app-config.json");
71 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
72 .getResourceAsStream("/sr-app-config-invalid.json");
73
Charles Chan82ab1932016-01-30 23:22:37 -080074 ApplicationId subject = APP_ID;
Charles Chan1963f4f2016-02-18 14:22:42 -080075 String key = SegmentRoutingManager.SR_APP_ID;
Charles Chan82ab1932016-01-30 23:22:37 -080076 ObjectMapper mapper = new ObjectMapper();
Charles Chan636e3472016-02-22 23:02:41 -080077 JsonNode jsonNode = mapper.readTree(jsonStream);
78 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Charles Chan82ab1932016-01-30 23:22:37 -080079 ConfigApplyDelegate delegate = new MockDelegate();
80
81 config = new SegmentRoutingAppConfig();
82 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chan43547ca2016-02-10 20:46:58 -080083 invalidConfig = new SegmentRoutingAppConfig();
84 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Charles Chan82ab1932016-01-30 23:22:37 -080085 }
86
87 /**
Charles Chan43547ca2016-02-10 20:46:58 -080088 * Tests config validity.
89 *
90 * @throws Exception
91 */
92 @Test
93 public void testIsValid() throws Exception {
94 assertTrue(config.isValid());
95 assertFalse(invalidConfig.isValid());
96 }
97
98 /**
99 * Tests vRouterMacs getter.
Charles Chan82ab1932016-01-30 23:22:37 -0800100 *
101 * @throws Exception
102 */
103 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800104 public void testVRouterMacs() throws Exception {
105 Set<MacAddress> vRouterMacs = config.vRouterMacs();
106 assertNotNull("vRouterMacs should not be null", vRouterMacs);
107 assertThat(vRouterMacs.size(), is(2));
108 assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
109 assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
Charles Chan82ab1932016-01-30 23:22:37 -0800110 }
111
112 /**
Charles Chan43547ca2016-02-10 20:46:58 -0800113 * Tests vRouterMacs setter.
Charles Chan82ab1932016-01-30 23:22:37 -0800114 *
115 * @throws Exception
116 */
117 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800118 public void testSetVRouterMacs() throws Exception {
Charles Chan82ab1932016-01-30 23:22:37 -0800119 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chan43547ca2016-02-10 20:46:58 -0800120 builder.add(ROUTER_MAC_3);
Charles Chan82ab1932016-01-30 23:22:37 -0800121 config.setVRouterMacs(builder.build());
122
Charles Chan57bd98c2016-02-22 19:27:29 -0800123 Set<MacAddress> vRouterMacs = config.vRouterMacs();
124 assertThat(vRouterMacs.size(), is(1));
125 assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
Charles Chan43547ca2016-02-10 20:46:58 -0800126 }
127
128 /**
129 * Tests vRouterId getter.
130 *
131 * @throws Exception
132 */
133 @Test
134 public void testVRouterId() throws Exception {
Charles Chan57bd98c2016-02-22 19:27:29 -0800135 Optional<DeviceId> vRouterId = config.vRouterId();
136 assertTrue(vRouterId.isPresent());
137 assertThat(vRouterId.get(), is(VROUTER_ID_1));
Charles Chan43547ca2016-02-10 20:46:58 -0800138 }
139
140 /**
141 * Tests vRouterId setter.
142 *
143 * @throws Exception
144 */
145 @Test
146 public void testSetVRouterId() throws Exception {
147 config.setVRouterId(VROUTER_ID_2);
Charles Chan57bd98c2016-02-22 19:27:29 -0800148
149 Optional<DeviceId> vRouterId = config.vRouterId();
150 assertTrue(vRouterId.isPresent());
151 assertThat(vRouterId.get(), is(VROUTER_ID_2));
Charles Chan43547ca2016-02-10 20:46:58 -0800152 }
153
154 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800155 * Tests suppressSubnet getter.
Charles Chan43547ca2016-02-10 20:46:58 -0800156 *
157 * @throws Exception
158 */
159 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800160 public void testSuppressSubnet() throws Exception {
161 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
162 assertNotNull("suppressSubnet should not be null", suppressSubnet);
163 assertThat(suppressSubnet.size(), is(2));
164 assertTrue(suppressSubnet.contains(PORT_1));
165 assertTrue(suppressSubnet.contains(PORT_2));
Charles Chan43547ca2016-02-10 20:46:58 -0800166 }
167
168 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800169 * Tests suppressSubnet setter.
Charles Chan43547ca2016-02-10 20:46:58 -0800170 *
171 * @throws Exception
172 */
173 @Test
Charles Chan57bd98c2016-02-22 19:27:29 -0800174 public void testSetSuppressSubnet() throws Exception {
175 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
176 builder.add(PORT_3);
177 config.setSuppressSubnet(builder.build());
Charles Chan43547ca2016-02-10 20:46:58 -0800178
Charles Chan57bd98c2016-02-22 19:27:29 -0800179 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
180 assertNotNull("suppressSubnet should not be null", suppressSubnet);
181 assertThat(suppressSubnet.size(), is(1));
182 assertTrue(suppressSubnet.contains(PORT_3));
183 }
184
185 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700186 * Tests suppressHostByPort getter.
Charles Chan57bd98c2016-02-22 19:27:29 -0800187 *
188 * @throws Exception
189 */
190 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700191 public void testSuppressHostByPort() throws Exception {
192 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
193 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
194 assertThat(suppressHostByPort.size(), is(2));
195 assertTrue(suppressHostByPort.contains(PORT_1));
196 assertTrue(suppressHostByPort.contains(PORT_2));
Charles Chan57bd98c2016-02-22 19:27:29 -0800197 }
198
199 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700200 * Tests suppressHostByPort setter.
Charles Chan57bd98c2016-02-22 19:27:29 -0800201 *
202 * @throws Exception
203 */
204 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700205 public void testSetSuppressHostByPort() throws Exception {
Charles Chan57bd98c2016-02-22 19:27:29 -0800206 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
207 builder.add(PORT_3);
Charles Chan3bf64b92016-05-20 10:55:40 -0700208 config.setSuppressHostByPort(builder.build());
Charles Chan57bd98c2016-02-22 19:27:29 -0800209
Charles Chan3bf64b92016-05-20 10:55:40 -0700210 Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort();
211 assertNotNull("suppressHostByPort should not be null", suppressHostByPort);
212 assertThat(suppressHostByPort.size(), is(1));
213 assertTrue(suppressHostByPort.contains(PORT_3));
Charles Chan82ab1932016-01-30 23:22:37 -0800214 }
215
Charles Chan370a65b2016-05-10 17:29:47 -0700216 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700217 * Tests suppressHostByProvider getter.
Charles Chan370a65b2016-05-10 17:29:47 -0700218 *
219 * @throws Exception
220 */
221 @Test
Charles Chan3bf64b92016-05-20 10:55:40 -0700222 public void testSuppressHostByProvider() throws Exception {
223 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
224 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
225 assertThat(supprsuppressHostByProvider.size(), is(2));
226 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1));
227 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2));
Charles Chan370a65b2016-05-10 17:29:47 -0700228 }
229
230 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700231 * Tests suppressHostByProvider setter.
Charles Chan370a65b2016-05-10 17:29:47 -0700232 *
233 * @throws Exception
234 */
235 @Test
236 public void testSetHostLearning() throws Exception {
Charles Chan3bf64b92016-05-20 10:55:40 -0700237 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
238 builder.add(PROVIDER_3);
239 config.setSuppressHostByProvider(builder.build());
240
241 Set<String> supprsuppressHostByProvider = config.suppressHostByProvider();
242 assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider);
243 assertThat(supprsuppressHostByProvider.size(), is(1));
244 assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3));
Charles Chan370a65b2016-05-10 17:29:47 -0700245 }
246
Charles Chan82ab1932016-01-30 23:22:37 -0800247 private class MockDelegate implements ConfigApplyDelegate {
248 @Override
249 public void onApply(Config config) {
250 }
251 }
252}