blob: 7f165d833211222bae79f711f555eafcba5c9902 [file] [log] [blame]
Charles Chan531a78b2015-12-01 10:00:51 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
26import org.onosproject.net.config.Config;
27import org.onosproject.net.config.ConfigApplyDelegate;
28
Charles Chana35b02c2016-02-22 23:02:41 -080029import java.io.InputStream;
Charles Chan531a78b2015-12-01 10:00:51 -080030import java.util.HashMap;
31import java.util.HashSet;
32import java.util.Map;
33import java.util.Set;
34
35import static org.junit.Assert.assertThat;
36import static org.hamcrest.Matchers.is;
37import static org.junit.Assert.assertTrue;
38
39/**
Charles Chan5270ed02016-01-30 23:22:37 -080040 * Tests for class {@link SegmentRoutingDeviceConfig}.
Charles Chan531a78b2015-12-01 10:00:51 -080041 */
Charles Chan5270ed02016-01-30 23:22:37 -080042public class SegmentRoutingDeviceConfigTest {
43 private SegmentRoutingDeviceConfig config;
Pier Ventree0ae7a32016-11-23 09:57:42 -080044 private SegmentRoutingDeviceConfig ipv6Config;
Charles Chan531a78b2015-12-01 10:00:51 -080045 private Map<Integer, Set<Integer>> adjacencySids1;
46 private Map<Integer, Set<Integer>> adjacencySids2;
47
48 @Before
49 public void setUp() throws Exception {
Charles Chana35b02c2016-02-22 23:02:41 -080050 InputStream jsonStream = SegmentRoutingDeviceConfigTest.class
Charles Chanfc5c7802016-05-17 13:13:55 -070051 .getResourceAsStream("/device.json");
Pier Ventree0ae7a32016-11-23 09:57:42 -080052 InputStream ipv6JsonStream = SegmentRoutingDeviceConfigTest.class
53 .getResourceAsStream("/device-ipv6.json");
Charles Chan531a78b2015-12-01 10:00:51 -080054
55 adjacencySids1 = new HashMap<>();
56 Set<Integer> ports1 = new HashSet<>();
57 ports1.add(2);
58 ports1.add(3);
59 adjacencySids1.put(100, ports1);
60 Set<Integer> ports2 = new HashSet<>();
61 ports2.add(4);
62 ports2.add(5);
63 adjacencySids1.put(200, ports2);
64
65 adjacencySids2 = new HashMap<>();
66 Set<Integer> ports3 = new HashSet<>();
67 ports3.add(6);
68 adjacencySids2.put(300, ports3);
69
70 DeviceId subject = DeviceId.deviceId("of:0000000000000001");
Charles Chan5270ed02016-01-30 23:22:37 -080071 String key = "segmentrouting";
Charles Chan531a78b2015-12-01 10:00:51 -080072 ObjectMapper mapper = new ObjectMapper();
Charles Chana35b02c2016-02-22 23:02:41 -080073 JsonNode jsonNode = mapper.readTree(jsonStream);
Pier Ventree0ae7a32016-11-23 09:57:42 -080074 JsonNode ipv6JsonNode = mapper.readTree(ipv6JsonStream);
Charles Chan531a78b2015-12-01 10:00:51 -080075 ConfigApplyDelegate delegate = new MockDelegate();
76
Charles Chan5270ed02016-01-30 23:22:37 -080077 config = new SegmentRoutingDeviceConfig();
Charles Chan531a78b2015-12-01 10:00:51 -080078 config.init(subject, key, jsonNode, mapper, delegate);
Pier Ventree0ae7a32016-11-23 09:57:42 -080079
80 ipv6Config = new SegmentRoutingDeviceConfig();
81 ipv6Config.init(subject, key, ipv6JsonNode, mapper, delegate);
82 }
83
84 @Test
85 public void testIsValid() {
86 assertTrue(config.isValid());
87 assertTrue(ipv6Config.isValid());
Charles Chan531a78b2015-12-01 10:00:51 -080088 }
89
90 @Test
91 public void testName() throws Exception {
92 assertTrue(config.name().isPresent());
93 assertThat(config.name().get(), is("Leaf-R1"));
94 }
95
96 @Test
97 public void testSetName() throws Exception {
98 config.setName("Spine-R1");
99 assertTrue(config.name().isPresent());
100 assertThat(config.name().get(), is("Spine-R1"));
101 }
102
103 @Test
104 public void testRouterIp() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105 assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
106 assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.1.254")));
107 assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a8:0101")));
Charles Chan531a78b2015-12-01 10:00:51 -0800108 }
109
110 @Test
111 public void testSetRouterIp() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112 config.setRouterIpv4("10.0.2.254");
113 assertThat(config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
114 ipv6Config.setRouterIpv4("10.0.2.254");
115 assertThat(ipv6Config.routerIpv4(), is(IpAddress.valueOf("10.0.2.254")));
116 ipv6Config.setRouterIpv6("2000::c0a9:0101");
117 assertThat(ipv6Config.routerIpv6(), is(IpAddress.valueOf("2000::c0a9:0101")));
Charles Chan531a78b2015-12-01 10:00:51 -0800118 }
119
120 @Test
121 public void testRouterMac() throws Exception {
122 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:01:80")));
123 }
124
125 @Test
126 public void testSetRouterMac() throws Exception {
127 config.setRouterMac("00:00:00:00:02:80");
128 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:02:80")));
129 }
130
131 @Test
132 public void testNodeSid() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800133 assertThat(config.nodeSidIPv4(), is(101));
134 assertThat(ipv6Config.nodeSidIPv4(), is(101));
135 assertThat(ipv6Config.nodeSidIPv6(), is(111));
Charles Chan531a78b2015-12-01 10:00:51 -0800136 }
137
138 @Test
139 public void testSetNodeSid() throws Exception {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800140 config.setNodeSidIPv4(200);
141 assertThat(config.nodeSidIPv4(), is(200));
142 ipv6Config.setNodeSidIPv4(200);
143 assertThat(ipv6Config.nodeSidIPv4(), is(200));
144 ipv6Config.setNodeSidIPv6(201);
145 assertThat(ipv6Config.nodeSidIPv6(), is(201));
Charles Chan531a78b2015-12-01 10:00:51 -0800146 }
147
148 @Test
149 public void testIsEdgeRouter() throws Exception {
150 assertThat(config.isEdgeRouter(), is(true));
151 }
152
153 @Test
154 public void testSetIsEdgeRouter() throws Exception {
155 config.setIsEdgeRouter(false);
156 assertThat(config.isEdgeRouter(), is(false));
157 }
158
159 @Test
160 public void testAdjacencySids() throws Exception {
161 assertThat(config.adjacencySids(), is(adjacencySids1));
162 }
163
164 @Test
165 public void testSetAdjacencySids() throws Exception {
166 config.setAdjacencySids(adjacencySids2);
167 assertThat(config.adjacencySids(), is(adjacencySids2));
168 }
169
170 private class MockDelegate implements ConfigApplyDelegate {
171 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800172 public void onApply(Config configFile) {
Charles Chan531a78b2015-12-01 10:00:51 -0800173 }
174 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800175}