Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame^] | 1 | package net.onrc.onos.intent; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.EventListener; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.Iterator; |
| 8 | import java.util.LinkedList; |
| 9 | import java.util.List; |
| 10 | import java.util.Map.Entry; |
| 11 | |
| 12 | import net.onrc.onos.intent.Intent.IntentState; |
| 13 | |
| 14 | /** |
| 15 | * @author Toshio Koide (t-koide@onlab.us) |
| 16 | */ |
| 17 | public class IntentMap { |
| 18 | public enum ChangedEventType { |
| 19 | /** |
| 20 | * Added new intent. |
| 21 | */ |
| 22 | ADDED, |
| 23 | |
| 24 | /** |
| 25 | * Removed existing intent. |
| 26 | * The specified intent is an instance of Intent class (not a child class) |
| 27 | * Only id and state are valid. |
| 28 | */ |
| 29 | REMOVED, |
| 30 | |
| 31 | /** |
| 32 | * Changed state of existing intent. |
| 33 | * The specified intent is an instance of Intent class (not a child class) |
| 34 | * Only id and state are valid. |
| 35 | */ |
| 36 | STATE_CHANGED, |
| 37 | } |
| 38 | |
| 39 | public class ChangedEvent { |
| 40 | public ChangedEvent(ChangedEventType eventType, Intent intent) { |
| 41 | this.eventType = eventType; |
| 42 | this.intent = intent; |
| 43 | } |
| 44 | public ChangedEventType eventType; |
| 45 | public Intent intent; |
| 46 | } |
| 47 | |
| 48 | public interface ChangedListener extends EventListener { |
| 49 | void intentsChange(LinkedList<ChangedEvent> events); |
| 50 | } |
| 51 | |
| 52 | private HashSet<ChangedListener> listeners = new HashSet<>(); |
| 53 | protected HashMap<String, Intent> intents = new HashMap<>(); |
| 54 | |
| 55 | public void executeOperations(List<IntentOperation> operations) { |
| 56 | LinkedList<ChangedEvent> events = new LinkedList<>(); |
| 57 | for (IntentOperation operation: operations) { |
| 58 | switch (operation.operator) { |
| 59 | case ADD: |
| 60 | intents.put(operation.intent.getId(), operation.intent); |
| 61 | events.add(new ChangedEvent(ChangedEventType.ADDED, operation.intent)); |
| 62 | break; |
| 63 | case REMOVE: |
| 64 | Intent intent = intents.get(operation.intent.getId()); |
| 65 | if (intent == null) { |
| 66 | // TODO throw exception |
| 67 | } |
| 68 | else { |
| 69 | intent.setState(Intent.IntentState.DEL_REQ); |
| 70 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, |
| 71 | new Intent(intent.getId(), Intent.IntentState.DEL_REQ))); |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | for (ChangedListener listener: listeners) { |
| 77 | listener.intentsChange(events); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public void purge() { |
| 82 | Iterator<Entry<String, Intent>> i = intents.entrySet().iterator(); |
| 83 | while (i.hasNext()) { |
| 84 | Entry<String, Intent> entry = i.next(); |
| 85 | Intent intent = entry.getValue(); |
| 86 | if (intent.getState() == IntentState.DEL_ACK |
| 87 | || intent.getState() == IntentState.INST_NACK) { |
| 88 | i.remove(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public Collection<Intent> getAllIntents() { |
| 94 | return intents.values(); |
| 95 | } |
| 96 | |
| 97 | public Intent getIntent(String key) { |
| 98 | return intents.get(key); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | public void addChangeListener(ChangedListener listener) { |
| 103 | listeners.add(listener); |
| 104 | } |
| 105 | |
| 106 | public void removeChangedListener(ChangedListener listener) { |
| 107 | listeners.remove(listener); |
| 108 | } |
| 109 | } |