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<>(); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame^] | 52 | private HashMap<String, Intent> intents = new HashMap<>(); |
| 53 | |
| 54 | protected void putIntent(Intent intent) { |
| 55 | if (intents.containsKey(intent.getId())) |
| 56 | removeIntent(intent.getId()); |
| 57 | intents.put(intent.getId(), intent); |
| 58 | } |
| 59 | |
| 60 | protected void removeIntent(String intentId) { |
| 61 | intents.remove(intentId); |
| 62 | } |
| 63 | |
| 64 | public Intent getIntent(String intentId) { |
| 65 | return intents.get(intentId); |
| 66 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 67 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 68 | public void executeOperations(IntentOperationList operations) { |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 69 | LinkedList<ChangedEvent> events = new LinkedList<>(); |
| 70 | for (IntentOperation operation: operations) { |
| 71 | switch (operation.operator) { |
| 72 | case ADD: |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame^] | 73 | putIntent(operation.intent); |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 74 | events.add(new ChangedEvent(ChangedEventType.ADDED, operation.intent)); |
| 75 | break; |
| 76 | case REMOVE: |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame^] | 77 | Intent intent = getIntent(operation.intent.getId()); |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 78 | if (intent == null) { |
| 79 | // TODO throw exception |
| 80 | } |
| 81 | else { |
| 82 | intent.setState(Intent.IntentState.DEL_REQ); |
| 83 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, |
| 84 | new Intent(intent.getId(), Intent.IntentState.DEL_REQ))); |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | for (ChangedListener listener: listeners) { |
| 90 | listener.intentsChange(events); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void purge() { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame^] | 95 | LinkedList<String> removeIds = new LinkedList<>(); |
| 96 | for (Entry<String, Intent> entry: intents.entrySet()) { |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 97 | Intent intent = entry.getValue(); |
| 98 | if (intent.getState() == IntentState.DEL_ACK |
| 99 | || intent.getState() == IntentState.INST_NACK) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame^] | 100 | removeIds.add(intent.getId()); |
| 101 | } |
| 102 | } |
| 103 | for (String intentId: removeIds) { |
| 104 | removeIntent(intentId); |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | public Collection<Intent> getAllIntents() { |
| 109 | return intents.values(); |
| 110 | } |
| 111 | |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 112 | public void addChangeListener(ChangedListener listener) { |
| 113 | listeners.add(listener); |
| 114 | } |
| 115 | |
| 116 | public void removeChangedListener(ChangedListener listener) { |
| 117 | listeners.remove(listener); |
| 118 | } |
| 119 | } |