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