blob: 24655b69371e5404d90540d461b5c26a7c099284 [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 Cascone87892e22017-11-13 16:01:29 -080033 * Instance of an action, and its runtime parameters, of a table entry in a protocol-independent pipeline.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040034 */
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 Cascone87892e22017-11-13 16:01:29 -080067 * Returns all runtime parameters of this action. Return an empty collection if the action doesn't take any runtime
68 * 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()));
Carmelo Cascone0316b962018-04-11 17:39:07 -070098 return this.id().toString() + stringParams.toString();
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040099 }
Carmelo Cascone7b821702017-06-19 11:26:08 +0900100
101 /**
102 * Returns an action builder.
103 *
104 * @return a new builder
105 */
106 public static Builder builder() {
107 return new Builder();
108 }
109
110 /**
111 * Builder of protocol-independent actions.
112 */
113 public static final class Builder {
114
115 private PiActionId actionId;
116 private Map<PiActionParamId, PiActionParam> runtimeParams = Maps.newHashMap();
117
118 private Builder() {
119 // hides constructor.
120 }
121
122 /**
123 * Sets the identifier of this action.
124 *
125 * @param actionId action identifier
126 * @return this
127 */
128 public Builder withId(PiActionId actionId) {
129 this.actionId = actionId;
130 return this;
131 }
132
133 /**
134 * Adds a runtime parameter.
135 *
136 * @param param action parameter
137 * @return this
138 */
139 public Builder withParameter(PiActionParam param) {
140 checkNotNull(param);
141 runtimeParams.put(param.id(), param);
142 return this;
143 }
144
145 /**
146 * Adds many runtime parameters.
147 *
148 * @param params collection of action parameters
149 * @return this
150 */
151 public Builder withParameters(Collection<PiActionParam> params) {
152 checkNotNull(params);
153 params.forEach(this::withParameter);
154 return this;
155 }
156
157 /**
158 * Returns a new action instance.
159 *
160 * @return action
161 */
162 public PiAction build() {
163 checkNotNull(actionId);
164 return new PiAction(actionId, runtimeParams);
165 }
166 }
Carmelo Cascone1022a4e2017-05-25 00:16:18 -0400167}