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