Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 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; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 20 | import com.google.common.base.Objects; |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.Maps; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiActionId; |
| 24 | import org.onosproject.net.pi.model.PiActionParamId; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 25 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 26 | import java.util.Collection; |
| 27 | import java.util.Map; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 28 | import java.util.StringJoiner; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 33 | * Instance of an action, and its runtime parameters, of a table entry in a |
| 34 | * protocol-independent pipeline. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 35 | */ |
| 36 | @Beta |
| 37 | public final class PiAction implements PiTableAction { |
| 38 | |
| 39 | private final PiActionId actionId; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 40 | private final ImmutableMap<PiActionParamId, PiActionParam> runtimeParams; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 41 | |
| 42 | /** |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 43 | * Creates a new action instance for the given action identifier and runtime |
| 44 | * parameters. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 45 | * |
| 46 | * @param actionId action identifier |
| 47 | * @param runtimeParams list of runtime parameters |
| 48 | */ |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 49 | private PiAction(PiActionId actionId, |
| 50 | Map<PiActionParamId, PiActionParam> runtimeParams) { |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 51 | this.actionId = actionId; |
| 52 | this.runtimeParams = ImmutableMap.copyOf(runtimeParams); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Type type() { |
| 57 | return Type.ACTION; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Return the identifier of this action. |
| 62 | * |
| 63 | * @return action identifier |
| 64 | */ |
| 65 | public PiActionId id() { |
| 66 | return actionId; |
| 67 | } |
| 68 | |
| 69 | /** |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 70 | * Returns all runtime parameters of this action. Return an empty collection |
| 71 | * if the action doesn't take any runtime parameters. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 72 | * |
| 73 | * @return list of byte sequences |
| 74 | */ |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 75 | public Collection<PiActionParam> parameters() { |
| 76 | return runtimeParams.values(); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object o) { |
| 81 | if (this == o) { |
| 82 | return true; |
| 83 | } |
| 84 | if (o == null || getClass() != o.getClass()) { |
| 85 | return false; |
| 86 | } |
| 87 | PiAction piAction = (PiAction) o; |
| 88 | return Objects.equal(actionId, piAction.actionId) && |
| 89 | Objects.equal(runtimeParams, piAction.runtimeParams); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
| 94 | return Objects.hashCode(actionId, runtimeParams); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | StringJoiner stringParams = new StringJoiner(", ", "(", ")"); |
| 100 | this.parameters().forEach(p -> stringParams.add(p.toString())); |
Carmelo Cascone | 0316b96 | 2018-04-11 17:39:07 -0700 | [diff] [blame] | 101 | return this.id().toString() + stringParams.toString(); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 102 | } |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Returns an action builder. |
| 106 | * |
| 107 | * @return a new builder |
| 108 | */ |
| 109 | public static Builder builder() { |
| 110 | return new Builder(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Builder of protocol-independent actions. |
| 115 | */ |
| 116 | public static final class Builder { |
| 117 | |
| 118 | private PiActionId actionId; |
| 119 | private Map<PiActionParamId, PiActionParam> runtimeParams = Maps.newHashMap(); |
| 120 | |
| 121 | private Builder() { |
| 122 | // hides constructor. |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Sets the identifier of this action. |
| 127 | * |
| 128 | * @param actionId action identifier |
| 129 | * @return this |
| 130 | */ |
| 131 | public Builder withId(PiActionId actionId) { |
| 132 | this.actionId = actionId; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds a runtime parameter. |
| 138 | * |
| 139 | * @param param action parameter |
| 140 | * @return this |
| 141 | */ |
| 142 | public Builder withParameter(PiActionParam param) { |
| 143 | checkNotNull(param); |
| 144 | runtimeParams.put(param.id(), param); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adds many runtime parameters. |
| 150 | * |
| 151 | * @param params collection of action parameters |
| 152 | * @return this |
| 153 | */ |
| 154 | public Builder withParameters(Collection<PiActionParam> params) { |
| 155 | checkNotNull(params); |
| 156 | params.forEach(this::withParameter); |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns a new action instance. |
| 162 | * |
| 163 | * @return action |
| 164 | */ |
| 165 | public PiAction build() { |
| 166 | checkNotNull(actionId); |
| 167 | return new PiAction(actionId, runtimeParams); |
| 168 | } |
| 169 | } |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 170 | } |