blob: 3ba25bb1096df0b0e5de8449a365174bcb9c43b2 [file] [log] [blame]
Phaneendra Manda65440bf2016-02-20 19:41:21 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Phaneendra Manda65440bf2016-02-20 19:41:21 +05303 *
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.vtnrsc;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkArgument;
20
21import java.util.Objects;
22
23import org.onlab.packet.IPv4;
24import org.onlab.packet.IpAddress;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053025import org.onlab.packet.MacAddress;
Phaneendra Manda65440bf2016-02-20 19:41:21 +053026import org.onosproject.net.PortNumber;
27
28/**
29 * Representation for five tuple information from packet.
30 */
31public final class DefaultFiveTuple implements FiveTuple {
32
Phaneendra Mandab212bc92016-07-08 16:50:11 +053033 private final MacAddress macSrc;
34 private final MacAddress macDst;
Phaneendra Manda65440bf2016-02-20 19:41:21 +053035 private final IpAddress ipSrc;
36 private final IpAddress ipDst;
37 private final PortNumber portSrc;
38 private final PortNumber portDst;
39 private final byte protocol;
40 private final TenantId tenantId;
41
42 /**
43 * Constructor for packet five tuple information.
44 *
45 * @param protocol protocol of the packet
46 * @param ipSrc source ip address of the packet
47 * @param ipDst destination ip address of the packet
48 * @param portSrc source port of the packet
49 * @param portDst destination port of the packet
50 */
51 private DefaultFiveTuple(byte protocol, IpAddress ipSrc, IpAddress ipDst, PortNumber portSrc, PortNumber portDst,
Phaneendra Mandab212bc92016-07-08 16:50:11 +053052 TenantId tenantId, MacAddress macSrc, MacAddress macDst) {
Phaneendra Manda65440bf2016-02-20 19:41:21 +053053
54 this.protocol = protocol;
55 this.ipSrc = ipSrc;
56 this.ipDst = ipDst;
57 this.portSrc = portSrc;
58 this.portDst = portDst;
59 this.tenantId = tenantId;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053060 this.macSrc = macSrc;
61 this.macDst = macDst;
Phaneendra Manda65440bf2016-02-20 19:41:21 +053062 }
63
64 @Override
65 public byte protocol() {
66 return protocol;
67 }
68
69 @Override
70 public IpAddress ipSrc() {
71 return ipSrc;
72 }
73
74 @Override
75 public IpAddress ipDst() {
76 return ipDst;
77 }
78
79 @Override
80 public PortNumber portSrc() {
81 return portSrc;
82 }
83
84 @Override
85 public PortNumber portDst() {
86 return portDst;
87 }
88
89 @Override
Phaneendra Mandab212bc92016-07-08 16:50:11 +053090 public MacAddress macSrc() {
91 return macSrc;
92 }
93
94 @Override
95 public MacAddress macDst() {
96 return macDst;
97 }
98
99 @Override
Phaneendra Manda65440bf2016-02-20 19:41:21 +0530100 public TenantId tenantId() {
101 return tenantId;
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109 if (obj instanceof DefaultFiveTuple) {
110 final DefaultFiveTuple other = (DefaultFiveTuple) obj;
111 return Objects.equals(this.protocol, other.protocol) &&
112 Objects.equals(this.ipSrc, other.ipSrc) &&
113 Objects.equals(this.ipDst, other.ipDst) &&
114 Objects.equals(this.portSrc, other.portSrc) &&
115 Objects.equals(this.portDst, other.portDst);
116 }
117 return false;
118 }
119
120 @Override
121 public int hashCode() {
122 return Objects.hash(this.protocol, this.ipSrc, this.ipDst, this.portSrc, this.portDst);
123 }
124
125 @Override
126 public String toString() {
127 return toStringHelper(this)
128 .omitNullValues()
129 .add("protocol", protocol)
130 .add("ipSrc", ipSrc)
131 .add("ipDst", ipDst)
132 .add("portSrc", portSrc)
133 .add("portDst", portDst)
134 .toString();
135 }
136
137 /**
138 * To create an instance of the builder.
139 *
140 * @return instance of builder
141 */
142 public static Builder builder() {
143 return new Builder();
144 }
145
146 /**
147 * Builder class for Five tuple info.
148 */
149 public static final class Builder implements FiveTuple.Builder {
150
151 private IpAddress ipSrc;
152 private IpAddress ipDst;
153 private PortNumber portSrc;
154 private PortNumber portDst;
155 private byte protocol;
156 private TenantId tenantId;
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530157 private MacAddress macSrc;
158 private MacAddress macDst;
Phaneendra Manda65440bf2016-02-20 19:41:21 +0530159
160 @Override
161 public Builder setIpSrc(IpAddress ipSrc) {
162 this.ipSrc = ipSrc;
163 return this;
164 }
165
166 @Override
167 public Builder setIpDst(IpAddress ipDst) {
168 this.ipDst = ipDst;
169 return this;
170 }
171
172 @Override
173 public Builder setPortSrc(PortNumber portSrc) {
174 this.portSrc = portSrc;
175 return this;
176 }
177
178 @Override
179 public Builder setPortDst(PortNumber portDst) {
180 this.portDst = portDst;
181 return this;
182 }
183
184 @Override
185 public Builder setProtocol(byte protocol) {
186 this.protocol = protocol;
187 return this;
188 }
189
190 @Override
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530191 public Builder setTenantId(TenantId tenantId) {
Phaneendra Manda65440bf2016-02-20 19:41:21 +0530192 this.tenantId = tenantId;
193 return this;
194 }
195
196 @Override
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530197 public Builder setMacSrc(MacAddress macSrc) {
198 this.macSrc = macSrc;
199 return this;
200 }
201
202 @Override
203 public Builder setMacDst(MacAddress macDst) {
204 this.macDst = macDst;
205 return this;
206 }
207
208 @Override
Phaneendra Manda65440bf2016-02-20 19:41:21 +0530209 public FiveTuple build() {
210 checkArgument(protocol == IPv4.PROTOCOL_TCP || protocol == IPv4.PROTOCOL_UDP ||
211 protocol == IPv4.PROTOCOL_ICMP, "Unsupported value for protocol while creating five tuple");
212
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530213 return new DefaultFiveTuple(protocol, ipSrc, ipDst, portSrc, portDst, tenantId, macSrc, macDst);
Phaneendra Manda65440bf2016-02-20 19:41:21 +0530214 }
215 }
216}