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