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