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