Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | package org.onosproject.inbandtelemetry.impl; |
| 17 | |
| 18 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.collect.Sets; |
pierventre | 52ef933 | 2021-07-09 22:42:17 +0200 | [diff] [blame^] | 25 | import com.google.common.util.concurrent.MoreExecutors; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 26 | import org.easymock.Capture; |
| 27 | import org.easymock.EasyMock; |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 31 | import org.onlab.junit.TestUtils; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 34 | import org.onlab.packet.TpPort; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 35 | import org.onlab.packet.VlanId; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 36 | import org.onosproject.TestApplicationId; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 39 | import org.onosproject.inbandtelemetry.api.IntIntent; |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 40 | import org.onosproject.inbandtelemetry.api.IntIntentId; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 41 | import org.onosproject.mastership.MastershipService; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DefaultDevice; |
| 44 | import org.onosproject.net.DefaultHost; |
| 45 | import org.onosproject.net.DefaultPort; |
| 46 | import org.onosproject.net.Device; |
| 47 | import org.onosproject.net.DeviceId; |
| 48 | import org.onosproject.net.Host; |
| 49 | import org.onosproject.net.HostId; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.PortNumber; |
| 52 | import org.onosproject.net.behaviour.inbandtelemetry.IntObjective; |
| 53 | import org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 54 | import org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig; |
| 55 | import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 56 | import org.onosproject.net.config.NetworkConfigEvent; |
| 57 | import org.onosproject.net.config.NetworkConfigListener; |
| 58 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 59 | import org.onosproject.net.config.NetworkConfigService; |
| 60 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 62 | import org.onosproject.net.flow.TrafficSelector; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 63 | import org.onosproject.net.host.HostService; |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 64 | import org.onosproject.store.service.ConsistentMap; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 65 | import org.onosproject.store.service.StorageService; |
| 66 | import org.onosproject.store.service.TestStorageService; |
| 67 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 68 | import java.io.IOException; |
| 69 | import java.io.InputStream; |
| 70 | import java.util.List; |
| 71 | import java.util.Map; |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 72 | import java.util.Set; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 73 | |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 74 | import static org.easymock.EasyMock.anyObject; |
| 75 | import static org.easymock.EasyMock.createNiceMock; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 76 | import static org.easymock.EasyMock.eq; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 77 | import static org.easymock.EasyMock.expect; |
| 78 | import static org.easymock.EasyMock.expectLastCall; |
| 79 | import static org.easymock.EasyMock.newCapture; |
| 80 | import static org.easymock.EasyMock.replay; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 81 | import static org.easymock.EasyMock.reset; |
| 82 | import static org.easymock.EasyMock.verify; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 83 | import static org.junit.Assert.assertEquals; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 84 | import static org.junit.Assert.assertTrue; |
pierventre | 52ef933 | 2021-07-09 22:42:17 +0200 | [diff] [blame^] | 85 | import static org.onlab.junit.TestTools.assertAfter; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 86 | import static org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable.IntFunctionality.POSTCARD; |
| 87 | import static org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable.IntFunctionality.SINK; |
| 88 | import static org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable.IntFunctionality.SOURCE; |
| 89 | import static org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable.IntFunctionality.TRANSIT; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 90 | |
| 91 | public class SimpleIntManagerTest { |
| 92 | private static final String APP_NAME = "org.onosproject.inbandtelemetry"; |
| 93 | private static final ApplicationId APP_ID = new TestApplicationId(APP_NAME); |
| 94 | private static final IpAddress COLLECTOR_IP = IpAddress.valueOf("10.0.0.1"); |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 95 | private static final TpPort COLLECTOR_PORT = TpPort.tpPort(32766); |
| 96 | private static final int MIN_FLOW_HOP_LATENCY_CHANGE_NS = 32; |
| 97 | private static final String INT_REPORT_CONFIG_KEY = "report"; |
| 98 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:leaf1"); |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 99 | private static final String WATCHED_SUBNET_1 = "192.168.10.0/24"; |
| 100 | private static final String WATCHED_SUBNET_2 = "192.168.20.0/24"; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 101 | private static final TrafficSelector FLOW_SELECTOR1 = DefaultTrafficSelector.builder() |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 102 | .matchIPDst(IpPrefix.valueOf(WATCHED_SUBNET_1)) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 103 | .matchVlanId(VlanId.vlanId((short) 10)) |
| 104 | .build(); |
| 105 | private static final TrafficSelector FLOW_SELECTOR2 = DefaultTrafficSelector.builder() |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 106 | .matchIPDst(IpPrefix.valueOf(WATCHED_SUBNET_2)) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 107 | .matchVlanId(VlanId.vlanId((short) 20)) |
| 108 | .build(); |
| 109 | private static final Device DEFAULT_DEVICE = |
| 110 | new DefaultDevice(null, DEVICE_ID, Device.Type.SWITCH, "", "", "", "", null); |
| 111 | private static final List<Port> DEVICE_PORTS = ImmutableList.of( |
| 112 | new DefaultPort(DEFAULT_DEVICE, PortNumber.portNumber(1), true), |
| 113 | new DefaultPort(DEFAULT_DEVICE, PortNumber.portNumber(2), true) |
| 114 | ); |
| 115 | private static final Host HOST1 = |
| 116 | new DefaultHost(null, HostId.hostId("00:00:00:00:00:01/None"), null, |
| 117 | VlanId.NONE, ImmutableSet.of(), ImmutableSet.of(), true); |
| 118 | private static final Host HOST2 = |
| 119 | new DefaultHost(null, HostId.hostId("00:00:00:00:00:02/None"), null, |
| 120 | VlanId.NONE, ImmutableSet.of(), ImmutableSet.of(), true); |
| 121 | private static final Map<ConnectPoint, Host> HOSTS = ImmutableMap.of( |
| 122 | ConnectPoint.fromString("device:leaf1/1"), HOST1, |
| 123 | ConnectPoint.fromString("device:leaf1/2"), HOST2 |
| 124 | ); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 125 | |
| 126 | private SimpleIntManager manager; |
| 127 | private StorageService storageService; |
| 128 | private MastershipService mastershipService; |
| 129 | private CoreService coreService; |
| 130 | private HostService hostService; |
| 131 | private DeviceService deviceService; |
| 132 | private NetworkConfigRegistry networkConfigRegistry; |
| 133 | private NetworkConfigService networkConfigService; |
| 134 | private NetworkConfigListener networkConfigListener; |
| 135 | |
| 136 | |
| 137 | @Before |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 138 | public void setup() throws IOException { |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 139 | storageService = new TestStorageService(); |
| 140 | mastershipService = createNiceMock(MastershipService.class); |
| 141 | coreService = createNiceMock(CoreService.class); |
| 142 | hostService = createNiceMock(HostService.class); |
| 143 | deviceService = createNiceMock(DeviceService.class); |
| 144 | expect(deviceService.getDevices()).andReturn(ImmutableList.of()).anyTimes(); |
| 145 | networkConfigRegistry = createNiceMock(NetworkConfigRegistry.class); |
| 146 | networkConfigService = createNiceMock(NetworkConfigService.class); |
| 147 | |
| 148 | manager = new SimpleIntManager(); |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 149 | manager.coreService = coreService; |
| 150 | manager.deviceService = deviceService; |
| 151 | manager.storageService = storageService; |
| 152 | manager.mastershipService = mastershipService; |
| 153 | manager.hostService = hostService; |
| 154 | manager.netcfgService = networkConfigService; |
| 155 | manager.netcfgRegistry = networkConfigRegistry; |
pierventre | 52ef933 | 2021-07-09 22:42:17 +0200 | [diff] [blame^] | 156 | manager.eventExecutor = MoreExecutors.newDirectExecutorService(); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 157 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 158 | expect(coreService.registerApplication(APP_NAME)) |
| 159 | .andReturn(APP_ID).anyTimes(); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 160 | networkConfigRegistry.registerConfigFactory(anyObject()); |
| 161 | expectLastCall().once(); |
| 162 | |
| 163 | Capture<NetworkConfigListener> capture = newCapture(); |
| 164 | networkConfigService.addListener(EasyMock.capture(capture)); |
| 165 | expectLastCall().once(); |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 166 | IntReportConfig config = getIntReportConfig("/report-config.json"); |
| 167 | expect(networkConfigService.getConfig(APP_ID, IntReportConfig.class)) |
| 168 | .andReturn(config) |
| 169 | .anyTimes(); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 170 | replay(mastershipService, deviceService, coreService, |
| 171 | hostService, networkConfigRegistry, networkConfigService); |
| 172 | manager.activate(); |
| 173 | networkConfigListener = capture.getValue(); |
| 174 | } |
| 175 | |
| 176 | @After |
| 177 | public void teardown() { |
| 178 | manager.deactivate(); |
| 179 | } |
| 180 | |
| 181 | @Test |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 182 | public void testPushIntAppConfig() throws IOException { |
| 183 | IntReportConfig config = getIntReportConfig("/report-config.json"); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 184 | NetworkConfigEvent event = |
| 185 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, APP_ID, |
| 186 | config, null, IntReportConfig.class); |
| 187 | networkConfigListener.event(event); |
| 188 | |
| 189 | // We expected that the manager will store the device config which |
| 190 | // converted from the app config. |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 191 | IntDeviceConfig expectedConfig = createIntDeviceConfig(); |
| 192 | IntDeviceConfig actualConfig = manager.getConfig(); |
| 193 | assertEquals(expectedConfig, actualConfig); |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 194 | |
| 195 | // Install watch subnets via netcfg |
| 196 | // In the report-config.json, there are 3 subnets we want to watch |
| 197 | // For subnet 0.0.0.0/0, the IntManager will create only one IntIntent with an empty selector. |
| 198 | Set<IntIntent> expectedIntIntents = Sets.newHashSet(); |
| 199 | ConsistentMap<IntIntentId, IntIntent> intentMap = TestUtils.getField(manager, "intentMap"); |
| 200 | IntIntent.Builder baseIntentBuilder = IntIntent.builder() |
| 201 | .withReportType(IntIntent.IntReportType.TRACKED_FLOW) |
| 202 | .withReportType(IntIntent.IntReportType.DROPPED_PACKET) |
| 203 | .withReportType(IntIntent.IntReportType.CONGESTED_QUEUE) |
| 204 | .withTelemetryMode(IntIntent.TelemetryMode.POSTCARD); |
| 205 | |
| 206 | // Watch IP Src == subnet 1 |
| 207 | TrafficSelector expectedSelector = DefaultTrafficSelector.builder() |
| 208 | .matchIPSrc(IpPrefix.valueOf(WATCHED_SUBNET_1)) |
| 209 | .build(); |
| 210 | expectedIntIntents.add(baseIntentBuilder.withSelector(expectedSelector).build()); |
| 211 | // Watch IP Dst == subnet 1 |
| 212 | expectedSelector = DefaultTrafficSelector.builder() |
| 213 | .matchIPDst(IpPrefix.valueOf(WATCHED_SUBNET_1)) |
| 214 | .build(); |
| 215 | expectedIntIntents.add(baseIntentBuilder.withSelector(expectedSelector).build()); |
| 216 | // Watch IP Src == subnet 2 |
| 217 | expectedSelector = DefaultTrafficSelector.builder() |
| 218 | .matchIPSrc(IpPrefix.valueOf(WATCHED_SUBNET_2)) |
| 219 | .build(); |
| 220 | expectedIntIntents.add(baseIntentBuilder.withSelector(expectedSelector).build()); |
| 221 | // Watch IP Dst == subnet 2 |
| 222 | expectedSelector = DefaultTrafficSelector.builder() |
| 223 | .matchIPDst(IpPrefix.valueOf(WATCHED_SUBNET_2)) |
| 224 | .build(); |
| 225 | expectedIntIntents.add(baseIntentBuilder.withSelector(expectedSelector).build()); |
| 226 | // Any packets |
| 227 | expectedSelector = DefaultTrafficSelector.emptySelector(); |
| 228 | expectedIntIntents.add(baseIntentBuilder.withSelector(expectedSelector).build()); |
| 229 | |
| 230 | // The INT intent installation order can be random, so we need to collect |
| 231 | // all expected INT intents and check if actual intent exists. |
pierventre | 52ef933 | 2021-07-09 22:42:17 +0200 | [diff] [blame^] | 232 | assertAfter(50, 100, () -> assertEquals(5, intentMap.size())); |
Yi Tseng | 83a640a | 2021-03-29 01:58:15 -0700 | [diff] [blame] | 233 | intentMap.entrySet().forEach(entry -> { |
| 234 | IntIntent actualIntIntent = entry.getValue().value(); |
| 235 | assertTrue(expectedIntIntents.contains(actualIntIntent)); |
| 236 | }); |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testConfigNonIntDevice() { |
| 241 | reset(deviceService); |
| 242 | Device device = getMockDevice(false, DEVICE_ID); |
| 243 | expect(deviceService.getDevice(DEVICE_ID)) |
| 244 | .andReturn(device) |
| 245 | .anyTimes(); |
| 246 | expect(deviceService.getDevices()) |
| 247 | .andReturn(ImmutableSet.of(device)) |
| 248 | .anyTimes(); |
| 249 | replay(deviceService, device); |
| 250 | assertTrue(manager.configDevice(DEVICE_ID)); |
| 251 | verify(); |
| 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void testConfigSourceDevice() { |
| 256 | reset(deviceService, hostService); |
| 257 | Device device = getMockDevice(true, DEVICE_ID); |
| 258 | IntProgrammable intProg = getMockIntProgrammable(true, false, false, false); |
| 259 | setUpDeviceTest(device, intProg, true, false); |
| 260 | IntObjective intObj = IntObjective.builder() |
| 261 | .withSelector(FLOW_SELECTOR2) |
| 262 | .build(); |
| 263 | expect(intProg.addIntObjective(eq(intObj))) |
| 264 | .andReturn(true) |
| 265 | .once(); |
| 266 | expect(intProg.setSourcePort(PortNumber.portNumber(1))).andReturn(true).once(); |
| 267 | expect(intProg.setSourcePort(PortNumber.portNumber(2))).andReturn(true).once(); |
| 268 | replay(deviceService, hostService, device, intProg); |
| 269 | installTestIntents(); |
| 270 | assertTrue(manager.configDevice(DEVICE_ID)); |
| 271 | verify(intProg); |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testConfigTransitDevice() { |
| 276 | reset(deviceService, hostService); |
| 277 | Device device = getMockDevice(true, DEVICE_ID); |
| 278 | IntProgrammable intProg = getMockIntProgrammable(false, true, false, false); |
| 279 | setUpDeviceTest(device, intProg, false, false); |
| 280 | replay(deviceService, hostService, device, intProg); |
| 281 | installTestIntents(); |
| 282 | assertTrue(manager.configDevice(DEVICE_ID)); |
| 283 | verify(intProg); |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testConfigSinkDevice() { |
| 288 | reset(deviceService, hostService); |
| 289 | Device device = getMockDevice(true, DEVICE_ID); |
| 290 | IntProgrammable intProg = getMockIntProgrammable(false, false, true, false); |
| 291 | setUpDeviceTest(device, intProg, true, true); |
| 292 | expect(intProg.setSinkPort(PortNumber.portNumber(1))).andReturn(true).once(); |
| 293 | expect(intProg.setSinkPort(PortNumber.portNumber(2))).andReturn(true).once(); |
| 294 | replay(deviceService, hostService, device, intProg); |
| 295 | installTestIntents(); |
| 296 | assertTrue(manager.configDevice(DEVICE_ID)); |
| 297 | verify(intProg); |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | public void testConfigPostcardOnlyDevice() { |
| 302 | reset(deviceService, hostService); |
| 303 | Device device = getMockDevice(true, DEVICE_ID); |
| 304 | IntProgrammable intProg = getMockIntProgrammable(false, false, false, true); |
| 305 | setUpDeviceTest(device, intProg, true, true); |
| 306 | IntObjective intObj = IntObjective.builder() |
| 307 | .withSelector(FLOW_SELECTOR1) |
| 308 | .build(); |
| 309 | expect(intProg.addIntObjective(eq(intObj))) |
| 310 | .andReturn(true) |
| 311 | .once(); |
| 312 | replay(deviceService, hostService, device, intProg); |
| 313 | installTestIntents(); |
| 314 | assertTrue(manager.configDevice(DEVICE_ID)); |
| 315 | verify(intProg); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Utilities |
| 320 | */ |
| 321 | private void installTestIntents() { |
| 322 | // Pre-install an INT intent to the manager. |
| 323 | IntIntent.Builder intentBuilder = IntIntent.builder() |
| 324 | .withHeaderType(IntIntent.IntHeaderType.HOP_BY_HOP) |
| 325 | .withReportType(IntIntent.IntReportType.TRACKED_FLOW); |
| 326 | |
| 327 | IntIntent postcardIntent = intentBuilder |
| 328 | .withTelemetryMode(IntIntent.TelemetryMode.POSTCARD) |
| 329 | .withSelector(FLOW_SELECTOR1) |
| 330 | .build(); |
| 331 | IntIntent nonPoscardIntent = intentBuilder |
| 332 | .withTelemetryMode(IntIntent.TelemetryMode.INBAND_TELEMETRY) |
| 333 | .withSelector(FLOW_SELECTOR2) |
| 334 | .build(); |
| 335 | manager.installIntIntent(nonPoscardIntent); |
| 336 | manager.installIntIntent(postcardIntent); |
| 337 | } |
| 338 | |
| 339 | private void setUpDeviceTest(Device device, IntProgrammable intProg, |
| 340 | boolean hostConnected, boolean setupIntConfig) { |
| 341 | expect(device.as(IntProgrammable.class)) |
| 342 | .andReturn(intProg) |
| 343 | .anyTimes(); |
| 344 | expect(deviceService.getDevice(eq(DEVICE_ID))) |
| 345 | .andReturn(device) |
| 346 | .anyTimes(); |
| 347 | expect(deviceService.getDevices()) |
| 348 | .andReturn(ImmutableList.of(device)) |
| 349 | .anyTimes(); |
| 350 | if (setupIntConfig) { |
| 351 | IntDeviceConfig expectedConfig = createIntDeviceConfig(); |
| 352 | expect(intProg.setupIntConfig(eq(expectedConfig))) |
| 353 | .andReturn(true) |
| 354 | .atLeastOnce(); |
| 355 | } |
| 356 | expect(deviceService.getPorts(DEVICE_ID)) |
| 357 | .andReturn(DEVICE_PORTS) |
| 358 | .anyTimes(); |
| 359 | |
| 360 | if (hostConnected) { |
| 361 | HOSTS.forEach((cp, host) -> { |
| 362 | expect(hostService.getConnectedHosts(eq(cp))) |
| 363 | .andReturn(ImmutableSet.of(host)) |
| 364 | .anyTimes(); |
| 365 | }); |
| 366 | expect(hostService.getConnectedHosts(eq(DEVICE_ID))) |
| 367 | .andReturn(Sets.newHashSet(HOSTS.values())); |
| 368 | } else { |
| 369 | expect(hostService.getConnectedHosts(eq(DEVICE_ID))) |
| 370 | .andReturn(ImmutableSet.of()) |
| 371 | .anyTimes(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private IntReportConfig getIntReportConfig(String fileName) throws IOException { |
| 376 | IntReportConfig config = new IntReportConfig(); |
| 377 | InputStream jsonStream = getClass().getResourceAsStream(fileName); |
| 378 | ObjectMapper mapper = new ObjectMapper(); |
| 379 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 380 | config.init(APP_ID, INT_REPORT_CONFIG_KEY, jsonNode, mapper, c -> { |
| 381 | }); |
| 382 | return config; |
| 383 | } |
| 384 | |
| 385 | private Device getMockDevice(boolean supportInt, DeviceId deviceId) { |
| 386 | Device device = createNiceMock(Device.class); |
| 387 | expect(device.is(IntProgrammable.class)) |
| 388 | .andReturn(supportInt) |
| 389 | .anyTimes(); |
| 390 | expect(device.id()) |
| 391 | .andReturn(deviceId) |
| 392 | .anyTimes(); |
| 393 | return device; |
| 394 | } |
| 395 | |
| 396 | private IntProgrammable getMockIntProgrammable(boolean supportSource, boolean supportTransit, boolean supportSink, |
| 397 | boolean supportPostcard) { |
| 398 | IntProgrammable intProg = createNiceMock(IntProgrammable.class); |
| 399 | if (supportSource) { |
| 400 | expect(intProg.supportsFunctionality(SOURCE)) |
| 401 | .andReturn(true).anyTimes(); |
| 402 | } |
| 403 | if (supportTransit) { |
| 404 | expect(intProg.supportsFunctionality(TRANSIT)) |
| 405 | .andReturn(true).anyTimes(); |
| 406 | } |
| 407 | if (supportSink) { |
| 408 | expect(intProg.supportsFunctionality(SINK)) |
| 409 | .andReturn(true).anyTimes(); |
| 410 | } |
| 411 | if (supportPostcard) { |
| 412 | expect(intProg.supportsFunctionality(POSTCARD)) |
| 413 | .andReturn(true).anyTimes(); |
| 414 | } |
| 415 | expect(intProg.init()) |
| 416 | .andReturn(true) |
| 417 | .anyTimes(); |
| 418 | return intProg; |
| 419 | } |
| 420 | |
| 421 | private IntDeviceConfig createIntDeviceConfig() { |
| 422 | return IntDeviceConfig.builder() |
| 423 | .withMinFlowHopLatencyChangeNs(MIN_FLOW_HOP_LATENCY_CHANGE_NS) |
| 424 | .withCollectorPort(COLLECTOR_PORT) |
| 425 | .withCollectorIp(COLLECTOR_IP) |
| 426 | .enabled(true) |
| 427 | .build(); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 428 | } |
| 429 | } |