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