blob: 91f6e04394c4a730b1897ddb30ff6194b1de8e01 [file] [log] [blame]
Carmelo Cascone7b821702017-06-19 11:26:08 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone7b821702017-06-19 11:26:08 +09003 *
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.Objects;
21import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiActionParamId;
Carmelo Cascone7b821702017-06-19 11:26:08 +090023
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone4efa4982018-04-18 18:58:56 +090026import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone7b821702017-06-19 11:26:08 +090027
28/**
Carmelo Cascone4efa4982018-04-18 18:58:56 +090029 * Instance of an action runtime parameter in a match+action table of a
30 * protocol-independent pipeline.
Carmelo Cascone7b821702017-06-19 11:26:08 +090031 */
32@Beta
33public final class PiActionParam {
34
35 private final PiActionParamId id;
36 private final ImmutableByteSequence value;
37
38 /**
Carmelo Cascone4efa4982018-04-18 18:58:56 +090039 * Creates an action's runtime parameter for the given param ID and byte
40 * sequence value.
Carmelo Cascone7b821702017-06-19 11:26:08 +090041 *
42 * @param id parameter identifier
43 * @param value value
44 */
45 public PiActionParam(PiActionParamId id, ImmutableByteSequence value) {
46 this.id = checkNotNull(id);
47 this.value = checkNotNull(value);
48 checkArgument(value.size() > 0, "Value can't have size 0");
49 }
50
51 /**
Carmelo Cascone4efa4982018-04-18 18:58:56 +090052 * Creates an action's runtime parameter for the given param ID and byte
53 * value.
54 *
55 * @param id parameter identifier
56 * @param value value
57 */
58 public PiActionParam(PiActionParamId id, byte value) {
59 this(id, copyFrom(value));
60 }
61
62 /**
63 * Creates an action's runtime parameter for the given param ID and short
64 * value.
65 *
66 * @param id parameter identifier
67 * @param value value
68 */
69 public PiActionParam(PiActionParamId id, short value) {
70 this(id, copyFrom(value));
71 }
72
73 /**
74 * Creates an action's runtime parameter for the given param ID and int
75 * value.
76 *
77 * @param id parameter identifier
78 * @param value value
79 */
80 public PiActionParam(PiActionParamId id, int value) {
81 this(id, copyFrom(value));
82 }
83
84 /**
85 * Creates an action's runtime parameter for the given param ID and long
86 * value.
87 *
88 * @param id parameter identifier
89 * @param value value
90 */
91 public PiActionParam(PiActionParamId id, long value) {
92 this(id, copyFrom(value));
93 }
94
95 /**
96 * Creates an action's runtime parameter for the given param ID and byte
97 * array value.
98 *
99 * @param id parameter identifier
100 * @param value value
101 */
102 public PiActionParam(PiActionParamId id, byte[] value) {
103 this(id, copyFrom(value));
104 }
105
106 /**
Daniele Moro7aa13e62021-02-23 15:28:07 +0100107 * Creates an action's runtime parameter for the given param ID and string
108 * value.
109 *
110 * @param id parameter identifier
111 * @param value value
112 */
113 public PiActionParam(PiActionParamId id, String value) {
114 this(id, copyFrom(value));
115 }
116
117 /**
Carmelo Cascone7b821702017-06-19 11:26:08 +0900118 * Returns the identifier of this parameter.
119 *
120 * @return parameter identifier
121 */
122 public PiActionParamId id() {
123 return id;
124 }
125
126 /**
127 * Returns the value of this parameter.
128 *
129 * @return parameter value
130 */
131 public ImmutableByteSequence value() {
132 return value;
133 }
134
135 @Override
136 public boolean equals(Object o) {
137 if (this == o) {
138 return true;
139 }
140 if (o == null || getClass() != o.getClass()) {
141 return false;
142 }
143 PiActionParam that = (PiActionParam) o;
144 return Objects.equal(id, that.id) &&
145 Objects.equal(value, that.value);
146 }
147
148 @Override
149 public int hashCode() {
150 return Objects.hashCode(id, value);
151 }
152
153 @Override
154 public String toString() {
155 return this.id.toString() + "=" + this.value().toString();
156 }
157}