Phaneendra Manda
Committed by Thomas Vachuska

[ONOS-3833]Implementation for FiveTuple class to store 5 tuple packet info

Change-Id: I15f2b41187c1577677428a332b44fa1c5351cb05
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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.vtnrsc;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkArgument;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onlab.packet.IPv4;
24 +import org.onlab.packet.IpAddress;
25 +import org.onosproject.net.PortNumber;
26 +
27 +/**
28 + * Representation for five tuple information from packet.
29 + */
30 +public final class DefaultFiveTuple implements FiveTuple {
31 +
32 + private final IpAddress ipSrc;
33 + private final IpAddress ipDst;
34 + private final PortNumber portSrc;
35 + private final PortNumber portDst;
36 + private final byte protocol;
37 + private final TenantId tenantId;
38 +
39 + /**
40 + * Constructor for packet five tuple information.
41 + *
42 + * @param protocol protocol of the packet
43 + * @param ipSrc source ip address of the packet
44 + * @param ipDst destination ip address of the packet
45 + * @param portSrc source port of the packet
46 + * @param portDst destination port of the packet
47 + */
48 + private DefaultFiveTuple(byte protocol, IpAddress ipSrc, IpAddress ipDst, PortNumber portSrc, PortNumber portDst,
49 + TenantId tenantId) {
50 +
51 + this.protocol = protocol;
52 + this.ipSrc = ipSrc;
53 + this.ipDst = ipDst;
54 + this.portSrc = portSrc;
55 + this.portDst = portDst;
56 + this.tenantId = tenantId;
57 + }
58 +
59 + @Override
60 + public byte protocol() {
61 + return protocol;
62 + }
63 +
64 + @Override
65 + public IpAddress ipSrc() {
66 + return ipSrc;
67 + }
68 +
69 + @Override
70 + public IpAddress ipDst() {
71 + return ipDst;
72 + }
73 +
74 + @Override
75 + public PortNumber portSrc() {
76 + return portSrc;
77 + }
78 +
79 + @Override
80 + public PortNumber portDst() {
81 + return portDst;
82 + }
83 +
84 + @Override
85 + public TenantId tenantId() {
86 + return tenantId;
87 + }
88 +
89 + @Override
90 + public boolean equals(Object obj) {
91 + if (this == obj) {
92 + return true;
93 + }
94 + if (obj instanceof DefaultFiveTuple) {
95 + final DefaultFiveTuple other = (DefaultFiveTuple) obj;
96 + return Objects.equals(this.protocol, other.protocol) &&
97 + Objects.equals(this.ipSrc, other.ipSrc) &&
98 + Objects.equals(this.ipDst, other.ipDst) &&
99 + Objects.equals(this.portSrc, other.portSrc) &&
100 + Objects.equals(this.portDst, other.portDst);
101 + }
102 + return false;
103 + }
104 +
105 + @Override
106 + public int hashCode() {
107 + return Objects.hash(this.protocol, this.ipSrc, this.ipDst, this.portSrc, this.portDst);
108 + }
109 +
110 + @Override
111 + public String toString() {
112 + return toStringHelper(this)
113 + .omitNullValues()
114 + .add("protocol", protocol)
115 + .add("ipSrc", ipSrc)
116 + .add("ipDst", ipDst)
117 + .add("portSrc", portSrc)
118 + .add("portDst", portDst)
119 + .toString();
120 + }
121 +
122 + /**
123 + * To create an instance of the builder.
124 + *
125 + * @return instance of builder
126 + */
127 + public static Builder builder() {
128 + return new Builder();
129 + }
130 +
131 + /**
132 + * Builder class for Five tuple info.
133 + */
134 + public static final class Builder implements FiveTuple.Builder {
135 +
136 + private IpAddress ipSrc;
137 + private IpAddress ipDst;
138 + private PortNumber portSrc;
139 + private PortNumber portDst;
140 + private byte protocol;
141 + private TenantId tenantId;
142 +
143 + @Override
144 + public Builder setIpSrc(IpAddress ipSrc) {
145 + this.ipSrc = ipSrc;
146 + return this;
147 + }
148 +
149 + @Override
150 + public Builder setIpDst(IpAddress ipDst) {
151 + this.ipDst = ipDst;
152 + return this;
153 + }
154 +
155 + @Override
156 + public Builder setPortSrc(PortNumber portSrc) {
157 + this.portSrc = portSrc;
158 + return this;
159 + }
160 +
161 + @Override
162 + public Builder setPortDst(PortNumber portDst) {
163 + this.portDst = portDst;
164 + return this;
165 + }
166 +
167 + @Override
168 + public Builder setProtocol(byte protocol) {
169 + this.protocol = protocol;
170 + return this;
171 + }
172 +
173 + @Override
174 + public org.onosproject.vtnrsc.FiveTuple.Builder setTenantId(TenantId tenantId) {
175 + this.tenantId = tenantId;
176 + return this;
177 + }
178 +
179 + @Override
180 + public FiveTuple build() {
181 + checkArgument(protocol == IPv4.PROTOCOL_TCP || protocol == IPv4.PROTOCOL_UDP ||
182 + protocol == IPv4.PROTOCOL_ICMP, "Unsupported value for protocol while creating five tuple");
183 +
184 + return new DefaultFiveTuple(protocol, ipSrc, ipDst, portSrc, portDst, tenantId);
185 + }
186 + }
187 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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.vtnrsc;
17 +
18 +import static org.hamcrest.MatcherAssert.assertThat;
19 +import static org.hamcrest.Matchers.is;
20 +import static org.hamcrest.Matchers.notNullValue;
21 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
22 +
23 +import org.junit.Test;
24 +import org.onlab.packet.IPv4;
25 +import org.onlab.packet.IpAddress;
26 +import org.onosproject.net.PortNumber;
27 +
28 +import com.google.common.testing.EqualsTester;
29 +
30 +/**
31 + * Unit tests for FiveTuple class.
32 + */
33 +public class DefaultFiveTupleTest {
34 +
35 +
36 + final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
37 + .setIpDst(IpAddress.valueOf("2.2.2.2"))
38 + .setPortSrc(PortNumber.portNumber(500))
39 + .setPortDst(PortNumber.portNumber(1000))
40 + .setProtocol(IPv4.PROTOCOL_TCP)
41 + .setTenantId(TenantId.tenantId("aaa"))
42 + .build();
43 +
44 + final FiveTuple sameAsFiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
45 + .setIpDst(IpAddress.valueOf("2.2.2.2"))
46 + .setPortSrc(PortNumber.portNumber(500))
47 + .setPortDst(PortNumber.portNumber(1000))
48 + .setProtocol(IPv4.PROTOCOL_TCP)
49 + .setTenantId(TenantId.tenantId("aaa"))
50 + .build();
51 +
52 + final FiveTuple fiveTuple2 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("3.3.3.3"))
53 + .setIpDst(IpAddress.valueOf("4.4.4.4"))
54 + .setPortSrc(PortNumber.portNumber(1500))
55 + .setPortDst(PortNumber.portNumber(2000))
56 + .setProtocol(IPv4.PROTOCOL_UDP)
57 + .setTenantId(TenantId.tenantId("bbb"))
58 + .build();
59 +
60 + /**
61 + * Checks that the FiveTuple class is immutable.
62 + */
63 + @Test
64 + public void testImmutability() {
65 + assertThatClassIsImmutable(DefaultFiveTuple.class);
66 + }
67 +
68 + /**
69 + * Checks the operation of equals() methods.
70 + */
71 + @Test
72 + public void testEquals() {
73 + new EqualsTester().addEqualityGroup(fiveTuple1, sameAsFiveTuple1).addEqualityGroup(fiveTuple2)
74 + .testEquals();
75 + }
76 +
77 + /**
78 + * Checks the construction of a FiveTuple object.
79 + */
80 + @Test
81 + public void testConstruction() {
82 + final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
83 + .setIpDst(IpAddress.valueOf("2.2.2.2"))
84 + .setPortSrc(PortNumber.portNumber(500))
85 + .setPortDst(PortNumber.portNumber(1000))
86 + .setProtocol(IPv4.PROTOCOL_TCP)
87 + .setTenantId(TenantId.tenantId("aaa"))
88 + .build();
89 +
90 + assertThat(fiveTuple1, is(notNullValue()));
91 + assertThat(fiveTuple1.protocol(), is(IPv4.PROTOCOL_TCP));
92 + assertThat(fiveTuple1.ipSrc(), is(IpAddress.valueOf("1.1.1.1")));
93 + assertThat(fiveTuple1.ipDst(), is(IpAddress.valueOf("2.2.2.2")));
94 + assertThat(fiveTuple1.portSrc(), is(PortNumber.portNumber(500)));
95 + assertThat(fiveTuple1.portDst(), is(PortNumber.portNumber(1000)));
96 + assertThat(fiveTuple1.tenantId(), is(TenantId.tenantId("aaa")));
97 + }
98 +}