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 | |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 42 | public static class ChangedEvent { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 120 | if (intents.containsKey(intent.getId())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | removeIntent(intent.getId()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 122 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | intents.put(intent.getId(), intent); |
| 124 | events.add(new ChangedEvent(ChangedEventType.ADDED, intent)); |
| 125 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 126 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | protected void removeIntent(String intentId) { |
| 128 | Intent intent = intents.remove(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 129 | if (intent == null) { |
| 130 | return; |
| 131 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | events.add(new ChangedEvent(ChangedEventType.REMOVED, intent)); |
| 133 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 134 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | protected void setState(String intentId, IntentState state) { |
| 136 | Intent intent = intents.get(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 137 | if (intent == null) { |
| 138 | return; |
| 139 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | intent.setState(state); |
| 141 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, intent)); |
| 142 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 143 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | //================================================================================ |
| 145 | // helper methods (protected) |
| 146 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 147 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | protected void handleAddOperation(IntentOperation operation) { |
| 149 | putIntent(operation.intent); |
| 150 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 151 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | protected void handleRemoveOperation(IntentOperation operation) { |
| 153 | Intent intent = getIntent(operation.intent.getId()); |
| 154 | if (intent == null) { |
| 155 | // TODO error handling |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 156 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | } else { |
| 158 | setState(intent.getId(), IntentState.DEL_REQ); |
| 159 | } |
| 160 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 161 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | protected void handleErrorOperation(IntentOperation operation) { |
| 163 | //TODO put error message into the intent |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 164 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | ErrorIntent errorIntent = (ErrorIntent) operation.intent; |
| 166 | Intent targetIntent = intents.get(errorIntent.getId()); |
| 167 | if (targetIntent == null) { |
| 168 | // TODO error handling |
| 169 | return; |
| 170 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 171 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | switch (targetIntent.getState()) { |
| 173 | case CREATED: |
| 174 | case INST_REQ: |
| 175 | case INST_ACK: |
| 176 | case REROUTE_REQ: |
| 177 | setState(targetIntent.getId(), IntentState.INST_NACK); |
| 178 | break; |
| 179 | case DEL_REQ: |
| 180 | setState(targetIntent.getId(), IntentState.DEL_PENDING); |
| 181 | break; |
| 182 | case INST_NACK: |
| 183 | case DEL_PENDING: |
| 184 | case DEL_ACK: |
| 185 | // do nothing |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | protected void notifyEvents() { |
| 191 | for (ChangedListener listener : listeners) { |
| 192 | listener.intentsChange(events); |
| 193 | } |
| 194 | events.clear(); |
| 195 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 196 | } |