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"; |
| 29 | |
| 30 | private final String protocol; |
| 31 | private final Integer portRangeMax; |
| 32 | private final Integer portRangeMin; |
| 33 | |
| 34 | /** |
| 35 | * A default constructor. |
| 36 | * |
| 37 | * @param protocol protocol |
| 38 | * @param portRangeMax port range max |
| 39 | * @param portRangeMin port range min |
| 40 | */ |
| 41 | public DefaultKubevirtLoadBalancerRule(String protocol, |
| 42 | Integer portRangeMax, |
| 43 | Integer portRangeMin) { |
| 44 | this.protocol = protocol; |
| 45 | this.portRangeMax = portRangeMax; |
| 46 | this.portRangeMin = portRangeMin; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public String protocol() { |
| 51 | return protocol; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Integer portRangeMax() { |
| 56 | return portRangeMax; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Integer portRangeMin() { |
| 61 | return portRangeMin; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean equals(Object o) { |
| 66 | if (this == o) { |
| 67 | return true; |
| 68 | } |
| 69 | if (o == null || getClass() != o.getClass()) { |
| 70 | return false; |
| 71 | } |
| 72 | DefaultKubevirtLoadBalancerRule that = (DefaultKubevirtLoadBalancerRule) o; |
| 73 | return protocol.equals(that.protocol) && |
| 74 | portRangeMax.equals(that.portRangeMax) && |
| 75 | portRangeMin.equals(that.portRangeMin); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
| 80 | return Objects.hash(protocol, portRangeMax, portRangeMin); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return MoreObjects.toStringHelper(this) |
| 86 | .add("protocol", protocol) |
| 87 | .add("portRangeMax", portRangeMax) |
| 88 | .add("portRangeMin", portRangeMin) |
| 89 | .toString(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns new builder instance. |
| 94 | * |
| 95 | * @return kubevirt load balancer rule builder |
| 96 | */ |
| 97 | public static Builder builder() { |
| 98 | return new Builder(); |
| 99 | } |
| 100 | |
| 101 | public static final class Builder implements KubevirtLoadBalancerRule.Builder { |
| 102 | |
| 103 | private String protocol; |
| 104 | private Integer portRangeMax; |
| 105 | private Integer portRangeMin; |
| 106 | |
| 107 | @Override |
| 108 | public KubevirtLoadBalancerRule build() { |
| 109 | checkArgument(protocol != null, NOT_NULL_MSG, "protocol"); |
| 110 | checkArgument(portRangeMax != null, NOT_NULL_MSG, "portRangeMax"); |
| 111 | checkArgument(portRangeMin != null, NOT_NULL_MSG, "portRangeMin"); |
| 112 | |
| 113 | return new DefaultKubevirtLoadBalancerRule(protocol, portRangeMax, portRangeMin); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Builder portRangeMax(Integer portRangeMax) { |
| 118 | this.portRangeMax = portRangeMax; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Builder portRangeMin(Integer portRangeMin) { |
| 124 | this.portRangeMin = portRangeMin; |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Builder protocol(String protocol) { |
| 130 | this.protocol = protocol; |
| 131 | return this; |
| 132 | } |
| 133 | } |
| 134 | } |