Brian O'Connor

Updates to Intent Manager and store interface

Change-Id: Ida612bf5d0f4abe7e81d2f307a80f989603015e7
...@@ -15,10 +15,11 @@ ...@@ -15,10 +15,11 @@
15 */ 15 */
16 package org.onosproject.net.intent; 16 package org.onosproject.net.intent;
17 17
18 +import java.util.Collection;
19 +
18 /** 20 /**
19 * Facade for receiving notifications from the intent batch service. 21 * Facade for receiving notifications from the intent batch service.
20 */ 22 */
21 -@Deprecated
22 public interface IntentBatchDelegate { 23 public interface IntentBatchDelegate {
23 24
24 /** 25 /**
...@@ -26,12 +27,6 @@ public interface IntentBatchDelegate { ...@@ -26,12 +27,6 @@ public interface IntentBatchDelegate {
26 * 27 *
27 * @param operations batch of operations 28 * @param operations batch of operations
28 */ 29 */
29 - void execute(IntentOperations operations); 30 + void execute(Collection<IntentData> operations);
30 31
31 - /**
32 - * Cancesl the specified batch of intent operations.
33 - *
34 - * @param operations batch of operations to be cancelled
35 - */
36 - void cancel(IntentOperations operations);
37 } 32 }
......
...@@ -45,6 +45,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> { ...@@ -45,6 +45,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
45 * @param intentId intent identification 45 * @param intentId intent identification
46 * @return intent or null if not found 46 * @return intent or null if not found
47 */ 47 */
48 + @Deprecated
48 Intent getIntent(IntentId intentId); 49 Intent getIntent(IntentId intentId);
49 50
50 /** 51 /**
...@@ -53,6 +54,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> { ...@@ -53,6 +54,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
53 * @param intentId intent identification 54 * @param intentId intent identification
54 * @return current intent state 55 * @return current intent state
55 */ 56 */
57 + @Deprecated
56 IntentState getIntentState(IntentId intentId); 58 IntentState getIntentState(IntentId intentId);
57 59
58 /** 60 /**
...@@ -62,6 +64,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> { ...@@ -62,6 +64,7 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
62 * @param intentId original intent identifier 64 * @param intentId original intent identifier
63 * @return compiled installable intents 65 * @return compiled installable intents
64 */ 66 */
67 + @Deprecated
65 List<Intent> getInstallableIntents(IntentId intentId); 68 List<Intent> getInstallableIntents(IntentId intentId);
66 69
67 /** 70 /**
...@@ -74,6 +77,26 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> { ...@@ -74,6 +77,26 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
74 List<Operation> batchWrite(BatchWrite batch); 77 List<Operation> batchWrite(BatchWrite batch);
75 78
76 /** 79 /**
80 + * Returns the intent with the specified identifier.
81 + *
82 + * @param key key
83 + * @return intent or null if not found
84 + */
85 + default Intent getIntent(String key) { //FIXME remove when impl.
86 + return null;
87 + }
88 +
89 + /**
90 + * Returns the intent data object associated with the specified key.
91 + *
92 + * @param key key to look up
93 + * @return intent data object
94 + */
95 + default IntentData getIntentData(String key) { //FIXME remove when impl.
96 + return null;
97 + }
98 +
99 + /**
77 * Adds a new operation, which should be persisted and delegated. 100 * Adds a new operation, which should be persisted and delegated.
78 * 101 *
79 * @param intent operation 102 * @param intent operation
......
...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl; ...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl;
17 17
18 import com.google.common.collect.Maps; 18 import com.google.common.collect.Maps;
19 import org.onlab.util.AbstractAccumulator; 19 import org.onlab.util.AbstractAccumulator;
20 +import org.onosproject.net.intent.IntentBatchDelegate;
20 import org.onosproject.net.intent.IntentData; 21 import org.onosproject.net.intent.IntentData;
21 22
22 import java.util.List; 23 import java.util.List;
...@@ -37,16 +38,20 @@ public class IntentAccumulator extends AbstractAccumulator<IntentData> { ...@@ -37,16 +38,20 @@ public class IntentAccumulator extends AbstractAccumulator<IntentData> {
37 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt 38 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt
38 private static final Timer TIMER = new Timer("intent-op-batching"); 39 private static final Timer TIMER = new Timer("intent-op-batching");
39 40
41 + private final IntentBatchDelegate delegate;
42 +
40 /** 43 /**
41 * Creates an intent operation accumulator. 44 * Creates an intent operation accumulator.
42 */ 45 */
43 - protected IntentAccumulator() { 46 + protected IntentAccumulator(IntentBatchDelegate delegate) {
44 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); 47 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
48 + this.delegate = delegate;
45 } 49 }
46 50
47 @Override 51 @Override
48 public void processEvents(List<IntentData> ops) { 52 public void processEvents(List<IntentData> ops) {
49 Map<String, IntentData> opMap = reduce(ops); 53 Map<String, IntentData> opMap = reduce(ops);
54 + delegate.execute(opMap.values());
50 // FIXME kick off the work 55 // FIXME kick off the work
51 //for (IntentData data : opMap.values()) {} 56 //for (IntentData data : opMap.values()) {}
52 } 57 }
......
...@@ -15,15 +15,11 @@ ...@@ -15,15 +15,11 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl; 16 package org.onosproject.net.intent.impl;
17 17
18 -import java.util.Collection; 18 +import com.google.common.collect.HashMultimap;
19 -import java.util.Collections; 19 +import com.google.common.collect.Lists;
20 -import java.util.List; 20 +import com.google.common.collect.Maps;
21 -import java.util.Map; 21 +import com.google.common.collect.Multimap;
22 -import java.util.Set; 22 +import com.google.common.collect.Sets;
23 -import java.util.concurrent.CountDownLatch;
24 -import java.util.concurrent.TimeUnit;
25 -import java.util.concurrent.atomic.AtomicLong;
26 -
27 import org.hamcrest.Description; 23 import org.hamcrest.Description;
28 import org.hamcrest.TypeSafeMatcher; 24 import org.hamcrest.TypeSafeMatcher;
29 import org.junit.After; 25 import org.junit.After;
...@@ -52,21 +48,20 @@ import org.onosproject.net.intent.IntentTestsMocks; ...@@ -52,21 +48,20 @@ import org.onosproject.net.intent.IntentTestsMocks;
52 import org.onosproject.net.resource.LinkResourceAllocations; 48 import org.onosproject.net.resource.LinkResourceAllocations;
53 import org.onosproject.store.trivial.impl.SimpleIntentStore; 49 import org.onosproject.store.trivial.impl.SimpleIntentStore;
54 50
55 -import com.google.common.collect.HashMultimap; 51 +import java.util.Collection;
56 -import com.google.common.collect.Lists; 52 +import java.util.Collections;
57 -import com.google.common.collect.Maps; 53 +import java.util.List;
58 -import com.google.common.collect.Multimap; 54 +import java.util.Map;
59 -import com.google.common.collect.Sets; 55 +import java.util.Set;
56 +import java.util.concurrent.CountDownLatch;
57 +import java.util.concurrent.TimeUnit;
58 +import java.util.concurrent.atomic.AtomicLong;
60 59
61 import static org.hamcrest.MatcherAssert.assertThat; 60 import static org.hamcrest.MatcherAssert.assertThat;
62 import static org.hamcrest.Matchers.hasSize; 61 import static org.hamcrest.Matchers.hasSize;
63 -import static org.junit.Assert.assertEquals; 62 +import static org.junit.Assert.*;
64 -import static org.junit.Assert.assertNotNull;
65 -import static org.junit.Assert.assertTrue;
66 import static org.onlab.util.Tools.delay; 63 import static org.onlab.util.Tools.delay;
67 -import static org.onosproject.net.intent.IntentState.FAILED; 64 +import static org.onosproject.net.intent.IntentState.*;
68 -import static org.onosproject.net.intent.IntentState.INSTALLED;
69 -import static org.onosproject.net.intent.IntentState.WITHDRAWN;
70 65
71 /** 66 /**
72 * Test intent manager and transitions. 67 * Test intent manager and transitions.
...@@ -305,14 +300,16 @@ public class IntentManagerTest { ...@@ -305,14 +300,16 @@ public class IntentManagerTest {
305 } 300 }
306 //the batch has not yet been removed when we receive the last event 301 //the batch has not yet been removed when we receive the last event
307 // FIXME: this doesn't guarantee to avoid the race 302 // FIXME: this doesn't guarantee to avoid the race
308 - for (int tries = 0; tries < 10; tries++) { 303 +
309 - if (manager.batchService.getPendingOperations().isEmpty()) { 304 + //FIXME
310 - break; 305 +// for (int tries = 0; tries < 10; tries++) {
311 - } 306 +// if (manager.batchService.getPendingOperations().isEmpty()) {
312 - delay(10); 307 +// break;
313 - } 308 +// }
314 - assertTrue("There are still pending batch operations.", 309 +// delay(10);
315 - manager.batchService.getPendingOperations().isEmpty()); 310 +// }
311 +// assertTrue("There are still pending batch operations.",
312 +// manager.batchService.getPendingOperations().isEmpty());
316 313
317 extensionService.unregisterCompiler(MockIntent.class); 314 extensionService.unregisterCompiler(MockIntent.class);
318 extensionService.unregisterInstaller(MockInstallableIntent.class); 315 extensionService.unregisterInstaller(MockInstallableIntent.class);
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
15 */ 15 */
16 package org.onosproject.store.trivial.impl; 16 package org.onosproject.store.trivial.impl;
17 17
18 -import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Lists; 18 import com.google.common.collect.Lists;
19 +import com.google.common.collect.Maps;
20 import org.apache.felix.scr.annotations.Activate; 20 import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component; 21 import org.apache.felix.scr.annotations.Component;
22 import org.apache.felix.scr.annotations.Deactivate; 22 import org.apache.felix.scr.annotations.Deactivate;
...@@ -36,10 +36,10 @@ import org.slf4j.Logger; ...@@ -36,10 +36,10 @@ import org.slf4j.Logger;
36 import java.util.Collections; 36 import java.util.Collections;
37 import java.util.List; 37 import java.util.List;
38 import java.util.Map; 38 import java.util.Map;
39 -import java.util.concurrent.ConcurrentHashMap; 39 +import java.util.stream.Collectors;
40 40
41 -import static com.google.common.base.Preconditions.*; 41 +import static com.google.common.base.Preconditions.checkArgument;
42 -import static org.onosproject.net.intent.IntentState.WITHDRAWN; 42 +import static com.google.common.base.Preconditions.checkNotNull;
43 import static org.slf4j.LoggerFactory.getLogger; 43 import static org.slf4j.LoggerFactory.getLogger;
44 44
45 @Component(immediate = true) 45 @Component(immediate = true)
...@@ -51,11 +51,8 @@ public class SimpleIntentStore ...@@ -51,11 +51,8 @@ public class SimpleIntentStore
51 private final Logger log = getLogger(getClass()); 51 private final Logger log = getLogger(getClass());
52 52
53 // current state maps FIXME.. make this a IntentData map 53 // current state maps FIXME.. make this a IntentData map
54 - private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>(); 54 + private final Map<String, IntentData> current = Maps.newConcurrentMap();
55 - private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>(); 55 + private final Map<String, IntentData> pending = Maps.newConcurrentMap(); //String is "key"
56 - private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>();
57 -
58 - private final Map<String, IntentData> pending = new ConcurrentHashMap<>(); //String is "key"
59 56
60 @Activate 57 @Activate
61 public void activate() { 58 public void activate() {
...@@ -67,45 +64,32 @@ public class SimpleIntentStore ...@@ -67,45 +64,32 @@ public class SimpleIntentStore
67 log.info("Stopped"); 64 log.info("Stopped");
68 } 65 }
69 66
70 - private void createIntent(Intent intent) {
71 - if (intents.containsKey(intent.id())) {
72 - return;
73 - }
74 - intents.put(intent.id(), intent);
75 - this.setState(intent, IntentState.INSTALL_REQ);
76 - }
77 -
78 - private void removeIntent(IntentId intentId) {
79 - checkState(getIntentState(intentId) == WITHDRAWN,
80 - "Intent state for {} is not WITHDRAWN.", intentId);
81 - intents.remove(intentId);
82 - installable.remove(intentId);
83 - states.remove(intentId);
84 - }
85 -
86 @Override 67 @Override
87 public long getIntentCount() { 68 public long getIntentCount() {
88 - return intents.size(); 69 + return current.size();
89 } 70 }
90 71
91 @Override 72 @Override
92 public Iterable<Intent> getIntents() { 73 public Iterable<Intent> getIntents() {
93 - return ImmutableSet.copyOf(intents.values()); 74 + return current.values().stream()
75 + .map(IntentData::intent)
76 + .collect(Collectors.toList());
94 } 77 }
95 78
96 @Override 79 @Override
97 public Intent getIntent(IntentId intentId) { 80 public Intent getIntent(IntentId intentId) {
98 - return intents.get(intentId); 81 + throw new UnsupportedOperationException("deprecated");
99 } 82 }
100 83
101 @Override 84 @Override
102 public IntentState getIntentState(IntentId id) { 85 public IntentState getIntentState(IntentId id) {
103 - return states.get(id); 86 + throw new UnsupportedOperationException("deprecated");
104 } 87 }
105 88
106 private void setState(Intent intent, IntentState state) { 89 private void setState(Intent intent, IntentState state) {
90 + //FIXME
107 IntentId id = intent.id(); 91 IntentId id = intent.id();
108 - states.put(id, state); 92 +// states.put(id, state);
109 IntentEvent.Type type = null; 93 IntentEvent.Type type = null;
110 94
111 switch (state) { 95 switch (state) {
...@@ -133,16 +117,23 @@ public class SimpleIntentStore ...@@ -133,16 +117,23 @@ public class SimpleIntentStore
133 } 117 }
134 118
135 private void setInstallableIntents(IntentId intentId, List<Intent> result) { 119 private void setInstallableIntents(IntentId intentId, List<Intent> result) {
136 - installable.put(intentId, result); 120 + //FIXME
121 +// installable.put(intentId, result);
137 } 122 }
138 123
139 @Override 124 @Override
140 public List<Intent> getInstallableIntents(IntentId intentId) { 125 public List<Intent> getInstallableIntents(IntentId intentId) {
141 - return installable.get(intentId); 126 + throw new UnsupportedOperationException("deprecated");
127 + }
128 +
129 + @Override
130 + public IntentData getIntentData(String key) {
131 + return current.get(key);
142 } 132 }
143 133
144 private void removeInstalledIntents(IntentId intentId) { 134 private void removeInstalledIntents(IntentId intentId) {
145 - installable.remove(intentId); 135 + //FIXME
136 +// installable.remove(intentId);
146 } 137 }
147 138
148 /** 139 /**
...@@ -165,14 +156,14 @@ public class SimpleIntentStore ...@@ -165,14 +156,14 @@ public class SimpleIntentStore
165 "CREATE_INTENT takes 1 argument. %s", op); 156 "CREATE_INTENT takes 1 argument. %s", op);
166 Intent intent = (Intent) op.args().get(0); 157 Intent intent = (Intent) op.args().get(0);
167 // TODO: what if it failed? 158 // TODO: what if it failed?
168 - createIntent(intent); 159 +// createIntent(intent); FIXME
169 break; 160 break;
170 161
171 case REMOVE_INTENT: 162 case REMOVE_INTENT:
172 checkArgument(op.args().size() == 1, 163 checkArgument(op.args().size() == 1,
173 "REMOVE_INTENT takes 1 argument. %s", op); 164 "REMOVE_INTENT takes 1 argument. %s", op);
174 IntentId intentId = (IntentId) op.args().get(0); 165 IntentId intentId = (IntentId) op.args().get(0);
175 - removeIntent(intentId); 166 +// removeIntent(intentId); FIXME
176 break; 167 break;
177 168
178 case REMOVE_INSTALLED: 169 case REMOVE_INSTALLED:
......