Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | /** |
| 26 | * Default implementation class of kubevirt security group rule. |
| 27 | */ |
| 28 | public final class DefaultKubevirtSecurityGroupRule implements KubevirtSecurityGroupRule { |
| 29 | |
| 30 | private static final String NOT_NULL_MSG = "Security Group Rule % cannot be null"; |
| 31 | |
| 32 | private final String id; |
| 33 | private final String securityGroupId; |
| 34 | private final String direction; |
| 35 | private final String etherType; |
| 36 | private final Integer portRangeMax; |
| 37 | private final Integer portRangeMin; |
| 38 | private final String protocol; |
| 39 | private final IpPrefix remoteIpPrefix; |
| 40 | private final String remoteGroupId; |
| 41 | |
| 42 | /** |
| 43 | * A default constructor. |
| 44 | * |
| 45 | * @param id security group rule identifier |
| 46 | * @param securityGroupId security group identifier |
| 47 | * @param direction traffic direction |
| 48 | * @param etherType ethernet type |
| 49 | * @param portRangeMax maximum port range |
| 50 | * @param portRangeMin minimum port range |
| 51 | * @param protocol network protocol |
| 52 | * @param remoteIpPrefix remote IP prefix |
| 53 | * @param remoteGroupId remote group identifier |
| 54 | */ |
| 55 | public DefaultKubevirtSecurityGroupRule(String id, String securityGroupId, |
| 56 | String direction, String etherType, |
| 57 | Integer portRangeMax, Integer portRangeMin, |
| 58 | String protocol, IpPrefix remoteIpPrefix, |
| 59 | String remoteGroupId) { |
| 60 | this.id = id; |
| 61 | this.securityGroupId = securityGroupId; |
| 62 | this.direction = direction; |
| 63 | this.etherType = etherType; |
| 64 | this.portRangeMax = portRangeMax; |
| 65 | this.portRangeMin = portRangeMin; |
| 66 | this.protocol = protocol; |
| 67 | this.remoteIpPrefix = remoteIpPrefix; |
| 68 | this.remoteGroupId = remoteGroupId; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String id() { |
| 73 | return id; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String securityGroupId() { |
| 78 | return securityGroupId; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String direction() { |
| 83 | return direction; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String etherType() { |
| 88 | return etherType; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Integer portRangeMax() { |
| 93 | return portRangeMax; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Integer portRangeMin() { |
| 98 | return portRangeMin; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String protocol() { |
| 103 | return protocol; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public IpPrefix remoteIpPrefix() { |
| 108 | return remoteIpPrefix; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String remoteGroupId() { |
| 113 | return remoteGroupId; |
| 114 | } |
| 115 | |
| 116 | @Override |
Jian Li | 8f944d4 | 2021-03-23 00:43:29 +0900 | [diff] [blame^] | 117 | public KubevirtSecurityGroupRule updateDirection(String updated) { |
| 118 | return DefaultKubevirtSecurityGroupRule.builder() |
| 119 | .remoteGroupId(remoteGroupId) |
| 120 | .etherType(etherType) |
| 121 | .protocol(protocol) |
| 122 | .portRangeMin(portRangeMin) |
| 123 | .portRangeMax(portRangeMax) |
| 124 | .securityGroupId(securityGroupId) |
| 125 | .remoteIpPrefix(remoteIpPrefix) |
| 126 | .id(id) |
| 127 | .direction(updated) |
| 128 | .build(); |
| 129 | } |
| 130 | |
| 131 | @Override |
Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +0900 | [diff] [blame] | 132 | public boolean equals(Object o) { |
| 133 | if (this == o) { |
| 134 | return true; |
| 135 | } |
| 136 | if (o == null || getClass() != o.getClass()) { |
| 137 | return false; |
| 138 | } |
| 139 | DefaultKubevirtSecurityGroupRule that = (DefaultKubevirtSecurityGroupRule) o; |
| 140 | return id.equals(that.id) && securityGroupId.equals(that.securityGroupId) && |
| 141 | direction.equals(that.direction) && |
| 142 | Objects.equals(etherType, that.etherType) && |
| 143 | Objects.equals(portRangeMax, that.portRangeMax) && |
| 144 | Objects.equals(portRangeMin, that.portRangeMin) && |
| 145 | Objects.equals(protocol, that.protocol) && |
| 146 | Objects.equals(remoteIpPrefix, that.remoteIpPrefix) && |
| 147 | Objects.equals(remoteGroupId, that.remoteGroupId); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return Objects.hash(id, securityGroupId, direction, etherType, portRangeMax, |
| 153 | portRangeMin, protocol, remoteIpPrefix, remoteGroupId); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String toString() { |
| 158 | return MoreObjects.toStringHelper(this) |
| 159 | .add("id", id) |
| 160 | .add("securityGroupId", securityGroupId) |
| 161 | .add("direction", direction) |
| 162 | .add("etherType", etherType) |
| 163 | .add("portRangeMax", portRangeMax) |
| 164 | .add("portRangeMin", portRangeMin) |
| 165 | .add("protocol", protocol) |
| 166 | .add("remoteIpPrefix", remoteIpPrefix) |
| 167 | .add("remoteGroupId", remoteGroupId) |
| 168 | .toString(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns new builder instance. |
| 173 | * |
| 174 | * @return kubevirt security group rule builder |
| 175 | */ |
| 176 | public static Builder builder() { |
| 177 | return new Builder(); |
| 178 | } |
| 179 | |
| 180 | public static final class Builder implements KubevirtSecurityGroupRule.Builder { |
| 181 | |
| 182 | private String id; |
| 183 | private String securityGroupId; |
| 184 | private String direction; |
| 185 | private String etherType; |
| 186 | private Integer portRangeMax; |
| 187 | private Integer portRangeMin; |
| 188 | private String protocol; |
| 189 | private IpPrefix remoteIpPrefix; |
| 190 | private String remoteGroupId; |
| 191 | |
| 192 | @Override |
| 193 | public KubevirtSecurityGroupRule build() { |
| 194 | checkArgument(id != null, NOT_NULL_MSG, "id"); |
| 195 | checkArgument(securityGroupId != null, NOT_NULL_MSG, "securityGroupId"); |
| 196 | checkArgument(direction != null, NOT_NULL_MSG, "direction"); |
| 197 | |
| 198 | return new DefaultKubevirtSecurityGroupRule(id, securityGroupId, |
| 199 | direction, etherType, portRangeMax, portRangeMin, protocol, |
| 200 | remoteIpPrefix, remoteGroupId); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Builder id(String id) { |
| 205 | this.id = id; |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Builder securityGroupId(String securityGroupId) { |
| 211 | this.securityGroupId = securityGroupId; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Builder direction(String direction) { |
| 217 | this.direction = direction; |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public Builder etherType(String etherType) { |
| 223 | this.etherType = etherType; |
| 224 | return this; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public Builder portRangeMax(Integer portRangeMax) { |
| 229 | this.portRangeMax = portRangeMax; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Builder portRangeMin(Integer portRangeMin) { |
| 235 | this.portRangeMin = portRangeMin; |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Builder protocol(String protocol) { |
| 241 | this.protocol = protocol; |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Builder remoteIpPrefix(IpPrefix remoteIpPrefix) { |
| 247 | this.remoteIpPrefix = remoteIpPrefix; |
| 248 | return this; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public Builder remoteGroupId(String remoteGroupId) { |
| 253 | this.remoteGroupId = remoteGroupId; |
| 254 | return this; |
| 255 | } |
| 256 | } |
| 257 | } |