Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -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 com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.TestApplicationId; |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 29 | import org.onosproject.net.config.Config; |
| 30 | import org.onosproject.net.config.ConfigApplyDelegate; |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 31 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 32 | |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 33 | import java.io.InputStream; |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 34 | import java.util.Optional; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | |
| 37 | import static org.hamcrest.Matchers.is; |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 38 | import static org.junit.Assert.*; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Tests for class {@link SegmentRoutingAppConfig}. |
| 42 | */ |
| 43 | public class SegmentRoutingAppConfigTest { |
| 44 | private static final ApplicationId APP_ID = |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 45 | new TestApplicationId(SegmentRoutingManager.SR_APP_ID); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 46 | |
| 47 | private SegmentRoutingAppConfig config; |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 48 | private SegmentRoutingAppConfig invalidConfig; |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 49 | |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 50 | private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 51 | private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 52 | private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03"); |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 53 | private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1"); |
| 54 | private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2"); |
| 55 | private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3"); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 56 | private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1"); |
| 57 | private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Initialize test related variables. |
| 61 | * |
| 62 | * @throws Exception |
| 63 | */ |
| 64 | @Before |
| 65 | public void setUp() throws Exception { |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 66 | InputStream jsonStream = SegmentRoutingAppConfigTest.class |
| 67 | .getResourceAsStream("/sr-app-config.json"); |
| 68 | InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class |
| 69 | .getResourceAsStream("/sr-app-config-invalid.json"); |
| 70 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 71 | ApplicationId subject = APP_ID; |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 72 | String key = SegmentRoutingManager.SR_APP_ID; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 73 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 74 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 75 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 76 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 77 | |
| 78 | config = new SegmentRoutingAppConfig(); |
| 79 | config.init(subject, key, jsonNode, mapper, delegate); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 80 | invalidConfig = new SegmentRoutingAppConfig(); |
| 81 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 85 | * Tests config validity. |
| 86 | * |
| 87 | * @throws Exception |
| 88 | */ |
| 89 | @Test |
| 90 | public void testIsValid() throws Exception { |
| 91 | assertTrue(config.isValid()); |
| 92 | assertFalse(invalidConfig.isValid()); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests vRouterMacs getter. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 97 | * |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 101 | public void testVRouterMacs() throws Exception { |
| 102 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 103 | assertNotNull("vRouterMacs should not be null", vRouterMacs); |
| 104 | assertThat(vRouterMacs.size(), is(2)); |
| 105 | assertTrue(vRouterMacs.contains(ROUTER_MAC_1)); |
| 106 | assertTrue(vRouterMacs.contains(ROUTER_MAC_2)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 110 | * Tests vRouterMacs setter. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 111 | * |
| 112 | * @throws Exception |
| 113 | */ |
| 114 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 115 | public void testSetVRouterMacs() throws Exception { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 116 | ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder(); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 117 | builder.add(ROUTER_MAC_3); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 118 | config.setVRouterMacs(builder.build()); |
| 119 | |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 120 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 121 | assertThat(vRouterMacs.size(), is(1)); |
| 122 | assertTrue(vRouterMacs.contains(ROUTER_MAC_3)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests vRouterId getter. |
| 127 | * |
| 128 | * @throws Exception |
| 129 | */ |
| 130 | @Test |
| 131 | public void testVRouterId() throws Exception { |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 132 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 133 | assertTrue(vRouterId.isPresent()); |
| 134 | assertThat(vRouterId.get(), is(VROUTER_ID_1)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Tests vRouterId setter. |
| 139 | * |
| 140 | * @throws Exception |
| 141 | */ |
| 142 | @Test |
| 143 | public void testSetVRouterId() throws Exception { |
| 144 | config.setVRouterId(VROUTER_ID_2); |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 145 | |
| 146 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 147 | assertTrue(vRouterId.isPresent()); |
| 148 | assertThat(vRouterId.get(), is(VROUTER_ID_2)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 152 | * Tests suppressSubnet getter. |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 153 | * |
| 154 | * @throws Exception |
| 155 | */ |
| 156 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 157 | public void testSuppressSubnet() throws Exception { |
| 158 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 159 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 160 | assertThat(suppressSubnet.size(), is(2)); |
| 161 | assertTrue(suppressSubnet.contains(PORT_1)); |
| 162 | assertTrue(suppressSubnet.contains(PORT_2)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 166 | * Tests suppressSubnet setter. |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 167 | * |
| 168 | * @throws Exception |
| 169 | */ |
| 170 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 171 | public void testSetSuppressSubnet() throws Exception { |
| 172 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 173 | builder.add(PORT_3); |
| 174 | config.setSuppressSubnet(builder.build()); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 175 | |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 176 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 177 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 178 | assertThat(suppressSubnet.size(), is(1)); |
| 179 | assertTrue(suppressSubnet.contains(PORT_3)); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Tests suppressHost getter. |
| 184 | * |
| 185 | * @throws Exception |
| 186 | */ |
| 187 | @Test |
| 188 | public void testSuppressHost() throws Exception { |
| 189 | Set<ConnectPoint> suppressHost = config.suppressHost(); |
| 190 | assertNotNull("suppressHost should not be null", suppressHost); |
| 191 | assertThat(suppressHost.size(), is(2)); |
| 192 | assertTrue(suppressHost.contains(PORT_1)); |
| 193 | assertTrue(suppressHost.contains(PORT_2)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests suppressHost setter. |
| 198 | * |
| 199 | * @throws Exception |
| 200 | */ |
| 201 | @Test |
| 202 | public void testSetSuppressHost() throws Exception { |
| 203 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 204 | builder.add(PORT_3); |
| 205 | config.setSuppressHost(builder.build()); |
| 206 | |
| 207 | Set<ConnectPoint> suppressHost = config.suppressHost(); |
| 208 | assertNotNull("suppressHost should not be null", suppressHost); |
| 209 | assertThat(suppressHost.size(), is(1)); |
| 210 | assertTrue(suppressHost.contains(PORT_3)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame^] | 213 | /** |
| 214 | * Tests hostLearning getter. |
| 215 | * |
| 216 | * @throws Exception |
| 217 | */ |
| 218 | @Test |
| 219 | public void testHostLearning() throws Exception { |
| 220 | assertFalse(config.hostLearning()); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests hostLearning setter. |
| 225 | * |
| 226 | * @throws Exception |
| 227 | */ |
| 228 | @Test |
| 229 | public void testSetHostLearning() throws Exception { |
| 230 | config.setHostLearning(true); |
| 231 | assertTrue(config.hostLearning()); |
| 232 | } |
| 233 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 234 | private class MockDelegate implements ConfigApplyDelegate { |
| 235 | @Override |
| 236 | public void onApply(Config config) { |
| 237 | } |
| 238 | } |
| 239 | } |