karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2023-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 | package org.onosproject.netflow; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | /** |
| 25 | * Flow template fields. |
| 26 | * Ref: https://www.ietf.org/rfc/rfc3954.txt |
| 27 | */ |
| 28 | public final class FlowTemplateField { |
| 29 | |
| 30 | /* |
| 31 | 0 1 2 3 |
| 32 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 33 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | | Field Type 1 | Field Length 1 | |
| 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | | Field Type 2 | Field Length 2 | |
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | */ |
| 39 | |
| 40 | private FlowField flowField; |
| 41 | |
| 42 | private int length; |
| 43 | |
| 44 | private FlowTemplateField(Builder builder) { |
| 45 | this.flowField = builder.flowField; |
| 46 | this.length = builder.length; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a numeric value that represents the type of the field. |
| 51 | * |
| 52 | * @return flow field |
| 53 | */ |
| 54 | public FlowField getFlowField() { |
| 55 | return flowField; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns length of the corresponding Field Type, in bytes. |
| 60 | * |
| 61 | * @return flow value length |
| 62 | */ |
| 63 | public int getLength() { |
| 64 | return length; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | int hash = 7; |
| 70 | hash = 97 * hash + Objects.hashCode(this.flowField); |
| 71 | hash = 97 * hash + this.length; |
| 72 | return hash; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean equals(Object obj) { |
| 77 | if (this == obj) { |
| 78 | return true; |
| 79 | } |
| 80 | if (obj == null) { |
| 81 | return false; |
| 82 | } |
| 83 | if (getClass() != obj.getClass()) { |
| 84 | return false; |
| 85 | } |
| 86 | final FlowTemplateField other = (FlowTemplateField) obj; |
| 87 | if (this.length != other.length) { |
| 88 | return false; |
| 89 | } |
| 90 | return this.flowField == other.flowField; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return MoreObjects.toStringHelper(getClass()) |
| 96 | .add("flowField", flowField) |
| 97 | .add("length", length) |
| 98 | .toString(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | public static class Builder { |
| 103 | private FlowField flowField; |
| 104 | |
| 105 | private int length; |
| 106 | |
| 107 | /** |
| 108 | * Setter for flowfield. |
| 109 | * |
| 110 | * @param fieldId flow field. |
| 111 | * @return this class builder. |
| 112 | */ |
| 113 | public Builder flowField(int fieldId) { |
| 114 | this.flowField = FlowField.getField(fieldId) |
| 115 | .orElseThrow(() -> new RuntimeException("Unsupported flow field")); |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Setter for flow template length. |
| 121 | * |
| 122 | * @param length flow template length. |
| 123 | * @return this class builder. |
| 124 | */ |
| 125 | public Builder length(int length) { |
| 126 | this.length = length; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks arguments for flow template field. |
| 132 | */ |
| 133 | private void checkArguments() { |
| 134 | checkState(length != 0, "Flow length can be zero."); |
| 135 | checkNotNull(flowField, "Flow field can not be null."); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Builds flow template field. |
| 140 | * |
| 141 | * @return flow template field. |
| 142 | */ |
| 143 | public FlowTemplateField build() { |
| 144 | checkArguments(); |
| 145 | return new FlowTemplateField(this); |
| 146 | } |
| 147 | } |
| 148 | } |