Ray Milkey
Committed by Brian O'Connor

Start IntentOperations removal

Change-Id: Ib5fb9c19b37e447a62c61fa33bb98f3d789cbefa
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.intent;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.IdGenerator;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.flow.TrafficSelector;
import com.google.common.testing.EqualsTester;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Tests for the IntentOperations class.
*/
public class IntentOperationsTest {
final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3);
final TrafficSelector selector = new IntentTestsMocks.MockSelector();
final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
private final ApplicationId appId = new DefaultApplicationId(1, "IntentOperationsTest");
private Intent intent;
protected IdGenerator idGenerator = new MockIdGenerator();
@Before
public void setUp() {
Intent.bindIdGenerator(idGenerator);
intent = new PointToPointIntent(NetTestTools.APP_ID,
selector,
treatment,
ingress,
egress);
}
@After
public void tearDown() {
Intent.unbindIdGenerator(idGenerator);
}
/**
* Checks that the IntentOperation and IntentOperations classes are immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(IntentOperations.class);
assertThatClassIsImmutable(IntentOperations.Builder.class);
assertThatClassIsImmutable(IntentOperation.class);
}
/**
* Tests equals(), hashCode() and toString() methods.
*/
@Test
public void testEquals() {
final IntentOperations operations1 =
IntentOperations.builder(appId)
.addSubmitOperation(intent)
.build();
final IntentOperations sameAsOperations1 =
IntentOperations.builder(appId)
.addSubmitOperation(intent)
.build();
final IntentOperations operations2 =
IntentOperations.builder(appId)
.addReplaceOperation(intent.id(), intent)
.build();
new EqualsTester()
.addEqualityGroup(operations1, sameAsOperations1)
.addEqualityGroup(operations2)
.testEquals();
}
/**
* Checks that objects are created correctly.
*/
@Test
public void testConstruction() {
final IntentOperations operations =
IntentOperations.builder(appId)
.addUpdateOperation(intent.id())
.addWithdrawOperation(intent.id())
.build();
final List<IntentOperation> operationList = operations.operations();
assertThat(operationList, hasSize(2));
for (final IntentOperation operation : operationList) {
assertThat(operation.type(),
isOneOf(IntentOperation.Type.UPDATE,
IntentOperation.Type.WITHDRAW));
assertThat(operation.intent(), is((Intent) null));
assertThat(operation.intentId(), is(intent.id()));
}
}
}
......@@ -50,7 +50,6 @@ import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.IntentState;
import org.onosproject.net.intent.IntentTestsMocks;
import org.onosproject.net.resource.LinkResourceAllocations;
import org.onosproject.store.trivial.impl.SimpleIntentBatchQueue;
import org.onosproject.store.trivial.impl.SimpleIntentStore;
import com.google.common.collect.HashMultimap;
......@@ -278,7 +277,6 @@ public class IntentManagerTest {
manager = new IntentManager();
flowRuleService = new MockFlowRuleService();
manager.store = new SimpleIntentStore();
manager.batchService = new SimpleIntentBatchQueue();
manager.eventDispatcher = new TestEventDispatcher();
manager.trackerService = new TestIntentTracker();
manager.flowRuleService = flowRuleService;
......
......@@ -84,7 +84,6 @@ import org.onosproject.net.intent.HostToHostIntent;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentId;
import org.onosproject.net.intent.IntentOperation;
import org.onosproject.net.intent.IntentOperations;
import org.onosproject.net.intent.IntentState;
import org.onosproject.net.intent.LinkCollectionIntent;
import org.onosproject.net.intent.MultiPointToSinglePointIntent;
......@@ -316,8 +315,7 @@ public final class KryoNamespaces {
ObstacleConstraint.class,
AnnotationConstraint.class,
BooleanConstraint.class,
IntentOperation.class,
IntentOperations.class
IntentOperation.class
)
.register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class)
.register(new URISerializer(), URI.class)
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.trivial.impl;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.intent.IntentBatchDelegate;
import org.onosproject.net.intent.IntentBatchListener;
import org.onosproject.net.intent.IntentBatchService;
import org.onosproject.net.intent.IntentOperations;
import org.slf4j.Logger;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.slf4j.LoggerFactory.getLogger;
@Component(immediate = true)
@Service
public class SimpleIntentBatchQueue implements IntentBatchService {
private final Logger log = getLogger(getClass());
private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
private final Set<IntentOperations> currentBatches = Sets.newHashSet();
private IntentBatchDelegate delegate;
@Activate
public void activate() {
log.info("Started");
}
@Deactivate
public void deactivate() {
log.info("Stopped");
}
@Override
public void addIntentOperations(IntentOperations operations) {
checkState(delegate != null, "No delegate set");
synchronized (this) {
pendingBatches.add(operations);
if (currentBatches.isEmpty()) {
IntentOperations work = pendingBatches.poll();
currentBatches.add(work);
delegate.execute(work);
}
}
}
@Override
public void removeIntentOperations(IntentOperations operations) {
// we allow at most one outstanding batch at a time
synchronized (this) {
checkState(currentBatches.remove(operations), "Operations not found in current ops.");
checkState(currentBatches.isEmpty(), "More than one outstanding batch.");
IntentOperations work = pendingBatches.poll();
if (work != null) {
currentBatches.add(work);
delegate.execute(work);
}
}
}
@Override
public Set<IntentOperations> getPendingOperations() {
synchronized (this) {
Set<IntentOperations> set = Sets.newHashSet(pendingBatches);
set.addAll(currentBatches); // TODO refactor this current vs. pending
return set;
}
}
@Override
public boolean isLocalLeader(ApplicationId applicationId) {
return true;
}
@Override
public void setDelegate(IntentBatchDelegate delegate) {
this.delegate = checkNotNull(delegate, "Delegate cannot be null");
}
@Override
public void unsetDelegate(IntentBatchDelegate delegate) {
if (this.delegate != null && this.delegate.equals(delegate)) {
this.delegate = null;
}
}
@Override
public void addListener(IntentBatchListener listener) {
// no-op
//TODO: we are always the master
}
@Override
public void removeListener(IntentBatchListener listener) {
// no-op
//TODO: we are always the master
}
}