Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Charles Chan | 531a78b | 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; |
| 26 | import org.onosproject.net.config.Config; |
| 27 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 28 | |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 29 | import java.io.InputStream; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static org.junit.Assert.assertThat; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.junit.Assert.assertTrue; |
| 38 | |
| 39 | /** |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 40 | * Tests for class {@link SegmentRoutingDeviceConfig}. |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 41 | */ |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 42 | public class SegmentRoutingDeviceConfigTest { |
| 43 | private SegmentRoutingDeviceConfig config; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 44 | private SegmentRoutingDeviceConfig ipv6Config; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 45 | private Map<Integer, Set<Integer>> adjacencySids1; |
| 46 | private Map<Integer, Set<Integer>> adjacencySids2; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 50 | InputStream jsonStream = SegmentRoutingDeviceConfigTest.class |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 51 | .getResourceAsStream("/device.json"); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 52 | InputStream ipv6JsonStream = SegmentRoutingDeviceConfigTest.class |
| 53 | .getResourceAsStream("/device-ipv6.json"); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 54 | |
| 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 Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 71 | String key = "segmentrouting"; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 72 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 73 | JsonNode jsonNode = mapper.readTree(jsonStream); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 74 | JsonNode ipv6JsonNode = mapper.readTree(ipv6JsonStream); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 75 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 76 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 77 | config = new SegmentRoutingDeviceConfig(); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 78 | config.init(subject, key, jsonNode, mapper, delegate); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 79 | |
| 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 Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 88 | } |
| 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 Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 105 | 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 Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testSetRouterIp() throws Exception { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 112 | 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 Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 118 | } |
| 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 Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 133 | assertThat(config.nodeSidIPv4(), is(101)); |
| 134 | assertThat(ipv6Config.nodeSidIPv4(), is(101)); |
| 135 | assertThat(ipv6Config.nodeSidIPv6(), is(111)); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testSetNodeSid() throws Exception { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 140 | 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 Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 146 | } |
| 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 Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 172 | public void onApply(Config configFile) { |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 175 | } |