blob: e9619100780024f50519228a9a493747b0f4dffb [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 Lie2a04ce2020-07-01 19:07:02 +090038
39 private static final String NOT_NULL_MSG = "Host % cannot be null";
40
41 private static final String OVSDB = "ovsdb:";
42
43 /**
44 * A default constructor of kubernetes host.
45 *
46 * @param hostIp host IP address
47 * @param nodeNames node names
48 * @param state host state
Jian Li077b07e2020-09-01 16:55:25 +090049 * @param tunBridges a set of tunnel bridges
Jian Lie2a04ce2020-07-01 19:07:02 +090050 */
51 protected DefaultK8sHost(IpAddress hostIp, Set<String> nodeNames,
Jian Li077b07e2020-09-01 16:55:25 +090052 K8sHostState state, Set<K8sTunnelBridge> tunBridges) {
Jian Lie2a04ce2020-07-01 19:07:02 +090053 this.hostIp = hostIp;
54 this.nodeNames = nodeNames;
55 this.state = state;
Jian Li077b07e2020-09-01 16:55:25 +090056 this.tunBridges = tunBridges;
Jian Lie2a04ce2020-07-01 19:07:02 +090057 }
58
59 @Override
60 public IpAddress hostIp() {
61 return hostIp;
62 }
63
64 @Override
65 public Set<String> nodeNames() {
66 return ImmutableSet.copyOf(nodeNames);
67 }
68
69 @Override
70 public K8sHostState state() {
71 return state;
72 }
73
74 @Override
75 public DeviceId ovsdb() {
76 return DeviceId.deviceId(OVSDB + hostIp.toString());
77 }
78
79 @Override
Jian Li077b07e2020-09-01 16:55:25 +090080 public Set<K8sTunnelBridge> tunBridges() {
81 return ImmutableSet.copyOf(tunBridges);
82 }
83
84 @Override
Jian Lie2a04ce2020-07-01 19:07:02 +090085 public K8sHost updateState(K8sHostState newState) {
86 return new Builder()
87 .hostIp(hostIp)
88 .nodeNames(nodeNames)
89 .state(newState)
Jian Li077b07e2020-09-01 16:55:25 +090090 .tunBridges(tunBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +090091 .build();
92 }
93
94 @Override
95 public K8sHost updateNodeNames(Set<String> nodeNames) {
96 return new Builder()
97 .hostIp(hostIp)
98 .nodeNames(nodeNames)
99 .state(state)
Jian Li077b07e2020-09-01 16:55:25 +0900100 .tunBridges(tunBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +0900101 .build();
102 }
103
104 @Override
105 public boolean equals(Object o) {
106 if (this == o) {
107 return true;
108 }
109 if (o == null || getClass() != o.getClass()) {
110 return false;
111 }
112 DefaultK8sHost that = (DefaultK8sHost) o;
113 return Objects.equals(hostIp, that.hostIp) &&
114 Objects.equals(nodeNames, that.nodeNames) &&
Jian Li077b07e2020-09-01 16:55:25 +0900115 state == that.state &&
116 Objects.equals(tunBridges, that.tunBridges);
Jian Lie2a04ce2020-07-01 19:07:02 +0900117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hash(hostIp, nodeNames, state);
122 }
123
124 @Override
125 public String toString() {
126 return MoreObjects.toStringHelper(this)
127 .add("hostIp", hostIp)
128 .add("nodeNames", nodeNames)
129 .add("state", state)
Jian Li077b07e2020-09-01 16:55:25 +0900130 .add("tunBridges", tunBridges)
Jian Lie2a04ce2020-07-01 19:07:02 +0900131 .toString();
132 }
133
134 /**
135 * Returns new builder instance.
136 *
137 * @return kubernetes host builder
138 */
139 public static Builder builder() {
140 return new Builder();
141 }
142
143 public static final class Builder implements K8sHost.Builder {
144
145 private IpAddress hostIp;
146 private Set<String> nodeNames;
147 private K8sHostState state;
Jian Li077b07e2020-09-01 16:55:25 +0900148 private Set<K8sTunnelBridge> tunBridges;
Jian Lie2a04ce2020-07-01 19:07:02 +0900149
150 // private constructor not intended to use from external
151 private Builder() {
152 }
153
154 @Override
155 public K8sHost build() {
156 checkArgument(hostIp != null, NOT_NULL_MSG, "hostIp");
157 checkArgument(state != null, NOT_NULL_MSG, "state");
158
159 if (nodeNames == null) {
160 nodeNames = new HashSet<>();
161 }
162
Jian Li077b07e2020-09-01 16:55:25 +0900163 if (tunBridges == null) {
164 tunBridges = new HashSet<>();
165 }
166
167 return new DefaultK8sHost(hostIp, nodeNames, state, tunBridges);
Jian Lie2a04ce2020-07-01 19:07:02 +0900168 }
169
170 @Override
171 public Builder hostIp(IpAddress hostIp) {
172 this.hostIp = hostIp;
173 return this;
174 }
175
176 @Override
177 public Builder nodeNames(Set<String> nodeNames) {
178 this.nodeNames = nodeNames;
179 return this;
180 }
181
182 @Override
183 public Builder state(K8sHostState state) {
184 this.state = state;
185 return this;
186 }
Jian Li077b07e2020-09-01 16:55:25 +0900187
188 @Override
189 public Builder tunBridges(Set<K8sTunnelBridge> tunBridges) {
190 this.tunBridges = tunBridges;
191 return this;
192 }
Jian Lie2a04ce2020-07-01 19:07:02 +0900193 }
194}