blob: a6e4d0e6e9442601e19a7798e61254625c276c29 [file] [log] [blame]
Jian Lie2a04ce2020-07-01 19:07:02 +09001/*
2 * Copyright 2020-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.k8snode.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
22
23import java.util.HashSet;
24import java.util.Objects;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkArgument;
28
29/**
30 * Representation of a kubernetes host.
31 */
32public class DefaultK8sHost implements K8sHost {
33
34 private final IpAddress hostIp;
35 private final Set<String> nodeNames;
36 private final K8sHostState state;
Jian Li077b07e2020-09-01 16:55:25 +090037 private final Set<K8sTunnelBridge> tunBridges;
Jian Li019ce6a2020-09-09 10:23:21 +090038 private final Set<K8sRouterBridge> routerBridges;
Jian Lie2a04ce2020-07-01 19:07:02 +090039
40 private static final String NOT_NULL_MSG = "Host % cannot be null";
41
42 private static final String OVSDB = "ovsdb:";
43
44 /**
45 * A default constructor of kubernetes host.
46 *
47 * @param hostIp host IP address
48 * @param nodeNames node names
49 * @param state host state
Jian Li077b07e2020-09-01 16:55:25 +090050 * @param tunBridges a set of tunnel bridges
Jian Li019ce6a2020-09-09 10:23:21 +090051 * @param routerBridges a set of router bridges
Jian Lie2a04ce2020-07-01 19:07:02 +090052 */
53 protected DefaultK8sHost(IpAddress hostIp, Set<String> nodeNames,
Jian Li019ce6a2020-09-09 10:23:21 +090054 K8sHostState state, Set<K8sTunnelBridge> tunBridges,
55 Set<K8sRouterBridge> routerBridges) {
Jian Lie2a04ce2020-07-01 19:07:02 +090056 this.hostIp = hostIp;
57 this.nodeNames = nodeNames;
58 this.state = state;
Jian Li077b07e2020-09-01 16:55:25 +090059 this.tunBridges = tunBridges;
Jian Li019ce6a2020-09-09 10:23:21 +090060 this.routerBridges = routerBridges;
Jian Lie2a04ce2020-07-01 19:07:02 +090061 }
62
63 @Override
64 public IpAddress hostIp() {
65 return hostIp;
66 }
67
68 @Override
69 public Set<String> nodeNames() {
70 return ImmutableSet.copyOf(nodeNames);
71 }
72
73 @Override
74 public K8sHostState state() {
75 return state;
76 }
77
78 @Override
79 public DeviceId ovsdb() {
80 return DeviceId.deviceId(OVSDB + hostIp.toString());
81 }
82
83 @Override
Jian Li077b07e2020-09-01 16:55:25 +090084 public Set<K8sTunnelBridge> tunBridges() {
85 return ImmutableSet.copyOf(tunBridges);
86 }
87
88 @Override
Jian Li019ce6a2020-09-09 10:23:21 +090089 public Set<K8sRouterBridge> routerBridges() {
90 return ImmutableSet.copyOf(routerBridges);
91 }
92
93 @Override
Jian Lie2a04ce2020-07-01 19:07:02 +090094 public K8sHost updateState(K8sHostState newState) {
95 return new Builder()
96 .hostIp(hostIp)
97 .nodeNames(nodeNames)
98 .state(newState)
Jian Li077b07e2020-09-01 16:55:25 +090099 .tunBridges(tunBridges)
Jian Li019ce6a2020-09-09 10:23:21 +0900100 .routerBridges(routerBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +0900101 .build();
102 }
103
104 @Override
105 public K8sHost updateNodeNames(Set<String> nodeNames) {
106 return new Builder()
107 .hostIp(hostIp)
108 .nodeNames(nodeNames)
109 .state(state)
Jian Li077b07e2020-09-01 16:55:25 +0900110 .tunBridges(tunBridges)
Jian Li019ce6a2020-09-09 10:23:21 +0900111 .routerBridges(routerBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +0900112 .build();
113 }
114
115 @Override
116 public boolean equals(Object o) {
117 if (this == o) {
118 return true;
119 }
120 if (o == null || getClass() != o.getClass()) {
121 return false;
122 }
123 DefaultK8sHost that = (DefaultK8sHost) o;
124 return Objects.equals(hostIp, that.hostIp) &&
125 Objects.equals(nodeNames, that.nodeNames) &&
Jian Li077b07e2020-09-01 16:55:25 +0900126 state == that.state &&
Jian Li019ce6a2020-09-09 10:23:21 +0900127 Objects.equals(tunBridges, that.tunBridges) &&
128 Objects.equals(routerBridges, that.routerBridges);
Jian Lie2a04ce2020-07-01 19:07:02 +0900129 }
130
131 @Override
132 public int hashCode() {
Jian Li019ce6a2020-09-09 10:23:21 +0900133 return Objects.hash(hostIp, nodeNames, state, tunBridges, routerBridges);
Jian Lie2a04ce2020-07-01 19:07:02 +0900134 }
135
136 @Override
137 public String toString() {
138 return MoreObjects.toStringHelper(this)
139 .add("hostIp", hostIp)
140 .add("nodeNames", nodeNames)
141 .add("state", state)
Jian Li077b07e2020-09-01 16:55:25 +0900142 .add("tunBridges", tunBridges)
Jian Li019ce6a2020-09-09 10:23:21 +0900143 .add("routerBridges", routerBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +0900144 .toString();
145 }
146
147 /**
148 * Returns new builder instance.
149 *
150 * @return kubernetes host builder
151 */
152 public static Builder builder() {
153 return new Builder();
154 }
155
156 public static final class Builder implements K8sHost.Builder {
157
158 private IpAddress hostIp;
159 private Set<String> nodeNames;
160 private K8sHostState state;
Jian Li077b07e2020-09-01 16:55:25 +0900161 private Set<K8sTunnelBridge> tunBridges;
Jian Li019ce6a2020-09-09 10:23:21 +0900162 private Set<K8sRouterBridge> routerBridges;
Jian Lie2a04ce2020-07-01 19:07:02 +0900163
164 // private constructor not intended to use from external
165 private Builder() {
166 }
167
168 @Override
169 public K8sHost build() {
170 checkArgument(hostIp != null, NOT_NULL_MSG, "hostIp");
171 checkArgument(state != null, NOT_NULL_MSG, "state");
172
173 if (nodeNames == null) {
174 nodeNames = new HashSet<>();
175 }
176
Jian Li077b07e2020-09-01 16:55:25 +0900177 if (tunBridges == null) {
178 tunBridges = new HashSet<>();
179 }
180
Jian Li019ce6a2020-09-09 10:23:21 +0900181 if (routerBridges == null) {
182 routerBridges = new HashSet<>();
183 }
184
185 return new DefaultK8sHost(hostIp, nodeNames, state, tunBridges, routerBridges);
Jian Lie2a04ce2020-07-01 19:07:02 +0900186 }
187
188 @Override
189 public Builder hostIp(IpAddress hostIp) {
190 this.hostIp = hostIp;
191 return this;
192 }
193
194 @Override
195 public Builder nodeNames(Set<String> nodeNames) {
196 this.nodeNames = nodeNames;
197 return this;
198 }
199
200 @Override
201 public Builder state(K8sHostState state) {
202 this.state = state;
203 return this;
204 }
Jian Li077b07e2020-09-01 16:55:25 +0900205
206 @Override
207 public Builder tunBridges(Set<K8sTunnelBridge> tunBridges) {
208 this.tunBridges = tunBridges;
209 return this;
210 }
Jian Li019ce6a2020-09-09 10:23:21 +0900211
212 @Override
213 public Builder routerBridges(Set<K8sRouterBridge> routerBridges) {
214 this.routerBridges = routerBridges;
215 return this;
216 }
Jian Lie2a04ce2020-07-01 19:07:02 +0900217 }
218}