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