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 | import com.hazelcast.core.IMap; |
| 20 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.intent.Intent; |
| 26 | import org.onlab.onos.net.intent.IntentEvent; |
| 27 | import org.onlab.onos.net.intent.IntentId; |
| 28 | import org.onlab.onos.net.intent.IntentState; |
| 29 | import org.onlab.onos.net.intent.IntentStore; |
| 30 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 31 | import org.onlab.onos.store.hz.AbstractHazelcastStore; |
| 32 | import org.onlab.onos.store.hz.SMap; |
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; |
| 35 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.concurrent.ConcurrentHashMap; |
| 41 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 42 | import static com.google.common.base.Verify.verify; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 43 | import static org.onlab.onos.net.intent.IntentState.*; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 45 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class DistributedIntentStore |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 49 | extends AbstractHazelcastStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 50 | implements IntentStore { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 53 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 54 | // Assumption: IntentId will not have synonyms |
| 55 | private SMap<IntentId, Intent> intents; |
| 56 | private SMap<IntentId, IntentState> states; |
| 57 | |
| 58 | // Map to store instance local intermediate state transition |
| 59 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 60 | |
| 61 | private SMap<IntentId, List<Intent>> installable; |
| 62 | |
| 63 | @Override |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 64 | @Activate |
| 65 | public void activate() { |
Yuta HIGUCHI | 71fa493 | 2014-10-28 22:27:49 -0700 | [diff] [blame] | 66 | // 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] | 67 | // As a short term workaround, relax Kryo config to |
| 68 | // registrationRequired=false |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 69 | super.activate(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 70 | super.serializer = new KryoSerializer() { |
| 71 | |
| 72 | @Override |
| 73 | protected void setupKryoPool() { |
| 74 | serializerPool = KryoNamespace.newBuilder() |
| 75 | .setRegistrationRequired(false) |
| 76 | .register(KryoNamespaces.API) |
| 77 | .build() |
| 78 | .populate(1); |
| 79 | } |
| 80 | |
| 81 | }; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 82 | |
| 83 | // TODO: enable near cache, allow read from backup for this IMap |
| 84 | IMap<byte[], byte[]> rawIntents = super.theInstance.getMap("intents"); |
| 85 | intents = new SMap<>(rawIntents , super.serializer); |
| 86 | |
| 87 | // TODO: disable near cache, disable read from backup for this IMap |
| 88 | IMap<byte[], byte[]> rawStates = super.theInstance.getMap("intent-states"); |
| 89 | states = new SMap<>(rawStates , super.serializer); |
| 90 | |
| 91 | transientStates.clear(); |
| 92 | |
| 93 | // TODO: disable near cache, disable read from backup for this IMap |
| 94 | IMap<byte[], byte[]> rawInstallables = super.theInstance.getMap("installable-intents"); |
| 95 | installable = new SMap<>(rawInstallables , super.serializer); |
| 96 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public IntentEvent createIntent(Intent intent) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 107 | Intent existing = intents.putIfAbsent(intent.id(), intent); |
| 108 | if (existing != null) { |
| 109 | // duplicate, ignore |
| 110 | return null; |
| 111 | } else { |
| 112 | return this.setState(intent, IntentState.SUBMITTED); |
| 113 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public IntentEvent removeIntent(IntentId intentId) { |
| 118 | Intent intent = intents.remove(intentId); |
| 119 | installable.remove(intentId); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 120 | if (intent == null) { |
| 121 | // was already removed |
| 122 | return null; |
| 123 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 124 | IntentEvent event = this.setState(intent, WITHDRAWN); |
| 125 | states.remove(intentId); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 126 | transientStates.remove(intentId); |
| 127 | // TODO: Should we callremoveInstalledIntents if this Intent was |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 128 | return event; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public long getIntentCount() { |
| 133 | return intents.size(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Iterable<Intent> getIntents() { |
| 138 | return ImmutableSet.copyOf(intents.values()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Intent getIntent(IntentId intentId) { |
| 143 | return intents.get(intentId); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 148 | final IntentState localState = transientStates.get(id); |
| 149 | if (localState != null) { |
| 150 | return localState; |
| 151 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 152 | return states.get(id); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public IntentEvent setState(Intent intent, IntentState state) { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 157 | final IntentId id = intent.id(); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 158 | IntentEvent.Type type = null; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 159 | IntentState prev = null; |
| 160 | boolean transientStateChangeOnly = false; |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 161 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 162 | // TODO: enable sanity checking if Debug enabled, etc. |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 163 | switch (state) { |
| 164 | case SUBMITTED: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 165 | prev = states.putIfAbsent(id, SUBMITTED); |
| 166 | verify(prev == null, "Illegal state transition attempted from %s to SUBMITTED", prev); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 167 | type = IntentEvent.Type.SUBMITTED; |
| 168 | break; |
| 169 | case INSTALLED: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 170 | // parking state transition |
| 171 | prev = states.replace(id, INSTALLED); |
| 172 | verify(prev != null, "Illegal state transition attempted from non-SUBMITTED to INSTALLED"); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 173 | type = IntentEvent.Type.INSTALLED; |
| 174 | break; |
| 175 | case FAILED: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 176 | prev = states.replace(id, FAILED); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 177 | type = IntentEvent.Type.FAILED; |
| 178 | break; |
| 179 | case WITHDRAWN: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 180 | prev = states.replace(id, WITHDRAWN); |
| 181 | verify(prev != null, "Illegal state transition attempted from non-WITHDRAWING to WITHDRAWN"); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 182 | type = IntentEvent.Type.WITHDRAWN; |
| 183 | break; |
| 184 | default: |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 185 | transientStateChangeOnly = true; |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 188 | if (!transientStateChangeOnly) { |
| 189 | log.debug("Parking State change: {} {}=>{}", id, prev, state); |
| 190 | } |
| 191 | // Update instance local state, which includes non-parking state transition |
| 192 | prev = transientStates.put(id, state); |
| 193 | log.debug("Transient State change: {} {}=>{}", id, prev, state); |
| 194 | |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 195 | if (type == null) { |
| 196 | return null; |
| 197 | } |
| 198 | return new IntentEvent(type, intent); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 202 | public void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 203 | installable.put(intentId, result); |
| 204 | } |
| 205 | |
| 206 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 207 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 208 | return installable.get(intentId); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void removeInstalledIntents(IntentId intentId) { |
| 213 | installable.remove(intentId); |
| 214 | } |
| 215 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 216 | // FIXME add handler to react to remote event |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 217 | } |