Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.store.trivial.impl; |
| 2 | |
| 3 | import static org.onlab.onos.net.intent.IntentState.COMPILED; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
| 6 | import java.util.HashMap; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | |
| 10 | import org.apache.felix.scr.annotations.Activate; |
| 11 | import org.apache.felix.scr.annotations.Component; |
| 12 | import org.apache.felix.scr.annotations.Deactivate; |
| 13 | import org.apache.felix.scr.annotations.Service; |
| 14 | import org.onlab.onos.net.intent.InstallableIntent; |
| 15 | import org.onlab.onos.net.intent.Intent; |
| 16 | import org.onlab.onos.net.intent.IntentEvent; |
| 17 | import org.onlab.onos.net.intent.IntentId; |
| 18 | import org.onlab.onos.net.intent.IntentState; |
| 19 | import org.onlab.onos.net.intent.IntentStore; |
| 20 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 21 | import org.onlab.onos.store.AbstractStore; |
| 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableSet; |
| 25 | |
| 26 | @Component(immediate = true) |
| 27 | @Service |
| 28 | public class SimpleIntentStore |
| 29 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 30 | implements IntentStore { |
| 31 | |
| 32 | private final Logger log = getLogger(getClass()); |
| 33 | private final Map<IntentId, Intent> intents = new HashMap<>(); |
| 34 | private final Map<IntentId, IntentState> states = new HashMap<>(); |
| 35 | private final Map<IntentId, List<InstallableIntent>> installable = new HashMap<>(); |
| 36 | |
| 37 | @Activate |
| 38 | public void activate() { |
| 39 | log.info("Started"); |
| 40 | } |
| 41 | |
| 42 | @Deactivate |
| 43 | public void deactivate() { |
| 44 | log.info("Stopped"); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public IntentEvent createIntent(Intent intent) { |
| 49 | intents.put(intent.getId(), intent); |
| 50 | return this.setState(intent, IntentState.SUBMITTED); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public IntentEvent removeIntent(IntentId intentId) { |
| 55 | Intent intent = intents.remove(intentId); |
| 56 | installable.remove(intentId); |
| 57 | IntentEvent event = this.setState(intent, IntentState.WITHDRAWN); |
| 58 | states.remove(intentId); |
| 59 | return event; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public long getIntentCount() { |
| 64 | return intents.size(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Iterable<Intent> getIntents() { |
| 69 | return ImmutableSet.copyOf(intents.values()); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Intent getIntent(IntentId intentId) { |
| 74 | return intents.get(intentId); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public IntentState getIntentState(IntentId id) { |
| 79 | return states.get(id); |
| 80 | } |
| 81 | |
| 82 | // TODO return dispatch event here... replace with state transition methods |
| 83 | @Override |
| 84 | public IntentEvent setState(Intent intent, IntentState newState) { |
| 85 | IntentId id = intent.getId(); |
| 86 | IntentState oldState = states.get(id); |
| 87 | states.put(id, newState); |
| 88 | return new IntentEvent(intent, newState, oldState, System.currentTimeMillis()); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public IntentEvent addInstallableIntents(IntentId intentId, List<InstallableIntent> result) { |
| 93 | installable.put(intentId, result); |
| 94 | return this.setState(intents.get(intentId), COMPILED); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public List<InstallableIntent> getInstallableIntents(IntentId intentId) { |
| 99 | return installable.get(intentId); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void removeInstalledIntents(IntentId intentId) { |
| 104 | installable.remove(intentId); |
| 105 | } |
| 106 | |
| 107 | } |