Brian O'Connor

Refactored intermediate IntentUpdate classes

Change-Id: I3d4a435ef4aa97559d5407d49f45519098c3f193
......@@ -15,7 +15,10 @@
*/
package org.onosproject.net.intent;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchEntry;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.flow.FlowRuleOperations;
import java.util.List;
......@@ -30,7 +33,32 @@ public interface IntentInstaller<T extends Intent> {
* @return flow rule operations to complete install
* @throws IntentException if issues are encountered while installing the intent
*/
@Deprecated
List<FlowRuleBatchOperation> install(T intent);
// FIXME
default FlowRuleOperations.Builder install2(T intent) {
FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
for (FlowRuleBatchOperation batch : install(intent)) {
for (FlowRuleBatchEntry entry : batch.getOperations()) {
FlowRule rule = entry.target();
switch (entry.operator()) {
case ADD:
builder.add(rule);
break;
case REMOVE:
builder.remove(rule);
break;
case MODIFY:
builder.modify(rule);
break;
default:
break;
}
}
builder.newStage();
}
return builder;
}
/**
* Uninstalls the specified intent from the environment.
......@@ -39,7 +67,32 @@ public interface IntentInstaller<T extends Intent> {
* @return flow rule operations to complete uninstall
* @throws IntentException if issues are encountered while uninstalling the intent
*/
@Deprecated
List<FlowRuleBatchOperation> uninstall(T intent);
// FIXME
default FlowRuleOperations.Builder uninstall2(T intent) {
FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
for (FlowRuleBatchOperation batch : uninstall(intent)) {
for (FlowRuleBatchEntry entry : batch.getOperations()) {
FlowRule rule = entry.target();
switch (entry.operator()) {
case ADD:
builder.add(rule);
break;
case REMOVE:
builder.remove(rule);
break;
case MODIFY:
builder.modify(rule);
break;
default:
break;
}
}
builder.newStage();
}
return builder;
}
/**
* Replaces the specified intent with a new one in the environment.
......@@ -49,6 +102,31 @@ public interface IntentInstaller<T extends Intent> {
* @return flow rule operations to complete the replace
* @throws IntentException if issues are encountered while uninstalling the intent
*/
@Deprecated
List<FlowRuleBatchOperation> replace(T oldIntent, T newIntent);
// FIXME
default FlowRuleOperations.Builder replace2(T oldIntent, T newIntent) {
FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
for (FlowRuleBatchOperation batch : replace(oldIntent, newIntent)) {
for (FlowRuleBatchEntry entry : batch.getOperations()) {
FlowRule rule = entry.target();
switch (entry.operator()) {
case ADD:
builder.add(rule);
break;
case REMOVE:
builder.remove(rule);
break;
case MODIFY:
builder.modify(rule);
break;
default:
break;
}
}
builder.newStage();
}
return builder;
}
}
......
......@@ -16,10 +16,12 @@
package org.onosproject.net.intent.impl;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentData;
import org.onosproject.net.intent.IntentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -30,25 +32,29 @@ class Compiling implements IntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final IntentData pending;
private final IntentData current;
Compiling(IntentManager intentManager, Intent intent) {
Compiling(IntentManager intentManager, IntentData pending, IntentData current) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.pending = checkNotNull(pending);
this.current = current;
}
@Override
public Optional<IntentUpdate> execute() {
try {
return Optional.of(new Installing(intentManager, intent, intentManager.compileIntent(intent, null)));
List<Intent> installables = (current != null) ? current.installables() : null;
pending.setInstallables(intentManager.compileIntent(pending.intent(), installables));
return Optional.of(new Installing(intentManager, pending, current));
} catch (PathNotFoundException e) {
log.debug("Path not found for intent {}", intent);
log.debug("Path not found for intent {}", pending.intent());
// TODO: revisit to implement failure handling
return Optional.of(new DoNothing());
return Optional.of(new CompilingFailed(pending)); //FIXME failed state transition
} catch (IntentException e) {
log.warn("Unable to compile intent {} due to:", intent.id(), e);
log.warn("Unable to compile intent {} due to:", pending.intent().id(), e);
// TODO: revisit to implement failure handling
return Optional.of(new DoNothing());
return Optional.of(new CompilingFailed(pending)); //FIXME failed state transition
}
}
}
......
......@@ -15,8 +15,26 @@
*/
package org.onosproject.net.intent.impl;
import org.onosproject.net.intent.IntentData;
import static org.onosproject.net.intent.IntentState.FAILED;
/**
* Represents a phase doing nothing.
* Represents a phase where the compile has failed.
*/
class DoNothing implements CompletedIntentUpdate {
class CompilingFailed implements CompletedIntentUpdate {
private final IntentData intentData;
CompilingFailed(IntentData intentData) {
this.intentData = intentData;
this.intentData.setState(FAILED);
}
@Override
public IntentData data() {
return intentData;
}
//FIXME we also need to decide what to do with the current intent's resources i.e. cleanup or revert
}
......
......@@ -15,11 +15,8 @@
*/
package org.onosproject.net.intent.impl;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentData;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
......@@ -27,38 +24,10 @@ import java.util.Optional;
*/
interface CompletedIntentUpdate extends IntentUpdate {
/**
* Moves forward with the contained current batch.
* This method is invoked when the batch is successfully completed.
*/
default void batchSuccess() {}
/**
* Reverts the contained batches.
* This method is invoked when the batch results in failure.
*/
default void batchFailed() {}
/**
* Returns the current FlowRuleBatchOperation.
*
* @return current FlowRuleBatchOperation
*/
default FlowRuleBatchOperation currentBatch() {
return null;
}
/**
* Returns all of installable intents this instance holds.
*
* @return all of installable intents
*/
default List<Intent> allInstallables() {
return Collections.emptyList();
}
@Override
default Optional<IntentUpdate> execute() {
return Optional.empty();
}
IntentData data();
}
......
......@@ -15,7 +15,6 @@
*/
package org.onosproject.net.intent.impl;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentData;
import java.util.Optional;
......@@ -27,17 +26,17 @@ class InstallRequest implements IntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final IntentData currentState;
private final IntentData pending;
InstallRequest(IntentManager intentManager, Intent intent, IntentData currentState) {
InstallRequest(IntentManager intentManager, IntentData intentData) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.currentState = currentState;
this.pending = checkNotNull(intentData);
}
@Override
public Optional<IntentUpdate> execute() {
return Optional.of(new Compiling(intentManager, intent)); //FIXME
//FIXME... store hack
IntentData current = intentManager.store.getIntentData(pending.key());
return Optional.of(new Compiling(intentManager, pending, current));
}
}
......
......@@ -15,60 +15,21 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentState;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.net.intent.IntentData;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.intent.IntentState.FAILED;
import static org.onosproject.net.intent.IntentState.INSTALLING;
class Installed implements CompletedIntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final List<Intent> installables;
private IntentState intentState;
private final List<FlowRuleBatchOperation> batches;
private int currentBatch = 0;
Installed(IntentManager intentManager,
Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.installables = ImmutableList.copyOf(checkNotNull(installables));
this.batches = new LinkedList<>(checkNotNull(batches));
this.intentState = INSTALLING;
}
@Override
public void batchSuccess() {
currentBatch++;
}
@Override
public List<Intent> allInstallables() {
return installables;
}
private final IntentData intentData;
@Override
public FlowRuleBatchOperation currentBatch() {
return currentBatch < batches.size() ? batches.get(currentBatch) : null;
Installed(IntentData intentData) {
this.intentData = checkNotNull(intentData);
this.intentData.setState(INSTALLING);
}
@Override
public void batchFailed() {
for (int i = batches.size() - 1; i >= currentBatch; i--) {
batches.remove(i);
}
intentState = FAILED;
batches.addAll(intentManager.uninstallIntent(intent, installables));
// TODO we might want to try to recompile the new intent
public IntentData data() {
return intentData;
}
}
......
......@@ -15,13 +15,11 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.flow.FlowRuleOperations;
import org.onosproject.net.intent.IntentData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -32,26 +30,27 @@ class Installing implements IntentUpdate {
private static final Logger log = LoggerFactory.getLogger(Installing.class);
private final IntentManager intentManager;
private final Intent intent;
private final List<Intent> installables;
private final IntentData pending;
private final IntentData current;
// TODO: define an interface and use it, instead of IntentManager
Installing(IntentManager intentManager, Intent intent, List<Intent> installables) {
Installing(IntentManager intentManager, IntentData pending, IntentData current) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.installables = ImmutableList.copyOf(checkNotNull(installables));
this.pending = checkNotNull(pending);
this.current = current;
}
@Override
public Optional<IntentUpdate> execute() {
try {
List<FlowRuleBatchOperation> converted = intentManager.convert(installables);
FlowRuleOperations flowRules = intentManager.coordinate(pending.installables());
// TODO: call FlowRuleService API to push FlowRules and track resources,
// which the submitted intent will use.
return Optional.of(new Installed(intentManager, intent, installables, converted));
intentManager.flowRuleService.apply(flowRules);
return Optional.of(new Installed(pending));
} catch (FlowRuleBatchOperationConversionException e) {
log.warn("Unable to install intent {} due to:", intent.id(), e.getCause());
return Optional.of(new InstallingFailed(intentManager, intent, installables, e.converted()));
log.warn("Unable to install intent {} due to:", pending.intent().id(), e.getCause());
return Optional.of(new InstallingFailed(pending));
}
}
}
......
......@@ -15,53 +15,23 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.net.intent.IntentData;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.intent.IntentState.FAILED;
class InstallingFailed implements CompletedIntentUpdate {
private IntentManager intentManager;
private final Intent intent;
private final List<Intent> installables;
private final List<FlowRuleBatchOperation> batches;
private int currentBatch = 0;
InstallingFailed(IntentManager intentManager,
Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) {
this.intentManager = intentManager;
this.intent = checkNotNull(intent);
this.installables = ImmutableList.copyOf(checkNotNull(installables));
this.batches = new LinkedList<>(checkNotNull(batches));
}
private final IntentData intentData;
@Override
public List<Intent> allInstallables() {
return installables;
InstallingFailed(IntentData intentData) {
this.intentData = checkNotNull(intentData);
this.intentData.setState(FAILED); //FIXME maybe should be "BROKEN"
//TODO consider adding the flow rule operations here
}
@Override
public void batchSuccess() {
currentBatch++;
}
@Override
public FlowRuleBatchOperation currentBatch() {
return currentBatch < batches.size() ? batches.get(currentBatch) : null;
}
@Override
public void batchFailed() {
for (int i = batches.size() - 1; i >= currentBatch; i--) {
batches.remove(i);
}
batches.addAll(intentManager.uninstallIntent(intent, installables));
// TODO we might want to try to recompile the new intent
public IntentData data() {
return intentData;
}
}
......
......@@ -15,7 +15,6 @@
*/
package org.onosproject.net.intent.impl;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentData;
import java.util.Optional;
......@@ -26,17 +25,20 @@ class WithdrawRequest implements IntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final IntentData currentState;
private final IntentData pending;
WithdrawRequest(IntentManager intentManager, Intent intent, IntentData currentState) {
WithdrawRequest(IntentManager intentManager, IntentData intentData) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.currentState = currentState;
this.pending = checkNotNull(intentData);
}
@Override
public Optional<IntentUpdate> execute() {
return Optional.of(new Withdrawing(intentManager, intent, currentState.installables())); //FIXME
//FIXME... store hack
IntentData current = intentManager.store.getIntentData(pending.key());
//TODO perhaps we want to validate that the pending and current are the
// same version i.e. they are the same
// Note: this call is not just the symmetric version of submit
return Optional.of(new Withdrawing(intentManager, pending, current));
}
}
......
......@@ -15,11 +15,9 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.flow.FlowRuleOperations;
import org.onosproject.net.intent.IntentData;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -28,19 +26,21 @@ class Withdrawing implements IntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final List<Intent> installables;
private final IntentData pending;
private final IntentData current;
Withdrawing(IntentManager intentManager, Intent intent, List<Intent> installables) {
Withdrawing(IntentManager intentManager, IntentData pending, IntentData current) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.installables = ImmutableList.copyOf(installables);
this.pending = checkNotNull(pending);
this.current = checkNotNull(current);
}
@Override
public Optional<IntentUpdate> execute() {
List<FlowRuleBatchOperation> batches = intentManager.uninstallIntent(intent, installables);
FlowRuleOperations flowRules
= intentManager.uninstallIntent(current.intent(), current.installables());
intentManager.flowRuleService.apply(flowRules); //FIXME
return Optional.of(new Withdrawn(intentManager, intent, installables, batches));
return Optional.of(new Withdrawn(pending));
}
}
......
......@@ -15,53 +15,22 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.intent.Intent;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.net.intent.IntentData;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.intent.IntentState.WITHDRAWING;
class Withdrawn implements CompletedIntentUpdate {
// TODO: define an interface and use it, instead of IntentManager
private final IntentManager intentManager;
private final Intent intent;
private final List<Intent> installables;
private final List<FlowRuleBatchOperation> batches;
private int currentBatch;
Withdrawn(IntentManager intentManager,
Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) {
this.intentManager = checkNotNull(intentManager);
this.intent = checkNotNull(intent);
this.installables = ImmutableList.copyOf(installables);
this.batches = new LinkedList<>(batches);
this.currentBatch = 0;
}
@Override
public List<Intent> allInstallables() {
return installables;
}
@Override
public void batchSuccess() {
currentBatch++;
}
private final IntentData intentData;
@Override
public FlowRuleBatchOperation currentBatch() {
return currentBatch < batches.size() ? batches.get(currentBatch) : null;
Withdrawn(IntentData intentData) {
this.intentData = checkNotNull(intentData);
this.intentData.setState(WITHDRAWING);
}
@Override
public void batchFailed() {
for (int i = batches.size() - 1; i >= currentBatch; i--) {
batches.remove(i);
}
batches.addAll(intentManager.uninstallIntent(intent, installables));
public IntentData data() {
return intentData;
}
}
......