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 | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 58 | private static final String PROVIDER_1 = "org.onosproject.provider.host"; |
| 59 | private static final String PROVIDER_2 = "org.onosproject.netcfghost"; |
| 60 | private static final String PROVIDER_3 = "org.onosproject.anotherprovider"; |
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 |
| 70 | .getResourceAsStream("/sr-app-config.json"); |
| 71 | InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class |
| 72 | .getResourceAsStream("/sr-app-config-invalid.json"); |
| 73 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 74 | ApplicationId subject = APP_ID; |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 75 | String key = SegmentRoutingManager.SR_APP_ID; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 76 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | 636e347 | 2016-02-22 23:02:41 -0800 | [diff] [blame] | 77 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 78 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
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); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 88 | * Tests config validity. |
| 89 | * |
| 90 | * @throws Exception |
| 91 | */ |
| 92 | @Test |
| 93 | public void testIsValid() throws Exception { |
| 94 | assertTrue(config.isValid()); |
| 95 | assertFalse(invalidConfig.isValid()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests vRouterMacs getter. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 100 | * |
| 101 | * @throws Exception |
| 102 | */ |
| 103 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 104 | public void testVRouterMacs() throws Exception { |
| 105 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 106 | assertNotNull("vRouterMacs should not be null", vRouterMacs); |
| 107 | assertThat(vRouterMacs.size(), is(2)); |
| 108 | assertTrue(vRouterMacs.contains(ROUTER_MAC_1)); |
| 109 | assertTrue(vRouterMacs.contains(ROUTER_MAC_2)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 113 | * Tests vRouterMacs setter. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 114 | * |
| 115 | * @throws Exception |
| 116 | */ |
| 117 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 118 | public void testSetVRouterMacs() throws Exception { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 119 | ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder(); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 120 | builder.add(ROUTER_MAC_3); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 121 | config.setVRouterMacs(builder.build()); |
| 122 | |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 123 | Set<MacAddress> vRouterMacs = config.vRouterMacs(); |
| 124 | assertThat(vRouterMacs.size(), is(1)); |
| 125 | assertTrue(vRouterMacs.contains(ROUTER_MAC_3)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Tests vRouterId getter. |
| 130 | * |
| 131 | * @throws Exception |
| 132 | */ |
| 133 | @Test |
| 134 | public void testVRouterId() throws Exception { |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 135 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 136 | assertTrue(vRouterId.isPresent()); |
| 137 | assertThat(vRouterId.get(), is(VROUTER_ID_1)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests vRouterId setter. |
| 142 | * |
| 143 | * @throws Exception |
| 144 | */ |
| 145 | @Test |
| 146 | public void testSetVRouterId() throws Exception { |
| 147 | config.setVRouterId(VROUTER_ID_2); |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 148 | |
| 149 | Optional<DeviceId> vRouterId = config.vRouterId(); |
| 150 | assertTrue(vRouterId.isPresent()); |
| 151 | assertThat(vRouterId.get(), is(VROUTER_ID_2)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 155 | * Tests suppressSubnet getter. |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 156 | * |
| 157 | * @throws Exception |
| 158 | */ |
| 159 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 160 | public void testSuppressSubnet() throws Exception { |
| 161 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 162 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 163 | assertThat(suppressSubnet.size(), is(2)); |
| 164 | assertTrue(suppressSubnet.contains(PORT_1)); |
| 165 | assertTrue(suppressSubnet.contains(PORT_2)); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 169 | * Tests suppressSubnet setter. |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 170 | * |
| 171 | * @throws Exception |
| 172 | */ |
| 173 | @Test |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 174 | public void testSetSuppressSubnet() throws Exception { |
| 175 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 176 | builder.add(PORT_3); |
| 177 | config.setSuppressSubnet(builder.build()); |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 178 | |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 179 | Set<ConnectPoint> suppressSubnet = config.suppressSubnet(); |
| 180 | assertNotNull("suppressSubnet should not be null", suppressSubnet); |
| 181 | assertThat(suppressSubnet.size(), is(1)); |
| 182 | assertTrue(suppressSubnet.contains(PORT_3)); |
| 183 | } |
| 184 | |
| 185 | /** |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 186 | * Tests suppressHostByPort getter. |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 187 | * |
| 188 | * @throws Exception |
| 189 | */ |
| 190 | @Test |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 191 | public void testSuppressHostByPort() throws Exception { |
| 192 | Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort(); |
| 193 | assertNotNull("suppressHostByPort should not be null", suppressHostByPort); |
| 194 | assertThat(suppressHostByPort.size(), is(2)); |
| 195 | assertTrue(suppressHostByPort.contains(PORT_1)); |
| 196 | assertTrue(suppressHostByPort.contains(PORT_2)); |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 200 | * Tests suppressHostByPort setter. |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 201 | * |
| 202 | * @throws Exception |
| 203 | */ |
| 204 | @Test |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 205 | public void testSetSuppressHostByPort() throws Exception { |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 206 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 207 | builder.add(PORT_3); |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 208 | config.setSuppressHostByPort(builder.build()); |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 209 | |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 210 | Set<ConnectPoint> suppressHostByPort = config.suppressHostByPort(); |
| 211 | assertNotNull("suppressHostByPort should not be null", suppressHostByPort); |
| 212 | assertThat(suppressHostByPort.size(), is(1)); |
| 213 | assertTrue(suppressHostByPort.contains(PORT_3)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 216 | /** |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 217 | * Tests suppressHostByProvider getter. |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 218 | * |
| 219 | * @throws Exception |
| 220 | */ |
| 221 | @Test |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 222 | public void testSuppressHostByProvider() throws Exception { |
| 223 | Set<String> supprsuppressHostByProvider = config.suppressHostByProvider(); |
| 224 | assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider); |
| 225 | assertThat(supprsuppressHostByProvider.size(), is(2)); |
| 226 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_1)); |
| 227 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_2)); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 231 | * Tests suppressHostByProvider setter. |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 232 | * |
| 233 | * @throws Exception |
| 234 | */ |
| 235 | @Test |
| 236 | public void testSetHostLearning() throws Exception { |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame^] | 237 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 238 | builder.add(PROVIDER_3); |
| 239 | config.setSuppressHostByProvider(builder.build()); |
| 240 | |
| 241 | Set<String> supprsuppressHostByProvider = config.suppressHostByProvider(); |
| 242 | assertNotNull("suppressHostByProvider should not be null", supprsuppressHostByProvider); |
| 243 | assertThat(supprsuppressHostByProvider.size(), is(1)); |
| 244 | assertTrue(supprsuppressHostByProvider.contains(PROVIDER_3)); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 247 | private class MockDelegate implements ConfigApplyDelegate { |
| 248 | @Override |
| 249 | public void onApply(Config config) { |
| 250 | } |
| 251 | } |
| 252 | } |