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; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 44 | private Map<Integer, Set<Integer>> adjacencySids1; |
| 45 | private Map<Integer, Set<Integer>> adjacencySids2; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() throws Exception { |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 49 | InputStream jsonStream = SegmentRoutingDeviceConfigTest.class |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 50 | .getResourceAsStream("/device.json"); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 51 | |
| 52 | adjacencySids1 = new HashMap<>(); |
| 53 | Set<Integer> ports1 = new HashSet<>(); |
| 54 | ports1.add(2); |
| 55 | ports1.add(3); |
| 56 | adjacencySids1.put(100, ports1); |
| 57 | Set<Integer> ports2 = new HashSet<>(); |
| 58 | ports2.add(4); |
| 59 | ports2.add(5); |
| 60 | adjacencySids1.put(200, ports2); |
| 61 | |
| 62 | adjacencySids2 = new HashMap<>(); |
| 63 | Set<Integer> ports3 = new HashSet<>(); |
| 64 | ports3.add(6); |
| 65 | adjacencySids2.put(300, ports3); |
| 66 | |
| 67 | DeviceId subject = DeviceId.deviceId("of:0000000000000001"); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 68 | String key = "segmentrouting"; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 69 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 70 | JsonNode jsonNode = mapper.readTree(jsonStream); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 71 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 72 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 73 | config = new SegmentRoutingDeviceConfig(); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 74 | config.init(subject, key, jsonNode, mapper, delegate); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testName() throws Exception { |
| 79 | assertTrue(config.name().isPresent()); |
| 80 | assertThat(config.name().get(), is("Leaf-R1")); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testSetName() throws Exception { |
| 85 | config.setName("Spine-R1"); |
| 86 | assertTrue(config.name().isPresent()); |
| 87 | assertThat(config.name().get(), is("Spine-R1")); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testRouterIp() throws Exception { |
| 92 | assertThat(config.routerIp(), is(IpAddress.valueOf("10.0.1.254"))); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testSetRouterIp() throws Exception { |
| 97 | config.setRouterIp("10.0.2.254"); |
| 98 | assertThat(config.routerIp(), is(IpAddress.valueOf("10.0.2.254"))); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testRouterMac() throws Exception { |
| 103 | assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:01:80"))); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testSetRouterMac() throws Exception { |
| 108 | config.setRouterMac("00:00:00:00:02:80"); |
| 109 | assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:02:80"))); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testNodeSid() throws Exception { |
| 114 | assertThat(config.nodeSid(), is(101)); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testSetNodeSid() throws Exception { |
| 119 | config.setNodeSid(200); |
| 120 | assertThat(config.nodeSid(), is(200)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testIsEdgeRouter() throws Exception { |
| 125 | assertThat(config.isEdgeRouter(), is(true)); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testSetIsEdgeRouter() throws Exception { |
| 130 | config.setIsEdgeRouter(false); |
| 131 | assertThat(config.isEdgeRouter(), is(false)); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testAdjacencySids() throws Exception { |
| 136 | assertThat(config.adjacencySids(), is(adjacencySids1)); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testSetAdjacencySids() throws Exception { |
| 141 | config.setAdjacencySids(adjacencySids2); |
| 142 | assertThat(config.adjacencySids(), is(adjacencySids2)); |
| 143 | } |
| 144 | |
| 145 | private class MockDelegate implements ConfigApplyDelegate { |
| 146 | @Override |
| 147 | public void onApply(Config config) { |
| 148 | } |
| 149 | } |
| 150 | } |