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; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * @author Toshio Koide (t-koide@onlab.us) |
| 17 | */ |
| 18 | public class IntentMap { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | private HashSet<ChangedListener> listeners = new HashSet<>(); |
| 20 | private HashMap<String, Intent> intents = new HashMap<>(); |
| 21 | private LinkedList<ChangedEvent> events = new LinkedList<>(); |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 22 | private static final Logger log = LoggerFactory.getLogger(IntentMap.class); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 23 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | public enum ChangedEventType { |
| 25 | /** |
| 26 | * Added new intent. |
| 27 | */ |
| 28 | ADDED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 29 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Removed 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 | REMOVED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 36 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Changed state of existing intent. |
| 39 | * The specified intent is an instance of Intent class (not a child class) |
| 40 | * Only id and state are valid. |
| 41 | */ |
| 42 | STATE_CHANGED, |
| 43 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 44 | |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 45 | public static class ChangedEvent { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | public ChangedEvent(ChangedEventType eventType, Intent intent) { |
| 47 | this.eventType = eventType; |
| 48 | this.intent = intent; |
| 49 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 50 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | public ChangedEventType eventType; |
| 52 | public Intent intent; |
Pavlin Radoslavov | b630929 | 2014-04-11 03:25:47 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Get a string representation of the object. |
| 56 | * <p/> |
| 57 | * The string has the following form: |
| 58 | * [eventType=XXX intent=XXX] |
| 59 | * |
| 60 | * @return a string representation of the object. |
| 61 | */ |
| 62 | @Override |
| 63 | public String toString() { |
| 64 | StringBuilder ret = new StringBuilder(); |
| 65 | ret.append("[eventType="); |
| 66 | ret.append(this.eventType.toString()); |
| 67 | ret.append(" intent="); |
| 68 | ret.append(this.intent.toString()); |
| 69 | ret.append("]"); |
| 70 | return ret.toString(); |
| 71 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 73 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | public interface ChangedListener extends EventListener { |
| 75 | void intentsChange(LinkedList<ChangedEvent> events); |
| 76 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | //================================================================================ |
| 79 | // public methods |
| 80 | //================================================================================ |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | public void executeOperations(IntentOperationList operations) { |
| 83 | for (IntentOperation operation : operations) { |
| 84 | switch (operation.operator) { |
| 85 | case ADD: |
| 86 | handleAddOperation(operation); |
| 87 | break; |
| 88 | case REMOVE: |
| 89 | handleRemoveOperation(operation); |
| 90 | break; |
| 91 | case ERROR: |
| 92 | handleErrorOperation(operation); |
| 93 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 94 | default: |
| 95 | log.error("Unknown intent operation {}", operation.operator); |
| 96 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | notifyEvents(); |
| 100 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 101 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame^] | 102 | /** |
| 103 | * Purge all Intents that are in a state allowing them to be removed. |
| 104 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | public void purge() { |
| 106 | LinkedList<String> removeIds = new LinkedList<>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame^] | 107 | |
| 108 | // Collect the IDs of all intents that can be removed |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | for (Entry<String, Intent> entry : intents.entrySet()) { |
| 110 | Intent intent = entry.getValue(); |
| 111 | if (intent.getState() == IntentState.DEL_ACK |
| 112 | || intent.getState() == IntentState.INST_NACK) { |
| 113 | removeIds.add(intent.getId()); |
| 114 | } |
| 115 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame^] | 116 | |
| 117 | purge(removeIds); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Purge a collection of Intents specified by Intent IDs. |
| 122 | * |
| 123 | * NOTE: The caller needs to make sure those intents are in a state |
| 124 | * that allows them to be removed. |
| 125 | * |
| 126 | * @param removeIds the collection of Intent IDs to purge. |
| 127 | */ |
| 128 | public void purge(Collection<String> removeIds) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | for (String intentId : removeIds) { |
| 130 | removeIntent(intentId); |
| 131 | } |
| 132 | notifyEvents(); |
| 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 | public void changeStates(IntentStateList states) { |
| 136 | for (Entry<String, IntentState> state : states.entrySet()) { |
| 137 | setState(state.getKey(), state.getValue()); |
| 138 | } |
| 139 | notifyEvents(); |
| 140 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 141 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | public Intent getIntent(String intentId) { |
| 143 | return intents.get(intentId); |
| 144 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 145 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | public Collection<Intent> getAllIntents() { |
| 147 | return intents.values(); |
| 148 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 149 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | public void addChangeListener(ChangedListener listener) { |
| 151 | listeners.add(listener); |
| 152 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 153 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | public void removeChangedListener(ChangedListener listener) { |
| 155 | listeners.remove(listener); |
| 156 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 157 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | //================================================================================ |
| 159 | // methods that affect intents map (protected) |
| 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 putIntent(Intent intent) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 163 | if (intents.containsKey(intent.getId())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | removeIntent(intent.getId()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 165 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 166 | intents.put(intent.getId(), intent); |
| 167 | events.add(new ChangedEvent(ChangedEventType.ADDED, intent)); |
| 168 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 169 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | protected void removeIntent(String intentId) { |
| 171 | Intent intent = intents.remove(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 172 | if (intent == null) { |
| 173 | return; |
| 174 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | events.add(new ChangedEvent(ChangedEventType.REMOVED, intent)); |
| 176 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 177 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | protected void setState(String intentId, IntentState state) { |
| 179 | Intent intent = intents.get(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 180 | if (intent == null) { |
| 181 | return; |
| 182 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | intent.setState(state); |
| 184 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, intent)); |
| 185 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 186 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | //================================================================================ |
| 188 | // helper methods (protected) |
| 189 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 190 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | protected void handleAddOperation(IntentOperation operation) { |
| 192 | putIntent(operation.intent); |
| 193 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 194 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | protected void handleRemoveOperation(IntentOperation operation) { |
| 196 | Intent intent = getIntent(operation.intent.getId()); |
| 197 | if (intent == null) { |
| 198 | // TODO error handling |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 199 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 200 | } else { |
| 201 | setState(intent.getId(), IntentState.DEL_REQ); |
| 202 | } |
| 203 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 204 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | protected void handleErrorOperation(IntentOperation operation) { |
| 206 | //TODO put error message into the intent |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 207 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | ErrorIntent errorIntent = (ErrorIntent) operation.intent; |
| 209 | Intent targetIntent = intents.get(errorIntent.getId()); |
| 210 | if (targetIntent == null) { |
| 211 | // TODO error handling |
| 212 | return; |
| 213 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | switch (targetIntent.getState()) { |
| 216 | case CREATED: |
| 217 | case INST_REQ: |
| 218 | case INST_ACK: |
| 219 | case REROUTE_REQ: |
| 220 | setState(targetIntent.getId(), IntentState.INST_NACK); |
| 221 | break; |
| 222 | case DEL_REQ: |
| 223 | setState(targetIntent.getId(), IntentState.DEL_PENDING); |
| 224 | break; |
| 225 | case INST_NACK: |
| 226 | case DEL_PENDING: |
| 227 | case DEL_ACK: |
| 228 | // do nothing |
| 229 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 230 | default: |
| 231 | log.error("Unknown intent state {}", targetIntent.getState()); |
| 232 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | protected void notifyEvents() { |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame^] | 237 | if (events.isEmpty()) { |
| 238 | return; |
| 239 | } |
| 240 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 241 | for (ChangedListener listener : listeners) { |
| 242 | listener.intentsChange(events); |
| 243 | } |
| 244 | events.clear(); |
| 245 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 246 | } |