Jian Li | daa7d6a | 2021-04-13 17:22:56 +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 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | |
| 24 | /** |
| 25 | * Default implementation class of kubevirt load balancer rule. |
| 26 | */ |
| 27 | public final class DefaultKubevirtLoadBalancerRule implements KubevirtLoadBalancerRule { |
| 28 | private static final String NOT_NULL_MSG = "Load Balancer Rule % cannot be null"; |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 29 | private static final String TCP = "TCP"; |
| 30 | private static final String UDP = "UDP"; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 31 | |
| 32 | private final String protocol; |
| 33 | private final Integer portRangeMax; |
| 34 | private final Integer portRangeMin; |
| 35 | |
| 36 | /** |
| 37 | * A default constructor. |
| 38 | * |
| 39 | * @param protocol protocol |
| 40 | * @param portRangeMax port range max |
| 41 | * @param portRangeMin port range min |
| 42 | */ |
| 43 | public DefaultKubevirtLoadBalancerRule(String protocol, |
| 44 | Integer portRangeMax, |
| 45 | Integer portRangeMin) { |
| 46 | this.protocol = protocol; |
| 47 | this.portRangeMax = portRangeMax; |
| 48 | this.portRangeMin = portRangeMin; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String protocol() { |
| 53 | return protocol; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Integer portRangeMax() { |
| 58 | return portRangeMax; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Integer portRangeMin() { |
| 63 | return portRangeMin; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean equals(Object o) { |
| 68 | if (this == o) { |
| 69 | return true; |
| 70 | } |
| 71 | if (o == null || getClass() != o.getClass()) { |
| 72 | return false; |
| 73 | } |
| 74 | DefaultKubevirtLoadBalancerRule that = (DefaultKubevirtLoadBalancerRule) o; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 75 | |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 76 | return protocol.equals(that.protocol) && |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 77 | Objects.equals(portRangeMax, that.portRangeMax) && |
| 78 | Objects.equals(portRangeMin, that.portRangeMin); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int hashCode() { |
| 83 | return Objects.hash(protocol, portRangeMax, portRangeMin); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String toString() { |
| 88 | return MoreObjects.toStringHelper(this) |
| 89 | .add("protocol", protocol) |
| 90 | .add("portRangeMax", portRangeMax) |
| 91 | .add("portRangeMin", portRangeMin) |
| 92 | .toString(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns new builder instance. |
| 97 | * |
| 98 | * @return kubevirt load balancer rule builder |
| 99 | */ |
| 100 | public static Builder builder() { |
| 101 | return new Builder(); |
| 102 | } |
| 103 | |
| 104 | public static final class Builder implements KubevirtLoadBalancerRule.Builder { |
| 105 | |
| 106 | private String protocol; |
| 107 | private Integer portRangeMax; |
| 108 | private Integer portRangeMin; |
| 109 | |
| 110 | @Override |
| 111 | public KubevirtLoadBalancerRule build() { |
| 112 | checkArgument(protocol != null, NOT_NULL_MSG, "protocol"); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 113 | |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 114 | if (protocol.equalsIgnoreCase(TCP) || protocol.equalsIgnoreCase(UDP)) { |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 115 | checkArgument(portRangeMax != null, NOT_NULL_MSG, "portRangeMax"); |
| 116 | checkArgument(portRangeMin != null, NOT_NULL_MSG, "portRangeMin"); |
| 117 | } |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 118 | return new DefaultKubevirtLoadBalancerRule(protocol, portRangeMax, portRangeMin); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Builder portRangeMax(Integer portRangeMax) { |
| 123 | this.portRangeMax = portRangeMax; |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Builder portRangeMin(Integer portRangeMin) { |
| 129 | this.portRangeMin = portRangeMin; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Builder protocol(String protocol) { |
| 135 | this.protocol = protocol; |
| 136 | return this; |
| 137 | } |
| 138 | } |
| 139 | } |