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