Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [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.net.pi.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableSet; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.util.ItemNotFoundException; |
| 27 | import org.onosproject.net.DeviceId; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | import org.onosproject.net.config.ConfigFactory; |
| 31 | import org.onosproject.net.config.NetworkConfigListener; |
| 32 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 33 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 34 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 35 | import org.onosproject.net.device.DeviceDescription; |
| 36 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 37 | import org.onosproject.net.device.PortDescription; |
| 38 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 39 | import org.onosproject.net.driver.Behaviour; |
| 40 | import org.onosproject.net.driver.Driver; |
| 41 | import org.onosproject.net.driver.DriverAdapter; |
| 42 | import org.onosproject.net.driver.DriverAdminService; |
| 43 | import org.onosproject.net.driver.DriverAdminServiceAdapter; |
| 44 | import org.onosproject.net.driver.DriverProvider; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 45 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 46 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 47 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 48 | |
| 49 | import java.io.IOException; |
| 50 | import java.io.InputStream; |
| 51 | import java.util.HashMap; |
| 52 | import java.util.HashSet; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
| 55 | import java.util.Set; |
| 56 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 57 | import static org.junit.Assert.assertEquals; |
| 58 | import static org.junit.Assert.assertFalse; |
| 59 | import static org.junit.Assert.assertTrue; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 60 | import static org.onosproject.pipelines.basic.PipeconfLoader.BASIC_PIPECONF; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 61 | |
| 62 | |
| 63 | /** |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 64 | * Unit Test Class for PiPipeconfManager. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 65 | */ |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 66 | public class PiPipeconfManagerTest { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 67 | |
| 68 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("test:test"); |
| 69 | private static final String BASE_DRIVER = "baseDriver"; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 70 | |
| 71 | //Mock util sets and classes |
| 72 | private final NetworkConfigRegistry cfgService = new MockNetworkConfigRegistry(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 73 | private final DriverAdminService driverAdminService = new MockDriverAdminService(); |
| 74 | private Driver baseDriver = new MockDriver(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 75 | |
| 76 | private final Set<ConfigFactory> cfgFactories = new HashSet<>(); |
| 77 | private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 78 | private final Set<DriverProvider> providers = new HashSet<>(); |
| 79 | |
| 80 | private final PiPipeconfConfig piPipeconfConfig = new PiPipeconfConfig(); |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 81 | private final InputStream jsonStream = PiPipeconfManagerTest.class |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 82 | .getResourceAsStream("/org/onosproject/net/pi/impl/piPipeconfId.json"); |
| 83 | private final BasicDeviceConfig basicDeviceConfig = new BasicDeviceConfig(); |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 84 | private final InputStream jsonStreamBasic = PiPipeconfManagerTest.class |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 85 | .getResourceAsStream("/org/onosproject/net/pi/impl/basic.json"); |
| 86 | |
| 87 | |
| 88 | //Services |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 89 | private PiPipeconfManager piPipeconfService; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 90 | private PiPipeconf piPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 91 | |
| 92 | @Before |
| 93 | public void setUp() throws IOException { |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 94 | piPipeconfService = new PiPipeconfManager(); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 95 | piPipeconf = BASIC_PIPECONF; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 96 | piPipeconfService.cfgService = cfgService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 97 | piPipeconfService.driverAdminService = driverAdminService; |
| 98 | String key = "piPipeconf"; |
| 99 | ObjectMapper mapper = new ObjectMapper(); |
| 100 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 101 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 102 | piPipeconfConfig.init(DEVICE_ID, key, jsonNode, mapper, delegate); |
| 103 | String keyBasic = "basic"; |
| 104 | JsonNode jsonNodeBasic = mapper.readTree(jsonStreamBasic); |
| 105 | basicDeviceConfig.init(DEVICE_ID, keyBasic, jsonNodeBasic, mapper, delegate); |
| 106 | piPipeconfService.activate(); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void activate() { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 111 | assertEquals("Incorrect driver admin service", driverAdminService, piPipeconfService.driverAdminService); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 112 | assertEquals("Incorrect driverAdminService service", driverAdminService, piPipeconfService.driverAdminService); |
| 113 | assertEquals("Incorrect configuration service", cfgService, piPipeconfService.cfgService); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 114 | assertTrue("Incorrect config factory", cfgFactories.contains(piPipeconfService.configFactory)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void deactivate() { |
| 119 | piPipeconfService.deactivate(); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 120 | assertEquals("Incorrect driver admin service", null, piPipeconfService.driverAdminService); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 121 | assertEquals("Incorrect driverAdminService service", null, piPipeconfService.driverAdminService); |
| 122 | assertEquals("Incorrect configuration service", null, piPipeconfService.cfgService); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 123 | assertFalse("Config factory should be unregistered", cfgFactories.contains(piPipeconfService.configFactory)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void register() { |
| 128 | piPipeconfService.register(piPipeconf); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 129 | assertTrue("PiPipeconf should be registered", piPipeconfService.pipeconfs.containsValue(piPipeconf)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void getPipeconf() { |
| 134 | piPipeconfService.register(piPipeconf); |
| 135 | assertEquals("Returned PiPipeconf is not correct", piPipeconf, |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 136 | piPipeconfService.getPipeconf(piPipeconf.id()).get()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | @Test |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 141 | public void mergeDriver() { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 142 | PiPipeconfId piPipeconfId = cfgService.getConfig(DEVICE_ID, PiPipeconfConfig.class).piPipeconfId(); |
| 143 | assertEquals(piPipeconf.id(), piPipeconfId); |
| 144 | |
| 145 | String baseDriverName = cfgService.getConfig(DEVICE_ID, BasicDeviceConfig.class).driver(); |
| 146 | assertEquals(BASE_DRIVER, baseDriverName); |
| 147 | |
| 148 | piPipeconfService.register(piPipeconf); |
| 149 | assertEquals("Returned PiPipeconf is not correct", piPipeconf, |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 150 | piPipeconfService.getPipeconf(piPipeconf.id()).get()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 151 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 152 | String mergedDriverName = piPipeconfService.getMergedDriver(DEVICE_ID, piPipeconfId); |
| 153 | |
| 154 | String expectedName = BASE_DRIVER + ":" + piPipeconfId.id(); |
| 155 | assertEquals(expectedName, mergedDriverName); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 156 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 157 | //we assume that the provider is 1 and that it contains 1 driver |
| 158 | //we also assume that everything after driverAdminService.registerProvider(provider); has been tested. |
| 159 | assertTrue("Provider should be registered", providers.size() == 1); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 160 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 161 | assertTrue("Merged driver name should be valid", |
| 162 | mergedDriverName != null && !mergedDriverName.isEmpty()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 163 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 164 | DriverProvider provider = providers.iterator().next(); |
| 165 | assertTrue("Provider should contain one driver", provider.getDrivers().size() == 1); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 166 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 167 | Driver driver = provider.getDrivers().iterator().next(); |
| 168 | |
| 169 | Set<Class<? extends Behaviour>> expectedBehaviours = Sets.newHashSet(); |
| 170 | expectedBehaviours.addAll(BASIC_PIPECONF.behaviours()); |
| 171 | expectedBehaviours.addAll(baseDriver.behaviours()); |
| 172 | assertEquals("The driver contains wrong behaviours", expectedBehaviours, driver.behaviours()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 176 | @Override |
| 177 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 178 | cfgFactories.add(configFactory); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 183 | cfgFactories.remove(configFactory); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void addListener(NetworkConfigListener listener) { |
| 188 | netCfgListeners.add(listener); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void removeListener(NetworkConfigListener listener) { |
| 193 | netCfgListeners.remove(listener); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 198 | DeviceId did = (DeviceId) subject; |
| 199 | if (configClass.equals(PiPipeconfConfig.class) |
| 200 | && did.equals(DEVICE_ID)) { |
| 201 | return (C) piPipeconfConfig; |
| 202 | } else if (configClass.equals(BasicDeviceConfig.class) |
| 203 | && did.equals(DEVICE_ID)) { |
| 204 | return (C) basicDeviceConfig; |
| 205 | } |
| 206 | return null; |
| 207 | } |
| 208 | } |
| 209 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 210 | private class MockDriverAdminService extends DriverAdminServiceAdapter { |
| 211 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 212 | @Override |
| 213 | public Driver getDriver(String driverName) { |
| 214 | if (driverName.equals(BASE_DRIVER)) { |
| 215 | return baseDriver; |
| 216 | } |
| 217 | throw new ItemNotFoundException("Driver not found"); |
| 218 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 219 | |
| 220 | @Override |
| 221 | public void registerProvider(DriverProvider provider) { |
| 222 | providers.add(provider); |
| 223 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 224 | |
| 225 | @Override |
| 226 | public Set<DriverProvider> getProviders() { |
| 227 | return providers; |
| 228 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private class MockDelegate implements ConfigApplyDelegate { |
| 232 | @Override |
| 233 | public void onApply(Config configFile) { |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private class MockDriver extends DriverAdapter { |
| 238 | |
| 239 | @Override |
| 240 | public List<Driver> parents() { |
| 241 | return ImmutableList.of(); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String manufacturer() { |
Thomas Vachuska | a10137c | 2018-04-03 16:45:59 -0700 | [diff] [blame] | 246 | return "Open Networking Foundation"; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String hwVersion() { |
| 251 | return "testHW"; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) { |
| 256 | return MockDeviceDescriptionDiscovery.class; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public Map<String, String> properties() { |
| 261 | return new HashMap<>(); |
| 262 | } |
| 263 | |
| 264 | @Override |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 265 | public String getProperty(String name) { |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 270 | public Set<Class<? extends Behaviour>> behaviours() { |
| 271 | return ImmutableSet.of(DeviceDescriptionDiscovery.class); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public String swVersion() { |
| 276 | return "testSW"; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public String name() { |
| 281 | return BASE_DRIVER; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private class MockDeviceDescriptionDiscovery extends AbstractHandlerBehaviour |
| 286 | implements DeviceDescriptionDiscovery { |
| 287 | @Override |
| 288 | public DeviceDescription discoverDeviceDetails() { |
| 289 | return null; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public List<PortDescription> discoverPortDetails() { |
| 294 | return null; |
| 295 | } |
| 296 | } |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 297 | } |