[ONOS-7732] Automating switch workflow - workflow program counter & cli refactoring

Change-Id: I90f2085dbb253c8ea4598be31a8fd12760a1f739
This commit is contained in:
jaegonkim 2019-05-18 11:34:45 +09:00 committed by Jaegon Kim
parent a36f65c29a
commit 4064ed1f87
4 changed files with 76 additions and 42 deletions

View File

@ -25,6 +25,7 @@ import org.onlab.osgi.ServiceNotFoundException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URI; import java.net.URI;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -43,9 +44,9 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
private String initWorkletType; private String initWorkletType;
/** /**
* List of worklet description. * Sequential program(List of worklet desc) to be executed.
*/ */
private List<WorkletDescription> workletDescList; private List<WorkletDescription> program;
/** /**
* Set of workflow attributes. * Set of workflow attributes.
@ -63,7 +64,7 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
private ImmutableListWorkflow(Builder builder) { private ImmutableListWorkflow(Builder builder) {
super(builder.id); super(builder.id);
this.initWorkletType = builder.initWorkletType; this.initWorkletType = builder.initWorkletType;
workletDescList = ImmutableList.copyOf(builder.workletDescList); program = ImmutableList.copyOf(builder.workletDescList);
attributes = ImmutableSet.copyOf(builder.attributes); attributes = ImmutableSet.copyOf(builder.attributes);
} }
@ -86,7 +87,7 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
ProgramCounter pc = current.clone(); ProgramCounter pc = current.clone();
for (int i = current.workletIndex(); i < workletDescList.size(); pc = increased(pc), i++) { for (int i = current.workletIndex(); i < program.size(); pc = increased(pc), i++) {
if (cnt++ > Worklet.MAX_WORKS) { if (cnt++ > Worklet.MAX_WORKS) {
throw new WorkflowException("Maximum worklet execution exceeded"); throw new WorkflowException("Maximum worklet execution exceeded");
@ -139,22 +140,21 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
public ProgramCounter increased(ProgramCounter pc) throws WorkflowException { public ProgramCounter increased(ProgramCounter pc) throws WorkflowException {
int increaedIndex = pc.workletIndex() + 1; int increaedIndex = pc.workletIndex() + 1;
if (increaedIndex >= workletDescList.size()) { if (increaedIndex >= program.size()) {
throw new WorkflowException("Out of bound in program counter(" + pc + ")"); throw new WorkflowException("Out of bound in program counter(" + pc + ")");
} }
WorkletDescription workletDesc = workletDescList.get(increaedIndex); WorkletDescription workletDesc = program.get(increaedIndex);
return ProgramCounter.valueOf(workletDesc.tag(), increaedIndex); return ProgramCounter.valueOf(workletDesc.tag(), increaedIndex);
} }
@Override @Override
public Worklet getWorkletInstance(ProgramCounter pc) throws WorkflowException { public Worklet getWorkletInstance(ProgramCounter pc) throws WorkflowException {
return getWorkletInstance(workletDescList.get(pc.workletIndex()).tag()); return getWorkletInstance(program.get(pc.workletIndex()).tag());
} }
@Override private Worklet getWorkletInstance(String workletType) throws WorkflowException {
public Worklet getWorkletInstance(String workletType) throws WorkflowException {
if (Worklet.Common.INIT.tag().equals(workletType)) { if (Worklet.Common.INIT.tag().equals(workletType)) {
return Worklet.Common.INIT; return Worklet.Common.INIT;
@ -198,14 +198,23 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
} }
@Override @Override
public List<WorkletDescription> getWorkletDescList() { public List<ProgramCounter> getProgram() {
return ImmutableList.copyOf(workletDescList);
int size = program.size();
List pcList = new ArrayList();
for (int i = 0; i < size; i++) {
pcList.add(ProgramCounter.valueOf(program.get(i).tag(), i));
}
return pcList;
} }
@Override @Override
public WorkletDescription getWorkletDesc(ProgramCounter pc) { public WorkletDescription getWorkletDesc(ProgramCounter pc) {
Optional<WorkletDescription> workletDescription = workletDescList.stream().filter(a -> Objects.equals(a.tag(), Optional<WorkletDescription> workletDescription =
workletDescList.get(pc.workletIndex()).tag())).findAny(); program.stream()
.filter(a -> Objects.equals(a.tag(), program.get(pc.workletIndex()).tag()))
.findAny();
if (workletDescription.isPresent()) { if (workletDescription.isPresent()) {
return workletDescription.get(); return workletDescription.get();
} }
@ -220,8 +229,8 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
* @return index of class in worklet type list * @return index of class in worklet type list
*/ */
private int getClassIndex(Class aClass) { private int getClassIndex(Class aClass) {
for (int i = 0; i < workletDescList.size(); i++) { for (int i = 0; i < program.size(); i++) {
if (Objects.equals(aClass.getName(), workletDescList.get(i))) { if (Objects.equals(aClass.getName(), program.get(i))) {
return i; return i;
} }
} }
@ -265,7 +274,7 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
} }
return Objects.equals(this.id(), ((ImmutableListWorkflow) obj).id()) return Objects.equals(this.id(), ((ImmutableListWorkflow) obj).id())
&& Objects.equals(this.initWorkletType, ((ImmutableListWorkflow) obj).initWorkletType) && Objects.equals(this.initWorkletType, ((ImmutableListWorkflow) obj).initWorkletType)
&& Objects.equals(this.workletDescList, ((ImmutableListWorkflow) obj).workletDescList) && Objects.equals(this.program, ((ImmutableListWorkflow) obj).program)
&& Objects.equals(this.attributes, ((ImmutableListWorkflow) obj).attributes); && Objects.equals(this.attributes, ((ImmutableListWorkflow) obj).attributes);
} }
@ -274,7 +283,7 @@ public final class ImmutableListWorkflow extends AbstractWorkflow {
return MoreObjects.toStringHelper(getClass()) return MoreObjects.toStringHelper(getClass())
.add("id", id()) .add("id", id())
.add("initWorklet", initWorkletType) .add("initWorklet", initWorkletType)
.add("workList", workletDescList) .add("workList", program)
.add("attributes", attributes) .add("attributes", attributes)
.toString(); .toString();
} }

View File

@ -67,15 +67,6 @@ public interface Workflow {
*/ */
Worklet getWorkletInstance(ProgramCounter pc) throws WorkflowException; Worklet getWorkletInstance(ProgramCounter pc) throws WorkflowException;
/**
* Returns instance of worklet.
*
* @param workletType class name of worklet
* @return instance of worklet
* @throws WorkflowException workflow exception
*/
Worklet getWorkletInstance(String workletType) throws WorkflowException;
/** /**
* Builds workflow context. * Builds workflow context.
* *
@ -104,11 +95,11 @@ public interface Workflow {
Set<WorkflowAttribute> attributes(); Set<WorkflowAttribute> attributes();
/** /**
* Returns worklet desc list. * Build the list of ProgramCounters, and returns.
* *
* @return worklet description list * @return program counter list
*/ */
List<WorkletDescription> getWorkletDescList(); List<ProgramCounter> getProgram();
/** /**
* Returns worklet description. * Returns worklet description.

View File

@ -53,14 +53,22 @@ public class WorkplaceStoreCommand extends AbstractShellCommand {
@Completion(WorkplaceNameCompleter.class) @Completion(WorkplaceNameCompleter.class)
private String name = null; private String name = null;
@Option(name = "-f", aliases = "--filter", description = "including filter", @Option(name = "-wf", aliases = "--whitefilter", description = "whitelisting filter",
required = false, multiValued = false) required = false, multiValued = false)
private String inFilter = null; private String inFilter = null;
@Option(name = "-e", aliases = "--excludefilter", description = "excluding filter", @Option(name = "-bf", aliases = "--blackfilter", description = "blacklisting filter",
required = false, multiValued = false) required = false, multiValued = false)
private String exFilter = null; private String exFilter = null;
@Option(name = "-nc", aliases = "--notcompleted", description = "not completed workflow context",
required = false, multiValued = false)
private boolean notCompleted = false;
@Option(name = "-s", aliases = "--simpleprint", description = "simplified print format",
required = false, multiValued = false)
private boolean simplePrint = false;
@Override @Override
protected void doExecute() { protected void doExecute() {
@ -180,35 +188,61 @@ public class WorkplaceStoreCommand extends AbstractShellCommand {
*/ */
private void printWorkplaceContexts(WorkplaceStore workplaceStore, String workplaceName) { private void printWorkplaceContexts(WorkplaceStore workplaceStore, String workplaceName) {
for (WorkflowContext context : workplaceStore.getWorkplaceContexts(workplaceName)) { for (WorkflowContext context : workplaceStore.getWorkplaceContexts(workplaceName)) {
if (notCompleted && context.current().isCompleted()) {
continue;
}
String str = context.toString(); String str = context.toString();
if (Objects.nonNull(inFilter)) { if (Objects.nonNull(inFilter)) {
if (str.indexOf(inFilter) != -1) { if (str.indexOf(inFilter) != -1) {
if (Objects.nonNull(exFilter)) { if (Objects.nonNull(exFilter)) {
if (str.indexOf(exFilter) == -1) { if (str.indexOf(exFilter) == -1) {
print(" - " + context); printContext(context);
} }
} else { } else {
print(" - " + context); printContext(context);
} }
} }
} else { } else {
if (Objects.nonNull(exFilter)) { if (Objects.nonNull(exFilter)) {
if (str.indexOf(exFilter) == -1) { if (str.indexOf(exFilter) == -1) {
print(" - " + context); printContext(context);
} }
} else { } else {
print(" - " + context); printContext(context);
} }
} }
} }
} }
/**
* Prints a workflow context.
* @param context a workflow context
*/
private void printContext(WorkflowContext context) {
if (simplePrint) {
String str = " - "
+ "name=" + context.name()
+ ", state=" + context.state()
+ ", current=" + context.current().workletType();
print(str);
} else {
print(" - " + context);
}
}
/** /**
* Gets workplace string. * Gets workplace string.
* @param workplace workplace * @param workplace workplace
* @return workplace string * @return workplace string
*/ */
private String getWorkplaceString(Workplace workplace) { private String getWorkplaceString(Workplace workplace) {
return workplace.toString(); if (simplePrint) {
return workplace.name();
} else {
return workplace.toString();
}
} }
} }

View File

@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.node.MissingNode;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import org.onosproject.net.config.NetworkConfigRegistry; import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.config.NetworkConfigService; import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.workflow.api.ProgramCounter;
import org.onosproject.workflow.api.WorkflowDefinitionException; import org.onosproject.workflow.api.WorkflowDefinitionException;
import org.onosproject.workflow.api.WorkflowService; import org.onosproject.workflow.api.WorkflowService;
import org.onosproject.workflow.api.WorkflowExecutionService; import org.onosproject.workflow.api.WorkflowExecutionService;
@ -40,7 +41,6 @@ import org.onosproject.workflow.api.Workflow;
import org.onosproject.workflow.api.Worklet; import org.onosproject.workflow.api.Worklet;
import org.onosproject.workflow.api.WorkflowContext; import org.onosproject.workflow.api.WorkflowContext;
import org.onosproject.workflow.api.JsonDataModel; import org.onosproject.workflow.api.JsonDataModel;
import org.onosproject.workflow.api.WorkletDescription;
import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Deactivate;
@ -175,9 +175,9 @@ public class WorkflowManager implements WorkflowService {
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>();
for (WorkletDescription workletType : workflow.getWorkletDescList()) { for (ProgramCounter pc : workflow.getProgram()) {
Worklet worklet = workflow.getWorkletInstance(workletType.tag()); Worklet worklet = workflow.getWorkletInstance(pc);
if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) { if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
continue; continue;
} }
@ -204,7 +204,7 @@ public class WorkflowManager implements WorkflowService {
String path = matcher.group(1); String path = matcher.group(1);
WorkletDataModelFieldDesc desc = WorkletDataModelFieldDesc desc =
new WorkletDataModelFieldDesc(workletType.tag(), path, field.getType(), new WorkletDataModelFieldDesc(pc.workletType(), path, field.getType(),
jsonDataModel.optional()); jsonDataModel.optional());
WorkletDataModelFieldDesc existing = descMap.get(path); WorkletDataModelFieldDesc existing = descMap.get(path);
@ -298,9 +298,9 @@ public class WorkflowManager implements WorkflowService {
throw new WorkflowDataModelException(workflow.id(), worklowDescJson, errors); throw new WorkflowDataModelException(workflow.id(), worklowDescJson, errors);
} }
for (WorkletDescription workletType : workflow.getWorkletDescList()) { for (ProgramCounter pc : workflow.getProgram()) {
Worklet worklet = workflow.getWorkletInstance(workletType.tag()); Worklet worklet = workflow.getWorkletInstance(pc);
if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) { if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
continue; continue;
} }