blob: 7cc93a9ce2785874dbe93ee17be70d74d456132f [file] [log] [blame]
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +09001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +09003 *
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.opencord.cordvtn.impl;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Strings;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Sets;
22import org.onlab.packet.TpPort;
23import org.onosproject.net.DeviceId;
24import org.opencord.cordvtn.api.net.CidrAddr;
25import org.opencord.cordvtn.api.node.CordVtnNode;
26import org.opencord.cordvtn.api.node.CordVtnNodeState;
27import org.opencord.cordvtn.api.node.SshAccessInfo;
28
29import java.util.Objects;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34import static org.opencord.cordvtn.api.Constants.DEFAULT_OVSDB_PORT;
35import static org.opencord.cordvtn.api.Constants.DEFAULT_TUNNEL;
36
37/**
38 * Representation of a compute infrastructure node for CORD VTN service.
39 */
40public final class DefaultCordVtnNode implements CordVtnNode {
41
42 private final String hostname;
43 private final CidrAddr hostMgmtIp;
44 private final CidrAddr localMgmtIp;
45 private final CidrAddr dataIp;
46 private final DeviceId integrationBridgeId;
47 private final String dataIface;
48 private final String hostMgmtIface;
49 private final TpPort ovsdbPort;
50 private final SshAccessInfo sshInfo;
51 private final CordVtnNodeState state;
52
53 private DefaultCordVtnNode(String hostname,
54 CidrAddr hostMgmtIp,
55 CidrAddr localMgmtIp,
56 CidrAddr dataIp,
57 DeviceId integrationBridgeId,
58 String dataIface,
59 String hostMgmtIface,
60 TpPort ovsdbPort,
61 SshAccessInfo sshInfo,
62 CordVtnNodeState state) {
63 this.hostname = hostname;
64 this.hostMgmtIp = hostMgmtIp;
65 this.localMgmtIp = localMgmtIp;
66 this.dataIp = dataIp;
67 this.integrationBridgeId = integrationBridgeId;
68 this.dataIface = dataIface;
69 this.hostMgmtIface = hostMgmtIface;
70 this.ovsdbPort = ovsdbPort;
71 this.sshInfo = sshInfo;
72 this.state = state;
73 }
74
75 /**
76 * Returns cordvtn node with the new state.
77 *
78 * @param node cordvtn node
79 * @param state cordvtn node state
80 * @return cordvtn node
81 */
82 public static CordVtnNode updatedState(CordVtnNode node, CordVtnNodeState state) {
83 return new DefaultCordVtnNode(node.hostname(),
84 node.hostManagementIp(),
85 node.localManagementIp(),
86 node.dataIp(),
87 node.integrationBridgeId(),
88 node.dataInterface(),
89 node.hostManagementInterface(),
90 node.ovsdbPort(),
91 node.sshInfo(),
92 state);
93 }
94
95 @Override
96 public String hostname() {
97 return this.hostname;
98 }
99
100 @Override
101 public CidrAddr hostManagementIp() {
102 return this.hostMgmtIp;
103 }
104
105 @Override
106 public CidrAddr localManagementIp() {
107 return this.localMgmtIp;
108 }
109
110 @Override
111 public CidrAddr dataIp() {
112 return this.dataIp;
113 }
114
115 @Override
116 public DeviceId integrationBridgeId() {
117 return this.integrationBridgeId;
118 }
119
120 @Override
121 public String dataInterface() {
122 return this.dataIface;
123 }
124
125 @Override
126 public String hostManagementInterface() {
127 return this.hostMgmtIface;
128 }
129
130 @Override
131 public TpPort ovsdbPort() {
132 return this.ovsdbPort;
133 }
134
135 @Override
136 public SshAccessInfo sshInfo() {
137 return this.sshInfo;
138 }
139
140 @Override
141 public CordVtnNodeState state() {
142 return this.state;
143 }
144
145 @Override
146 public DeviceId ovsdbId() {
147 return DeviceId.deviceId("ovsdb:" + this.hostMgmtIp.ip().toString());
148 }
149
150 @Override
151 public Set<String> systemInterfaces() {
152 Set<String> ifaces = Sets.newHashSet(DEFAULT_TUNNEL, dataIface);
153 if (hostMgmtIface != null) {
154 ifaces.add(hostMgmtIface);
155 }
156 return ImmutableSet.copyOf(ifaces);
157 }
158
159 @Override
160 public boolean equals(Object obj) {
161 if (this == obj) {
162 return true;
163 }
164
165 if (obj instanceof DefaultCordVtnNode) {
166 DefaultCordVtnNode that = (DefaultCordVtnNode) obj;
167 if (Objects.equals(hostname, that.hostname) &&
168 Objects.equals(hostMgmtIp, that.hostMgmtIp) &&
169 Objects.equals(localMgmtIp, that.localMgmtIp) &&
170 Objects.equals(dataIp, that.dataIp) &&
171 Objects.equals(integrationBridgeId, that.integrationBridgeId) &&
172 Objects.equals(dataIface, that.dataIface) &&
173 Objects.equals(hostMgmtIface, that.hostMgmtIface) &&
174 Objects.equals(ovsdbPort, that.ovsdbPort) &&
175 Objects.equals(sshInfo, that.sshInfo)) {
176 return true;
177 }
178 }
179 return false;
180 }
181
182 @Override
183 public int hashCode() {
184 return Objects.hash(hostname,
185 hostMgmtIp,
186 localMgmtIp,
187 dataIp,
188 integrationBridgeId,
189 dataIface,
190 hostMgmtIface,
191 ovsdbPort,
192 sshInfo);
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .add("hostname", hostname)
199 .add("hostMgmtIp", hostMgmtIp)
200 .add("localMgmtIp", localMgmtIp)
201 .add("dataIp", dataIp)
202 .add("integrationBridgeId", integrationBridgeId)
203 .add("dataIface", dataIface)
204 .add("hostMgmtIface", hostMgmtIface)
205 .add("ovsdbPort", ovsdbPort)
206 .add("sshInfo", sshInfo)
207 .add("state", state)
208 .toString();
209 }
210
211 /**
212 * Returns new node builder instance.
213 *
214 * @return cordvtn node builder
215 */
216 public static Builder builder() {
217 return new Builder();
218 }
219
220 public static final class Builder implements CordVtnNode.Builder {
221
222 private String hostname;
223 private CidrAddr hostMgmtIp;
224 private CidrAddr localMgmtIp;
225 private CidrAddr dataIp;
226 private DeviceId integrationBridgeId;
227 private String dataIface;
228 private String hostMgmtIface;
229 private TpPort ovsdbPort = TpPort.tpPort(DEFAULT_OVSDB_PORT);
230 private SshAccessInfo sshInfo;
231 private CordVtnNodeState state = CordVtnNodeState.INIT;
232
233 private Builder() {
234 }
235
236 @Override
237 public CordVtnNode build() {
238 checkArgument(!Strings.isNullOrEmpty(hostname), "Hostname cannot be null");
239 checkNotNull(hostMgmtIp, "Host management IP address cannot be null");
240 checkNotNull(localMgmtIp, "Local management IP address cannot be null");
241 checkNotNull(dataIp, "Data IP address cannot be null");
242 checkNotNull(integrationBridgeId, "Integration bridge ID cannot be null");
243 checkArgument(!Strings.isNullOrEmpty(dataIface), "Data interface cannot be null");
244 if (hostMgmtIface != null) {
245 checkArgument(!Strings.isNullOrEmpty(hostMgmtIface),
246 "Host management interface cannot be empty string");
247 }
248 checkNotNull(sshInfo, "SSH access information cannot be null");
249 checkNotNull(state, "Node state cannot be null");
250
251 return new DefaultCordVtnNode(hostname,
252 hostMgmtIp,
253 localMgmtIp,
254 dataIp,
255 integrationBridgeId,
256 dataIface,
257 hostMgmtIface,
258 ovsdbPort,
259 sshInfo, state);
260 }
261
262 @Override
263 public Builder hostname(String hostname) {
264 this.hostname = hostname;
265 return this;
266 }
267
268 @Override
269 public Builder hostManagementIp(CidrAddr hostMgmtIp) {
270 this.hostMgmtIp = hostMgmtIp;
271 return this;
272 }
273
274 @Override
275 public Builder localManagementIp(CidrAddr localMgmtIp) {
276 this.localMgmtIp = localMgmtIp;
277 return this;
278 }
279
280 @Override
281 public Builder dataIp(CidrAddr dataIp) {
282 this.dataIp = dataIp;
283 return this;
284 }
285
286 @Override
287 public Builder integrationBridgeId(DeviceId deviceId) {
288 this.integrationBridgeId = deviceId;
289 return this;
290 }
291
292 @Override
293 public Builder dataInterface(String dataIface) {
294 this.dataIface = dataIface;
295 return this;
296 }
297
298 @Override
299 public Builder hostManagementInterface(String hostMgmtIface) {
300 this.hostMgmtIface = hostMgmtIface;
301 return this;
302 }
303
304 @Override
305 public Builder ovsdbPort(TpPort port) {
306 this.ovsdbPort = port;
307 return this;
308 }
309
310 @Override
311 public Builder sshInfo(SshAccessInfo sshInfo) {
312 this.sshInfo = sshInfo;
313 return this;
314 }
315
316 @Override
317 public Builder state(CordVtnNodeState state) {
318 this.state = state;
319 return this;
320 }
321 }
322}