Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | e098172 | 2016-01-14 16:02:48 -0800 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.IdGenerator; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 30 | import org.onosproject.event.AbstractListenerManager; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 32 | import org.onosproject.net.config.NetworkConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRuleService; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 35 | import org.onosproject.net.group.GroupKey; |
| 36 | import org.onosproject.net.group.GroupService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentService; |
| 45 | import org.onosproject.net.intent.IntentState; |
| 46 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.Key; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 49 | import org.onosproject.net.intent.PointToPointIntent; |
| 50 | import org.onosproject.net.intent.impl.compiler.PointToPointIntentCompiler; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 53 | import org.onosproject.net.intent.impl.phase.Skipped; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 54 | import org.osgi.service.component.ComponentContext; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 55 | import org.onosproject.net.resource.ResourceService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 58 | import java.util.Collection; |
| 59 | import java.util.EnumSet; |
| 60 | import java.util.List; |
| 61 | import java.util.Map; |
Sho SHIMIZU | ab541a5 | 2016-01-13 23:29:32 -0800 | [diff] [blame] | 62 | import java.util.Objects; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 63 | import java.util.Optional; |
Sho SHIMIZU | ad8ab27 | 2016-01-13 22:08:58 -0800 | [diff] [blame] | 64 | import java.util.concurrent.CompletableFuture; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 65 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 67 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 69 | import static com.google.common.base.Strings.isNullOrEmpty; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 70 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 71 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 72 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 73 | import static org.onosproject.net.intent.IntentState.*; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 74 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 75 | import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 76 | import static org.onosproject.security.AppGuard.checkPermission; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 77 | import static org.onosproject.security.AppPermission.Type.INTENT_READ; |
| 78 | import static org.onosproject.security.AppPermission.Type.INTENT_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 79 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 80 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 81 | /** |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 82 | * An implementation of intent service. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 83 | */ |
| 84 | @Component(immediate = true) |
| 85 | @Service |
| 86 | public class IntentManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 87 | extends AbstractListenerManager<IntentEvent, IntentListener> |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 88 | implements IntentService, IntentExtensionService { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 89 | |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 90 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 91 | |
Sho SHIMIZU | 17c09da | 2016-03-31 12:54:17 -0700 | [diff] [blame] | 92 | private static final String INTENT_NULL = "Intent cannot be null"; |
| 93 | private static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 94 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 95 | private static final EnumSet<IntentState> RECOMPILE |
| 96 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 97 | private static final EnumSet<IntentState> WITHDRAW |
| 98 | = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 99 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 100 | private static final boolean DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL = false; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 101 | @Property(name = "skipReleaseResourcesOnWithdrawal", |
| 102 | boolValue = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL, |
| 103 | label = "Indicates whether skipping resource releases on withdrawal is enabled or not") |
| 104 | private boolean skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 105 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 106 | private static final int DEFAULT_NUM_THREADS = 12; |
| 107 | @Property(name = "numThreads", |
| 108 | intValue = DEFAULT_NUM_THREADS, |
| 109 | label = "Number of worker threads") |
| 110 | private int numThreads = DEFAULT_NUM_THREADS; |
| 111 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected IntentStore store; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 119 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 214ac32 | 2015-02-23 19:30:15 -0800 | [diff] [blame] | 122 | protected FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 123 | |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 125 | protected FlowObjectiveService flowObjectiveService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 128 | protected ResourceService resourceService; |
| 129 | |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected ComponentConfigService configService; |
| 132 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected GroupService groupService; |
| 135 | |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | private NetworkConfigService networkConfigService; |
| 138 | |
| 139 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 140 | private ExecutorService batchExecutor; |
| 141 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 142 | |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 143 | private final IntentInstaller intentInstaller = new IntentInstaller(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 144 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 145 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 146 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 147 | private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 148 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 149 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 150 | private IdGenerator idGenerator; |
| 151 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 152 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 153 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 154 | @Activate |
| 155 | public void activate() { |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 156 | configService.registerProperties(getClass()); |
| 157 | |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 158 | intentInstaller.init(store, trackerService, flowRuleService, flowObjectiveService, |
| 159 | networkConfigService); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 160 | if (skipReleaseResourcesOnWithdrawal) { |
| 161 | store.setDelegate(testOnlyDelegate); |
| 162 | } else { |
| 163 | store.setDelegate(delegate); |
| 164 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 165 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 167 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch", log)); |
| 168 | workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log)); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 169 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 170 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 171 | log.info("Started"); |
| 172 | } |
| 173 | |
| 174 | @Deactivate |
| 175 | public void deactivate() { |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 176 | intentInstaller.init(null, null, null, null, null); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 177 | if (skipReleaseResourcesOnWithdrawal) { |
| 178 | store.unsetDelegate(testOnlyDelegate); |
| 179 | } else { |
| 180 | store.unsetDelegate(delegate); |
| 181 | } |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 182 | configService.unregisterProperties(getClass(), false); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 183 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 184 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 185 | batchExecutor.shutdown(); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 186 | workerExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 187 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 188 | log.info("Stopped"); |
| 189 | } |
| 190 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 191 | @Modified |
| 192 | public void modified(ComponentContext context) { |
| 193 | if (context == null) { |
| 194 | skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL; |
| 195 | logConfig("Default config"); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | String s = Tools.get(context.getProperties(), "skipReleaseResourcesOnWithdrawal"); |
| 200 | boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim()); |
| 201 | if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) { |
| 202 | store.unsetDelegate(testOnlyDelegate); |
| 203 | store.setDelegate(delegate); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 204 | skipReleaseResourcesOnWithdrawal = false; |
| 205 | logConfig("Reconfigured skip release resources on withdrawal"); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 206 | } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) { |
| 207 | store.unsetDelegate(delegate); |
| 208 | store.setDelegate(testOnlyDelegate); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 209 | skipReleaseResourcesOnWithdrawal = true; |
| 210 | logConfig("Reconfigured skip release resources on withdrawal"); |
| 211 | } |
| 212 | |
| 213 | s = Tools.get(context.getProperties(), "numThreads"); |
| 214 | int newNumThreads = isNullOrEmpty(s) ? numThreads : Integer.parseInt(s); |
| 215 | if (newNumThreads != numThreads) { |
| 216 | numThreads = newNumThreads; |
| 217 | ExecutorService oldWorkerExecutor = workerExecutor; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 218 | workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log)); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 219 | if (oldWorkerExecutor != null) { |
| 220 | oldWorkerExecutor.shutdown(); |
| 221 | } |
| 222 | logConfig("Reconfigured number of worker threads"); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | private void logConfig(String prefix) { |
| 227 | log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal); |
| 228 | } |
| 229 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 230 | @Override |
| 231 | public void submit(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 232 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 233 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 234 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 235 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void withdraw(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 240 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 241 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 242 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 243 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 246 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 247 | public void purge(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 248 | checkPermission(INTENT_WRITE); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 249 | checkNotNull(intent, INTENT_NULL); |
| 250 | IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null); |
| 251 | store.addPending(data); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 252 | |
| 253 | // remove associated group if there is one |
| 254 | if (intent instanceof PointToPointIntent) { |
| 255 | PointToPointIntent pointIntent = (PointToPointIntent) intent; |
| 256 | DeviceId deviceId = pointIntent.ingressPoint().deviceId(); |
| 257 | GroupKey groupKey = PointToPointIntentCompiler.makeGroupKey(intent.id()); |
| 258 | groupService.removeGroup(deviceId, groupKey, |
| 259 | intent.appId()); |
| 260 | } |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 264 | public Intent getIntent(Key key) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 265 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 266 | return store.getIntent(key); |
| 267 | } |
| 268 | |
| 269 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 270 | public Iterable<Intent> getIntents() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 271 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 272 | return store.getIntents(); |
| 273 | } |
| 274 | |
| 275 | @Override |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 276 | public void addPending(IntentData intentData) { |
| 277 | checkPermission(INTENT_WRITE); |
| 278 | checkNotNull(intentData, INTENT_NULL); |
| 279 | //TODO we might consider further checking / assertions |
| 280 | store.addPending(intentData); |
| 281 | } |
| 282 | |
| 283 | @Override |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 284 | public Iterable<IntentData> getIntentData() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 285 | checkPermission(INTENT_READ); |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 286 | return store.getIntentData(false, 0); |
| 287 | } |
| 288 | |
| 289 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 290 | public long getIntentCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 291 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 292 | return store.getIntentCount(); |
| 293 | } |
| 294 | |
| 295 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 296 | public IntentState getIntentState(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 297 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 298 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 299 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 303 | public List<Intent> getInstallableIntents(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 304 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 305 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 306 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 310 | public boolean isLocal(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 311 | checkPermission(INTENT_READ); |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 312 | return store.isMaster(intentKey); |
| 313 | } |
| 314 | |
| 315 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 316 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 317 | checkPermission(INTENT_WRITE); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 318 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 323 | checkPermission(INTENT_WRITE); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 324 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 329 | checkPermission(INTENT_READ); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 330 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 334 | public Iterable<Intent> getPending() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 335 | checkPermission(INTENT_READ); |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 336 | return store.getPending(); |
| 337 | } |
| 338 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 339 | // Store delegate to re-post events emitted from the store. |
| 340 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 341 | @Override |
| 342 | public void notify(IntentEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 343 | post(event); |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 344 | switch (event.type()) { |
| 345 | case WITHDRAWN: |
| 346 | // release resources allocated to withdrawn intent |
| 347 | if (!resourceService.release(event.subject().id())) { |
| 348 | log.error("Failed to release resources allocated to {}", event.subject().id()); |
| 349 | } |
| 350 | break; |
| 351 | default: |
| 352 | break; |
| 353 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 354 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 355 | |
| 356 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 357 | public void process(IntentData data) { |
| 358 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 359 | } |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 360 | |
| 361 | @Override |
| 362 | public void onUpdate(IntentData intentData) { |
| 363 | trackerService.trackIntent(intentData); |
| 364 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 367 | // Store delegate enabled only when performing intent throughput tests |
| 368 | private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate { |
| 369 | @Override |
| 370 | public void process(IntentData data) { |
| 371 | accumulator.add(data); |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public void onUpdate(IntentData data) { |
| 376 | trackerService.trackIntent(data); |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public void notify(IntentEvent event) { |
| 381 | post(event); |
| 382 | } |
| 383 | } |
| 384 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 385 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 386 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 387 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 388 | for (Key key : intentKeys) { |
| 389 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 390 | if (intent == null) { |
| 391 | continue; |
| 392 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 393 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Jonathan Hart | 0cca3e8 | 2015-09-23 17:54:11 -0700 | [diff] [blame] | 396 | if (compileAllFailed) { |
| 397 | // If required, compile all currently failed intents. |
| 398 | for (Intent intent : getIntents()) { |
| 399 | IntentState state = getIntentState(intent.key()); |
| 400 | if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) { |
| 401 | if (WITHDRAW.contains(state)) { |
| 402 | withdraw(intent); |
| 403 | } else { |
| 404 | submit(intent); |
| 405 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 409 | } |
| 410 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 411 | // Topology change delegate |
| 412 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 413 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 414 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 415 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 416 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 417 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 418 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 419 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 420 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 421 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 422 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 423 | log.debug("Execute {} operation(s).", operations.size()); |
| 424 | log.trace("Execute operations: {}", operations); |
Sho SHIMIZU | 94b03b1 | 2015-04-10 14:53:13 -0700 | [diff] [blame] | 425 | |
| 426 | // batchExecutor is single-threaded, so only one batch is in flight at a time |
Sho SHIMIZU | 489aa9b | 2016-01-14 17:19:32 -0800 | [diff] [blame] | 427 | CompletableFuture.runAsync(() -> { |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 428 | // process intent until the phase reaches one of the final phases |
| 429 | List<CompletableFuture<IntentData>> futures = operations.stream() |
| 430 | .map(x -> CompletableFuture.completedFuture(x) |
| 431 | .thenApply(IntentManager.this::createInitialPhase) |
Sho SHIMIZU | 4a14185 | 2016-01-14 18:55:40 -0800 | [diff] [blame] | 432 | .thenApplyAsync(IntentProcessPhase::process, workerExecutor) |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 433 | .thenApply(FinalIntentProcessPhase::data) |
| 434 | .exceptionally(e -> { |
| 435 | //FIXME |
| 436 | log.warn("Future failed: {}", e); |
| 437 | return null; |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 438 | })) |
| 439 | .collect(Collectors.toList()); |
Sho SHIMIZU | 62bbc60 | 2016-01-13 16:54:35 -0800 | [diff] [blame] | 440 | |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 441 | // write multiple data to store in order |
| 442 | store.batchWrite(Tools.allOf(futures).join().stream() |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 443 | .filter(Objects::nonNull) |
| 444 | .collect(Collectors.toList())); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 445 | }, batchExecutor).exceptionally(e -> { |
| 446 | log.error("Error submitting batches:", e); |
| 447 | // FIXME incomplete Intents should be cleaned up |
| 448 | // (transition to FAILED, etc.) |
| 449 | |
| 450 | // the batch has failed |
| 451 | // TODO: maybe we should do more? |
| 452 | log.error("Walk the plank, matey..."); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 453 | return null; |
| 454 | }).thenRun(accumulator::ready); |
| 455 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 456 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 457 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 458 | |
Sho SHIMIZU | c88b85e | 2016-01-14 18:45:14 -0800 | [diff] [blame] | 459 | private IntentProcessPhase createInitialPhase(IntentData data) { |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 460 | IntentData pending = store.getPendingData(data.key()); |
| 461 | if (pending == null || pending.version().isNewerThan(data.version())) { |
| 462 | /* |
| 463 | If the pending map is null, then this intent was compiled by a |
| 464 | previous batch iteration, so we can skip it. |
| 465 | If the pending map has a newer request, it will get compiled as |
| 466 | part of the next batch, so we can skip it. |
| 467 | */ |
| 468 | return Skipped.getPhase(); |
| 469 | } |
Sho SHIMIZU | c88b85e | 2016-01-14 18:45:14 -0800 | [diff] [blame] | 470 | IntentData current = store.getIntentData(data.key()); |
| 471 | return newInitialPhase(processor, data, current); |
| 472 | } |
| 473 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 474 | private class InternalIntentProcessor implements IntentProcessor { |
| 475 | @Override |
| 476 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 477 | return compilerRegistry.compile(intent, previousInstallables); |
| 478 | } |
| 479 | |
| 480 | @Override |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 481 | public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 482 | intentInstaller.apply(toUninstall, toInstall); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 483 | } |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 484 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 485 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 486 | } |