blob: 1c9abd71681ae780cd9b5820c6742730d88edf81 [file] [log] [blame]
Charles Chan5270ed02016-01-30 23:22:37 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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 {
44 private static final ApplicationId APP_ID =
Charles Chan116188d2016-02-18 14:22:42 -080045 new TestApplicationId(SegmentRoutingManager.SR_APP_ID);
Charles Chan5270ed02016-01-30 23:22:37 -080046
47 private SegmentRoutingAppConfig config;
Charles Chanf2565a92016-02-10 20:46:58 -080048 private SegmentRoutingAppConfig invalidConfig;
Charles Chana35b02c2016-02-22 23:02:41 -080049
Charles Chanf2565a92016-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 Chand9681e72016-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 Chanf2565a92016-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 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
67 .getResourceAsStream("/sr-app-config.json");
68 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
69 .getResourceAsStream("/sr-app-config-invalid.json");
70
Charles Chan5270ed02016-01-30 23:22:37 -080071 ApplicationId subject = APP_ID;
Charles Chan116188d2016-02-18 14:22:42 -080072 String key = SegmentRoutingManager.SR_APP_ID;
Charles Chan5270ed02016-01-30 23:22:37 -080073 ObjectMapper mapper = new ObjectMapper();
Charles Chana35b02c2016-02-22 23:02:41 -080074 JsonNode jsonNode = mapper.readTree(jsonStream);
75 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Charles Chan5270ed02016-01-30 23:22:37 -080076 ConfigApplyDelegate delegate = new MockDelegate();
77
78 config = new SegmentRoutingAppConfig();
79 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chanf2565a92016-02-10 20:46:58 -080080 invalidConfig = new SegmentRoutingAppConfig();
81 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Charles Chan5270ed02016-01-30 23:22:37 -080082 }
83
84 /**
Charles Chanf2565a92016-02-10 20:46:58 -080085 * Tests config validity.
86 *
87 * @throws Exception
88 */
89 @Test
90 public void testIsValid() throws Exception {
91 assertTrue(config.isValid());
92 assertFalse(invalidConfig.isValid());
93 }
94
95 /**
96 * Tests vRouterMacs getter.
Charles Chan5270ed02016-01-30 23:22:37 -080097 *
98 * @throws Exception
99 */
100 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800101 public void testVRouterMacs() throws Exception {
102 Set<MacAddress> vRouterMacs = config.vRouterMacs();
103 assertNotNull("vRouterMacs should not be null", vRouterMacs);
104 assertThat(vRouterMacs.size(), is(2));
105 assertTrue(vRouterMacs.contains(ROUTER_MAC_1));
106 assertTrue(vRouterMacs.contains(ROUTER_MAC_2));
Charles Chan5270ed02016-01-30 23:22:37 -0800107 }
108
109 /**
Charles Chanf2565a92016-02-10 20:46:58 -0800110 * Tests vRouterMacs setter.
Charles Chan5270ed02016-01-30 23:22:37 -0800111 *
112 * @throws Exception
113 */
114 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800115 public void testSetVRouterMacs() throws Exception {
Charles Chan5270ed02016-01-30 23:22:37 -0800116 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chanf2565a92016-02-10 20:46:58 -0800117 builder.add(ROUTER_MAC_3);
Charles Chan5270ed02016-01-30 23:22:37 -0800118 config.setVRouterMacs(builder.build());
119
Charles Chand9681e72016-02-22 19:27:29 -0800120 Set<MacAddress> vRouterMacs = config.vRouterMacs();
121 assertThat(vRouterMacs.size(), is(1));
122 assertTrue(vRouterMacs.contains(ROUTER_MAC_3));
Charles Chanf2565a92016-02-10 20:46:58 -0800123 }
124
125 /**
126 * Tests vRouterId getter.
127 *
128 * @throws Exception
129 */
130 @Test
131 public void testVRouterId() throws Exception {
Charles Chand9681e72016-02-22 19:27:29 -0800132 Optional<DeviceId> vRouterId = config.vRouterId();
133 assertTrue(vRouterId.isPresent());
134 assertThat(vRouterId.get(), is(VROUTER_ID_1));
Charles Chanf2565a92016-02-10 20:46:58 -0800135 }
136
137 /**
138 * Tests vRouterId setter.
139 *
140 * @throws Exception
141 */
142 @Test
143 public void testSetVRouterId() throws Exception {
144 config.setVRouterId(VROUTER_ID_2);
Charles Chand9681e72016-02-22 19:27:29 -0800145
146 Optional<DeviceId> vRouterId = config.vRouterId();
147 assertTrue(vRouterId.isPresent());
148 assertThat(vRouterId.get(), is(VROUTER_ID_2));
Charles Chanf2565a92016-02-10 20:46:58 -0800149 }
150
151 /**
Charles Chand9681e72016-02-22 19:27:29 -0800152 * Tests suppressSubnet getter.
Charles Chanf2565a92016-02-10 20:46:58 -0800153 *
154 * @throws Exception
155 */
156 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800157 public void testSuppressSubnet() throws Exception {
158 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
159 assertNotNull("suppressSubnet should not be null", suppressSubnet);
160 assertThat(suppressSubnet.size(), is(2));
161 assertTrue(suppressSubnet.contains(PORT_1));
162 assertTrue(suppressSubnet.contains(PORT_2));
Charles Chanf2565a92016-02-10 20:46:58 -0800163 }
164
165 /**
Charles Chand9681e72016-02-22 19:27:29 -0800166 * Tests suppressSubnet setter.
Charles Chanf2565a92016-02-10 20:46:58 -0800167 *
168 * @throws Exception
169 */
170 @Test
Charles Chand9681e72016-02-22 19:27:29 -0800171 public void testSetSuppressSubnet() throws Exception {
172 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
173 builder.add(PORT_3);
174 config.setSuppressSubnet(builder.build());
Charles Chanf2565a92016-02-10 20:46:58 -0800175
Charles Chand9681e72016-02-22 19:27:29 -0800176 Set<ConnectPoint> suppressSubnet = config.suppressSubnet();
177 assertNotNull("suppressSubnet should not be null", suppressSubnet);
178 assertThat(suppressSubnet.size(), is(1));
179 assertTrue(suppressSubnet.contains(PORT_3));
180 }
181
182 /**
183 * Tests suppressHost getter.
184 *
185 * @throws Exception
186 */
187 @Test
188 public void testSuppressHost() throws Exception {
189 Set<ConnectPoint> suppressHost = config.suppressHost();
190 assertNotNull("suppressHost should not be null", suppressHost);
191 assertThat(suppressHost.size(), is(2));
192 assertTrue(suppressHost.contains(PORT_1));
193 assertTrue(suppressHost.contains(PORT_2));
194 }
195
196 /**
197 * Tests suppressHost setter.
198 *
199 * @throws Exception
200 */
201 @Test
202 public void testSetSuppressHost() throws Exception {
203 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
204 builder.add(PORT_3);
205 config.setSuppressHost(builder.build());
206
207 Set<ConnectPoint> suppressHost = config.suppressHost();
208 assertNotNull("suppressHost should not be null", suppressHost);
209 assertThat(suppressHost.size(), is(1));
210 assertTrue(suppressHost.contains(PORT_3));
Charles Chan5270ed02016-01-30 23:22:37 -0800211 }
212
213 private class MockDelegate implements ConfigApplyDelegate {
214 @Override
215 public void onApply(Config config) {
216 }
217 }
218}