blob: 213fa4e4d22d6eba51433e9ab2ef7a9f52fdbb55 [file] [log] [blame]
Jian Li073f1ba2021-02-28 03:50:15 +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 org.onlab.packet.IpAddress;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
25/**
26 * Default implementation class of kubevirt floating IP.
27 */
28public final class DefaultKubevirtFloatingIp implements KubevirtFloatingIp {
29
30 private static final String NOT_NULL_MSG = "Floating IP % cannot be null";
31
32 private final String id;
33 private final String routerName;
34 private final String podName;
Jian Lib636f702021-03-03 14:46:50 +090035 private final String networkName;
Jian Li073f1ba2021-02-28 03:50:15 +090036 private final IpAddress floatingIp;
37 private final IpAddress fixedIp;
38
39 /**
40 * A default constructor.
41 *
42 * @param id floating IP identifier
43 * @param routerName router name
44 * @param podName POD name
Jian Lib636f702021-03-03 14:46:50 +090045 * @param networkName network name
Jian Li073f1ba2021-02-28 03:50:15 +090046 * @param floatingIp floating IP address
47 * @param fixedIp fixed IP address
48 */
49 public DefaultKubevirtFloatingIp(String id, String routerName, String podName,
Jian Lib636f702021-03-03 14:46:50 +090050 String networkName, IpAddress floatingIp, IpAddress fixedIp) {
Jian Li073f1ba2021-02-28 03:50:15 +090051 this.id = id;
52 this.routerName = routerName;
53 this.podName = podName;
Jian Lib636f702021-03-03 14:46:50 +090054 this.networkName = networkName;
Jian Li073f1ba2021-02-28 03:50:15 +090055 this.floatingIp = floatingIp;
56 this.fixedIp = fixedIp;
57 }
58
59 @Override
60 public String id() {
61 return id;
62 }
63
64 @Override
65 public String routerName() {
66 return routerName;
67 }
68
69 @Override
Jian Lib636f702021-03-03 14:46:50 +090070 public String networkName() {
71 return networkName;
72 }
73
74 @Override
Jian Li073f1ba2021-02-28 03:50:15 +090075 public IpAddress floatingIp() {
76 return floatingIp;
77 }
78
79 @Override
80 public IpAddress fixedIp() {
81 return fixedIp;
82 }
83
84 @Override
85 public String podName() {
86 return podName;
87 }
88
89 @Override
90 public KubevirtFloatingIp updateFixedIp(IpAddress ip) {
91 return DefaultKubevirtFloatingIp.builder()
92 .id(id)
Jian Lib636f702021-03-03 14:46:50 +090093 .networkName(networkName)
Jian Li073f1ba2021-02-28 03:50:15 +090094 .routerName(routerName)
95 .floatingIp(floatingIp)
96 .fixedIp(ip)
97 .podName(podName)
98 .build();
99 }
100
101 @Override
102 public KubevirtFloatingIp updatePodName(String name) {
103 return DefaultKubevirtFloatingIp.builder()
104 .id(id)
Jian Lib636f702021-03-03 14:46:50 +0900105 .networkName(networkName)
Jian Li073f1ba2021-02-28 03:50:15 +0900106 .routerName(routerName)
107 .floatingIp(floatingIp)
108 .fixedIp(fixedIp)
109 .podName(name)
110 .build();
111 }
112
113 @Override
114 public boolean equals(Object o) {
115 if (this == o) {
116 return true;
117 }
118 if (o == null || getClass() != o.getClass()) {
119 return false;
120 }
121 DefaultKubevirtFloatingIp that = (DefaultKubevirtFloatingIp) o;
122 return id.equals(that.id) && routerName.equals(that.routerName) &&
123 Objects.equals(podName, that.podName) &&
Jian Lib636f702021-03-03 14:46:50 +0900124 networkName.equals(that.networkName) &&
Jian Li073f1ba2021-02-28 03:50:15 +0900125 floatingIp.equals(that.floatingIp) &&
126 Objects.equals(fixedIp, that.fixedIp);
127 }
128
129 @Override
130 public int hashCode() {
131 return Objects.hash(id, routerName, podName, floatingIp, fixedIp);
132 }
133
134 @Override
135 public String toString() {
136 return MoreObjects.toStringHelper(this)
137 .add("id", id)
138 .add("routerName", routerName)
139 .add("podName", podName)
Jian Lib636f702021-03-03 14:46:50 +0900140 .add("networkName", networkName)
Jian Li073f1ba2021-02-28 03:50:15 +0900141 .add("floatingIp", floatingIp)
142 .add("fixedIp", fixedIp)
143 .toString();
144 }
145
146 /**
147 * Returns new builder instance.
148 *
149 * @return kubevirt floating IP builder
150 */
151 public static Builder builder() {
152 return new Builder();
153 }
154
155 public static final class Builder implements KubevirtFloatingIp.Builder {
156
157 private String id;
158 private String routerName;
159 private String podName;
Jian Lib636f702021-03-03 14:46:50 +0900160 private String networkName;
Jian Li073f1ba2021-02-28 03:50:15 +0900161 private IpAddress floatingIp;
162 private IpAddress fixedIp;
163
164 @Override
165 public KubevirtFloatingIp build() {
166 checkArgument(id != null, NOT_NULL_MSG, "id");
Jian Lib636f702021-03-03 14:46:50 +0900167 checkArgument(networkName != null, NOT_NULL_MSG, "networkName");
Jian Li073f1ba2021-02-28 03:50:15 +0900168 checkArgument(routerName != null, NOT_NULL_MSG, "routerName");
169 checkArgument(floatingIp != null, NOT_NULL_MSG, "floatingIp");
170
Jian Lib636f702021-03-03 14:46:50 +0900171 return new DefaultKubevirtFloatingIp(id, routerName, podName, networkName, floatingIp, fixedIp);
Jian Li073f1ba2021-02-28 03:50:15 +0900172 }
173
174 @Override
175 public Builder id(String id) {
176 this.id = id;
177 return this;
178 }
179
180 @Override
181 public Builder routerName(String name) {
182 this.routerName = name;
183 return this;
184 }
185
186 @Override
Jian Lib636f702021-03-03 14:46:50 +0900187 public Builder networkName(String name) {
188 this.networkName = name;
189 return this;
190 }
191
192 @Override
Jian Li073f1ba2021-02-28 03:50:15 +0900193 public Builder floatingIp(IpAddress ip) {
194 this.floatingIp = ip;
195 return this;
196 }
197
198 @Override
199 public Builder fixedIp(IpAddress ip) {
200 this.fixedIp = ip;
201 return this;
202 }
203
204 @Override
205 public Builder podName(String name) {
206 this.podName = name;
207 return this;
208 }
209 }
210}