Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.Sets; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * Instance of an action set of a protocol-independent pipeline used |
| 28 | * when doing one-shot action selector programming. Contains a set of weighted |
| 29 | * actions, and it is equivalent to the action profile action set from P4Runtime |
| 30 | * specifications. |
| 31 | */ |
| 32 | @Beta |
| 33 | public final class PiActionSet implements PiTableAction { |
| 34 | |
| 35 | private final Set<WeightedAction> actionSet; |
| 36 | |
| 37 | private PiActionSet(Set<WeightedAction> actionSet) { |
| 38 | this.actionSet = actionSet; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the set of actions. |
| 43 | * |
| 44 | * @return the set of actions |
| 45 | */ |
| 46 | public Set<WeightedAction> actions() { |
| 47 | return actionSet; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public Type type() { |
| 52 | return Type.ACTION_SET; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean equals(Object o) { |
| 57 | if (this == o) { |
| 58 | return true; |
| 59 | } |
| 60 | if (o == null || getClass() != o.getClass()) { |
| 61 | return false; |
| 62 | } |
| 63 | PiActionSet that = (PiActionSet) o; |
| 64 | return Objects.equal(actionSet, that.actionSet); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Objects.hashCode(actionSet); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return MoreObjects.toStringHelper(this) |
| 75 | .add("actionSet", actionSet) |
| 76 | .toString(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a new builder of an action set. |
| 81 | * |
| 82 | * @return action set builder |
| 83 | */ |
| 84 | public static Builder builder() { |
| 85 | return new Builder(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Builder of an action set. |
| 90 | */ |
| 91 | public static final class Builder { |
| 92 | |
| 93 | private final Set<WeightedAction> actionSet = Sets.newHashSet(); |
| 94 | |
| 95 | private Builder() { |
| 96 | // hides constructor. |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Adds a weighted action to this action set. |
| 101 | * |
| 102 | * @param action The action to add |
| 103 | * @param weight The weight associated to the action |
| 104 | * @return this |
| 105 | */ |
| 106 | public Builder addWeightedAction( |
| 107 | PiAction action, int weight) { |
| 108 | actionSet.add(new WeightedAction(action, weight)); |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new action profile action set. |
| 114 | * |
| 115 | * @return action profile action set |
| 116 | */ |
| 117 | public PiActionSet build() { |
| 118 | return new PiActionSet(Set.copyOf(actionSet)); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Weighted action used in an actions set. |
| 124 | */ |
| 125 | public static final class WeightedAction { |
| 126 | public static final int DEFAULT_WEIGHT = 1; |
| 127 | |
| 128 | private final int weight; |
| 129 | private final PiAction action; |
| 130 | |
| 131 | /** |
| 132 | * Creates a new weighted action instance that can be used in an action |
| 133 | * set, from the given PI action and weight. |
| 134 | * |
| 135 | * @param action the action |
| 136 | * @param weight the weigh |
| 137 | */ |
| 138 | public WeightedAction(PiAction action, int weight) { |
| 139 | this.weight = weight; |
| 140 | this.action = action; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean equals(Object o) { |
| 145 | if (this == o) { |
| 146 | return true; |
| 147 | } |
| 148 | if (o == null || getClass() != o.getClass()) { |
| 149 | return false; |
| 150 | } |
| 151 | WeightedAction that = (WeightedAction) o; |
| 152 | return weight == that.weight && |
| 153 | Objects.equal(action, that.action); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int hashCode() { |
| 158 | return Objects.hashCode(weight, action); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public String toString() { |
| 163 | return MoreObjects.toStringHelper(this) |
| 164 | .add("weight", weight) |
| 165 | .add("action", action) |
| 166 | .toString(); |
| 167 | } |
| 168 | |
| 169 | public int weight() { |
| 170 | return weight; |
| 171 | } |
| 172 | |
| 173 | public PiAction action() { |
| 174 | return action; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |