blob: 4d8557ce48eb52991204afc9f90fdbdc5d263b01 [file] [log] [blame]
Jian Lidaa7d6a2021-04-13 17:22:56 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.api;
17
18import com.google.common.base.MoreObjects;
19
20import java.util.Objects;
21
22import static com.google.common.base.Preconditions.checkArgument;
23
24/**
25 * Default implementation class of kubevirt load balancer rule.
26 */
27public final class DefaultKubevirtLoadBalancerRule implements KubevirtLoadBalancerRule {
28 private static final String NOT_NULL_MSG = "Load Balancer Rule % cannot be null";
Jian Li7d13b592021-09-24 15:08:53 +090029 private static final String TCP = "TCP";
30 private static final String UDP = "UDP";
Jian Lidaa7d6a2021-04-13 17:22:56 +090031
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 Park05a94582021-05-12 10:57:02 +090075
Jian Lidaa7d6a2021-04-13 17:22:56 +090076 return protocol.equals(that.protocol) &&
Daniel Park05a94582021-05-12 10:57:02 +090077 Objects.equals(portRangeMax, that.portRangeMax) &&
78 Objects.equals(portRangeMin, that.portRangeMin);
Jian Lidaa7d6a2021-04-13 17:22:56 +090079 }
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 Lidaa7d6a2021-04-13 17:22:56 +0900113
Jian Li7d13b592021-09-24 15:08:53 +0900114 if (protocol.equalsIgnoreCase(TCP) || protocol.equalsIgnoreCase(UDP)) {
Daniel Park05a94582021-05-12 10:57:02 +0900115 checkArgument(portRangeMax != null, NOT_NULL_MSG, "portRangeMax");
116 checkArgument(portRangeMin != null, NOT_NULL_MSG, "portRangeMin");
117 }
Jian Lidaa7d6a2021-04-13 17:22:56 +0900118 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}