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