Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [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.provider.snmp.device.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.TestApplicationId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.CoreServiceAdapter; |
| 27 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 28 | import org.onosproject.net.AbstractProjectableModel; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.config.Config; |
| 34 | import org.onosproject.net.config.ConfigFactory; |
| 35 | import org.onosproject.net.config.NetworkConfigEvent; |
| 36 | import org.onosproject.net.config.NetworkConfigListener; |
| 37 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 39 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 40 | import org.onosproject.net.device.DeviceDescription; |
| 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceProvider; |
| 43 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 44 | import org.onosproject.net.device.DeviceProviderRegistryAdapter; |
| 45 | import org.onosproject.net.device.DeviceProviderService; |
| 46 | import org.onosproject.net.device.DeviceProviderServiceAdapter; |
| 47 | import org.onosproject.net.device.DeviceService; |
| 48 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 49 | import org.onosproject.net.device.DeviceStore; |
| 50 | import org.onosproject.net.device.DeviceStoreAdapter; |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 51 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 52 | import org.onosproject.net.provider.ProviderId; |
| 53 | import org.onosproject.snmp.SnmpController; |
| 54 | |
| 55 | import java.util.HashMap; |
| 56 | import java.util.HashSet; |
| 57 | import java.util.Set; |
| 58 | |
| 59 | import static org.junit.Assert.*; |
| 60 | import static org.onlab.junit.TestTools.assertAfter; |
| 61 | |
| 62 | /** |
| 63 | * Testing class for SnmpDeviceProvider. |
| 64 | */ |
| 65 | public class SnmpDeviceProviderTest { |
| 66 | |
| 67 | private final SnmpDeviceProvider provider = new SnmpDeviceProvider(); |
| 68 | private final SnmpController controller = new SnmpControllerAdapter(); |
| 69 | private final DeviceProviderRegistry providerRegistry = new MockDeviceProviderRegistry(); |
| 70 | private final DeviceService deviceService = new MockDeviceService(); |
| 71 | private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry(); |
| 72 | private final DeviceStore deviceStore = new MockDeviceStore(); |
| 73 | protected CoreService coreService = new MockCoreService(); |
| 74 | private final DeviceProviderService deviceProviderService = new MockDeviceProviderService(); |
| 75 | private final TestApplicationId applicationId = new TestApplicationId("TestAppId"); |
| 76 | private final NetworkConfigEvent deviceAddedEvent = |
| 77 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 78 | null, SnmpProviderConfig.class); |
| 79 | private final SnmpProviderConfig snmpProviderConfig = new MockSnmpProviderConfig(); |
| 80 | private final NetworkConfigEvent deviceAddedIrrelevantEvent = |
| 81 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 82 | null, BasicDeviceConfig.class); |
| 83 | private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1"); |
| 84 | private final DeviceId wrongDeviceId = DeviceId.deviceId("snmp:2.2.2.2:2"); |
| 85 | |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() throws Exception { |
| 89 | provider.controller = controller; |
| 90 | provider.providerRegistry = providerRegistry; |
| 91 | provider.deviceService = deviceService; |
| 92 | provider.netCfgService = netCfgService; |
| 93 | provider.deviceStore = deviceStore; |
| 94 | provider.coreService = coreService; |
| 95 | provider.activate(null); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testActivate() { |
| 100 | assertEquals("Incorrect provider service", deviceProviderService, provider.providerService); |
| 101 | assertEquals("Incorrect application id", applicationId, provider.appId); |
| 102 | assertEquals("Incorrect config factory", cfgFactory, provider.factory); |
| 103 | assertTrue("Incorrect network config listener", netCfgListeners.contains(provider.cfgLister)); |
| 104 | |
| 105 | |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testDeactivate() { |
| 110 | this.addDevice(); |
| 111 | provider.deactivate(null); |
| 112 | assertAfter(500, () -> |
| 113 | assertNull("Device should be removed", controller.getDevice(deviceId))); |
| 114 | assertNull("Network config factory not removed", cfgFactory); |
| 115 | assertFalse("Network config listener not removed", netCfgListeners.contains(provider.cfgLister)); |
| 116 | assertFalse("Provider not unregistered", providerRegistry.getProviders().contains(provider.id())); |
| 117 | assertNull("Provider registry not removed", provider.providerService); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void eventNotRelevant() { |
| 122 | assertFalse("Event should not be relevant", provider.cfgLister.isRelevant(deviceAddedIrrelevantEvent)); |
| 123 | assertFalse("Device should not be reachable", provider.isReachable(wrongDeviceId)); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void addDevice() { |
| 128 | assertTrue("Event should be relevant", provider.cfgLister.isRelevant(deviceAddedEvent)); |
| 129 | provider.cfgLister.event(deviceAddedEvent); |
| 130 | AbstractProjectableModel.setDriverService(null, new MockDriverService()); |
| 131 | //FIXME this needs sleep |
| 132 | assertAfter(500, () -> |
| 133 | assertNotNull("Device should be added to controller", controller.getDevice(deviceId))); |
| 134 | assertTrue("Device should be reachable", provider.isReachable(deviceId)); |
| 135 | } |
| 136 | |
| 137 | private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter { |
| 138 | |
| 139 | Set<ProviderId> providers = new HashSet<>(); |
| 140 | |
| 141 | @Override |
| 142 | public DeviceProviderService register(DeviceProvider provider) { |
| 143 | providers.add(provider.id()); |
| 144 | return deviceProviderService; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void unregister(DeviceProvider provider) { |
| 149 | providers.remove(provider.id()); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Set<ProviderId> getProviders() { |
| 154 | return providers; |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | private class MockDeviceService extends DeviceServiceAdapter { |
| 160 | @Override |
| 161 | public Device getDevice(DeviceId deviceId) { |
| 162 | return deviceStore.getDevice(deviceId); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private ConfigFactory cfgFactory; |
| 167 | private Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 168 | |
| 169 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 170 | |
| 171 | @Override |
| 172 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 173 | cfgFactory = configFactory; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 178 | cfgFactory = null; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void addListener(NetworkConfigListener listener) { |
| 183 | netCfgListeners.add(listener); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void removeListener(NetworkConfigListener listener) { |
| 188 | netCfgListeners.remove(listener); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | @Override |
| 193 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 194 | if (configClass.equals(SnmpProviderConfig.class)) { |
| 195 | return (C) snmpProviderConfig; |
| 196 | } else { |
| 197 | return (C) new BasicDeviceConfig(); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private class MockDeviceStore extends DeviceStoreAdapter { |
| 203 | protected HashMap<DeviceId, Device> devices = new HashMap<>(); |
| 204 | |
| 205 | @Override |
| 206 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 207 | DeviceDescription desc) { |
| 208 | |
| 209 | devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(), |
| 210 | desc.manufacturer(), desc.hwVersion(), |
| 211 | desc.swVersion(), desc.serialNumber(), |
| 212 | desc.chassisId(), DefaultAnnotations.builder().build())); |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Device getDevice(DeviceId deviceId) { |
| 218 | return devices.get(deviceId); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | private class MockCoreService extends CoreServiceAdapter { |
| 224 | @Override |
| 225 | public ApplicationId registerApplication(String name) { |
| 226 | return applicationId; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private class MockDeviceProviderService extends DeviceProviderServiceAdapter { |
| 231 | DeviceStore store = deviceStore; |
| 232 | |
| 233 | @Override |
| 234 | public void deviceConnected(DeviceId deviceId, DeviceDescription desc) { |
| 235 | store.createOrUpdateDevice(ProviderId.NONE, deviceId, desc); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private class MockSnmpProviderConfig extends SnmpProviderConfig { |
| 240 | protected SnmpDeviceInfo deviceInfo = new SnmpDeviceInfo(IpAddress.valueOf("1.1.1.1"), 1, "test", "test"); |
| 241 | |
| 242 | @Override |
| 243 | public Set<SnmpProviderConfig.SnmpDeviceInfo> getDevicesInfo() throws ConfigException { |
| 244 | return ImmutableSet.of(deviceInfo); |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | private class MockDriverService extends DriverServiceAdapter { |
| 250 | |
| 251 | } |
| 252 | } |