blob: 63006609e0ff457166dfd7f17eaed69a047a5213 [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;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
Carmelo Cascone7b821702017-06-19 11:26:08 +090022import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Maps;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040024
Carmelo Cascone7b821702017-06-19 11:26:08 +090025import java.util.Collection;
26import java.util.Map;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040027import java.util.StringJoiner;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Instance of an action, and its runtime parameters, of a table entry in a protocol-independent
33 * pipeline.
34 */
35@Beta
36public final class PiAction implements PiTableAction {
37
38 private final PiActionId actionId;
Carmelo Cascone7b821702017-06-19 11:26:08 +090039 private final Map<PiActionParamId, PiActionParam> runtimeParams;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040040
41 /**
42 * Creates a new action instance for the given action identifier and runtime parameters.
43 *
44 * @param actionId action identifier
45 * @param runtimeParams list of runtime parameters
46 */
Carmelo Cascone7b821702017-06-19 11:26:08 +090047 private PiAction(PiActionId actionId, Map<PiActionParamId, PiActionParam> runtimeParams) {
48 this.actionId = actionId;
49 this.runtimeParams = ImmutableMap.copyOf(runtimeParams);
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040050 }
51
52 @Override
53 public Type type() {
54 return Type.ACTION;
55 }
56
57 /**
58 * Return the identifier of this action.
59 *
60 * @return action identifier
61 */
62 public PiActionId id() {
63 return actionId;
64 }
65
66 /**
Carmelo Cascone7b821702017-06-19 11:26:08 +090067 * Returns all runtime parameters of this action.
68 * Return an empty collection if the action doesn't take any runtime parameters.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040069 *
70 * @return list of byte sequences
71 */
Carmelo Cascone7b821702017-06-19 11:26:08 +090072 public Collection<PiActionParam> parameters() {
73 return runtimeParams.values();
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040074 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
81 if (o == null || getClass() != o.getClass()) {
82 return false;
83 }
84 PiAction piAction = (PiAction) o;
85 return Objects.equal(actionId, piAction.actionId) &&
86 Objects.equal(runtimeParams, piAction.runtimeParams);
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hashCode(actionId, runtimeParams);
92 }
93
94 @Override
95 public String toString() {
96 StringJoiner stringParams = new StringJoiner(", ", "(", ")");
97 this.parameters().forEach(p -> stringParams.add(p.toString()));
98 return MoreObjects.toStringHelper(this)
99 .addValue(this.id().toString() + stringParams.toString())
100 .toString();
101 }
Carmelo Cascone7b821702017-06-19 11:26:08 +0900102
103 /**
104 * Returns an action builder.
105 *
106 * @return a new builder
107 */
108 public static Builder builder() {
109 return new Builder();
110 }
111
112 /**
113 * Builder of protocol-independent actions.
114 */
115 public static final class Builder {
116
117 private PiActionId actionId;
118 private Map<PiActionParamId, PiActionParam> runtimeParams = Maps.newHashMap();
119
120 private Builder() {
121 // hides constructor.
122 }
123
124 /**
125 * Sets the identifier of this action.
126 *
127 * @param actionId action identifier
128 * @return this
129 */
130 public Builder withId(PiActionId actionId) {
131 this.actionId = actionId;
132 return this;
133 }
134
135 /**
136 * Adds a runtime parameter.
137 *
138 * @param param action parameter
139 * @return this
140 */
141 public Builder withParameter(PiActionParam param) {
142 checkNotNull(param);
143 runtimeParams.put(param.id(), param);
144 return this;
145 }
146
147 /**
148 * Adds many runtime parameters.
149 *
150 * @param params collection of action parameters
151 * @return this
152 */
153 public Builder withParameters(Collection<PiActionParam> params) {
154 checkNotNull(params);
155 params.forEach(this::withParameter);
156 return this;
157 }
158
159 /**
160 * Returns a new action instance.
161 *
162 * @return action
163 */
164 public PiAction build() {
165 checkNotNull(actionId);
166 return new PiAction(actionId, runtimeParams);
167 }
168 }
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400169}