blob: 78a62fdfa7c5ffaabec04e1339031a15d5f7a7b1 [file] [log] [blame]
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04003 *
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040020import com.google.common.base.Objects;
Carmelo Cascone7b821702017-06-19 11:26:08 +090021import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Maps;
Carmelo Cascone87892e22017-11-13 16:01:29 -080023import org.onosproject.net.pi.model.PiActionId;
24import org.onosproject.net.pi.model.PiActionParamId;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040025
Carmelo Cascone7b821702017-06-19 11:26:08 +090026import java.util.Collection;
27import java.util.Map;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040028import java.util.StringJoiner;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
Carmelo Cascone99c59db2019-01-17 15:39:35 -080033 * Instance of an action, and its runtime parameters, of a table entry in a
34 * protocol-independent pipeline.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040035 */
36@Beta
37public final class PiAction implements PiTableAction {
38
39 private final PiActionId actionId;
Carmelo Cascone99c59db2019-01-17 15:39:35 -080040 private final ImmutableMap<PiActionParamId, PiActionParam> runtimeParams;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040041
42 /**
Carmelo Cascone99c59db2019-01-17 15:39:35 -080043 * Creates a new action instance for the given action identifier and runtime
44 * parameters.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040045 *
46 * @param actionId action identifier
47 * @param runtimeParams list of runtime parameters
48 */
Carmelo Cascone99c59db2019-01-17 15:39:35 -080049 private PiAction(PiActionId actionId,
50 Map<PiActionParamId, PiActionParam> runtimeParams) {
Carmelo Cascone7b821702017-06-19 11:26:08 +090051 this.actionId = actionId;
52 this.runtimeParams = ImmutableMap.copyOf(runtimeParams);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040053 }
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 Cascone99c59db2019-01-17 15:39:35 -080070 * Returns all runtime parameters of this action. Return an empty collection
71 * if the action doesn't take any runtime parameters.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040072 *
73 * @return list of byte sequences
74 */
Carmelo Cascone7b821702017-06-19 11:26:08 +090075 public Collection<PiActionParam> parameters() {
76 return runtimeParams.values();
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040077 }
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 Cascone0316b962018-04-11 17:39:07 -0700101 return this.id().toString() + stringParams.toString();
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400102 }
Carmelo Cascone7b821702017-06-19 11:26:08 +0900103
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 Cascone1022a4e2017-05-25 00:16:18 -0400170}