Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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.store.intent.impl; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 18 | import com.codahale.metrics.Timer; |
| 19 | import com.codahale.metrics.Timer.Context; |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 20 | import com.google.common.base.Verify; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 21 | import com.google.common.cache.CacheBuilder; |
| 22 | import com.google.common.cache.CacheLoader; |
| 23 | import com.google.common.cache.LoadingCache; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 32 | import org.onlab.metrics.MetricsService; |
Jonathan Hart | c036367 | 2015-01-20 16:21:08 -0800 | [diff] [blame] | 33 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.core.MetricsHelper; |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.BatchWrite; |
Jonathan Hart | c036367 | 2015-01-20 16:21:08 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.BatchWrite.Operation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentEvent; |
| 39 | import org.onosproject.net.intent.IntentId; |
| 40 | import org.onosproject.net.intent.IntentState; |
| 41 | import org.onosproject.net.intent.IntentStore; |
| 42 | import org.onosproject.net.intent.IntentStoreDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.store.AbstractStore; |
| 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.serializers.KryoSerializer; |
| 46 | import org.onosproject.store.serializers.StoreSerializer; |
| 47 | import org.onosproject.store.service.BatchWriteRequest; |
| 48 | import org.onosproject.store.service.BatchWriteRequest.Builder; |
| 49 | import org.onosproject.store.service.BatchWriteResult; |
| 50 | import org.onosproject.store.service.DatabaseAdminService; |
| 51 | import org.onosproject.store.service.DatabaseService; |
| 52 | import org.onosproject.store.service.impl.CMap; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 55 | import java.util.ArrayList; |
Sho SHIMIZU | 2bb988b | 2015-01-20 13:45:35 -0800 | [diff] [blame] | 56 | import java.util.Collections; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 57 | import java.util.EnumSet; |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 58 | import java.util.HashSet; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 61 | import java.util.Set; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 62 | import java.util.concurrent.ConcurrentHashMap; |
| 63 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 64 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | c036367 | 2015-01-20 16:21:08 -0800 | [diff] [blame] | 65 | import static org.onlab.metrics.MetricsUtil.startTimer; |
| 66 | import static org.onlab.metrics.MetricsUtil.stopTimer; |
| 67 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 68 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
| 69 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 70 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 71 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 72 | |
Yuta HIGUCHI | 9b108b3 | 2014-12-01 11:10:26 -0800 | [diff] [blame] | 73 | @Component(immediate = true, enabled = false) |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 74 | @Service |
| 75 | public class DistributedIntentStore |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 76 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 77 | implements IntentStore, MetricsHelper { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 78 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 79 | /** Valid parking state, which can transition to INSTALLED. */ |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 80 | private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(INSTALL_REQ, INSTALLED, FAILED); |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 81 | |
| 82 | /** Valid parking state, which can transition to WITHDRAWN. */ |
| 83 | private static final Set<IntentState> PRE_WITHDRAWN = EnumSet.of(INSTALLED, FAILED); |
| 84 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 85 | private static final Set<IntentState> PARKING = EnumSet.of(INSTALL_REQ, INSTALLED, WITHDRAWN, FAILED); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 86 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 87 | private final Logger log = getLogger(getClass()); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 88 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 89 | // Assumption: IntentId will not have synonyms |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 90 | private static final String INTENTS_TABLE = "intents"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 91 | private CMap<IntentId, Intent> intents; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 92 | |
| 93 | private static final String STATES_TABLE = "intent-states"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 94 | private CMap<IntentId, IntentState> states; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 95 | |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 96 | // TODO transient state issue remains for this impl.: ONOS-103 |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 97 | // Map to store instance local intermediate state transition |
| 98 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 99 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 100 | private static final String INSTALLABLE_TABLE = "installable-intents"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 101 | private CMap<IntentId, List<Intent>> installable; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 102 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 103 | private LoadingCache<IntentId, String> keyCache; |
| 104 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 105 | private StoreSerializer serializer; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected DatabaseAdminService dbAdminService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected DatabaseService dbService; |
| 112 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected MetricsService metricsService; |
| 115 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 116 | // TODO make this configurable |
| 117 | private boolean onlyLogTransitionError = true; |
| 118 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 119 | private Timer getInstallableIntentsTimer; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 120 | private Timer getIntentCountTimer; |
| 121 | private Timer getIntentsTimer; |
| 122 | private Timer getIntentTimer; |
| 123 | private Timer getIntentStateTimer; |
| 124 | |
| 125 | |
| 126 | private Timer createResponseTimer(String methodName) { |
| 127 | return createTimer("IntentStore", methodName, "responseTime"); |
| 128 | } |
| 129 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 130 | @Activate |
| 131 | public void activate() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 132 | getInstallableIntentsTimer = createResponseTimer("getInstallableIntents"); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 133 | getIntentCountTimer = createResponseTimer("getIntentCount"); |
| 134 | getIntentsTimer = createResponseTimer("getIntents"); |
| 135 | getIntentTimer = createResponseTimer("getIntent"); |
| 136 | getIntentStateTimer = createResponseTimer("getIntentState"); |
| 137 | |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 138 | // We need a way to add serializer for intents which has been plugged-in. |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 139 | // As a short term workaround, relax Kryo config to |
| 140 | // registrationRequired=false |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 141 | serializer = new KryoSerializer() { |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 142 | |
| 143 | @Override |
| 144 | protected void setupKryoPool() { |
| 145 | serializerPool = KryoNamespace.newBuilder() |
| 146 | .setRegistrationRequired(false) |
| 147 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 148 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 149 | .build(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 150 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 151 | }; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 152 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 153 | keyCache = CacheBuilder.newBuilder() |
| 154 | .softValues() |
| 155 | .build(new CacheLoader<IntentId, String>() { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 156 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 157 | @Override |
| 158 | public String load(IntentId key) { |
| 159 | return key.toString(); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | intents = new IntentIdMap<>(dbAdminService, dbService, INTENTS_TABLE, serializer); |
| 164 | |
| 165 | states = new IntentIdMap<>(dbAdminService, dbService, STATES_TABLE, serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 166 | |
| 167 | transientStates.clear(); |
| 168 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 169 | installable = new IntentIdMap<>(dbAdminService, dbService, INSTALLABLE_TABLE, serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 170 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 171 | log.info("Started"); |
| 172 | } |
| 173 | |
| 174 | @Deactivate |
| 175 | public void deactivate() { |
| 176 | log.info("Stopped"); |
| 177 | } |
| 178 | |
| 179 | @Override |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 180 | public MetricsService metricsService() { |
| 181 | return metricsService; |
| 182 | } |
| 183 | |
| 184 | @Override |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 185 | public long getIntentCount() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 186 | Context timer = startTimer(getIntentCountTimer); |
| 187 | try { |
| 188 | return intents.size(); |
| 189 | } finally { |
| 190 | stopTimer(timer); |
| 191 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Iterable<Intent> getIntents() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 196 | Context timer = startTimer(getIntentsTimer); |
| 197 | try { |
| 198 | return ImmutableSet.copyOf(intents.values()); |
| 199 | } finally { |
| 200 | stopTimer(timer); |
| 201 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Intent getIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 206 | Context timer = startTimer(getIntentTimer); |
| 207 | try { |
| 208 | return intents.get(intentId); |
| 209 | } finally { |
| 210 | stopTimer(timer); |
| 211 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 216 | Context timer = startTimer(getIntentStateTimer); |
| 217 | try { |
| 218 | final IntentState localState = transientStates.get(id); |
| 219 | if (localState != null) { |
| 220 | return localState; |
| 221 | } |
| 222 | return states.get(id); |
| 223 | } finally { |
| 224 | stopTimer(timer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 225 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 228 | private void verify(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { |
| 229 | if (onlyLogTransitionError) { |
| 230 | if (!expression) { |
| 231 | log.error(errorMessageTemplate.replace("%s", "{}"), errorMessageArgs); |
| 232 | } |
| 233 | } else { |
| 234 | Verify.verify(expression, errorMessageTemplate, errorMessageArgs); |
| 235 | } |
| 236 | } |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 237 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 238 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 239 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 240 | Context timer = startTimer(getInstallableIntentsTimer); |
| 241 | try { |
| 242 | return installable.get(intentId); |
| 243 | } finally { |
| 244 | stopTimer(timer); |
| 245 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 248 | protected String strIntentId(IntentId key) { |
| 249 | return keyCache.getUnchecked(key); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Distributed Map from IntentId to some value. |
| 254 | * |
| 255 | * @param <V> Map value type |
| 256 | */ |
| 257 | final class IntentIdMap<V> extends CMap<IntentId, V> { |
| 258 | |
| 259 | /** |
| 260 | * Creates a IntentIdMap instance. |
| 261 | * |
| 262 | * @param dbAdminService DatabaseAdminService to use for this instance |
| 263 | * @param dbService DatabaseService to use for this instance |
| 264 | * @param tableName table which this Map corresponds to |
| 265 | * @param serializer Value serializer |
| 266 | */ |
| 267 | public IntentIdMap(DatabaseAdminService dbAdminService, |
| 268 | DatabaseService dbService, |
| 269 | String tableName, |
| 270 | StoreSerializer serializer) { |
| 271 | super(dbAdminService, dbService, tableName, serializer); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | protected String sK(IntentId key) { |
| 276 | return strIntentId(key); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public List<Operation> batchWrite(BatchWrite batch) { |
Sho SHIMIZU | 2bb988b | 2015-01-20 13:45:35 -0800 | [diff] [blame] | 282 | if (batch.isEmpty()) { |
| 283 | return Collections.emptyList(); |
| 284 | } |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 285 | |
| 286 | List<Operation> failed = new ArrayList<>(); |
| 287 | final Builder builder = BatchWriteRequest.newBuilder(); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 288 | List<IntentEvent> events = Lists.newArrayList(); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 289 | |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 290 | final Set<IntentId> transitionedToParking = new HashSet<>(); |
| 291 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 292 | for (Operation op : batch.operations()) { |
| 293 | switch (op.type()) { |
| 294 | case CREATE_INTENT: |
| 295 | checkArgument(op.args().size() == 1, |
| 296 | "CREATE_INTENT takes 1 argument. %s", op); |
| 297 | Intent intent = op.arg(0); |
| 298 | builder.putIfAbsent(INTENTS_TABLE, strIntentId(intent.id()), serializer.encode(intent)); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 299 | builder.putIfAbsent(STATES_TABLE, strIntentId(intent.id()), serializer.encode(INSTALL_REQ)); |
| 300 | events.add(IntentEvent.getEvent(INSTALL_REQ, intent)); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 301 | break; |
| 302 | |
| 303 | case REMOVE_INTENT: |
| 304 | checkArgument(op.args().size() == 1, |
| 305 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 306 | IntentId intentId = (IntentId) op.arg(0); |
| 307 | builder.remove(INTENTS_TABLE, strIntentId(intentId)); |
| 308 | builder.remove(STATES_TABLE, strIntentId(intentId)); |
| 309 | builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); |
| 310 | break; |
| 311 | |
| 312 | case SET_STATE: |
| 313 | checkArgument(op.args().size() == 2, |
| 314 | "SET_STATE takes 2 arguments. %s", op); |
| 315 | intent = op.arg(0); |
| 316 | IntentState newState = op.arg(1); |
| 317 | builder.put(STATES_TABLE, strIntentId(intent.id()), serializer.encode(newState)); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 318 | if (PARKING.contains(newState)) { |
| 319 | transitionedToParking.add(intent.id()); |
Yuta HIGUCHI | 5cd352d | 2014-12-01 20:16:02 -0800 | [diff] [blame] | 320 | events.add(IntentEvent.getEvent(newState, intent)); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 321 | } else { |
| 322 | transitionedToParking.remove(intent.id()); |
| 323 | } |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 324 | break; |
| 325 | |
| 326 | case SET_INSTALLABLE: |
| 327 | checkArgument(op.args().size() == 2, |
| 328 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 329 | intentId = op.arg(0); |
| 330 | List<Intent> installableIntents = op.arg(1); |
| 331 | builder.put(INSTALLABLE_TABLE, strIntentId(intentId), serializer.encode(installableIntents)); |
| 332 | break; |
| 333 | |
| 334 | case REMOVE_INSTALLED: |
| 335 | checkArgument(op.args().size() == 1, |
| 336 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 337 | intentId = op.arg(0); |
| 338 | builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); |
| 339 | break; |
| 340 | |
| 341 | default: |
| 342 | log.warn("Unknown Operation encountered: {}", op); |
| 343 | failed.add(op); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | BatchWriteResult batchWriteResult = dbService.batchWrite(builder.build()); |
| 349 | if (batchWriteResult.isSuccessful()) { |
| 350 | // no-failure (except for invalid input) |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 351 | transitionedToParking.forEach((intentId) -> transientStates.remove(intentId)); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 352 | notifyDelegate(events); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 353 | return failed; |
| 354 | } else { |
| 355 | // everything failed |
| 356 | return batch.operations(); |
| 357 | } |
| 358 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 359 | } |