Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [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 | package org.onosproject.net.config.impl; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 21 | import org.onlab.packet.ChassisId; |
| 22 | import org.onosproject.net.Device.Type; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.MastershipRole; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.config.ConfigFactory; |
| 27 | import org.onosproject.net.config.NetworkConfigEvent; |
| 28 | import org.onosproject.net.config.NetworkConfigListener; |
| 29 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 32 | import org.onosproject.net.config.inject.DeviceInjectionConfig; |
| 33 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 34 | import org.onosproject.net.device.DefaultPortDescription; |
| 35 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 36 | import org.onosproject.net.device.DeviceProvider; |
| 37 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 38 | import org.onosproject.net.device.DeviceProviderService; |
| 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.device.PortDescription; |
| 41 | import org.onosproject.net.driver.DefaultDriverData; |
| 42 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 43 | import org.onosproject.net.driver.DriverService; |
| 44 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | import org.osgi.service.component.annotations.Activate; |
| 46 | import org.osgi.service.component.annotations.Component; |
| 47 | import org.osgi.service.component.annotations.Deactivate; |
| 48 | import org.osgi.service.component.annotations.Reference; |
| 49 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.EnumSet; |
| 54 | import java.util.List; |
| 55 | import java.util.Optional; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.ExecutorService; |
| 58 | import java.util.concurrent.TimeUnit; |
| 59 | |
| 60 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 61 | import static org.onlab.util.Tools.groupedThreads; |
| 62 | import static org.onosproject.net.PortNumber.portNumber; |
| 63 | import static org.onosproject.net.config.basics.SubjectFactories.DEVICE_SUBJECT_FACTORY; |
| 64 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 65 | |
| 66 | // TODO In theory just @Component should be sufficient, |
| 67 | // but won't work without @Service. Need investigation. |
| 68 | /** |
| 69 | * Component to monitor DeviceInjectionConfig changes. |
| 70 | */ |
| 71 | @Beta |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Component(immediate = true, service = DeviceInjectionConfigMonitor.class) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 73 | public class DeviceInjectionConfigMonitor { |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | private final ProviderId pid = new ProviderId("inject", "org.onosproject.inject"); |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 80 | protected NetworkConfigService netcfgService; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 83 | protected DeviceService deviceService; |
| 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 86 | protected DeviceProviderRegistry deviceProviderRegistry; |
| 87 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 89 | protected DriverService driverService; |
| 90 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 92 | protected NetworkConfigRegistry netcfgRegistry; |
| 93 | |
| 94 | private final List<ConfigFactory<?, ?>> factories = ImmutableList.of( |
| 95 | new ConfigFactory<DeviceId, DeviceInjectionConfig>(DEVICE_SUBJECT_FACTORY, |
| 96 | DeviceInjectionConfig.class, |
| 97 | DeviceInjectionConfig.CONFIG_KEY) { |
| 98 | @Override |
| 99 | public DeviceInjectionConfig createConfig() { |
| 100 | return new DeviceInjectionConfig(); |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | |
| 105 | private final InternalConfigListener listener = new InternalConfigListener(); |
| 106 | |
| 107 | private ExecutorService worker; |
| 108 | |
| 109 | private InternalDeviceProvider deviceProvider = new InternalDeviceProvider(); |
| 110 | |
| 111 | private DeviceProviderService providerService; |
| 112 | |
| 113 | |
| 114 | @Activate |
| 115 | public void activate() { |
| 116 | worker = newSingleThreadExecutor(groupedThreads("onos/inject", |
| 117 | "worker", |
| 118 | log)); |
| 119 | providerService = deviceProviderRegistry.register(deviceProvider); |
| 120 | |
| 121 | netcfgService.addListener(listener); |
| 122 | |
| 123 | factories.forEach(netcfgRegistry::registerConfigFactory); |
| 124 | |
| 125 | log.info("Started"); |
| 126 | } |
| 127 | |
| 128 | @Deactivate |
| 129 | public void deactivate() { |
| 130 | netcfgService.removeListener(listener); |
| 131 | |
| 132 | deviceProviderRegistry.unregister(deviceProvider); |
| 133 | |
| 134 | worker.shutdown(); |
| 135 | try { |
| 136 | worker.awaitTermination(5, TimeUnit.SECONDS); |
| 137 | } catch (InterruptedException e) { |
| 138 | log.warn("Interrupted.", e); |
| 139 | Thread.currentThread().interrupt(); |
| 140 | } |
| 141 | factories.forEach(netcfgRegistry::unregisterConfigFactory); |
| 142 | |
| 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | private void removeDevice(DeviceId did) { |
| 147 | providerService.deviceDisconnected(did); |
| 148 | } |
| 149 | |
| 150 | private void injectDevice(DeviceId did) { |
| 151 | Optional<BasicDeviceConfig> basic = |
| 152 | Optional.ofNullable(netcfgService.getConfig(did, BasicDeviceConfig.class)); |
| 153 | Optional<DeviceDescriptionDiscovery> discovery = basic |
| 154 | .map(BasicDeviceConfig::driver) |
| 155 | .map(driverService::getDriver) |
| 156 | .filter(drvr -> drvr.hasBehaviour(DeviceDescriptionDiscovery.class)) |
| 157 | .map(drvr -> drvr.createBehaviour(new DefaultDriverHandler(new DefaultDriverData(drvr, did)), |
| 158 | DeviceDescriptionDiscovery.class)); |
| 159 | |
| 160 | if (discovery.isPresent()) { |
| 161 | providerService.deviceConnected(did, |
| 162 | discovery.get().discoverDeviceDetails()); |
| 163 | providerService.updatePorts(did, |
| 164 | discovery.get().discoverPortDetails()); |
| 165 | } else { |
| 166 | |
| 167 | String unk = "UNKNOWN"; |
| 168 | DefaultDeviceDescription desc = new DefaultDeviceDescription( |
| 169 | did.uri(), |
| 170 | basic.map(BasicDeviceConfig::type).orElse(Type.SWITCH), |
| 171 | basic.map(BasicDeviceConfig::manufacturer).orElse(unk), |
| 172 | basic.map(BasicDeviceConfig::hwVersion).orElse(unk), |
| 173 | basic.map(BasicDeviceConfig::swVersion).orElse(unk), |
| 174 | basic.map(BasicDeviceConfig::serial).orElse(unk), |
| 175 | new ChassisId(), |
| 176 | true); |
| 177 | providerService.deviceConnected(did, desc); |
| 178 | |
| 179 | Optional<DeviceInjectionConfig> inject = |
| 180 | Optional.ofNullable(netcfgService.getConfig(did, DeviceInjectionConfig.class)); |
| 181 | |
| 182 | String ports = inject.map(DeviceInjectionConfig::ports).orElse("0"); |
| 183 | int numPorts = Integer.parseInt(ports); |
| 184 | List<PortDescription> portDescs = new ArrayList<>(numPorts); |
| 185 | for (int i = 1; i <= numPorts; ++i) { |
| 186 | // TODO inject port details if something like BasicPortConfig was created |
| 187 | PortNumber number = portNumber(i); |
| 188 | boolean isEnabled = true; |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 189 | portDescs.add(DefaultPortDescription.builder().withPortNumber(number) .isEnabled(isEnabled).build()); |
Yuta HIGUCHI | 76767fa | 2017-03-10 14:51:30 -0800 | [diff] [blame] | 190 | } |
| 191 | providerService.updatePorts(did, portDescs); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | final class InternalDeviceProvider implements DeviceProvider { |
| 196 | @Override |
| 197 | public ProviderId id() { |
| 198 | return pid; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void triggerProbe(DeviceId deviceId) { |
| 203 | worker.execute(() -> injectDevice(deviceId)); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 208 | providerService.receivedRoleReply(deviceId, newRole, newRole); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean isReachable(DeviceId deviceId) { |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 218 | boolean enable) { |
| 219 | // TODO handle request to change port state from controller |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Listens for Config updates. |
| 225 | */ |
| 226 | final class InternalConfigListener |
| 227 | implements NetworkConfigListener { |
| 228 | |
| 229 | /** |
| 230 | * Relevant {@link NetworkConfigEvent} type. |
| 231 | */ |
| 232 | private final Set<NetworkConfigEvent.Type> relevant |
| 233 | = ImmutableSet.copyOf(EnumSet.of( |
| 234 | NetworkConfigEvent.Type.CONFIG_ADDED, |
| 235 | NetworkConfigEvent.Type.CONFIG_UPDATED, |
| 236 | NetworkConfigEvent.Type.CONFIG_REMOVED)); |
| 237 | |
| 238 | @Override |
| 239 | public boolean isRelevant(NetworkConfigEvent event) { |
| 240 | return event.configClass() == DeviceInjectionConfig.class && |
| 241 | relevant.contains(event.type()); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void event(NetworkConfigEvent event) { |
| 246 | worker.execute(() -> processEvent(event)); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Process Config add/update/remove event. |
| 251 | * <p> |
| 252 | * Note: will be executed in the worker thread. |
| 253 | * |
| 254 | * @param event add/update/remove event |
| 255 | */ |
| 256 | protected void processEvent(NetworkConfigEvent event) { |
| 257 | |
| 258 | DeviceId did = (DeviceId) event.subject(); |
| 259 | if (!did.uri().getScheme().equals(pid.scheme())) { |
| 260 | log.warn("Attempt to inject unexpected scheme {}", did); |
| 261 | return; |
| 262 | } |
| 263 | log.debug("{} to {}: {}", event.type(), did, event); |
| 264 | |
| 265 | // TODO if there's exist one probably better to follow master |
| 266 | // if (deviceService.getRole(did) != MastershipRole.MASTER) { |
| 267 | // log.debug("Not the master, ignoring. {}", event); |
| 268 | // return; |
| 269 | // } |
| 270 | |
| 271 | switch (event.type()) { |
| 272 | case CONFIG_ADDED: |
| 273 | case CONFIG_UPDATED: |
| 274 | injectDevice(did); |
| 275 | break; |
| 276 | case CONFIG_REMOVED: |
| 277 | removeDevice(did); |
| 278 | break; |
| 279 | |
| 280 | case CONFIG_REGISTERED: |
| 281 | case CONFIG_UNREGISTERED: |
| 282 | default: |
| 283 | log.warn("Ignoring unexpected event: {}", event); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |