Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiActionParamId; |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 4efa498 | 2018-04-18 18:58:56 +0900 | [diff] [blame] | 26 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
Carmelo Cascone | 4efa498 | 2018-04-18 18:58:56 +0900 | [diff] [blame] | 29 | * Instance of an action runtime parameter in a match+action table of a |
| 30 | * protocol-independent pipeline. |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 31 | */ |
| 32 | @Beta |
| 33 | public final class PiActionParam { |
| 34 | |
| 35 | private final PiActionParamId id; |
| 36 | private final ImmutableByteSequence value; |
| 37 | |
| 38 | /** |
Carmelo Cascone | 4efa498 | 2018-04-18 18:58:56 +0900 | [diff] [blame] | 39 | * Creates an action's runtime parameter for the given param ID and byte |
| 40 | * sequence value. |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 41 | * |
| 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 Cascone | 4efa498 | 2018-04-18 18:58:56 +0900 | [diff] [blame] | 52 | * 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 Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 107 | * 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 | } |