Phaneendra Manda | 65440bf | 2016-02-20 19:41:21 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Phaneendra Manda | 65440bf | 2016-02-20 19:41:21 +0530 | [diff] [blame] | 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 | } |