Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.intent.Intent.IntentState; |
| 11 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * @author Toshio Koide (t-koide@onlab.us) |
| 15 | */ |
| 16 | public class IntentMap { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 17 | private HashSet<ChangedListener> listeners = new HashSet<>(); |
| 18 | private HashMap<String, Intent> intents = new HashMap<>(); |
| 19 | private LinkedList<ChangedEvent> events = new LinkedList<>(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 20 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | public enum ChangedEventType { |
| 22 | /** |
| 23 | * Added new intent. |
| 24 | */ |
| 25 | ADDED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 26 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Removed existing intent. |
| 29 | * The specified intent is an instance of Intent class (not a child class) |
| 30 | * Only id and state are valid. |
| 31 | */ |
| 32 | REMOVED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 33 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Changed state of existing intent. |
| 36 | * The specified intent is an instance of Intent class (not a child class) |
| 37 | * Only id and state are valid. |
| 38 | */ |
| 39 | STATE_CHANGED, |
| 40 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | public class ChangedEvent { |
| 43 | public ChangedEvent(ChangedEventType eventType, Intent intent) { |
| 44 | this.eventType = eventType; |
| 45 | this.intent = intent; |
| 46 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | public ChangedEventType eventType; |
| 49 | public Intent intent; |
| 50 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | public interface ChangedListener extends EventListener { |
| 53 | void intentsChange(LinkedList<ChangedEvent> events); |
| 54 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | //================================================================================ |
| 57 | // public methods |
| 58 | //================================================================================ |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 59 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | public void executeOperations(IntentOperationList operations) { |
| 61 | for (IntentOperation operation : operations) { |
| 62 | switch (operation.operator) { |
| 63 | case ADD: |
| 64 | handleAddOperation(operation); |
| 65 | break; |
| 66 | case REMOVE: |
| 67 | handleRemoveOperation(operation); |
| 68 | break; |
| 69 | case ERROR: |
| 70 | handleErrorOperation(operation); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | notifyEvents(); |
| 75 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 76 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | public void purge() { |
| 78 | LinkedList<String> removeIds = new LinkedList<>(); |
| 79 | for (Entry<String, Intent> entry : intents.entrySet()) { |
| 80 | Intent intent = entry.getValue(); |
| 81 | if (intent.getState() == IntentState.DEL_ACK |
| 82 | || intent.getState() == IntentState.INST_NACK) { |
| 83 | removeIds.add(intent.getId()); |
| 84 | } |
| 85 | } |
| 86 | for (String intentId : removeIds) { |
| 87 | removeIntent(intentId); |
| 88 | } |
| 89 | notifyEvents(); |
| 90 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 91 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | public void changeStates(IntentStateList states) { |
| 93 | for (Entry<String, IntentState> state : states.entrySet()) { |
| 94 | setState(state.getKey(), state.getValue()); |
| 95 | } |
| 96 | notifyEvents(); |
| 97 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 98 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | public Intent getIntent(String intentId) { |
| 100 | return intents.get(intentId); |
| 101 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | public Collection<Intent> getAllIntents() { |
| 104 | return intents.values(); |
| 105 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 106 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | public void addChangeListener(ChangedListener listener) { |
| 108 | listeners.add(listener); |
| 109 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 110 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | public void removeChangedListener(ChangedListener listener) { |
| 112 | listeners.remove(listener); |
| 113 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 114 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | //================================================================================ |
| 116 | // methods that affect intents map (protected) |
| 117 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 118 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 119 | protected void putIntent(Intent intent) { |
| 120 | if (intents.containsKey(intent.getId())) |
| 121 | removeIntent(intent.getId()); |
| 122 | intents.put(intent.getId(), intent); |
| 123 | events.add(new ChangedEvent(ChangedEventType.ADDED, intent)); |
| 124 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 125 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | protected void removeIntent(String intentId) { |
| 127 | Intent intent = intents.remove(intentId); |
| 128 | if (intent == null) return; |
| 129 | events.add(new ChangedEvent(ChangedEventType.REMOVED, intent)); |
| 130 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | protected void setState(String intentId, IntentState state) { |
| 133 | Intent intent = intents.get(intentId); |
| 134 | if (intent == null) return; |
| 135 | intent.setState(state); |
| 136 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, intent)); |
| 137 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 138 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | //================================================================================ |
| 140 | // helper methods (protected) |
| 141 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 142 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | protected void handleAddOperation(IntentOperation operation) { |
| 144 | putIntent(operation.intent); |
| 145 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | protected void handleRemoveOperation(IntentOperation operation) { |
| 148 | Intent intent = getIntent(operation.intent.getId()); |
| 149 | if (intent == null) { |
| 150 | // TODO error handling |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 151 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | } else { |
| 153 | setState(intent.getId(), IntentState.DEL_REQ); |
| 154 | } |
| 155 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 156 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | protected void handleErrorOperation(IntentOperation operation) { |
| 158 | //TODO put error message into the intent |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 159 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | ErrorIntent errorIntent = (ErrorIntent) operation.intent; |
| 161 | Intent targetIntent = intents.get(errorIntent.getId()); |
| 162 | if (targetIntent == null) { |
| 163 | // TODO error handling |
| 164 | return; |
| 165 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 166 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | switch (targetIntent.getState()) { |
| 168 | case CREATED: |
| 169 | case INST_REQ: |
| 170 | case INST_ACK: |
| 171 | case REROUTE_REQ: |
| 172 | setState(targetIntent.getId(), IntentState.INST_NACK); |
| 173 | break; |
| 174 | case DEL_REQ: |
| 175 | setState(targetIntent.getId(), IntentState.DEL_PENDING); |
| 176 | break; |
| 177 | case INST_NACK: |
| 178 | case DEL_PENDING: |
| 179 | case DEL_ACK: |
| 180 | // do nothing |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | protected void notifyEvents() { |
| 186 | for (ChangedListener listener : listeners) { |
| 187 | listener.intentsChange(events); |
| 188 | } |
| 189 | events.clear(); |
| 190 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 191 | } |