Jian Li | 073f1ba | 2021-02-28 03:50:15 +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 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | /** |
| 26 | * Default implementation class of kubevirt floating IP. |
| 27 | */ |
| 28 | public 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; |
| 35 | private final IpAddress floatingIp; |
| 36 | private final IpAddress fixedIp; |
| 37 | |
| 38 | /** |
| 39 | * A default constructor. |
| 40 | * |
| 41 | * @param id floating IP identifier |
| 42 | * @param routerName router name |
| 43 | * @param podName POD name |
| 44 | * @param floatingIp floating IP address |
| 45 | * @param fixedIp fixed IP address |
| 46 | */ |
| 47 | public DefaultKubevirtFloatingIp(String id, String routerName, String podName, |
| 48 | IpAddress floatingIp, IpAddress fixedIp) { |
| 49 | this.id = id; |
| 50 | this.routerName = routerName; |
| 51 | this.podName = podName; |
| 52 | this.floatingIp = floatingIp; |
| 53 | this.fixedIp = fixedIp; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String id() { |
| 58 | return id; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String routerName() { |
| 63 | return routerName; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public IpAddress floatingIp() { |
| 68 | return floatingIp; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public IpAddress fixedIp() { |
| 73 | return fixedIp; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String podName() { |
| 78 | return podName; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public KubevirtFloatingIp updateFixedIp(IpAddress ip) { |
| 83 | return DefaultKubevirtFloatingIp.builder() |
| 84 | .id(id) |
| 85 | .routerName(routerName) |
| 86 | .floatingIp(floatingIp) |
| 87 | .fixedIp(ip) |
| 88 | .podName(podName) |
| 89 | .build(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public KubevirtFloatingIp updatePodName(String name) { |
| 94 | return DefaultKubevirtFloatingIp.builder() |
| 95 | .id(id) |
| 96 | .routerName(routerName) |
| 97 | .floatingIp(floatingIp) |
| 98 | .fixedIp(fixedIp) |
| 99 | .podName(name) |
| 100 | .build(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean equals(Object o) { |
| 105 | if (this == o) { |
| 106 | return true; |
| 107 | } |
| 108 | if (o == null || getClass() != o.getClass()) { |
| 109 | return false; |
| 110 | } |
| 111 | DefaultKubevirtFloatingIp that = (DefaultKubevirtFloatingIp) o; |
| 112 | return id.equals(that.id) && routerName.equals(that.routerName) && |
| 113 | Objects.equals(podName, that.podName) && |
| 114 | floatingIp.equals(that.floatingIp) && |
| 115 | Objects.equals(fixedIp, that.fixedIp); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int hashCode() { |
| 120 | return Objects.hash(id, routerName, podName, floatingIp, fixedIp); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String toString() { |
| 125 | return MoreObjects.toStringHelper(this) |
| 126 | .add("id", id) |
| 127 | .add("routerName", routerName) |
| 128 | .add("podName", podName) |
| 129 | .add("floatingIp", floatingIp) |
| 130 | .add("fixedIp", fixedIp) |
| 131 | .toString(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns new builder instance. |
| 136 | * |
| 137 | * @return kubevirt floating IP builder |
| 138 | */ |
| 139 | public static Builder builder() { |
| 140 | return new Builder(); |
| 141 | } |
| 142 | |
| 143 | public static final class Builder implements KubevirtFloatingIp.Builder { |
| 144 | |
| 145 | private String id; |
| 146 | private String routerName; |
| 147 | private String podName; |
| 148 | private IpAddress floatingIp; |
| 149 | private IpAddress fixedIp; |
| 150 | |
| 151 | @Override |
| 152 | public KubevirtFloatingIp build() { |
| 153 | checkArgument(id != null, NOT_NULL_MSG, "id"); |
| 154 | checkArgument(routerName != null, NOT_NULL_MSG, "routerName"); |
| 155 | checkArgument(floatingIp != null, NOT_NULL_MSG, "floatingIp"); |
| 156 | |
| 157 | return new DefaultKubevirtFloatingIp(id, routerName, podName, floatingIp, fixedIp); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder id(String id) { |
| 162 | this.id = id; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Builder routerName(String name) { |
| 168 | this.routerName = name; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Builder floatingIp(IpAddress ip) { |
| 174 | this.floatingIp = ip; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Builder fixedIp(IpAddress ip) { |
| 180 | this.fixedIp = ip; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Builder podName(String name) { |
| 186 | this.podName = name; |
| 187 | return this; |
| 188 | } |
| 189 | } |
| 190 | } |