Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -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 | 1d0d9cc | 2016-11-14 16:26:24 -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.intent.impl; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.MastershipRole; |
| 23 | import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription; |
| 24 | import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState; |
| 25 | import org.onosproject.net.behaviour.protection.ProtectionConfig; |
| 26 | import org.onosproject.net.behaviour.protection.ProtectionConfigBehaviour; |
| 27 | import org.onosproject.net.config.ConfigFactory; |
| 28 | import org.onosproject.net.config.NetworkConfigEvent; |
| 29 | import org.onosproject.net.config.NetworkConfigListener; |
| 30 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 31 | import org.onosproject.net.config.NetworkConfigService; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.driver.DriverHandler; |
| 34 | import org.onosproject.net.driver.DriverService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Deactivate; |
| 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | import java.util.EnumSet; |
| 43 | import java.util.List; |
| 44 | import java.util.Map; |
| 45 | import java.util.Map.Entry; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
| 48 | import java.util.concurrent.CompletableFuture; |
| 49 | import java.util.concurrent.ExecutionException; |
| 50 | import java.util.concurrent.ExecutorService; |
| 51 | import java.util.concurrent.TimeUnit; |
| 52 | |
| 53 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 54 | import static org.onlab.util.Tools.groupedThreads; |
| 55 | import static org.onosproject.net.config.basics.SubjectFactories.DEVICE_SUBJECT_FACTORY; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 57 | |
| 58 | // TODO In theory just @Component should be sufficient, |
| 59 | // but won't work without @Service. Need investigation. |
| 60 | /** |
| 61 | * Component to monitor {@link ProtectionConfig} changes. |
| 62 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Component(immediate = true, service = ProtectionConfigMonitor.class) |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 64 | public class ProtectionConfigMonitor { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 69 | protected NetworkConfigService networkConfigService; |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 72 | protected DeviceService deviceService; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 75 | protected DriverService driverService; |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 78 | protected NetworkConfigRegistry cfgRegistry; |
| 79 | |
| 80 | private final List<ConfigFactory<?, ?>> factories = ImmutableList.of( |
| 81 | new ConfigFactory<DeviceId, ProtectionConfig>(DEVICE_SUBJECT_FACTORY, |
| 82 | ProtectionConfig.class, ProtectionConfig.CONFIG_KEY) { |
| 83 | @Override |
| 84 | public ProtectionConfig createConfig() { |
| 85 | return new ProtectionConfig(); |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | |
| 90 | private final ProtectionConfigListener listener = new ProtectionConfigListener(); |
| 91 | |
| 92 | private ExecutorService worker; |
| 93 | |
| 94 | |
| 95 | @Activate |
| 96 | public void activate() { |
| 97 | worker = newSingleThreadExecutor(groupedThreads("onos/protection", |
| 98 | "installer", |
| 99 | log)); |
| 100 | networkConfigService.addListener(listener); |
| 101 | |
| 102 | factories.forEach(cfgRegistry::registerConfigFactory); |
| 103 | |
| 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | public void deactivate() { |
| 109 | networkConfigService.removeListener(listener); |
| 110 | |
| 111 | worker.shutdown(); |
| 112 | try { |
| 113 | worker.awaitTermination(5, TimeUnit.SECONDS); |
| 114 | } catch (InterruptedException e) { |
| 115 | log.warn("Interrupted.", e); |
| 116 | Thread.currentThread().interrupt(); |
| 117 | } |
| 118 | factories.forEach(cfgRegistry::unregisterConfigFactory); |
| 119 | |
| 120 | log.info("Stopped"); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Retrieves {@link ProtectionConfigBehaviour} for the Device. |
| 125 | * |
| 126 | * @param did {@link DeviceId} of the Device to fetch |
| 127 | * @return {@link ProtectionConfigBehaviour} |
| 128 | * or throws {@link UnsupportedOperationException} on error. |
| 129 | */ |
| 130 | private ProtectionConfigBehaviour getBehaviour(DeviceId did) { |
| 131 | DriverHandler handler = driverService.createHandler(did); |
| 132 | if (!handler.hasBehaviour(ProtectionConfigBehaviour.class)) { |
| 133 | log.error("{} does not support protection", did); |
| 134 | throw new UnsupportedOperationException(did + " does not support protection"); |
| 135 | } |
| 136 | |
| 137 | return handler.behaviour(ProtectionConfigBehaviour.class); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Retrieves first virtual Port with specified fingerprint. |
| 142 | * |
| 143 | * @param behaviour to use to query the Device |
| 144 | * @param fingerprint to look for |
| 145 | * @return virtual Port {@link ConnectPoint} if found. |
| 146 | */ |
| 147 | private Optional<ConnectPoint> findFirstVirtualPort(ProtectionConfigBehaviour behaviour, |
| 148 | String fingerprint) { |
| 149 | |
| 150 | CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointState>> |
| 151 | states = behaviour.getProtectionEndpointStates(); |
| 152 | |
| 153 | Map<ConnectPoint, ProtectedTransportEndpointState> map; |
| 154 | try { |
| 155 | map = states.get(); |
| 156 | } catch (InterruptedException e1) { |
| 157 | log.error("Interrupted.", e1); |
| 158 | Thread.currentThread().interrupt(); |
| 159 | return Optional.empty(); |
| 160 | } catch (ExecutionException e1) { |
| 161 | log.error("Exception caught.", e1); |
| 162 | return Optional.empty(); |
| 163 | } |
| 164 | |
| 165 | // TODO this is not clean, should add utility method to API? |
| 166 | return map.entrySet().stream() |
| 167 | .filter(e -> fingerprint.equals(e.getValue().description().fingerprint())) |
| 168 | .map(Entry::getKey) |
| 169 | .findFirst(); |
| 170 | } |
| 171 | |
| 172 | private void addProtection(DeviceId did, ProtectionConfig added) { |
| 173 | ProtectedTransportEndpointDescription description = added.asDescription(); |
| 174 | log.info("adding protection {}-{}", did, description); |
| 175 | |
| 176 | ProtectionConfigBehaviour behaviour = getBehaviour(did); |
| 177 | |
| 178 | |
| 179 | CompletableFuture<ConnectPoint> result; |
| 180 | result = behaviour.createProtectionEndpoint(description); |
| 181 | result.handle((vPort, e) -> { |
| 182 | if (vPort != null) { |
| 183 | log.info("Virtual Port {} created for {}", vPort, description); |
| 184 | log.debug("{}", deviceService.getPort(vPort)); |
| 185 | } else { |
| 186 | log.error("Protection {} exceptionally failed.", added, e); |
| 187 | } |
| 188 | return vPort; |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | private void updateProtection(DeviceId did, ProtectionConfig before, ProtectionConfig after) { |
| 193 | ProtectedTransportEndpointDescription description = after.asDescription(); |
| 194 | log.info("updating protection {}-{}", did, description); |
| 195 | |
| 196 | ProtectionConfigBehaviour behaviour = getBehaviour(did); |
| 197 | |
| 198 | Optional<ConnectPoint> existing = findFirstVirtualPort(behaviour, after.fingerprint()); |
| 199 | if (!existing.isPresent()) { |
| 200 | log.warn("Update requested, but not found, falling back as add"); |
| 201 | addProtection(did, after); |
| 202 | return; |
| 203 | } |
| 204 | ConnectPoint vPort = existing.get(); |
| 205 | log.info("updating protection virtual Port {} : {}", vPort, description); |
| 206 | behaviour.updateProtectionEndpoint(vPort, description) |
| 207 | .handle((vPortNew, e) -> { |
| 208 | if (vPort != null) { |
| 209 | log.info("Virtual Port {} updated for {}", vPort, description); |
| 210 | log.debug("{}", deviceService.getPort(vPort)); |
| 211 | } else { |
| 212 | log.error("Protection {} -> {} exceptionally failed.", |
| 213 | before, after, e); |
| 214 | } |
| 215 | return vPort; |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | private void removeProtection(DeviceId did, ProtectionConfig removed) { |
| 220 | ProtectedTransportEndpointDescription description = removed.asDescription(); |
| 221 | log.info("removing protection {}-{}", did, description); |
| 222 | |
| 223 | ProtectionConfigBehaviour behaviour = getBehaviour(did); |
| 224 | |
| 225 | Optional<ConnectPoint> existing = findFirstVirtualPort(behaviour, removed.fingerprint()); |
| 226 | if (!existing.isPresent()) { |
| 227 | log.warn("Remove requested, but not found, ignoring"); |
| 228 | return; |
| 229 | } |
| 230 | ConnectPoint vPort = existing.get(); |
| 231 | |
| 232 | log.info("removing protection virtual port {} : {}", vPort, description); |
| 233 | behaviour.deleteProtectionEndpoint(vPort) |
| 234 | .handle((result, ex) -> { |
| 235 | if (ex != null) { |
| 236 | log.info("removed protection {} : {}", vPort, result); |
| 237 | } else { |
| 238 | log.warn("removed protection {} failed.", vPort, ex); |
| 239 | } |
| 240 | return result; |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Listens for new {@link ProtectionConfig} to install/remove. |
| 246 | */ |
| 247 | public class ProtectionConfigListener |
| 248 | implements NetworkConfigListener { |
| 249 | |
| 250 | /** |
| 251 | * Relevant {@link NetworkConfigEvent} type. |
| 252 | */ |
| 253 | private final Set<NetworkConfigEvent.Type> relevant |
| 254 | = ImmutableSet.copyOf(EnumSet.of( |
| 255 | NetworkConfigEvent.Type.CONFIG_ADDED, |
| 256 | NetworkConfigEvent.Type.CONFIG_UPDATED, |
| 257 | NetworkConfigEvent.Type.CONFIG_REMOVED)); |
| 258 | |
| 259 | @Override |
| 260 | public boolean isRelevant(NetworkConfigEvent event) { |
| 261 | return event.configClass() == ProtectionConfig.class && |
| 262 | relevant.contains(event.type()); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void event(NetworkConfigEvent event) { |
| 267 | worker.execute(() -> processEvent(event)); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Process {@link ProtectionConfig} add/update/remove event. |
| 272 | * <p> |
| 273 | * Note: will be executed in the worker thread. |
| 274 | * |
| 275 | * @param event {@link ProtectionConfig} add/update/remove event |
| 276 | */ |
| 277 | protected void processEvent(NetworkConfigEvent event) { |
| 278 | |
| 279 | final DeviceId did = (DeviceId) event.subject(); |
| 280 | log.debug("{} to {}: {}", event.type(), did, event); |
| 281 | |
| 282 | if (deviceService.getRole(did) != MastershipRole.MASTER) { |
| 283 | log.debug("Not the master, ignoring. {}", event); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | switch (event.type()) { |
| 288 | case CONFIG_ADDED: |
| 289 | addProtection(did, |
| 290 | (ProtectionConfig) event.config().get()); |
| 291 | break; |
| 292 | case CONFIG_UPDATED: |
| 293 | updateProtection(did, |
| 294 | (ProtectionConfig) event.prevConfig().get(), |
| 295 | (ProtectionConfig) event.config().get()); |
| 296 | break; |
| 297 | case CONFIG_REMOVED: |
| 298 | removeProtection(did, |
| 299 | (ProtectionConfig) event.prevConfig().get()); |
| 300 | break; |
| 301 | |
| 302 | case CONFIG_REGISTERED: |
| 303 | case CONFIG_UNREGISTERED: |
| 304 | default: |
| 305 | log.warn("Ignoring unexpected event: {}", event); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | } |