Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.IdGenerator; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 27 | import org.onosproject.core.Permission; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 28 | import org.onosproject.event.ListenerRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.event.EventDeliveryService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.FlowRuleOperations; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRuleService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.FlowRuleIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.Intent; |
| 36 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentService; |
| 43 | import org.onosproject.net.intent.IntentState; |
| 44 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.Key; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.impl.phase.IntentWorker; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 52 | import java.util.Collection; |
| 53 | import java.util.EnumSet; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
| 57 | import java.util.concurrent.ExecutionException; |
| 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Future; |
| 60 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 61 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 63 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 64 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 65 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 66 | import static org.onosproject.net.intent.IntentState.*; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 67 | import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 69 | import static org.onosproject.security.AppGuard.checkPermission; |
| 70 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * An implementation of Intent Manager. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | @Service |
| 77 | public class IntentManager |
| 78 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 79 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 80 | |
| 81 | public static final String INTENT_NULL = "Intent cannot be null"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 82 | public static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 83 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 84 | private static final int NUM_THREADS = 12; |
| 85 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 86 | private static final EnumSet<IntentState> RECOMPILE |
| 87 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 88 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 89 | private final ListenerRegistry<IntentEvent, IntentListener> |
| 90 | listenerRegistry = new ListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 91 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected IntentStore store; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 99 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 102 | protected EventDeliveryService eventDispatcher; |
| 103 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 214ac32 | 2015-02-23 19:30:15 -0800 | [diff] [blame] | 105 | protected FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 106 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 107 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 108 | private ExecutorService batchExecutor; |
| 109 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 110 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 111 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 112 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 113 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 114 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 115 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 116 | private IdGenerator idGenerator; |
| 117 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 118 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 119 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 120 | @Activate |
| 121 | public void activate() { |
| 122 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 123 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 124 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 125 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch")); |
| 126 | workerExecutor = newFixedThreadPool(NUM_THREADS, groupedThreads("onos/intent", "worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 127 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 128 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 129 | log.info("Started"); |
| 130 | } |
| 131 | |
| 132 | @Deactivate |
| 133 | public void deactivate() { |
| 134 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 135 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 136 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 137 | batchExecutor.shutdown(); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 138 | workerExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 139 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 140 | log.info("Stopped"); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void submit(Intent intent) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 145 | checkPermission(Permission.INTENT_WRITE); |
| 146 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 147 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 148 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 149 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void withdraw(Intent intent) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 154 | checkPermission(Permission.INTENT_WRITE); |
| 155 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 156 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 157 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 158 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 161 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 162 | public void purge(Intent intent) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 163 | checkPermission(Permission.INTENT_WRITE); |
| 164 | |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 165 | checkNotNull(intent, INTENT_NULL); |
| 166 | IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null); |
| 167 | store.addPending(data); |
| 168 | } |
| 169 | |
| 170 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 171 | public Intent getIntent(Key key) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 172 | checkPermission(Permission.INTENT_READ); |
| 173 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 174 | return store.getIntent(key); |
| 175 | } |
| 176 | |
| 177 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 178 | public Iterable<Intent> getIntents() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 179 | checkPermission(Permission.INTENT_READ); |
| 180 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 181 | return store.getIntents(); |
| 182 | } |
| 183 | |
| 184 | @Override |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 185 | public Iterable<IntentData> getIntentData() { |
| 186 | checkPermission(Permission.INTENT_READ); |
| 187 | return store.getIntentData(false, 0); |
| 188 | } |
| 189 | |
| 190 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 191 | public long getIntentCount() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 192 | checkPermission(Permission.INTENT_READ); |
| 193 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 194 | return store.getIntentCount(); |
| 195 | } |
| 196 | |
| 197 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 198 | public IntentState getIntentState(Key intentKey) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 199 | checkPermission(Permission.INTENT_READ); |
| 200 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 201 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 202 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 206 | public List<Intent> getInstallableIntents(Key intentKey) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 207 | checkPermission(Permission.INTENT_READ); |
| 208 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 209 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 210 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 214 | public boolean isLocal(Key intentKey) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 215 | checkPermission(Permission.INTENT_READ); |
| 216 | |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 217 | return store.isMaster(intentKey); |
| 218 | } |
| 219 | |
| 220 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 221 | public void addListener(IntentListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 222 | checkPermission(Permission.INTENT_EVENT); |
| 223 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 224 | listenerRegistry.addListener(listener); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void removeListener(IntentListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 229 | checkPermission(Permission.INTENT_EVENT); |
| 230 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 231 | listenerRegistry.removeListener(listener); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 236 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 241 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 246 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 250 | public Iterable<Intent> getPending() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 251 | checkPermission(Permission.INTENT_READ); |
| 252 | |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 253 | return store.getPending(); |
| 254 | } |
| 255 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 256 | // Store delegate to re-post events emitted from the store. |
| 257 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 258 | @Override |
| 259 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 260 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 261 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 262 | |
| 263 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 264 | public void process(IntentData data) { |
| 265 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 266 | } |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 267 | |
| 268 | @Override |
| 269 | public void onUpdate(IntentData intentData) { |
| 270 | trackerService.trackIntent(intentData); |
| 271 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 274 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 275 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 276 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 277 | for (Key key : intentKeys) { |
| 278 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 279 | if (intent == null) { |
| 280 | continue; |
| 281 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 282 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (compileAllFailed) { |
| 286 | // If required, compile all currently failed intents. |
| 287 | for (Intent intent : getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 288 | IntentState state = getIntentState(intent.key()); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 289 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 290 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 291 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 292 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 293 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 294 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 299 | //FIXME |
| 300 | // for (ApplicationId appId : batches.keySet()) { |
| 301 | // if (batchService.isLocalLeader(appId)) { |
| 302 | // execute(batches.get(appId).build()); |
| 303 | // } |
| 304 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 305 | } |
| 306 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 307 | // Topology change delegate |
| 308 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 309 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 310 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 311 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 312 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 313 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 314 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 315 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 316 | private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { |
Sho SHIMIZU | 0cb6fe6 | 2015-02-23 16:39:57 -0800 | [diff] [blame] | 317 | IntentData current = store.getIntentData(data.key()); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 318 | IntentProcessPhase initial = newInitialPhase(processor, data, current); |
| 319 | return workerExecutor.submit(new IntentWorker(initial)); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 322 | private class IntentBatchProcess implements Runnable { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 323 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 324 | protected final Collection<IntentData> data; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 325 | |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 326 | IntentBatchProcess(Collection<IntentData> data) { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 327 | this.data = checkNotNull(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void run() { |
| 332 | try { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 333 | /* |
| 334 | 1. wrap each intentdata in a runnable and submit |
| 335 | 2. wait for completion of all the work |
| 336 | 3. accumulate results and submit batch write of IntentData to store |
| 337 | (we can also try to update these individually) |
| 338 | */ |
| 339 | submitUpdates(waitForFutures(createIntentUpdates())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 340 | } catch (Exception e) { |
| 341 | log.error("Error submitting batches:", e); |
| 342 | // FIXME incomplete Intents should be cleaned up |
| 343 | // (transition to FAILED, etc.) |
| 344 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 345 | // the batch has failed |
| 346 | // TODO: maybe we should do more? |
| 347 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 348 | //FIXME |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 349 | // batchService.removeIntentOperations(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 350 | } |
Brian O'Connor | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 351 | accumulator.ready(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 354 | private List<Future<FinalIntentProcessPhase>> createIntentUpdates() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 355 | return data.stream() |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 356 | .map(IntentManager.this::submitIntentData) |
| 357 | .collect(Collectors.toList()); |
| 358 | } |
| 359 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 360 | private List<FinalIntentProcessPhase> waitForFutures(List<Future<FinalIntentProcessPhase>> futures) { |
| 361 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); |
| 362 | for (Future<FinalIntentProcessPhase> future : futures) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 363 | try { |
| 364 | updateBuilder.add(future.get()); |
| 365 | } catch (InterruptedException | ExecutionException e) { |
| 366 | //FIXME |
| 367 | log.warn("Future failed: {}", e); |
| 368 | } |
| 369 | } |
| 370 | return updateBuilder.build(); |
| 371 | } |
| 372 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 373 | private void submitUpdates(List<FinalIntentProcessPhase> updates) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 374 | store.batchWrite(updates.stream() |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 375 | .map(FinalIntentProcessPhase::data) |
| 376 | .collect(Collectors.toList())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 377 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 378 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 379 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 380 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 381 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 382 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 383 | log.debug("Execute {} operation(s).", operations.size()); |
| 384 | log.trace("Execute operations: {}", operations); |
Sho SHIMIZU | 94b03b1 | 2015-04-10 14:53:13 -0700 | [diff] [blame] | 385 | |
| 386 | // batchExecutor is single-threaded, so only one batch is in flight at a time |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 387 | batchExecutor.execute(new IntentBatchProcess(operations)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 388 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 389 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 390 | |
| 391 | private class InternalIntentProcessor implements IntentProcessor { |
| 392 | @Override |
| 393 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 394 | return compilerRegistry.compile(intent, previousInstallables); |
| 395 | } |
| 396 | |
| 397 | @Override |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 398 | public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 399 | IntentManager.this.apply(toUninstall, toInstall); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 400 | } |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 401 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 402 | |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 403 | private enum Direction { |
| 404 | ADD, |
| 405 | REMOVE |
| 406 | } |
| 407 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 408 | private void applyIntentData(Optional<IntentData> intentData, |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 409 | FlowRuleOperations.Builder builder, |
| 410 | Direction direction) { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 411 | if (!intentData.isPresent()) { |
Sho SHIMIZU | fcf861a | 2015-04-10 14:37:08 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 414 | IntentData data = intentData.get(); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 415 | |
Sho SHIMIZU | fcf861a | 2015-04-10 14:37:08 -0700 | [diff] [blame] | 416 | List<Intent> intentsToApply = data.installables(); |
| 417 | if (!intentsToApply.stream().allMatch(x -> x instanceof FlowRuleIntent)) { |
| 418 | throw new IllegalStateException("installable intents must be FlowRuleIntent"); |
| 419 | } |
| 420 | |
| 421 | if (direction == Direction.ADD) { |
| 422 | trackerService.addTrackedResources(data.key(), data.intent().resources()); |
| 423 | intentsToApply.forEach(installable -> |
| 424 | trackerService.addTrackedResources(data.key(), installable.resources())); |
| 425 | } else { |
| 426 | trackerService.removeTrackedResources(data.key(), data.intent().resources()); |
| 427 | intentsToApply.forEach(installable -> |
| 428 | trackerService.removeTrackedResources(data.intent().key(), |
| 429 | installable.resources())); |
| 430 | } |
| 431 | |
| 432 | // FIXME do FlowRuleIntents have stages??? Can we do uninstall work in parallel? I think so. |
| 433 | builder.newStage(); |
| 434 | |
| 435 | List<Collection<FlowRule>> stages = intentsToApply.stream() |
| 436 | .map(x -> (FlowRuleIntent) x) |
| 437 | .map(FlowRuleIntent::flowRules) |
| 438 | .collect(Collectors.toList()); |
| 439 | |
| 440 | for (Collection<FlowRule> rules : stages) { |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 441 | if (direction == Direction.ADD) { |
Sho SHIMIZU | fcf861a | 2015-04-10 14:37:08 -0700 | [diff] [blame] | 442 | rules.forEach(builder::add); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 443 | } else { |
Sho SHIMIZU | fcf861a | 2015-04-10 14:37:08 -0700 | [diff] [blame] | 444 | rules.forEach(builder::remove); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 445 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 450 | private void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 451 | // need to consider if FlowRuleIntent is only one as installable intent or not |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 452 | |
| 453 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 454 | applyIntentData(toUninstall, builder, Direction.REMOVE); |
| 455 | applyIntentData(toInstall, builder, Direction.ADD); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 456 | |
| 457 | FlowRuleOperations operations = builder.build(new FlowRuleOperationsContext() { |
| 458 | @Override |
| 459 | public void onSuccess(FlowRuleOperations ops) { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 460 | if (toInstall.isPresent()) { |
| 461 | IntentData installData = toInstall.get(); |
| 462 | log.debug("Completed installing: {}", installData.key()); |
| 463 | installData.setState(INSTALLED); |
| 464 | store.write(installData); |
| 465 | } else if (toUninstall.isPresent()) { |
| 466 | IntentData uninstallData = toUninstall.get(); |
| 467 | log.debug("Completed withdrawing: {}", uninstallData.key()); |
| 468 | switch (uninstallData.request()) { |
| 469 | case INSTALL_REQ: |
| 470 | uninstallData.setState(FAILED); |
| 471 | break; |
| 472 | case WITHDRAW_REQ: |
| 473 | default: //TODO "default" case should not happen |
| 474 | uninstallData.setState(WITHDRAWN); |
| 475 | break; |
| 476 | } |
| 477 | store.write(uninstallData); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 478 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void onError(FlowRuleOperations ops) { |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 483 | // if toInstall was cause of error, then recompile (manage/increment counter, when exceeded -> CORRUPT) |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 484 | if (toInstall.isPresent()) { |
| 485 | IntentData installData = toInstall.get(); |
| 486 | log.warn("Failed installation: {} {} on {}", |
| 487 | installData.key(), installData.intent(), ops); |
| 488 | installData.setState(CORRUPT); |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 489 | installData.incrementErrorCount(); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 490 | store.write(installData); |
| 491 | } |
Sho SHIMIZU | d493625 | 2015-04-10 15:03:57 -0700 | [diff] [blame] | 492 | // if toUninstall was cause of error, then CORRUPT (another job will clean this up) |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 493 | if (toUninstall.isPresent()) { |
| 494 | IntentData uninstallData = toUninstall.get(); |
| 495 | log.warn("Failed withdrawal: {} {} on {}", |
| 496 | uninstallData.key(), uninstallData.intent(), ops); |
| 497 | uninstallData.setState(CORRUPT); |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 498 | uninstallData.incrementErrorCount(); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 499 | store.write(uninstallData); |
| 500 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 501 | } |
| 502 | }); |
| 503 | |
Brian O'Connor | a767426 | 2015-06-04 13:55:18 -0700 | [diff] [blame] | 504 | if (log.isTraceEnabled()) { |
| 505 | log.trace("applying intent {} -> {} with {} rules: {}", |
| 506 | toUninstall.isPresent() ? toUninstall.get().key() : "<empty>", |
| 507 | toInstall.isPresent() ? toInstall.get().key() : "<empty>", |
| 508 | operations.stages().stream().mapToLong(i -> i.size()).sum(), |
| 509 | operations.stages()); |
| 510 | } |
| 511 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 512 | flowRuleService.apply(operations); |
| 513 | } |
| 514 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 515 | } |