blob: ddf90bbdec22efdffba20d691999c5bf7915cbb9 [file] [log] [blame]
Charles Chan531a78b2015-12-01 10:00:51 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan531a78b2015-12-01 10:00:51 -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 org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onosproject.net.DeviceId;
Charles Chan537fe852017-06-07 17:26:01 -070026import org.onosproject.net.PortNumber;
Charles Chan531a78b2015-12-01 10:00:51 -080027import org.onosproject.net.config.Config;
28import org.onosproject.net.config.ConfigApplyDelegate;
29
Charles Chana35b02c2016-02-22 23:02:41 -080030import java.io.InputStream;
Charles Chan531a78b2015-12-01 10:00:51 -080031import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Map;
34import java.util.Set;
35
Charles Chan537fe852017-06-07 17:26:01 -070036import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNull;
Charles Chan531a78b2015-12-01 10:00:51 -080038import static org.junit.Assert.assertThat;
39import static org.hamcrest.Matchers.is;
40import static org.junit.Assert.assertTrue;
41
42/**
Charles Chan5270ed02016-01-30 23:22:37 -080043 * Tests for class {@link SegmentRoutingDeviceConfig}.
Charles Chan531a78b2015-12-01 10:00:51 -080044 */
Charles Chan5270ed02016-01-30 23:22:37 -080045public class SegmentRoutingDeviceConfigTest {
46 private SegmentRoutingDeviceConfig config;
Pier Ventree0ae7a32016-11-23 09:57:42 -080047 private SegmentRoutingDeviceConfig ipv6Config;
Charles Chan537fe852017-06-07 17:26:01 -070048 private SegmentRoutingDeviceConfig pairConfig;
49 private SegmentRoutingDeviceConfig invalidConfig;
Charles Chan531a78b2015-12-01 10:00:51 -080050 private Map<Integer, Set<Integer>> adjacencySids1;
51 private Map<Integer, Set<Integer>> adjacencySids2;
Charles Chan537fe852017-06-07 17:26:01 -070052 private static final DeviceId PAIR_DEVICE_ID = DeviceId.deviceId("of:123456789ABCDEF0");
53 private static final PortNumber PAIR_LOCAL_PORT = PortNumber.portNumber(10);
Charles Chan531a78b2015-12-01 10:00:51 -080054
55 @Before
56 public void setUp() throws Exception {
Charles Chana35b02c2016-02-22 23:02:41 -080057 InputStream jsonStream = SegmentRoutingDeviceConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070058 .getResourceAsStream("/device.json");
Pier Ventree0ae7a32016-11-23 09:57:42 -080059 InputStream ipv6JsonStream = SegmentRoutingDeviceConfigTest.class
60 .getResourceAsStream("/device-ipv6.json");
Charles Chan537fe852017-06-07 17:26:01 -070061 InputStream pairJsonStream = SegmentRoutingDeviceConfigTest.class
62 .getResourceAsStream("/device-pair.json");
63 InputStream invalidJsonStream = SegmentRoutingDeviceConfigTest.class
64 .getResourceAsStream("/device-invalid.json");
Charles Chan531a78b2015-12-01 10:00:51 -080065
66 adjacencySids1 = new HashMap<>();
67 Set<Integer> ports1 = new HashSet<>();
68 ports1.add(2);
69 ports1.add(3);
70 adjacencySids1.put(100, ports1);
71 Set<Integer> ports2 = new HashSet<>();
72 ports2.add(4);
73 ports2.add(5);
74 adjacencySids1.put(200, ports2);
75
76 adjacencySids2 = new HashMap<>();
77 Set<Integer> ports3 = new HashSet<>();
78 ports3.add(6);
79 adjacencySids2.put(300, ports3);
80
81 DeviceId subject = DeviceId.deviceId("of:0000000000000001");
Charles Chan5270ed02016-01-30 23:22:37 -080082 String key = "segmentrouting";
Charles Chan531a78b2015-12-01 10:00:51 -080083 ObjectMapper mapper = new ObjectMapper();
Charles Chana35b02c2016-02-22 23:02:41 -080084 JsonNode jsonNode = mapper.readTree(jsonStream);
Pier Ventree0ae7a32016-11-23 09:57:42 -080085 JsonNode ipv6JsonNode = mapper.readTree(ipv6JsonStream);
Charles Chan537fe852017-06-07 17:26:01 -070086 JsonNode pairJsonNode = mapper.readTree(pairJsonStream);
87 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
Charles Chan531a78b2015-12-01 10:00:51 -080088 ConfigApplyDelegate delegate = new MockDelegate();
89
Charles Chan5270ed02016-01-30 23:22:37 -080090 config = new SegmentRoutingDeviceConfig();
Charles Chan531a78b2015-12-01 10:00:51 -080091 config.init(subject, key, jsonNode, mapper, delegate);
Pier Ventree0ae7a32016-11-23 09:57:42 -080092
93 ipv6Config = new SegmentRoutingDeviceConfig();
94 ipv6Config.init(subject, key, ipv6JsonNode, mapper, delegate);
Charles Chan537fe852017-06-07 17:26:01 -070095
96 pairConfig = new SegmentRoutingDeviceConfig();
97 pairConfig.init(subject, key, pairJsonNode, mapper, delegate);
98
99 invalidConfig = new SegmentRoutingDeviceConfig();
100 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800101 }
102
103 @Test
104 public void testIsValid() {
105 assertTrue(config.isValid());
106 assertTrue(ipv6Config.isValid());
Charles Chan537fe852017-06-07 17:26:01 -0700107 assertTrue(pairConfig.isValid());
108 assertFalse(invalidConfig.isValid());
Charles Chan531a78b2015-12-01 10:00:51 -0800109 }
110
111 @Test
112 public void testName() throws Exception {
113 assertTrue(config.name().isPresent());
114 assertThat(config.name().get(), is("Leaf-R1"));
115 }
116
117 @Test
118 public void testSetName() throws Exception {
119 config.setName("Spine-R1");
120 assertTrue(config.name().isPresent());
121 assertThat(config.name().get(), is("Spine-R1"));
122 }
123
124 @Test
125 public void testRouterIp() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800126 assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
127 assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
128 assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a8:0101")));
Charles Chan531a78b2015-12-01 10:00:51 -0800129 }
130
131 @Test
132 public void testSetRouterIp() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800133 config.setRouterIpv4("10.0.2.254");
134 assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
135 ipv6Config.setRouterIpv4("10.0.2.254");
136 assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
137 ipv6Config.setRouterIpv6("2000::c0a9:0101");
138 assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a9:0101")));
Charles Chan531a78b2015-12-01 10:00:51 -0800139 }
140
141 @Test
142 public void testRouterMac() throws Exception {
143 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:01:80")));
144 }
145
146 @Test
147 public void testSetRouterMac() throws Exception {
148 config.setRouterMac("00:00:00:00:02:80");
149 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:02:80")));
150 }
151
152 @Test
153 public void testNodeSid() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800154 assertThat(config.nodeSidIPv4(), is(101));
155 assertThat(ipv6Config.nodeSidIPv4(), is(101));
156 assertThat(ipv6Config.nodeSidIPv6(), is(111));
Charles Chan531a78b2015-12-01 10:00:51 -0800157 }
158
159 @Test
160 public void testSetNodeSid() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800161 config.setNodeSidIPv4(200);
162 assertThat(config.nodeSidIPv4(), is(200));
163 ipv6Config.setNodeSidIPv4(200);
164 assertThat(ipv6Config.nodeSidIPv4(), is(200));
165 ipv6Config.setNodeSidIPv6(201);
166 assertThat(ipv6Config.nodeSidIPv6(), is(201));
Charles Chan531a78b2015-12-01 10:00:51 -0800167 }
168
169 @Test
170 public void testIsEdgeRouter() throws Exception {
171 assertThat(config.isEdgeRouter(), is(true));
172 }
173
174 @Test
175 public void testSetIsEdgeRouter() throws Exception {
176 config.setIsEdgeRouter(false);
177 assertThat(config.isEdgeRouter(), is(false));
178 }
179
180 @Test
181 public void testAdjacencySids() throws Exception {
182 assertThat(config.adjacencySids(), is(adjacencySids1));
183 }
184
185 @Test
186 public void testSetAdjacencySids() throws Exception {
187 config.setAdjacencySids(adjacencySids2);
188 assertThat(config.adjacencySids(), is(adjacencySids2));
189 }
190
Charles Chan537fe852017-06-07 17:26:01 -0700191 @Test
192 public void testPairDeviceId() throws Exception {
193 assertNull(config.pairDeviceId());
194 assertNull(ipv6Config.pairDeviceId());
195 assertThat(pairConfig.pairDeviceId(), is(PAIR_DEVICE_ID));
196 }
197
198 @Test
199 public void testSetPairDeviceId() throws Exception {
200 config.setPairDeviceId(PAIR_DEVICE_ID);
201 assertThat(config.pairDeviceId(), is(PAIR_DEVICE_ID));
202 }
203
204 @Test
205 public void testPairLocalPort() throws Exception {
206 assertNull(config.pairLocalPort());
207 assertNull(ipv6Config.pairLocalPort());
208 assertThat(pairConfig.pairLocalPort(), is(PAIR_LOCAL_PORT));
209 }
210
211 @Test
212 public void testSetPairLocalPort() throws Exception {
213 config.setPairLocalPort(PAIR_LOCAL_PORT);
214 assertThat(config.pairLocalPort(), is(PAIR_LOCAL_PORT));
215 }
216
Charles Chan531a78b2015-12-01 10:00:51 -0800217 private class MockDelegate implements ConfigApplyDelegate {
218 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800219 public void onApply(Config configFile) {
Charles Chan531a78b2015-12-01 10:00:51 -0800220 }
221 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800222}