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 | */ |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.intent.impl; |
| 17 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 19 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.intent.Intent; |
| 27 | import org.onlab.onos.net.intent.IntentEvent; |
| 28 | import org.onlab.onos.net.intent.IntentId; |
| 29 | import org.onlab.onos.net.intent.IntentState; |
| 30 | import org.onlab.onos.net.intent.IntentStore; |
| 31 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 32 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 33 | import org.onlab.onos.store.serializers.KryoNamespaces; |
| 34 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 35 | import org.onlab.onos.store.serializers.StoreSerializer; |
| 36 | import org.onlab.onos.store.service.DatabaseAdminService; |
| 37 | import org.onlab.onos.store.service.DatabaseService; |
| 38 | import org.onlab.onos.store.service.impl.CMap; |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 39 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 42 | import java.util.EnumSet; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 43 | import java.util.List; |
| 44 | import java.util.Map; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 45 | import java.util.Set; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 46 | import java.util.concurrent.ConcurrentHashMap; |
| 47 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 48 | import static com.google.common.base.Verify.verify; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 49 | import static org.onlab.onos.net.intent.IntentState.*; |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 51 | |
Yuta HIGUCHI | 43db10f | 2014-11-19 17:23:30 -0800 | [diff] [blame] | 52 | @Component(immediate = false, enabled = false) |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 53 | @Service |
| 54 | public class DistributedIntentStore |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 55 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 56 | implements IntentStore { |
| 57 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 58 | /** Valid parking state, which can transition to INSTALLED. */ |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame^] | 59 | private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(SUBMITTED, INSTALLED, FAILED); |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 60 | |
| 61 | /** Valid parking state, which can transition to WITHDRAWN. */ |
| 62 | private static final Set<IntentState> PRE_WITHDRAWN = EnumSet.of(INSTALLED, FAILED); |
| 63 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 64 | private final Logger log = getLogger(getClass()); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 65 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 66 | // Assumption: IntentId will not have synonyms |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 67 | private CMap<IntentId, Intent> intents; |
| 68 | private CMap<IntentId, IntentState> states; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 69 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 70 | // TODO left behind transient state issue: ONOS-103 |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 71 | // Map to store instance local intermediate state transition |
| 72 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 73 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 74 | private CMap<IntentId, List<Intent>> installable; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 76 | private StoreSerializer serializer; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected DatabaseAdminService dbAdminService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected DatabaseService dbService; |
| 83 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 84 | @Activate |
| 85 | public void activate() { |
Yuta HIGUCHI | 71fa493 | 2014-10-28 22:27:49 -0700 | [diff] [blame] | 86 | // FIXME: 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] | 87 | // As a short term workaround, relax Kryo config to |
| 88 | // registrationRequired=false |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 89 | serializer = new KryoSerializer() { |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 90 | |
| 91 | @Override |
| 92 | protected void setupKryoPool() { |
| 93 | serializerPool = KryoNamespace.newBuilder() |
| 94 | .setRegistrationRequired(false) |
| 95 | .register(KryoNamespaces.API) |
| 96 | .build() |
| 97 | .populate(1); |
| 98 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 99 | }; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 100 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 101 | intents = new CMap<>(dbAdminService, dbService, "intents", serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 102 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 103 | states = new CMap<>(dbAdminService, dbService, "intent-states", serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 104 | |
| 105 | transientStates.clear(); |
| 106 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 107 | installable = new CMap<>(dbAdminService, dbService, "installable-intents", serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 108 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 109 | log.info("Started"); |
| 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | public void deactivate() { |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public IntentEvent createIntent(Intent intent) { |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 119 | boolean absent = intents.putIfAbsent(intent.id(), intent); |
| 120 | if (!absent) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 121 | // duplicate, ignore |
| 122 | return null; |
| 123 | } else { |
| 124 | return this.setState(intent, IntentState.SUBMITTED); |
| 125 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public IntentEvent removeIntent(IntentId intentId) { |
| 130 | Intent intent = intents.remove(intentId); |
| 131 | installable.remove(intentId); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 132 | if (intent == null) { |
| 133 | // was already removed |
| 134 | return null; |
| 135 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 136 | IntentEvent event = this.setState(intent, WITHDRAWN); |
| 137 | states.remove(intentId); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 138 | transientStates.remove(intentId); |
| 139 | // TODO: Should we callremoveInstalledIntents if this Intent was |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 140 | return event; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public long getIntentCount() { |
| 145 | return intents.size(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Iterable<Intent> getIntents() { |
| 150 | return ImmutableSet.copyOf(intents.values()); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Intent getIntent(IntentId intentId) { |
| 155 | return intents.get(intentId); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 160 | final IntentState localState = transientStates.get(id); |
| 161 | if (localState != null) { |
| 162 | return localState; |
| 163 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 164 | return states.get(id); |
| 165 | } |
| 166 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 167 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public IntentEvent setState(Intent intent, IntentState state) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 170 | final IntentId id = intent.id(); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 171 | IntentEvent.Type type = null; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 172 | final IntentState prevParking; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 173 | boolean transientStateChangeOnly = false; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 174 | boolean updated; |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 175 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 176 | // parking state transition |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 177 | switch (state) { |
| 178 | case SUBMITTED: |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 179 | prevParking = states.get(id); |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame^] | 180 | if (prevParking == null) { |
| 181 | updated = states.putIfAbsent(id, SUBMITTED); |
| 182 | verify(updated, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 183 | } else { |
| 184 | verify(prevParking == WITHDRAWN, |
| 185 | "Illegal state transition attempted from %s to SUBMITTED", |
| 186 | prevParking); |
| 187 | updated = states.replace(id, prevParking, SUBMITTED); |
| 188 | verify(updated, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 189 | } |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 190 | type = IntentEvent.Type.SUBMITTED; |
| 191 | break; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 192 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 193 | case INSTALLED: |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 194 | prevParking = states.get(id); |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 195 | verify(PRE_INSTALLED.contains(prevParking), |
| 196 | "Illegal state transition attempted from %s to INSTALLED", |
| 197 | prevParking); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 198 | updated = states.replace(id, prevParking, INSTALLED); |
| 199 | verify(updated, "Conditional replace %s => %s failed", prevParking, INSTALLED); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 200 | type = IntentEvent.Type.INSTALLED; |
| 201 | break; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 202 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 203 | case FAILED: |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 204 | prevParking = states.get(id); |
| 205 | updated = states.replace(id, prevParking, FAILED); |
| 206 | verify(updated, "Conditional replace %s => %s failed", prevParking, FAILED); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 207 | type = IntentEvent.Type.FAILED; |
| 208 | break; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 209 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 210 | case WITHDRAWN: |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 211 | prevParking = states.get(id); |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 212 | verify(PRE_WITHDRAWN.contains(prevParking), |
| 213 | "Illegal state transition attempted from %s to WITHDRAWN", |
| 214 | prevParking); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 215 | updated = states.replace(id, prevParking, WITHDRAWN); |
| 216 | verify(updated, "Conditional replace %s => %s failed", prevParking, WITHDRAWN); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 217 | type = IntentEvent.Type.WITHDRAWN; |
| 218 | break; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 219 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 220 | default: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 221 | transientStateChangeOnly = true; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 222 | prevParking = null; |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 223 | break; |
| 224 | } |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 225 | if (!transientStateChangeOnly) { |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 226 | log.debug("Parking State change: {} {}=>{}", id, prevParking, state); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 227 | } |
| 228 | // Update instance local state, which includes non-parking state transition |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 229 | final IntentState prevTransient = transientStates.put(id, state); |
| 230 | log.debug("Transient State change: {} {}=>{}", id, prevTransient, state); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 231 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 232 | if (type == null) { |
| 233 | return null; |
| 234 | } |
| 235 | return new IntentEvent(type, intent); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 239 | public void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 240 | installable.put(intentId, result); |
| 241 | } |
| 242 | |
| 243 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 244 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 245 | return installable.get(intentId); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void removeInstalledIntents(IntentId intentId) { |
| 250 | installable.remove(intentId); |
| 251 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 252 | } |