Ray Milkey
Committed by Brian O'Connor

Start IntentOperations removal

Change-Id: Ib5fb9c19b37e447a62c61fa33bb98f3d789cbefa
1 -/*
2 - * Copyright 2014 Open Networking Laboratory
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -package org.onosproject.net.intent;
17 -
18 -import java.util.List;
19 -
20 -import org.junit.After;
21 -import org.junit.Before;
22 -import org.junit.Test;
23 -import org.onosproject.core.ApplicationId;
24 -import org.onosproject.core.DefaultApplicationId;
25 -import org.onosproject.core.IdGenerator;
26 -import org.onosproject.net.ConnectPoint;
27 -import org.onosproject.net.NetTestTools;
28 -import org.onosproject.net.flow.TrafficSelector;
29 -
30 -import com.google.common.testing.EqualsTester;
31 -
32 -import static org.hamcrest.MatcherAssert.assertThat;
33 -import static org.hamcrest.Matchers.hasSize;
34 -import static org.hamcrest.Matchers.is;
35 -import static org.hamcrest.Matchers.isOneOf;
36 -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
37 -
38 -/**
39 - * Tests for the IntentOperations class.
40 - */
41 -public class IntentOperationsTest {
42 -
43 - final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
44 - final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3);
45 - final TrafficSelector selector = new IntentTestsMocks.MockSelector();
46 - final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
47 -
48 - private final ApplicationId appId = new DefaultApplicationId(1, "IntentOperationsTest");
49 -
50 - private Intent intent;
51 - protected IdGenerator idGenerator = new MockIdGenerator();
52 -
53 - @Before
54 - public void setUp() {
55 - Intent.bindIdGenerator(idGenerator);
56 -
57 - intent = new PointToPointIntent(NetTestTools.APP_ID,
58 - selector,
59 - treatment,
60 - ingress,
61 - egress);
62 - }
63 -
64 - @After
65 - public void tearDown() {
66 - Intent.unbindIdGenerator(idGenerator);
67 - }
68 -
69 - /**
70 - * Checks that the IntentOperation and IntentOperations classes are immutable.
71 - */
72 - @Test
73 - public void testImmutability() {
74 - assertThatClassIsImmutable(IntentOperations.class);
75 - assertThatClassIsImmutable(IntentOperations.Builder.class);
76 - assertThatClassIsImmutable(IntentOperation.class);
77 - }
78 -
79 - /**
80 - * Tests equals(), hashCode() and toString() methods.
81 - */
82 - @Test
83 - public void testEquals() {
84 - final IntentOperations operations1 =
85 - IntentOperations.builder(appId)
86 - .addSubmitOperation(intent)
87 - .build();
88 - final IntentOperations sameAsOperations1 =
89 - IntentOperations.builder(appId)
90 - .addSubmitOperation(intent)
91 - .build();
92 - final IntentOperations operations2 =
93 - IntentOperations.builder(appId)
94 - .addReplaceOperation(intent.id(), intent)
95 - .build();
96 -
97 - new EqualsTester()
98 - .addEqualityGroup(operations1, sameAsOperations1)
99 - .addEqualityGroup(operations2)
100 - .testEquals();
101 - }
102 -
103 - /**
104 - * Checks that objects are created correctly.
105 - */
106 - @Test
107 - public void testConstruction() {
108 - final IntentOperations operations =
109 - IntentOperations.builder(appId)
110 - .addUpdateOperation(intent.id())
111 - .addWithdrawOperation(intent.id())
112 - .build();
113 - final List<IntentOperation> operationList = operations.operations();
114 - assertThat(operationList, hasSize(2));
115 - for (final IntentOperation operation : operationList) {
116 - assertThat(operation.type(),
117 - isOneOf(IntentOperation.Type.UPDATE,
118 - IntentOperation.Type.WITHDRAW));
119 - assertThat(operation.intent(), is((Intent) null));
120 - assertThat(operation.intentId(), is(intent.id()));
121 - }
122 - }
123 -}
...@@ -50,7 +50,6 @@ import org.onosproject.net.intent.IntentService; ...@@ -50,7 +50,6 @@ import org.onosproject.net.intent.IntentService;
50 import org.onosproject.net.intent.IntentState; 50 import org.onosproject.net.intent.IntentState;
51 import org.onosproject.net.intent.IntentTestsMocks; 51 import org.onosproject.net.intent.IntentTestsMocks;
52 import org.onosproject.net.resource.LinkResourceAllocations; 52 import org.onosproject.net.resource.LinkResourceAllocations;
53 -import org.onosproject.store.trivial.impl.SimpleIntentBatchQueue;
54 import org.onosproject.store.trivial.impl.SimpleIntentStore; 53 import org.onosproject.store.trivial.impl.SimpleIntentStore;
55 54
56 import com.google.common.collect.HashMultimap; 55 import com.google.common.collect.HashMultimap;
...@@ -278,7 +277,6 @@ public class IntentManagerTest { ...@@ -278,7 +277,6 @@ public class IntentManagerTest {
278 manager = new IntentManager(); 277 manager = new IntentManager();
279 flowRuleService = new MockFlowRuleService(); 278 flowRuleService = new MockFlowRuleService();
280 manager.store = new SimpleIntentStore(); 279 manager.store = new SimpleIntentStore();
281 - manager.batchService = new SimpleIntentBatchQueue();
282 manager.eventDispatcher = new TestEventDispatcher(); 280 manager.eventDispatcher = new TestEventDispatcher();
283 manager.trackerService = new TestIntentTracker(); 281 manager.trackerService = new TestIntentTracker();
284 manager.flowRuleService = flowRuleService; 282 manager.flowRuleService = flowRuleService;
......
...@@ -84,7 +84,6 @@ import org.onosproject.net.intent.HostToHostIntent; ...@@ -84,7 +84,6 @@ import org.onosproject.net.intent.HostToHostIntent;
84 import org.onosproject.net.intent.Intent; 84 import org.onosproject.net.intent.Intent;
85 import org.onosproject.net.intent.IntentId; 85 import org.onosproject.net.intent.IntentId;
86 import org.onosproject.net.intent.IntentOperation; 86 import org.onosproject.net.intent.IntentOperation;
87 -import org.onosproject.net.intent.IntentOperations;
88 import org.onosproject.net.intent.IntentState; 87 import org.onosproject.net.intent.IntentState;
89 import org.onosproject.net.intent.LinkCollectionIntent; 88 import org.onosproject.net.intent.LinkCollectionIntent;
90 import org.onosproject.net.intent.MultiPointToSinglePointIntent; 89 import org.onosproject.net.intent.MultiPointToSinglePointIntent;
...@@ -316,8 +315,7 @@ public final class KryoNamespaces { ...@@ -316,8 +315,7 @@ public final class KryoNamespaces {
316 ObstacleConstraint.class, 315 ObstacleConstraint.class,
317 AnnotationConstraint.class, 316 AnnotationConstraint.class,
318 BooleanConstraint.class, 317 BooleanConstraint.class,
319 - IntentOperation.class, 318 + IntentOperation.class
320 - IntentOperations.class
321 ) 319 )
322 .register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class) 320 .register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class)
323 .register(new URISerializer(), URI.class) 321 .register(new URISerializer(), URI.class)
......
1 -/*
2 - * Copyright 2014 Open Networking Laboratory
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -package org.onosproject.store.trivial.impl;
17 -
18 -import com.google.common.collect.Sets;
19 -import org.apache.felix.scr.annotations.Activate;
20 -import org.apache.felix.scr.annotations.Component;
21 -import org.apache.felix.scr.annotations.Deactivate;
22 -import org.apache.felix.scr.annotations.Service;
23 -import org.onosproject.core.ApplicationId;
24 -import org.onosproject.net.intent.IntentBatchDelegate;
25 -import org.onosproject.net.intent.IntentBatchListener;
26 -import org.onosproject.net.intent.IntentBatchService;
27 -import org.onosproject.net.intent.IntentOperations;
28 -import org.slf4j.Logger;
29 -
30 -import java.util.LinkedList;
31 -import java.util.Queue;
32 -import java.util.Set;
33 -
34 -import static com.google.common.base.Preconditions.checkNotNull;
35 -import static com.google.common.base.Preconditions.checkState;
36 -import static org.slf4j.LoggerFactory.getLogger;
37 -
38 -@Component(immediate = true)
39 -@Service
40 -public class SimpleIntentBatchQueue implements IntentBatchService {
41 -
42 - private final Logger log = getLogger(getClass());
43 - private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
44 - private final Set<IntentOperations> currentBatches = Sets.newHashSet();
45 - private IntentBatchDelegate delegate;
46 -
47 - @Activate
48 - public void activate() {
49 - log.info("Started");
50 - }
51 -
52 - @Deactivate
53 - public void deactivate() {
54 - log.info("Stopped");
55 - }
56 -
57 - @Override
58 - public void addIntentOperations(IntentOperations operations) {
59 - checkState(delegate != null, "No delegate set");
60 - synchronized (this) {
61 - pendingBatches.add(operations);
62 - if (currentBatches.isEmpty()) {
63 - IntentOperations work = pendingBatches.poll();
64 - currentBatches.add(work);
65 - delegate.execute(work);
66 - }
67 - }
68 - }
69 -
70 - @Override
71 - public void removeIntentOperations(IntentOperations operations) {
72 - // we allow at most one outstanding batch at a time
73 - synchronized (this) {
74 - checkState(currentBatches.remove(operations), "Operations not found in current ops.");
75 - checkState(currentBatches.isEmpty(), "More than one outstanding batch.");
76 - IntentOperations work = pendingBatches.poll();
77 - if (work != null) {
78 - currentBatches.add(work);
79 - delegate.execute(work);
80 - }
81 - }
82 - }
83 -
84 - @Override
85 - public Set<IntentOperations> getPendingOperations() {
86 - synchronized (this) {
87 - Set<IntentOperations> set = Sets.newHashSet(pendingBatches);
88 - set.addAll(currentBatches); // TODO refactor this current vs. pending
89 - return set;
90 - }
91 - }
92 -
93 - @Override
94 - public boolean isLocalLeader(ApplicationId applicationId) {
95 - return true;
96 - }
97 -
98 - @Override
99 - public void setDelegate(IntentBatchDelegate delegate) {
100 - this.delegate = checkNotNull(delegate, "Delegate cannot be null");
101 - }
102 -
103 - @Override
104 - public void unsetDelegate(IntentBatchDelegate delegate) {
105 - if (this.delegate != null && this.delegate.equals(delegate)) {
106 - this.delegate = null;
107 - }
108 - }
109 -
110 - @Override
111 - public void addListener(IntentBatchListener listener) {
112 - // no-op
113 - //TODO: we are always the master
114 - }
115 -
116 - @Override
117 - public void removeListener(IntentBatchListener listener) {
118 - // no-op
119 - //TODO: we are always the master
120 - }
121 -}