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