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