Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.net.DeviceId; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 29 | |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 30 | import java.io.InputStream; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
| 35 | |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertFalse; |
| 37 | import static org.junit.Assert.assertNull; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 38 | import static org.junit.Assert.assertThat; |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.junit.Assert.assertTrue; |
| 41 | |
| 42 | /** |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 43 | * Tests for class {@link SegmentRoutingDeviceConfig}. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 44 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 45 | public class SegmentRoutingDeviceConfigTest { |
| 46 | private SegmentRoutingDeviceConfig config; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 47 | private SegmentRoutingDeviceConfig ipv6Config; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 48 | private SegmentRoutingDeviceConfig pairConfig; |
| 49 | private SegmentRoutingDeviceConfig invalidConfig; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 50 | private Map<Integer, Set<Integer>> adjacencySids1; |
| 51 | private Map<Integer, Set<Integer>> adjacencySids2; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 52 | private static final DeviceId PAIR_DEVICE_ID = DeviceId.deviceId("of:123456789ABCDEF0"); |
| 53 | private static final PortNumber PAIR_LOCAL_PORT = PortNumber.portNumber(10); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 57 | InputStream jsonStream = SegmentRoutingDeviceConfigTest.class |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 58 | .getResourceAsStream("/device.json"); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 59 | InputStream ipv6JsonStream = SegmentRoutingDeviceConfigTest.class |
| 60 | .getResourceAsStream("/device-ipv6.json"); |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 61 | InputStream pairJsonStream = SegmentRoutingDeviceConfigTest.class |
| 62 | .getResourceAsStream("/device-pair.json"); |
| 63 | InputStream invalidJsonStream = SegmentRoutingDeviceConfigTest.class |
| 64 | .getResourceAsStream("/device-invalid.json"); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 65 | |
| 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 Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 82 | String key = "segmentrouting"; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 83 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 84 | JsonNode jsonNode = mapper.readTree(jsonStream); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 85 | JsonNode ipv6JsonNode = mapper.readTree(ipv6JsonStream); |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 86 | JsonNode pairJsonNode = mapper.readTree(pairJsonStream); |
| 87 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 88 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 89 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 90 | config = new SegmentRoutingDeviceConfig(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 91 | config.init(subject, key, jsonNode, mapper, delegate); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 92 | |
| 93 | ipv6Config = new SegmentRoutingDeviceConfig(); |
| 94 | ipv6Config.init(subject, key, ipv6JsonNode, mapper, delegate); |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 95 | |
| 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 Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testIsValid() { |
| 105 | assertTrue(config.isValid()); |
| 106 | assertTrue(ipv6Config.isValid()); |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 107 | assertTrue(pairConfig.isValid()); |
| 108 | assertFalse(invalidConfig.isValid()); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 109 | } |
| 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 Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 126 | 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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testSetRouterIp() throws Exception { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 133 | 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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 139 | } |
| 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 Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 154 | assertThat(config.nodeSidIPv4(), is(101)); |
| 155 | assertThat(ipv6Config.nodeSidIPv4(), is(101)); |
| 156 | assertThat(ipv6Config.nodeSidIPv6(), is(111)); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testSetNodeSid() throws Exception { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 161 | 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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 167 | } |
| 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 Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 191 | @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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 217 | private class MockDelegate implements ConfigApplyDelegate { |
| 218 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 219 | public void onApply(Config configFile) { |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 222 | } |