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