Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -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 | 5270ed0 | 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 | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Charles Chan | 5270ed0 | 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 | 116188d | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 31 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 32 | |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 33 | import java.io.InputStream; |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 34 | import java.util.Optional; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | |
| 37 | import static org.hamcrest.Matchers.is; |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 38 | import static org.junit.Assert.*; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Tests for class {@link SegmentRoutingAppConfig}. |
| 42 | */ |
| 43 | public class SegmentRoutingAppConfigTest { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 44 | private SegmentRoutingAppConfig config; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 45 | private SegmentRoutingAppConfig invalidConfig; |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 46 | private SegmentRoutingAppConfig mplsEcmpConfig; |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 47 | |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 48 | private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 49 | private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 50 | private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03"); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 51 | private static final ConnectPoint PORT_1 = ConnectPoint.deviceConnectPoint("of:1/1"); |
| 52 | private static final ConnectPoint PORT_2 = ConnectPoint.deviceConnectPoint("of:1/2"); |
| 53 | private static final ConnectPoint PORT_3 = ConnectPoint.deviceConnectPoint("of:1/3"); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 54 | private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1"); |
| 55 | private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2"); |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 56 | private static final String PROVIDER_1 = "org.onosproject.provider.host"; |
| 57 | private static final String PROVIDER_2 = "org.onosproject.netcfghost"; |
| 58 | private static final String PROVIDER_3 = "org.onosproject.anotherprovider"; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Initialize test related variables. |
| 62 | * |
| 63 | * @throws Exception |
| 64 | */ |
| 65 | @Before |
| 66 | public void setUp() throws Exception { |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 67 | InputStream jsonStream = SegmentRoutingAppConfigTest.class |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 68 | .getResourceAsStream("/app.json"); |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 69 | InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 70 | .getResourceAsStream("/app-invalid.json"); |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 71 | InputStream mplsEcmpJsonStream = SegmentRoutingAppConfigTest.class |
| 72 | .getResourceAsStream("/app-ecmp.json"); |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 73 | |
Charles Chan | 2c15aca | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 74 | String key = SegmentRoutingManager.APP_NAME; |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 75 | ApplicationId subject = new TestApplicationId(key); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 76 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | a35b02c | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 77 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 78 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 79 | JsonNode mplsEcmpJsonNode = mapper.readTree(mplsEcmpJsonStream); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 80 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 81 | |
| 82 | config = new SegmentRoutingAppConfig(); |
| 83 | config.init(subject, key, jsonNode, mapper, delegate); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 84 | invalidConfig = new SegmentRoutingAppConfig(); |
| 85 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 86 | mplsEcmpConfig = new SegmentRoutingAppConfig(); |
| 87 | mplsEcmpConfig.init(subject, key, mplsEcmpJsonNode, mapper, delegate); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 91 | * Tests config validity. |
| 92 | * |
| 93 | * @throws Exception |
| 94 | */ |
| 95 | @Test |
| 96 | public void testIsValid() throws Exception { |
| 97 | assertTrue(config.isValid()); |
| 98 | assertFalse(invalidConfig.isValid()); |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 99 | assertTrue(mplsEcmpConfig.isValid()); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Test MPLS-ECMP default getter. By-default |
| 104 | * MPLS-ECMPS is false. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testDefaultMplsEcmp() { |
| 108 | boolean mplsEcmp = config.mplsEcmp(); |
| 109 | assertThat(mplsEcmp, is(false)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Test MPLS-ECMP getter. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testMplsEcmp() { |
| 117 | boolean mplsEcmp = mplsEcmpConfig.mplsEcmp(); |
| 118 | assertThat(mplsEcmp, is(true)); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Test MPLS-ECMP setter. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testSetMplsEcmp() { |
| 126 | /* |
| 127 | * In the config the value is not set. |
| 128 | */ |
| 129 | boolean mplsEcmp = config.mplsEcmp(); |
| 130 | assertThat(mplsEcmp, is(false)); |
| 131 | config.setMplsEcmp(true); |
| 132 | mplsEcmp = config.mplsEcmp(); |
| 133 | assertThat(mplsEcmp, is(true)); |
| 134 | /* |
| 135 | * In the mplsEcmpConfig the value is true, |
| 136 | */ |
| 137 | mplsEcmp = mplsEcmpConfig.mplsEcmp(); |
| 138 | assertThat(mplsEcmp, is(true)); |
| 139 | config.setMplsEcmp(false); |
| 140 | mplsEcmp = config.mplsEcmp(); |
| 141 | assertThat(mplsEcmp, is(false)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests vRouterMacs getter. |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 146 | * |
| 147 | * @throws Exception |
| 148 | */ |
| 149 | @Test |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 150 | public void testVRouterMacs() throws Exception { |
| 151 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 152 | assertNotNull("vRouterMacs should not be null", vRouterMacs); |
| 153 | assertThat(vRouterMacs.size(), is(2)); |
| 154 | assertTrue(vRouterMacs.contains(ROUTER_MAC_1)); |
| 155 | assertTrue(vRouterMacs.contains(ROUTER_MAC_2)); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 159 | * Tests vRouterMacs setter. |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 160 | * |
| 161 | * @throws Exception |
| 162 | */ |
| 163 | @Test |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 164 | public void testSetVRouterMacs() throws Exception { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 165 | ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder(); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 166 | builder.add(ROUTER_MAC_3); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 167 | config.setVRouterMacs(builder.build()); |
| 168 | |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 169 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 170 | assertThat(vRouterMacs.size(), is(1)); |
| 171 | assertTrue(vRouterMacs.contains(ROUTER_MAC_3)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests vRouterId getter. |
| 176 | * |
| 177 | * @throws Exception |
| 178 | */ |
| 179 | @Test |
| 180 | public void testVRouterId() throws Exception { |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 181 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 182 | assertTrue(vRouterId.isPresent()); |
| 183 | assertThat(vRouterId.get(), is(VROUTER_ID_1)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Tests vRouterId setter. |
| 188 | * |
| 189 | * @throws Exception |
| 190 | */ |
| 191 | @Test |
| 192 | public void testSetVRouterId() throws Exception { |
| 193 | config.setVRouterId(VROUTER_ID_2); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 194 | |
| 195 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 196 | assertTrue(vRouterId.isPresent()); |
| 197 | assertThat(vRouterId.get(), is(VROUTER_ID_2)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 201 | * Tests suppressSubnet getter. |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 202 | * |
| 203 | * @throws Exception |
| 204 | */ |
| 205 | @Test |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 206 | public void testSuppressSubnet() throws Exception { |
| 207 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 208 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 209 | assertThat(suppressSubnet.size(), is(2)); |
| 210 | assertTrue(suppressSubnet.contains(PORT_1)); |
| 211 | assertTrue(suppressSubnet.contains(PORT_2)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 215 | * Tests suppressSubnet setter. |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 216 | * |
| 217 | * @throws Exception |
| 218 | */ |
| 219 | @Test |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 220 | public void testSetSuppressSubnet() throws Exception { |
| 221 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 222 | builder.add(PORT_3); |
| 223 | config.setSuppressSubnet(builder.build()); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 224 | |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 225 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 226 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 227 | assertThat(suppressSubnet.size(), is(1)); |
| 228 | assertTrue(suppressSubnet.contains(PORT_3)); |
| 229 | } |
| 230 | |
| 231 | /** |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 232 | * Tests suppressHostByPort getter. |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 233 | * |
| 234 | * @throws Exception |
| 235 | */ |
| 236 | @Test |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 237 | public void testSuppressHostByPort() throws Exception { |
| 238 | Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort(); |
| 239 | assertNotNull("suppressHostByPort should not be null", suppressHostByPort); |
| 240 | assertThat(suppressHostByPort.size(), is(2)); |
| 241 | assertTrue(suppressHostByPort.contains(PORT_1)); |
| 242 | assertTrue(suppressHostByPort.contains(PORT_2)); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 246 | * Tests suppressHostByPort setter. |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 247 | * |
| 248 | * @throws Exception |
| 249 | */ |
| 250 | @Test |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 251 | public void testSetSuppressHostByPort() throws Exception { |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 252 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 253 | builder.add(PORT_3); |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 254 | config.setSuppressHostByPort(builder.build()); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 255 | |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 256 | Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort(); |
| 257 | assertNotNull("suppressHostByPort should not be null", suppressHostByPort); |
| 258 | assertThat(suppressHostByPort.size(), is(1)); |
| 259 | assertTrue(suppressHostByPort.contains(PORT_3)); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 262 | /** |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 263 | * Tests suppressHostByProvider getter. |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 264 | * |
| 265 | * @throws Exception |
| 266 | */ |
| 267 | @Test |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 268 | public void testSuppressHostByProvider() throws Exception { |
| 269 | Set<String> supprsuppressHostByProvider = config.suppressHostByProvider(); |
| 270 | assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider); |
| 271 | assertThat(supprsuppressHostByProvider.size(), is(2)); |
| 272 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1)); |
| 273 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2)); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 277 | * Tests suppressHostByProvider setter. |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 278 | * |
| 279 | * @throws Exception |
| 280 | */ |
| 281 | @Test |
| 282 | public void testSetHostLearning() throws Exception { |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 283 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 284 | builder.add(PROVIDER_3); |
| 285 | config.setSuppressHostByProvider(builder.build()); |
| 286 | |
| 287 | Set<String> supprsuppressHostByProvider = config.suppressHostByProvider(); |
| 288 | assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider); |
| 289 | assertThat(supprsuppressHostByProvider.size(), is(1)); |
| 290 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3)); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 293 | private class MockDelegate implements ConfigApplyDelegate { |
| 294 | @Override |
| 295 | public void onApply(Config config) { |
| 296 | } |
| 297 | } |
Pier Ventre | 9816178 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 298 | } |