Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Instance of a metadata for a packet I/O operation, with id and value for a protocol-independent pipeline. |
| 27 | */ |
| 28 | @Beta |
| 29 | public final class PiPacketMetadata { |
| 30 | |
| 31 | private final PiPacketMetadataId id; |
| 32 | private final ImmutableByteSequence value; |
| 33 | |
| 34 | /** |
| 35 | * Creates a new packet metadata instance for the given identifier and value. |
| 36 | * |
| 37 | * @param id packet metadata identifier |
| 38 | * @param value value for this metadata |
| 39 | */ |
| 40 | private PiPacketMetadata(PiPacketMetadataId id, ImmutableByteSequence value) { |
| 41 | this.id = id; |
| 42 | this.value = value; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Return the identifier of this packet metadata. |
| 47 | * |
| 48 | * @return packet metadata identifier |
| 49 | */ |
| 50 | public PiPacketMetadataId id() { |
| 51 | return id; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the value for the field in this metadata. |
| 56 | * |
| 57 | * @return value |
| 58 | */ |
| 59 | public ImmutableByteSequence value() { |
| 60 | return value; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean equals(Object o) { |
| 65 | if (this == o) { |
| 66 | return true; |
| 67 | } |
| 68 | if (o == null || getClass() != o.getClass()) { |
| 69 | return false; |
| 70 | } |
| 71 | PiPacketMetadata piPacket = (PiPacketMetadata) o; |
| 72 | return Objects.equal(id, piPacket.id()) && |
| 73 | Objects.equal(value, piPacket.value()); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public int hashCode() { |
| 78 | return Objects.hashCode(id, value); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String toString() { |
| 83 | return this.id().toString() + " = " + value.toString(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns a packet metadata builder. |
| 88 | * |
| 89 | * @return a new builder |
| 90 | */ |
| 91 | public static Builder builder() { |
| 92 | return new Builder(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Builder of protocol-independent packet metadatas. |
| 97 | */ |
| 98 | public static final class Builder { |
| 99 | |
| 100 | private PiPacketMetadataId id; |
| 101 | private ImmutableByteSequence value; |
| 102 | |
| 103 | private Builder() { |
| 104 | // hides constructor. |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sets the identifier of this packet metadata. |
| 109 | * |
| 110 | * @param id packet metadata identifier |
| 111 | * @return this |
| 112 | */ |
| 113 | public Builder withId(PiPacketMetadataId id) { |
| 114 | this.id = id; |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sets the value of this metadata. |
| 120 | * |
| 121 | * @param value value of the metadata |
| 122 | * @return this |
| 123 | */ |
| 124 | public Builder withValue(ImmutableByteSequence value) { |
| 125 | this.value = value; |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns a new packet metadata instance. |
| 131 | * |
| 132 | * @return packet metadata |
| 133 | */ |
| 134 | public PiPacketMetadata build() { |
| 135 | checkNotNull(id); |
| 136 | checkNotNull(value); |
| 137 | return new PiPacketMetadata(id, value); |
| 138 | } |
| 139 | } |
| 140 | } |