blob: 2c09e71ffbd89db39bd574c503389c77d2dad95e [file] [log] [blame]
karthik1977bc5ea1e2023-01-02 19:25:14 +05301/*
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 */
16package org.onosproject.netflow;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
22import com.google.common.base.MoreObjects;
23
24/**
25 * Flow is a collection of Flow Data Record(s).
26 * each containing a set of field values. The Type and
27 * Length of the fields have been previously defined in the
28 * Template Record referenced by the FlowSet ID or Template ID.
29 * Ref: https://www.ietf.org/rfc/rfc3954.txt
30 */
31public final class Flow {
32
33 /*
34 0 1 2 3
35 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
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Record 1 - Field Value 1 | Record 1 - Field Value 2 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 */
40
41 private FlowField field;
42
43 private Object value;
44
45 private Flow(Builder builder) {
46 this.field = builder.field;
47 this.value = builder.value;
48 }
49
50 /**
51 * The getter for flow fields.
52 *
53 * @return flow field
54 */
55 public FlowField getField() {
56 return field;
57 }
58
59 /**
60 * Returns flow value.
61 *
62 * @return flow value
63 */
64 public Object getValue() {
65 return value;
66 }
67
68 /*
69 * (non-Javadoc)
70 *
71 * @see java.lang.Object#equals(java.lang.Object)
72 */
73 @Override
74 public int hashCode() {
75 int hash = 5;
76 hash = 23 * hash + Objects.hashCode(this.field);
77 hash = 23 * hash + Objects.hashCode(this.value);
78 return hash;
79 }
80
81 /*
82 * (non-Javadoc)
83 *
84 * @see java.lang.Object#equals(java.lang.Object)
85 */
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) {
89 return true;
90 }
91 if (obj == null) {
92 return false;
93 }
94 if (getClass() != obj.getClass()) {
95 return false;
96 }
97 final Flow other = (Flow) obj;
98 if (this.field != other.field) {
99 return false;
100 }
101 return Objects.equals(this.value, other.value);
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(getClass())
107 .add("field", field)
108 .add("value", value)
109 .toString();
110 }
111
112 /**
113 * Flow data value builder.
114 */
115 public static class Builder {
116
117 private FlowField field;
118
119 private Object value;
120
121 /**
122 * Setter for flow fields.
123 *
124 * @param field flow field.
125 * @return this class builder.
126 */
127 public Builder field(FlowField field) {
128 this.field = field;
129 return this;
130 }
131
132 /**
133 * Setter for flow data value.
134 *
135 * @param value flow data value.
136 * @return this class builder.
137 */
138 public Builder value(Object value) {
139 this.value = value;
140 return this;
141 }
142
143 /**
144 * Checks arguments for flow data value.
145 */
146 private void checkArguments() {
147 checkNotNull(field, "flow field cannot be null");
148 checkNotNull(value, "value cannot be null");
149 }
150
151 /**
152 * Builds data flow.
153 *
154 * @return data flow object.
155 */
156 public Flow build() {
157 checkArguments();
158 return new Flow(this);
159 }
160
161 }
162
163}