FrankWang | ea70de3 | 2018-05-08 15:41:25 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.data; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
| 24 | import org.onosproject.net.pi.model.PiData; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.StringJoiner; |
| 28 | |
| 29 | /** |
| 30 | * Instance of a PiHeader in a protocol-independent pipeline. |
| 31 | */ |
| 32 | @Beta |
| 33 | public final class PiHeader implements PiData { |
| 34 | private final Boolean isValid; |
| 35 | private final ImmutableList<ImmutableByteSequence> bitStrings; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new protocol-independent header instance. |
| 39 | * |
| 40 | * @param isValid whether the header is valid |
| 41 | * @param bitStrings bitstrings |
| 42 | */ |
| 43 | private PiHeader(Boolean isValid, List<ImmutableByteSequence> bitStrings) { |
| 44 | this.isValid = isValid; |
| 45 | this.bitStrings = ImmutableList.copyOf(bitStrings); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns a new protocol-independent header. |
| 50 | * @param isValid whether the header is valid |
| 51 | * @param bitStrings bitstrings |
| 52 | * @return header |
| 53 | */ |
| 54 | public static PiHeader of(Boolean isValid, List<ImmutableByteSequence> bitStrings) { |
| 55 | return new PiHeader(isValid, bitStrings); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return whether the header is valid. |
| 60 | * |
| 61 | * @return bool |
| 62 | */ |
| 63 | public Boolean isValid() { |
| 64 | return this.isValid; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return the header bit strings. |
| 69 | * |
| 70 | * @return the list of bit string |
| 71 | */ |
| 72 | public List<ImmutableByteSequence> bitStrings() { |
| 73 | return this.bitStrings; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Type type() { |
| 78 | return Type.HEADER; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean equals(Object o) { |
| 83 | if (this == o) { |
| 84 | return true; |
| 85 | } |
| 86 | if (o == null || getClass() != o.getClass()) { |
| 87 | return false; |
| 88 | } |
| 89 | PiHeader header = (PiHeader) o; |
| 90 | return Objects.equal(isValid, header.isValid) && |
| 91 | Objects.equal(bitStrings, header.bitStrings); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return Objects.hashCode(isValid, bitStrings); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | StringJoiner stringParams = new StringJoiner(", ", "(", ")"); |
| 102 | this.bitStrings().forEach(p -> stringParams.add(p.toString())); |
| 103 | return MoreObjects.toStringHelper(getClass()) |
| 104 | .add("bitString", stringParams) |
| 105 | .add("isValid", isValid) |
| 106 | .toString(); |
| 107 | } |
| 108 | } |