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