Michele Santuari | 03df2a5 | 2016-11-24 10:35:13 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | 03df2a5 | 2016-11-24 10:35:13 +0100 | [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.domain.impl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.util.ItemNotFoundException; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.behaviour.DomainIntentConfigurable; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.domain.DomainIntent; |
| 32 | import org.onosproject.net.domain.DomainIntentOperation; |
| 33 | import org.onosproject.net.domain.DomainIntentOperations; |
| 34 | import org.onosproject.net.domain.DomainIntentService; |
| 35 | import org.onosproject.net.driver.DriverHandler; |
| 36 | import org.onosproject.net.driver.DriverService; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | import java.util.Objects; |
| 43 | import java.util.Optional; |
| 44 | import java.util.concurrent.ExecutorService; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
| 47 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 48 | import static org.onlab.util.Tools.groupedThreads; |
| 49 | import static org.onosproject.net.domain.DomainIntentOperation.Type.ADD; |
| 50 | import static org.onosproject.net.domain.DomainIntentOperation.Type.REMOVE; |
| 51 | |
| 52 | /** |
| 53 | * {@link DomainIntentService} implementation class. |
| 54 | */ |
| 55 | @Component(immediate = true) |
| 56 | @Service |
| 57 | public class DomainIntentManager implements DomainIntentService { |
| 58 | |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected DeviceService deviceService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected DriverService driverService; |
| 66 | |
| 67 | private ExecutorService executorService = |
| 68 | newFixedThreadPool(Runtime.getRuntime().availableProcessors(), |
| 69 | groupedThreads("onos/domain-intent-mgmt", "%d", log)); |
| 70 | |
| 71 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
| 72 | private final Map<DeviceId, DomainIntentConfigurable> driversMap = Maps.newConcurrentMap(); |
| 73 | |
| 74 | @Activate |
| 75 | public void activate() { |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
| 81 | log.info("Stopped"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void sumbit(DomainIntentOperations domainOperations) { |
| 86 | executorService.execute(new DomainIntentProcessor(domainOperations)); |
| 87 | } |
| 88 | |
| 89 | private class DomainIntentProcessor implements Runnable { |
| 90 | private final DomainIntentOperations idos; |
| 91 | |
| 92 | private final List<DomainIntentOperation> stages; |
| 93 | private boolean hasFailed = false; |
| 94 | |
| 95 | public DomainIntentProcessor(DomainIntentOperations dios) { |
| 96 | this.idos = checkNotNull(dios); |
| 97 | this.stages = Lists.newArrayList(checkNotNull(dios.stages())); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public synchronized void run() { |
| 102 | if (stages.size() > 0) { |
| 103 | process(stages.remove(0)); |
| 104 | } else if (!hasFailed) { |
| 105 | idos.callback().onSuccess(idos); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | private void process(DomainIntentOperation dio) { |
| 111 | Optional<DomainIntentConfigurable> config = |
| 112 | dio.intent().filteredIngressPoints().stream() |
| 113 | .map(x -> getDomainIntentConfigurable(x.connectPoint().deviceId())) |
| 114 | .filter(Objects::nonNull) |
| 115 | .findAny(); |
| 116 | DomainIntent domainIntent = null; |
| 117 | if (config.isPresent()) { |
| 118 | if (dio.type() == ADD) { |
| 119 | domainIntent = config.get().sumbit(dio.intent()); |
| 120 | } else if (dio.type() == REMOVE) { |
| 121 | domainIntent = config.get().remove(dio.intent()); |
| 122 | } |
| 123 | executorService.execute(this); |
| 124 | } else { |
| 125 | log.error("Ingresses devices does not support " + |
| 126 | "DomainIntentConfigurable. Installation failed"); |
| 127 | hasFailed = true; |
| 128 | } |
| 129 | if (domainIntent == null) { |
| 130 | log.error("Installation failed for Domain Intent {}", dio.intent()); |
| 131 | hasFailed = true; |
| 132 | } |
| 133 | if (hasFailed) { |
| 134 | DomainIntentOperations failedBuilder = DomainIntentOperations.builder() |
| 135 | .add(dio.intent()).build(); |
| 136 | idos.callback().onError(failedBuilder); |
| 137 | executorService.execute(this); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | private DomainIntentConfigurable getDomainIntentConfigurable(DeviceId deviceId) { |
| 144 | return driversMap.computeIfAbsent(deviceId, this::initDomainIntentDriver); |
| 145 | } |
| 146 | |
| 147 | private DomainIntentConfigurable initDomainIntentDriver(DeviceId deviceId) { |
| 148 | |
| 149 | // Attempt to lookup the handler in the cache |
| 150 | DriverHandler handler = driverHandlers.get(deviceId); |
| 151 | if (handler == null) { |
| 152 | try { |
| 153 | // Otherwise create it and if it has DomainIntentConfig behaviour, cache it |
| 154 | handler = driverService.createHandler(deviceId); |
| 155 | |
| 156 | if (!handler.driver().hasBehaviour(DomainIntentConfigurable.class)) { |
| 157 | log.warn("DomainIntentConfig behaviour not supported for device {}", |
| 158 | deviceId); |
| 159 | return null; |
| 160 | } |
| 161 | } catch (ItemNotFoundException e) { |
| 162 | log.warn("No applicable driver for device {}", deviceId); |
| 163 | return null; |
| 164 | } |
| 165 | driverHandlers.put(deviceId, handler); |
| 166 | } |
| 167 | // Always (re)initialize the pipeline behaviour |
| 168 | log.info("Driver {} bound to device {} ... initializing driver", |
| 169 | handler.driver().name(), deviceId); |
| 170 | |
| 171 | return handler.behaviour(DomainIntentConfigurable.class); |
| 172 | } |
| 173 | } |