blob: bf4a21d90e5a7cc42aaa2ae4ce32104a92cfb69a [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;
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpAddress;
21
22import java.util.Objects;
23import java.util.Set;
24
25import static com.google.common.base.Preconditions.checkArgument;
26
27/**
28 * Default implementation of kubevirt load balancer.
29 */
30public final class DefaultKubevirtLoadBalancer implements KubevirtLoadBalancer {
31
32 private static final String NOT_NULL_MSG = "Loadbalancer % cannot be null";
33
34 private final String name;
35 private final String description;
36 private final String networkId;
37 private final IpAddress vip;
38 private final Set<IpAddress> members;
39 private final Set<KubevirtLoadBalancerRule> rules;
40
41 /**
42 * Default constructor.
43 *
44 * @param name load balancer name
45 * @param description load balancer description
46 * @param networkId load balancer network identifier
47 * @param vip load balancer virtual IP address
48 * @param members load balancer members
49 * @param rules load balancer rules
50 */
51 public DefaultKubevirtLoadBalancer(String name, String description, String networkId,
52 IpAddress vip, Set<IpAddress> members,
53 Set<KubevirtLoadBalancerRule> rules) {
54 this.name = name;
55 this.description = description;
56 this.networkId = networkId;
57 this.vip = vip;
58 this.members = members;
59 this.rules = rules;
60 }
61
62 @Override
63 public String name() {
64 return name;
65 }
66
67 @Override
68 public String description() {
69 return description;
70 }
71
72 @Override
73 public String networkId() {
74 return networkId;
75 }
76
77 @Override
78 public IpAddress vip() {
79 return vip;
80 }
81
82 @Override
83 public Set<IpAddress> members() {
84 if (members == null) {
85 return ImmutableSet.of();
86 } else {
87 return ImmutableSet.copyOf(members);
88 }
89 }
90
91 @Override
92 public Set<KubevirtLoadBalancerRule> rules() {
93 if (rules == null) {
94 return ImmutableSet.of();
95 } else {
96 return ImmutableSet.copyOf(rules);
97 }
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) {
103 return true;
104 }
105 if (o == null || getClass() != o.getClass()) {
106 return false;
107 }
108 DefaultKubevirtLoadBalancer that = (DefaultKubevirtLoadBalancer) o;
109 return name.equals(that.name) && Objects.equals(description, that.description) &&
110 networkId.equals(that.networkId) && vip.equals(that.vip) &&
111 Objects.equals(members, that.members) && Objects.equals(rules, that.rules);
112 }
113
114 @Override
115 public int hashCode() {
116 return Objects.hash(name, description, networkId, vip, members, rules);
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(this)
122 .add("name", name)
123 .add("description", description)
124 .add("networkId", networkId)
125 .add("vip", vip)
126 .add("members", members)
127 .add("rules", rules)
128 .toString();
129 }
130
131 public static Builder builder() {
132 return new Builder();
133 }
134
135 public static final class Builder implements KubevirtLoadBalancer.Builder {
136
137 private String name;
138 private String description;
139 private String networkId;
140 private IpAddress vip;
141 private Set<IpAddress> members;
142 private Set<KubevirtLoadBalancerRule> rules;
143
144 // private constructor not intended to use from external
145 private Builder() {
146 }
147
148 @Override
149 public KubevirtLoadBalancer build() {
150 checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
151 checkArgument(name != null, NOT_NULL_MSG, "name");
152 checkArgument(vip != null, NOT_NULL_MSG, "vip");
153
154 return new DefaultKubevirtLoadBalancer(name, description, networkId, vip, members, rules);
155 }
156
157 @Override
158 public Builder name(String name) {
159 this.name = name;
160 return this;
161 }
162
163 @Override
164 public Builder description(String description) {
165 this.description = description;
166 return this;
167 }
168
169 @Override
170 public Builder networkId(String networkId) {
171 this.networkId = networkId;
172 return this;
173 }
174
175 @Override
176 public Builder vip(IpAddress vip) {
177 this.vip = vip;
178 return this;
179 }
180
181 @Override
182 public Builder members(Set<IpAddress> members) {
183 this.members = members;
184 return this;
185 }
186
187 @Override
188 public Builder rules(Set<KubevirtLoadBalancerRule> rules) {
189 this.rules = rules;
190 return this;
191 }
192 }
193}