blob: 8acef5cb3a9672f792435ad3607eaabc97db06d4 [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 Chanf2565a92016-02-10 20:46:58 -080027import org.onosproject.net.DeviceId;
Charles Chan5270ed02016-01-30 23:22:37 -080028import org.onosproject.net.config.Config;
29import org.onosproject.net.config.ConfigApplyDelegate;
30import org.onosproject.segmentrouting.SegmentRoutingService;
31import java.util.Set;
32
33import static org.hamcrest.Matchers.is;
34import static org.junit.Assert.assertThat;
35import static org.junit.Assert.assertTrue;
Charles Chanf2565a92016-02-10 20:46:58 -080036import static org.junit.Assert.assertFalse;
Charles Chan5270ed02016-01-30 23:22:37 -080037
38/**
39 * Tests for class {@link SegmentRoutingAppConfig}.
40 */
41public class SegmentRoutingAppConfigTest {
42 private static final ApplicationId APP_ID =
43 new TestApplicationId(SegmentRoutingService.SR_APP_ID);
44
45 private SegmentRoutingAppConfig config;
Charles Chanf2565a92016-02-10 20:46:58 -080046 private SegmentRoutingAppConfig invalidConfig;
47 private static final String JSON_STRING = "{" +
48 "\"vRouterMacs\" : [" +
49 " \"00:00:00:00:00:01\"," +
50 " \"00:00:00:00:00:02\"" +
51 "]," +
52 "\"vRouterId\" : \"of:1\"," +
53 "\"excludePorts\" : [" +
54 " \"port1\"," +
55 " \"port2\"" +
56 "]}";
57 private static final String INVALID_JSON_STRING = "{" +
58 "\"vRouterMacs\" : [" +
59 " \"00:00:00:00:00:01\"," +
60 " \"00:00:00:00:00:02\"" +
61 "]," +
62 "\"excludePorts\" : [" +
63 " \"port1\"," +
64 " \"port2\"" +
65 "]}";
66 private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01");
67 private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02");
68 private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03");
69 private static final String PORT_NAME_1 = "port1";
70 private static final String PORT_NAME_2 = "port2";
71 private static final String PORT_NAME_3 = "port3";
72 private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1");
73 private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2");
Charles Chan5270ed02016-01-30 23:22:37 -080074
75 /**
76 * Initialize test related variables.
77 *
78 * @throws Exception
79 */
80 @Before
81 public void setUp() throws Exception {
Charles Chan5270ed02016-01-30 23:22:37 -080082 ApplicationId subject = APP_ID;
83 String key = SegmentRoutingService.SR_APP_ID;
84 ObjectMapper mapper = new ObjectMapper();
Charles Chanf2565a92016-02-10 20:46:58 -080085 JsonNode jsonNode = mapper.readTree(JSON_STRING);
86 JsonNode invalidJsonNode = mapper.readTree(INVALID_JSON_STRING);
Charles Chan5270ed02016-01-30 23:22:37 -080087 ConfigApplyDelegate delegate = new MockDelegate();
88
89 config = new SegmentRoutingAppConfig();
90 config.init(subject, key, jsonNode, mapper, delegate);
Charles Chanf2565a92016-02-10 20:46:58 -080091 invalidConfig = new SegmentRoutingAppConfig();
92 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Charles Chan5270ed02016-01-30 23:22:37 -080093 }
94
95 /**
Charles Chanf2565a92016-02-10 20:46:58 -080096 * Tests config validity.
97 *
98 * @throws Exception
99 */
100 @Test
101 public void testIsValid() throws Exception {
102 assertTrue(config.isValid());
103 assertFalse(invalidConfig.isValid());
104 }
105
106 /**
107 * Tests vRouterMacs getter.
Charles Chan5270ed02016-01-30 23:22:37 -0800108 *
109 * @throws Exception
110 */
111 @Test
112 public void testVRouters() throws Exception {
Charles Chan5270ed02016-01-30 23:22:37 -0800113 Set<MacAddress> vRouters = config.vRouterMacs();
114 assertThat(vRouters.size(), is(2));
Charles Chanf2565a92016-02-10 20:46:58 -0800115 assertTrue(vRouters.contains(ROUTER_MAC_1));
116 assertTrue(vRouters.contains(ROUTER_MAC_2));
Charles Chan5270ed02016-01-30 23:22:37 -0800117 }
118
119 /**
Charles Chanf2565a92016-02-10 20:46:58 -0800120 * Tests vRouterMacs setter.
Charles Chan5270ed02016-01-30 23:22:37 -0800121 *
122 * @throws Exception
123 */
124 @Test
125 public void testSetVRouters() throws Exception {
126 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
Charles Chanf2565a92016-02-10 20:46:58 -0800127 builder.add(ROUTER_MAC_3);
Charles Chan5270ed02016-01-30 23:22:37 -0800128 config.setVRouterMacs(builder.build());
129
130 Set<MacAddress> macs = config.vRouterMacs();
131 assertThat(macs.size(), is(1));
Charles Chanf2565a92016-02-10 20:46:58 -0800132 assertTrue(macs.contains(ROUTER_MAC_3));
133 }
134
135 /**
136 * Tests vRouterId getter.
137 *
138 * @throws Exception
139 */
140 @Test
141 public void testVRouterId() throws Exception {
142 assertThat(config.vRouterId(), is(VROUTER_ID_1));
143 }
144
145 /**
146 * Tests vRouterId setter.
147 *
148 * @throws Exception
149 */
150 @Test
151 public void testSetVRouterId() throws Exception {
152 config.setVRouterId(VROUTER_ID_2);
153 assertThat(config.vRouterId(), is(VROUTER_ID_2));
154 }
155
156 /**
157 * Tests excludePort getter.
158 *
159 * @throws Exception
160 */
161 @Test
162 public void testExcludePorts() throws Exception {
163 Set<String> excludePorts = config.excludePorts();
164 assertThat(excludePorts.size(), is(2));
165 assertTrue(excludePorts.contains(PORT_NAME_1));
166 assertTrue(excludePorts.contains(PORT_NAME_2));
167 }
168
169 /**
170 * Tests excludePort setter.
171 *
172 * @throws Exception
173 */
174 @Test
175 public void testSetExcludePorts() throws Exception {
176 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
177 builder.add(PORT_NAME_3);
178 config.setExcludePorts(builder.build());
179
180 Set<String> excludePorts = config.excludePorts();
181 assertThat(excludePorts.size(), is(1));
182 assertTrue(excludePorts.contains(PORT_NAME_3));
Charles Chan5270ed02016-01-30 23:22:37 -0800183 }
184
185 private class MockDelegate implements ConfigApplyDelegate {
186 @Override
187 public void onApply(Config config) {
188 }
189 }
190}