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