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; |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 12 | |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 15 | |
| 16 | /** |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 17 | * The IntentMap contains all of the Intents in the system. It is also |
| 18 | * responsible for handling notifications to interested listeners when |
| 19 | * Intents are changed/updated. |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 20 | */ |
| 21 | public class IntentMap { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 22 | private final HashSet<ChangedListener> listeners = new HashSet<>(); |
| 23 | private final HashMap<String, Intent> intents = new HashMap<>(); |
| 24 | private final LinkedList<ChangedEvent> events = new LinkedList<>(); |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 25 | private static final Logger log = LoggerFactory.getLogger(IntentMap.class); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 26 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | public enum ChangedEventType { |
| 28 | /** |
| 29 | * Added new intent. |
| 30 | */ |
| 31 | ADDED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 32 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Removed existing intent. |
| 35 | * The specified intent is an instance of Intent class (not a child class) |
| 36 | * Only id and state are valid. |
| 37 | */ |
| 38 | REMOVED, |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 39 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Changed state of existing intent. |
| 42 | * The specified intent is an instance of Intent class (not a child class) |
| 43 | * Only id and state are valid. |
| 44 | */ |
| 45 | STATE_CHANGED, |
| 46 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 47 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 48 | /** |
| 49 | * This class is a wrapper for Intent event notifications. |
| 50 | */ |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 51 | public static class ChangedEvent { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Constructor. |
| 55 | * |
| 56 | * @param eventType the type of event |
| 57 | * @param intent the affected Intent |
| 58 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | public ChangedEvent(ChangedEventType eventType, Intent intent) { |
| 60 | this.eventType = eventType; |
| 61 | this.intent = intent; |
| 62 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | public ChangedEventType eventType; |
| 65 | public Intent intent; |
Pavlin Radoslavov | b630929 | 2014-04-11 03:25:47 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Get a string representation of the object. |
| 69 | * <p/> |
| 70 | * The string has the following form: |
| 71 | * [eventType=XXX intent=XXX] |
| 72 | * |
| 73 | * @return a string representation of the object. |
| 74 | */ |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | StringBuilder ret = new StringBuilder(); |
| 78 | ret.append("[eventType="); |
| 79 | ret.append(this.eventType.toString()); |
| 80 | ret.append(" intent="); |
| 81 | ret.append(this.intent.toString()); |
| 82 | ret.append("]"); |
| 83 | return ret.toString(); |
| 84 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 86 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 87 | /** |
| 88 | * An interface to listen to Intent updates. |
| 89 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | public interface ChangedListener extends EventListener { |
| 91 | void intentsChange(LinkedList<ChangedEvent> events); |
| 92 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | //================================================================================ |
| 95 | // public methods |
| 96 | //================================================================================ |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 97 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Performs the specified operations on a list of Intents. |
| 100 | * |
| 101 | * @param operations list of Intents and associated operations |
| 102 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | public void executeOperations(IntentOperationList operations) { |
| 104 | for (IntentOperation operation : operations) { |
Toshio Koide | 3818b4a | 2014-06-26 16:15:52 -0700 | [diff] [blame] | 105 | if (operation == null) { |
| 106 | log.error("executeOperation: null intent operation"); |
| 107 | continue; |
| 108 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | switch (operation.operator) { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 110 | case ADD: |
| 111 | handleAddOperation(operation); |
| 112 | break; |
| 113 | case REMOVE: |
| 114 | handleRemoveOperation(operation); |
| 115 | break; |
| 116 | case ERROR: |
| 117 | handleErrorOperation(operation); |
| 118 | break; |
| 119 | default: |
Toshio Koide | 3818b4a | 2014-06-26 16:15:52 -0700 | [diff] [blame] | 120 | log.error("Unknown intent operation: {}", operation.operator); |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 121 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | notifyEvents(); |
| 125 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 126 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Purge all Intents that are in a state allowing them to be removed. |
| 129 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | public void purge() { |
| 131 | LinkedList<String> removeIds = new LinkedList<>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 132 | |
| 133 | // Collect the IDs of all intents that can be removed |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | for (Entry<String, Intent> entry : intents.entrySet()) { |
| 135 | Intent intent = entry.getValue(); |
| 136 | if (intent.getState() == IntentState.DEL_ACK |
| 137 | || intent.getState() == IntentState.INST_NACK) { |
| 138 | removeIds.add(intent.getId()); |
| 139 | } |
| 140 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 141 | |
| 142 | purge(removeIds); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Purge a collection of Intents specified by Intent IDs. |
| 147 | * |
| 148 | * NOTE: The caller needs to make sure those intents are in a state |
| 149 | * that allows them to be removed. |
| 150 | * |
| 151 | * @param removeIds the collection of Intent IDs to purge. |
| 152 | */ |
| 153 | public void purge(Collection<String> removeIds) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | for (String intentId : removeIds) { |
| 155 | removeIntent(intentId); |
| 156 | } |
| 157 | notifyEvents(); |
| 158 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 159 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Update the states of intents in the map to match those provided. |
| 162 | * |
| 163 | * @param states list of new Intent states |
| 164 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | public void changeStates(IntentStateList states) { |
| 166 | for (Entry<String, IntentState> state : states.entrySet()) { |
| 167 | setState(state.getKey(), state.getValue()); |
| 168 | } |
| 169 | notifyEvents(); |
| 170 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 171 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Get an Intent from the map. |
| 174 | * |
| 175 | * @param intentId ID of requested Intent |
| 176 | * @return Intent with specified ID |
| 177 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | public Intent getIntent(String intentId) { |
| 179 | return intents.get(intentId); |
| 180 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 181 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Get all Intents from the map. |
| 184 | * |
| 185 | * @return a collection including all Intents |
| 186 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | public Collection<Intent> getAllIntents() { |
| 188 | return intents.values(); |
| 189 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 190 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Add a change listener. |
| 193 | * |
| 194 | * @param listener new listener to change events |
| 195 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | public void addChangeListener(ChangedListener listener) { |
| 197 | listeners.add(listener); |
| 198 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 199 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Remove a change listener. |
| 202 | * |
| 203 | * @param listener the listener to be removed |
| 204 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | public void removeChangedListener(ChangedListener listener) { |
| 206 | listeners.remove(listener); |
| 207 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 208 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 209 | //================================================================================ |
| 210 | // methods that affect intents map (protected) |
| 211 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 212 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Put an intent in the map. |
| 215 | * |
| 216 | * @param intent intent to be added |
| 217 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | protected void putIntent(Intent intent) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 219 | if (intents.containsKey(intent.getId())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 220 | removeIntent(intent.getId()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 221 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 222 | intents.put(intent.getId(), intent); |
| 223 | events.add(new ChangedEvent(ChangedEventType.ADDED, intent)); |
| 224 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 225 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Remove an intent from the map. |
| 228 | * |
| 229 | * @param intentId intent to be removed |
| 230 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | protected void removeIntent(String intentId) { |
| 232 | Intent intent = intents.remove(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 233 | if (intent == null) { |
| 234 | return; |
| 235 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 236 | events.add(new ChangedEvent(ChangedEventType.REMOVED, intent)); |
| 237 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 238 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Change the state of an Intent in the map. |
| 241 | * |
| 242 | * @param intentId ID of the Intent to change |
| 243 | * @param state new state for the specified Intent |
| 244 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 245 | protected void setState(String intentId, IntentState state) { |
| 246 | Intent intent = intents.get(intentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 247 | if (intent == null) { |
| 248 | return; |
| 249 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 250 | intent.setState(state); |
| 251 | events.add(new ChangedEvent(ChangedEventType.STATE_CHANGED, intent)); |
| 252 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 253 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 254 | //================================================================================ |
| 255 | // helper methods (protected) |
| 256 | //================================================================================ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 257 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Helper to add Intents to the map. |
| 260 | * |
| 261 | * @param operation the Intent to be added |
| 262 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | protected void handleAddOperation(IntentOperation operation) { |
| 264 | putIntent(operation.intent); |
| 265 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 266 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Helper to remove Intents from the map. |
| 269 | * |
| 270 | * @param operation the Intent to be removed |
| 271 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | protected void handleRemoveOperation(IntentOperation operation) { |
Toshio Koide | 3818b4a | 2014-06-26 16:15:52 -0700 | [diff] [blame] | 273 | if (operation == null) { |
| 274 | log.error("handleRemoveOperation: null operation"); |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 275 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | } |
Toshio Koide | 3818b4a | 2014-06-26 16:15:52 -0700 | [diff] [blame] | 277 | if (operation.intent == null) { |
| 278 | log.error("handleRemoveOperation: null intent"); |
| 279 | return; |
| 280 | } |
| 281 | String intentId = operation.intent.getId(); |
| 282 | Intent intent = getIntent(intentId); |
| 283 | if (intent == null) { |
| 284 | log.error("handleRemoveOperation: Intent ID {} doesn't exist", intentId); |
| 285 | return; |
| 286 | } |
| 287 | setState(intent.getId(), IntentState.DEL_REQ); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 288 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 289 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Helper to handle Intents in the error state. |
| 292 | * |
| 293 | * @param operation the Intent to be resolved |
| 294 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 295 | protected void handleErrorOperation(IntentOperation operation) { |
| 296 | //TODO put error message into the intent |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 297 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 298 | ErrorIntent errorIntent = (ErrorIntent) operation.intent; |
| 299 | Intent targetIntent = intents.get(errorIntent.getId()); |
| 300 | if (targetIntent == null) { |
| 301 | // TODO error handling |
| 302 | return; |
| 303 | } |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 304 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 305 | switch (targetIntent.getState()) { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 306 | case CREATED: |
| 307 | case INST_REQ: |
| 308 | case INST_ACK: |
| 309 | case REROUTE_REQ: |
| 310 | setState(targetIntent.getId(), IntentState.INST_NACK); |
| 311 | break; |
| 312 | case DEL_REQ: |
| 313 | setState(targetIntent.getId(), IntentState.DEL_PENDING); |
| 314 | break; |
| 315 | case INST_NACK: |
| 316 | case DEL_PENDING: |
| 317 | case DEL_ACK: |
| 318 | // do nothing |
| 319 | break; |
| 320 | default: |
| 321 | log.error("Unknown intent state {}", targetIntent.getState()); |
| 322 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Notify all registered listeners of events in queue. |
| 328 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 329 | protected void notifyEvents() { |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 330 | if (events.isEmpty()) { |
| 331 | return; |
| 332 | } |
| 333 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 334 | for (ChangedListener listener : listeners) { |
| 335 | listener.intentsChange(events); |
| 336 | } |
| 337 | events.clear(); |
| 338 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 339 | } |