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