Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
| 16 | package org.onosproject.openstackvtap.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 27 | * Default implementation of an immutable openstack vtap criterion. |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 28 | */ |
| 29 | public final class DefaultOpenstackVtapCriterion implements OpenstackVtapCriterion { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 30 | |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 31 | private final IpPrefix srcIpPrefix; |
| 32 | private final IpPrefix dstIpPrefix; |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 33 | private final byte ipProtocol; |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 34 | private final TpPort srcTpPort; |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 35 | private final TpPort dstTpPort; |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 36 | |
| 37 | // private constructor not intended to use from external |
| 38 | private DefaultOpenstackVtapCriterion(IpPrefix srcIpPrefix, |
| 39 | IpPrefix dstIpPrefix, |
| 40 | byte ipProtocol, |
| 41 | TpPort srcTpPort, |
| 42 | TpPort dstTpPort) { |
| 43 | this.srcIpPrefix = srcIpPrefix; |
| 44 | this.dstIpPrefix = dstIpPrefix; |
| 45 | this.ipProtocol = ipProtocol; |
| 46 | this.srcTpPort = srcTpPort; |
| 47 | this.dstTpPort = dstTpPort; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public IpPrefix srcIpPrefix() { |
| 52 | return srcIpPrefix; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public IpPrefix dstIpPrefix() { |
| 57 | return dstIpPrefix; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public byte ipProtocol() { |
| 62 | return ipProtocol; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public TpPort srcTpPort() { |
| 67 | return srcTpPort; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public TpPort dstTpPort() { |
| 72 | return dstTpPort; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | return MoreObjects.toStringHelper(getClass()) |
| 78 | .add("srcIpPrefix", srcIpPrefix) |
| 79 | .add("dstIpPrefix", dstIpPrefix) |
| 80 | .add("ipProtocol", ipProtocol) |
| 81 | .add("srcTpPort", srcTpPort) |
| 82 | .add("dstTpPort", dstTpPort) |
| 83 | .toString(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 88 | return Objects.hashCode(srcIpPrefix, dstIpPrefix, |
| 89 | ipProtocol, srcTpPort, dstTpPort); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 93 | public boolean equals(Object o) { |
| 94 | if (this == o) { |
| 95 | return true; |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 96 | } |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 97 | if (o == null || getClass() != o.getClass()) { |
| 98 | return false; |
| 99 | } |
| 100 | DefaultOpenstackVtapCriterion that = (DefaultOpenstackVtapCriterion) o; |
| 101 | return Objects.equal(this.srcIpPrefix, that.srcIpPrefix) && |
| 102 | Objects.equal(this.dstIpPrefix, that.dstIpPrefix) && |
| 103 | Objects.equal(this.ipProtocol, that.ipProtocol) && |
| 104 | Objects.equal(this.srcTpPort, that.srcTpPort) && |
| 105 | Objects.equal(this.dstTpPort, that.dstTpPort); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 109 | * Creates a new default openstack vtap criterion builder. |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 110 | * |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 111 | * @return default openstack vtap criterion builder |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 112 | */ |
| 113 | public static Builder builder() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
| 117 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 118 | * A builder class for openstack vtap criterion builder. |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 119 | */ |
| 120 | public static final class Builder implements OpenstackVtapCriterion.Builder { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 121 | private static final String NOT_NULL_MSG = "OpenstackVtapCriterion % cannot be null"; |
| 122 | |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 123 | private IpPrefix srcIpPrefix; |
| 124 | private IpPrefix dstIpPrefix; |
| 125 | private byte ipProtocol; |
| 126 | private TpPort srcTpPort; |
| 127 | private TpPort dstTpPort; |
| 128 | |
| 129 | // private constructor not intended to use from external |
| 130 | Builder() { |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public DefaultOpenstackVtapCriterion build() { |
| 135 | checkArgument(srcIpPrefix != null, NOT_NULL_MSG, "Source IP Prefix"); |
| 136 | checkArgument(dstIpPrefix != null, NOT_NULL_MSG, "Destination IP Prefix"); |
| 137 | |
| 138 | return new DefaultOpenstackVtapCriterion(srcIpPrefix, dstIpPrefix, |
| 139 | ipProtocol, srcTpPort, dstTpPort); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder srcIpPrefix(IpPrefix srcIpPrefix) { |
| 144 | this.srcIpPrefix = srcIpPrefix; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Builder dstIpPrefix(IpPrefix dstIpPrefix) { |
| 150 | this.dstIpPrefix = dstIpPrefix; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Builder ipProtocol(byte ipProtocol) { |
| 156 | this.ipProtocol = ipProtocol; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder srcTpPort(TpPort srcTpPort) { |
| 162 | this.srcTpPort = srcTpPort; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Builder dstTpPort(TpPort dstTpPort) { |
| 168 | this.dstTpPort = dstTpPort; |
| 169 | return this; |
| 170 | } |
| 171 | } |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 172 | |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 173 | } |