[ONOS-3833]Implementation for FiveTuple class to store 5 tuple packet info
Change-Id: I15f2b41187c1577677428a332b44fa1c5351cb05
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultFiveTuple.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultFiveTuple.java
new file mode 100644
index 0000000..b1bc606
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultFiveTuple.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.vtnrsc;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Objects;
+
+import org.onlab.packet.IPv4;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Representation for five tuple information from packet.
+ */
+public final class DefaultFiveTuple implements FiveTuple {
+
+ private final IpAddress ipSrc;
+ private final IpAddress ipDst;
+ private final PortNumber portSrc;
+ private final PortNumber portDst;
+ private final byte protocol;
+ private final TenantId tenantId;
+
+ /**
+ * Constructor for packet five tuple information.
+ *
+ * @param protocol protocol of the packet
+ * @param ipSrc source ip address of the packet
+ * @param ipDst destination ip address of the packet
+ * @param portSrc source port of the packet
+ * @param portDst destination port of the packet
+ */
+ private DefaultFiveTuple(byte protocol, IpAddress ipSrc, IpAddress ipDst, PortNumber portSrc, PortNumber portDst,
+ TenantId tenantId) {
+
+ this.protocol = protocol;
+ this.ipSrc = ipSrc;
+ this.ipDst = ipDst;
+ this.portSrc = portSrc;
+ this.portDst = portDst;
+ this.tenantId = tenantId;
+ }
+
+ @Override
+ public byte protocol() {
+ return protocol;
+ }
+
+ @Override
+ public IpAddress ipSrc() {
+ return ipSrc;
+ }
+
+ @Override
+ public IpAddress ipDst() {
+ return ipDst;
+ }
+
+ @Override
+ public PortNumber portSrc() {
+ return portSrc;
+ }
+
+ @Override
+ public PortNumber portDst() {
+ return portDst;
+ }
+
+ @Override
+ public TenantId tenantId() {
+ return tenantId;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof DefaultFiveTuple) {
+ final DefaultFiveTuple other = (DefaultFiveTuple) obj;
+ return Objects.equals(this.protocol, other.protocol) &&
+ Objects.equals(this.ipSrc, other.ipSrc) &&
+ Objects.equals(this.ipDst, other.ipDst) &&
+ Objects.equals(this.portSrc, other.portSrc) &&
+ Objects.equals(this.portDst, other.portDst);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.protocol, this.ipSrc, this.ipDst, this.portSrc, this.portDst);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .omitNullValues()
+ .add("protocol", protocol)
+ .add("ipSrc", ipSrc)
+ .add("ipDst", ipDst)
+ .add("portSrc", portSrc)
+ .add("portDst", portDst)
+ .toString();
+ }
+
+ /**
+ * To create an instance of the builder.
+ *
+ * @return instance of builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder class for Five tuple info.
+ */
+ public static final class Builder implements FiveTuple.Builder {
+
+ private IpAddress ipSrc;
+ private IpAddress ipDst;
+ private PortNumber portSrc;
+ private PortNumber portDst;
+ private byte protocol;
+ private TenantId tenantId;
+
+ @Override
+ public Builder setIpSrc(IpAddress ipSrc) {
+ this.ipSrc = ipSrc;
+ return this;
+ }
+
+ @Override
+ public Builder setIpDst(IpAddress ipDst) {
+ this.ipDst = ipDst;
+ return this;
+ }
+
+ @Override
+ public Builder setPortSrc(PortNumber portSrc) {
+ this.portSrc = portSrc;
+ return this;
+ }
+
+ @Override
+ public Builder setPortDst(PortNumber portDst) {
+ this.portDst = portDst;
+ return this;
+ }
+
+ @Override
+ public Builder setProtocol(byte protocol) {
+ this.protocol = protocol;
+ return this;
+ }
+
+ @Override
+ public org.onosproject.vtnrsc.FiveTuple.Builder setTenantId(TenantId tenantId) {
+ this.tenantId = tenantId;
+ return this;
+ }
+
+ @Override
+ public FiveTuple build() {
+ checkArgument(protocol == IPv4.PROTOCOL_TCP || protocol == IPv4.PROTOCOL_UDP ||
+ protocol == IPv4.PROTOCOL_ICMP, "Unsupported value for protocol while creating five tuple");
+
+ return new DefaultFiveTuple(protocol, ipSrc, ipDst, portSrc, portDst, tenantId);
+ }
+ }
+}
diff --git a/apps/vtn/vtnrsc/src/test/java/org/onosproject/vtnrsc/DefaultFiveTupleTest.java b/apps/vtn/vtnrsc/src/test/java/org/onosproject/vtnrsc/DefaultFiveTupleTest.java
new file mode 100644
index 0000000..310c697
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/test/java/org/onosproject/vtnrsc/DefaultFiveTupleTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.vtnrsc;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+import org.junit.Test;
+import org.onlab.packet.IPv4;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.PortNumber;
+
+import com.google.common.testing.EqualsTester;
+
+/**
+ * Unit tests for FiveTuple class.
+ */
+public class DefaultFiveTupleTest {
+
+
+ final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
+ .setIpDst(IpAddress.valueOf("2.2.2.2"))
+ .setPortSrc(PortNumber.portNumber(500))
+ .setPortDst(PortNumber.portNumber(1000))
+ .setProtocol(IPv4.PROTOCOL_TCP)
+ .setTenantId(TenantId.tenantId("aaa"))
+ .build();
+
+ final FiveTuple sameAsFiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
+ .setIpDst(IpAddress.valueOf("2.2.2.2"))
+ .setPortSrc(PortNumber.portNumber(500))
+ .setPortDst(PortNumber.portNumber(1000))
+ .setProtocol(IPv4.PROTOCOL_TCP)
+ .setTenantId(TenantId.tenantId("aaa"))
+ .build();
+
+ final FiveTuple fiveTuple2 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("3.3.3.3"))
+ .setIpDst(IpAddress.valueOf("4.4.4.4"))
+ .setPortSrc(PortNumber.portNumber(1500))
+ .setPortDst(PortNumber.portNumber(2000))
+ .setProtocol(IPv4.PROTOCOL_UDP)
+ .setTenantId(TenantId.tenantId("bbb"))
+ .build();
+
+ /**
+ * Checks that the FiveTuple class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultFiveTuple.class);
+ }
+
+ /**
+ * Checks the operation of equals() methods.
+ */
+ @Test
+ public void testEquals() {
+ new EqualsTester().addEqualityGroup(fiveTuple1, sameAsFiveTuple1).addEqualityGroup(fiveTuple2)
+ .testEquals();
+ }
+
+ /**
+ * Checks the construction of a FiveTuple object.
+ */
+ @Test
+ public void testConstruction() {
+ final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1"))
+ .setIpDst(IpAddress.valueOf("2.2.2.2"))
+ .setPortSrc(PortNumber.portNumber(500))
+ .setPortDst(PortNumber.portNumber(1000))
+ .setProtocol(IPv4.PROTOCOL_TCP)
+ .setTenantId(TenantId.tenantId("aaa"))
+ .build();
+
+ assertThat(fiveTuple1, is(notNullValue()));
+ assertThat(fiveTuple1.protocol(), is(IPv4.PROTOCOL_TCP));
+ assertThat(fiveTuple1.ipSrc(), is(IpAddress.valueOf("1.1.1.1")));
+ assertThat(fiveTuple1.ipDst(), is(IpAddress.valueOf("2.2.2.2")));
+ assertThat(fiveTuple1.portSrc(), is(PortNumber.portNumber(500)));
+ assertThat(fiveTuple1.portDst(), is(PortNumber.portNumber(1000)));
+ assertThat(fiveTuple1.tenantId(), is(TenantId.tenantId("aaa")));
+ }
+}