Add support for one shot action profile programming in PI
A P4 table annotated with @oneshot annotation can be programmed
only with the action profile action set. For these kind of tables
we don't issue read request for action profile groups and members.
Change-Id: I7b6a743f4f4df4190f17d958ebb4807aca5feda5
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionSet.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionSet.java
new file mode 100644
index 0000000..f2908d4
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionSet.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.pi.runtime;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.collect.Sets;
+
+import java.util.Set;
+
+/**
+ * Instance of an action set of a protocol-independent pipeline used
+ * when doing one-shot action selector programming. Contains a set of weighted
+ * actions, and it is equivalent to the action profile action set from P4Runtime
+ * specifications.
+ */
+@Beta
+public final class PiActionSet implements PiTableAction {
+
+ private final Set<WeightedAction> actionSet;
+
+ private PiActionSet(Set<WeightedAction> actionSet) {
+ this.actionSet = actionSet;
+ }
+
+ /**
+ * Returns the set of actions.
+ *
+ * @return the set of actions
+ */
+ public Set<WeightedAction> actions() {
+ return actionSet;
+ }
+
+ @Override
+ public Type type() {
+ return Type.ACTION_SET;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ PiActionSet that = (PiActionSet) o;
+ return Objects.equal(actionSet, that.actionSet);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(actionSet);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("actionSet", actionSet)
+ .toString();
+ }
+
+ /**
+ * Returns a new builder of an action set.
+ *
+ * @return action set builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder of an action set.
+ */
+ public static final class Builder {
+
+ private final Set<WeightedAction> actionSet = Sets.newHashSet();
+
+ private Builder() {
+ // hides constructor.
+ }
+
+ /**
+ * Adds a weighted action to this action set.
+ *
+ * @param action The action to add
+ * @param weight The weight associated to the action
+ * @return this
+ */
+ public Builder addWeightedAction(
+ PiAction action, int weight) {
+ actionSet.add(new WeightedAction(action, weight));
+ return this;
+ }
+
+ /**
+ * Creates a new action profile action set.
+ *
+ * @return action profile action set
+ */
+ public PiActionSet build() {
+ return new PiActionSet(Set.copyOf(actionSet));
+ }
+ }
+
+ /**
+ * Weighted action used in an actions set.
+ */
+ public static final class WeightedAction {
+ public static final int DEFAULT_WEIGHT = 1;
+
+ private final int weight;
+ private final PiAction action;
+
+ /**
+ * Creates a new weighted action instance that can be used in an action
+ * set, from the given PI action and weight.
+ *
+ * @param action the action
+ * @param weight the weigh
+ */
+ public WeightedAction(PiAction action, int weight) {
+ this.weight = weight;
+ this.action = action;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ WeightedAction that = (WeightedAction) o;
+ return weight == that.weight &&
+ Objects.equal(action, that.action);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(weight, action);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("weight", weight)
+ .add("action", action)
+ .toString();
+ }
+
+ public int weight() {
+ return weight;
+ }
+
+ public PiAction action() {
+ return action;
+ }
+ }
+}
+
+
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableAction.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableAction.java
index 284dde6..d803ca6 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableAction.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableAction.java
@@ -43,7 +43,13 @@
* Executes the action profile member specified by the given
* identifier.
*/
- ACTION_PROFILE_MEMBER_ID
+ ACTION_PROFILE_MEMBER_ID,
+
+ /**
+ * Executes the given action set. Used in one-shot action profile
+ * programming.
+ */
+ ACTION_SET
}
/**
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
index ee3c3c3..40ad4de 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
@@ -185,6 +185,8 @@
return "ACT_PROF_GROUP:" + ((PiActionProfileGroupId) tableAction).id();
case ACTION_PROFILE_MEMBER_ID:
return "ACT_PROF_MEMBER:" + ((PiActionProfileMemberId) tableAction).id();
+ case ACTION_SET:
+ return "ACTION_SET:" + tableAction.toString();
case ACTION:
default:
return tableAction.toString();