blob: a8d565cb1c93ce7d84191874814f1d898a7c885f [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 /**
Carmelo Cascone7b821702017-06-19 11:26:08 +0900107 * Returns the identifier of this parameter.
108 *
109 * @return parameter identifier
110 */
111 public PiActionParamId id() {
112 return id;
113 }
114
115 /**
116 * Returns the value of this parameter.
117 *
118 * @return parameter value
119 */
120 public ImmutableByteSequence value() {
121 return value;
122 }
123
124 @Override
125 public boolean equals(Object o) {
126 if (this == o) {
127 return true;
128 }
129 if (o == null || getClass() != o.getClass()) {
130 return false;
131 }
132 PiActionParam that = (PiActionParam) o;
133 return Objects.equal(id, that.id) &&
134 Objects.equal(value, that.value);
135 }
136
137 @Override
138 public int hashCode() {
139 return Objects.hashCode(id, value);
140 }
141
142 @Override
143 public String toString() {
144 return this.id.toString() + "=" + this.value().toString();
145 }
146}