Yi Tseng | 0f1ffd1 | 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 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import org.easymock.Capture; |
| 23 | import org.easymock.EasyMock; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.TpPort; |
| 29 | import org.onosproject.TestApplicationId; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig; |
| 34 | import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig; |
| 35 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 36 | import org.onosproject.net.config.NetworkConfigEvent; |
| 37 | import org.onosproject.net.config.NetworkConfigListener; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 39 | import org.onosproject.net.config.NetworkConfigService; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.host.HostService; |
| 42 | import org.onosproject.store.service.StorageService; |
| 43 | import org.onosproject.store.service.TestStorageService; |
| 44 | |
| 45 | import static org.easymock.EasyMock.anyObject; |
| 46 | import static org.easymock.EasyMock.createNiceMock; |
| 47 | import static org.easymock.EasyMock.expect; |
| 48 | import static org.easymock.EasyMock.expectLastCall; |
| 49 | import static org.easymock.EasyMock.newCapture; |
| 50 | import static org.easymock.EasyMock.replay; |
| 51 | import static org.junit.Assert.assertEquals; |
| 52 | import static org.onlab.junit.TestUtils.setField; |
| 53 | |
| 54 | public class SimpleIntManagerTest { |
| 55 | private static final String APP_NAME = "org.onosproject.inbandtelemetry"; |
| 56 | private static final ApplicationId APP_ID = new TestApplicationId(APP_NAME); |
| 57 | private static final IpAddress COLLECTOR_IP = IpAddress.valueOf("10.0.0.1"); |
| 58 | private static final TpPort COLLECTOR_PORT = TpPort.tpPort(5500); |
| 59 | private static final int MIN_FLOW_HOP_LATENCY_CHANGE_NS = 16; |
| 60 | |
| 61 | private SimpleIntManager manager; |
| 62 | private StorageService storageService; |
| 63 | private MastershipService mastershipService; |
| 64 | private CoreService coreService; |
| 65 | private HostService hostService; |
| 66 | private DeviceService deviceService; |
| 67 | private NetworkConfigRegistry networkConfigRegistry; |
| 68 | private NetworkConfigService networkConfigService; |
| 69 | private NetworkConfigListener networkConfigListener; |
| 70 | |
| 71 | |
| 72 | @Before |
| 73 | public void setup() { |
| 74 | storageService = new TestStorageService(); |
| 75 | mastershipService = createNiceMock(MastershipService.class); |
| 76 | coreService = createNiceMock(CoreService.class); |
| 77 | hostService = createNiceMock(HostService.class); |
| 78 | deviceService = createNiceMock(DeviceService.class); |
| 79 | expect(deviceService.getDevices()).andReturn(ImmutableList.of()).anyTimes(); |
| 80 | networkConfigRegistry = createNiceMock(NetworkConfigRegistry.class); |
| 81 | networkConfigService = createNiceMock(NetworkConfigService.class); |
| 82 | |
| 83 | manager = new SimpleIntManager(); |
| 84 | setField(manager, "coreService", coreService); |
| 85 | setField(manager, "deviceService", deviceService); |
| 86 | setField(manager, "storageService", storageService); |
| 87 | setField(manager, "mastershipService", mastershipService); |
| 88 | setField(manager, "hostService", hostService); |
| 89 | setField(manager, "netcfgService", networkConfigService); |
| 90 | setField(manager, "netcfgRegistry", networkConfigRegistry); |
| 91 | |
| 92 | expect(coreService.registerApplication(APP_NAME)).andReturn(APP_ID).once(); |
| 93 | networkConfigRegistry.registerConfigFactory(anyObject()); |
| 94 | expectLastCall().once(); |
| 95 | |
| 96 | Capture<NetworkConfigListener> capture = newCapture(); |
| 97 | networkConfigService.addListener(EasyMock.capture(capture)); |
| 98 | expectLastCall().once(); |
| 99 | replay(mastershipService, deviceService, coreService, |
| 100 | hostService, networkConfigRegistry, networkConfigService); |
| 101 | manager.activate(); |
| 102 | networkConfigListener = capture.getValue(); |
| 103 | } |
| 104 | |
| 105 | @After |
| 106 | public void teardown() { |
| 107 | manager.deactivate(); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testPushIntAppConfig() { |
| 112 | IntReportConfig config = new IntReportConfig(); |
| 113 | ObjectMapper mapper = new ObjectMapper(); |
| 114 | ConfigApplyDelegate delegate = configApply -> { }; |
| 115 | config.init(APP_ID, "report", JsonNodeFactory.instance.objectNode(), mapper, delegate); |
| 116 | config.setCollectorIp(COLLECTOR_IP) |
| 117 | .setCollectorPort(COLLECTOR_PORT) |
| 118 | .setMinFlowHopLatencyChangeNs(MIN_FLOW_HOP_LATENCY_CHANGE_NS); |
| 119 | |
| 120 | NetworkConfigEvent event = |
| 121 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, APP_ID, |
| 122 | config, null, IntReportConfig.class); |
| 123 | networkConfigListener.event(event); |
| 124 | |
| 125 | // We expected that the manager will store the device config which |
| 126 | // converted from the app config. |
| 127 | IntDeviceConfig deviceConfig = manager.getConfig(); |
| 128 | assertEquals(COLLECTOR_IP, deviceConfig.collectorIp()); |
| 129 | assertEquals(COLLECTOR_PORT, deviceConfig.collectorPort()); |
| 130 | assertEquals(MIN_FLOW_HOP_LATENCY_CHANGE_NS, deviceConfig.minFlowHopLatencyChangeNs()); |
| 131 | } |
| 132 | } |