blob: 0ff91a9925875295fa1a267f9efc3625b7821394 [file] [log] [blame]
Daniel Parka7d6e9f2016-01-18 17:54:14 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Daniel Parka7d6e9f2016-01-18 17:54:14 +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.onosproject.openstacknode;
17
18import com.google.common.base.MoreObjects;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070019import com.google.common.base.Strings;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090020import org.onlab.packet.IpAddress;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090021import org.onosproject.net.DeviceId;
Hyunsun Moon05d9b262016-07-03 18:38:44 -070022import org.onosproject.openstacknode.OpenstackNodeEvent.NodeState;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070023import org.onosproject.openstacknode.OpenstackNodeService.NodeType;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090024
25import java.util.Comparator;
26import java.util.Objects;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070027import java.util.Optional;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090028
Hyunsun Moon34bbe172016-06-28 19:18:40 -070029import static com.google.common.base.Preconditions.checkArgument;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090030import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070031import static org.onosproject.openstacknode.Constants.PATCH_INTG_BRIDGE;
Hyunsun Moon05d9b262016-07-03 18:38:44 -070032import static org.onosproject.openstacknode.OpenstackNodeEvent.NodeState.INIT;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090033
34/**
35 * Representation of a compute/gateway node for OpenstackSwitching/Routing service.
36 */
37public final class OpenstackNode {
38
Hyunsun Moon34bbe172016-06-28 19:18:40 -070039 private final String hostname;
40 private final NodeType type;
41 private final IpAddress managementIp;
daniel park917beb42017-03-16 18:07:15 +090042 private final Optional<IpAddress> dataIp;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070043 private final DeviceId integrationBridge;
44 private final Optional<DeviceId> routerBridge;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070045 private final Optional<String> uplink;
46 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
47 private final Optional<IpAddress> routerController;
daniel park917beb42017-03-16 18:07:15 +090048 private final Optional<String> vlanPort;
Hyunsun Moon05d9b262016-07-03 18:38:44 -070049 private final NodeState state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090050
51 public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
Hyunsun Moon34bbe172016-06-28 19:18:40 -070052 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
Daniel Parka7d6e9f2016-01-18 17:54:14 +090053
Hyunsun Moon34bbe172016-06-28 19:18:40 -070054 private OpenstackNode(String hostname,
55 NodeType type,
56 IpAddress managementIp,
daniel park917beb42017-03-16 18:07:15 +090057 Optional<IpAddress> dataIp,
Hyunsun Moon34bbe172016-06-28 19:18:40 -070058 DeviceId integrationBridge,
59 Optional<DeviceId> routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070060 Optional<String> uplink,
61 Optional<IpAddress> routerController,
daniel park917beb42017-03-16 18:07:15 +090062 Optional<String> vlanPort,
Hyunsun Moon05d9b262016-07-03 18:38:44 -070063 NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070064 this.hostname = hostname;
65 this.type = type;
66 this.managementIp = managementIp;
67 this.dataIp = dataIp;
68 this.integrationBridge = integrationBridge;
69 this.routerBridge = routerBridge;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070070 this.uplink = uplink;
71 this.routerController = routerController;
daniel park917beb42017-03-16 18:07:15 +090072 this.vlanPort = vlanPort;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070073 this.state = state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090074 }
75
76 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070077 * Returns OpenStack node with new state.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090078 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070079 * @param node openstack node
80 * @param state openstack node init state
81 * @return openstack node
Daniel Parka7d6e9f2016-01-18 17:54:14 +090082 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -070083 public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070084 return new OpenstackNode(node.hostname,
85 node.type,
86 node.managementIp,
87 node.dataIp,
88 node.integrationBridge,
89 node.routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070090 node.uplink,
91 node.routerController,
daniel park917beb42017-03-16 18:07:15 +090092 node.vlanPort,
Hyunsun Moon34bbe172016-06-28 19:18:40 -070093 state);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090094 }
95
96 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070097 * Returns hostname of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090098 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070099 * @return hostname
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900100 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700101 public String hostname() {
102 return hostname;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900103 }
104
105 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700106 * Returns the type of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900107 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700108 * @return node type
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900109 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700110 public NodeType type() {
111 return type;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900112 }
113
114 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700115 * Returns the management network IP address of the node.
116 *
117 * @return management network ip address
118 */
119 public IpAddress managementIp() {
120 return managementIp;
121 }
122
123 /**
124 * Returns the data network IP address of the node.
125 *
daniel park917beb42017-03-16 18:07:15 +0900126 * @return data network ip address; or empty value
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700127 */
daniel park917beb42017-03-16 18:07:15 +0900128 public Optional<IpAddress> dataIp() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700129 return dataIp;
130 }
131
132 /**
133 * Returns the integration bridge device ID.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900134 *
135 * @return device id
136 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700137 public DeviceId intBridge() {
138 return integrationBridge;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900139 }
140
141 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700142 * Returns the router bridge device ID.
143 * It returns valid value only if the node type is GATEWAY.
144 *
145 * @return device id; or empty device id
146 */
147 public Optional<DeviceId> routerBridge() {
148 return routerBridge;
149 }
150
151 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700152 * Returns the router bridge controller.
153 * It returns valid value only if the node type is GATEWAY.
154 *
155 * @return device id; or empty value
156 */
157 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
158 public Optional<IpAddress> routerController() {
159 return routerController;
160 }
161
162 /**
163 * Returns the uplink interface name.
164 * It returns valid value only if the node type is GATEWAY.
165 *
166 * @return uplink interface name; or empty value
167 */
168 public Optional<String> uplink() {
169 return uplink;
170 }
171
172 /**
daniel park917beb42017-03-16 18:07:15 +0900173 * Returns the vlan interface name.
174 *
175 * @return vlan interface name; or empty value
176 */
177 public Optional<String> vlanPort() {
178 return vlanPort;
179 }
180
181 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700182 * Returns the init state of the node.
183 *
184 * @return init state
185 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700186 public NodeState state() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700187 return state;
188 }
189
190 /**
191 * Returns the device ID of the OVSDB session of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900192 *
193 * @return device id
194 */
195 public DeviceId ovsdbId() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700196 return DeviceId.deviceId("ovsdb:" + managementIp.toString());
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900197 }
198
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700199 /**
200 * Returns the name of the port connected to the external network.
201 * It returns valid value only if the node is gateway node.
202 *
203 * @return external port name
204 */
205 public Optional<String> externalPortName() {
206 if (type == NodeType.GATEWAY) {
207 return Optional.of(PATCH_INTG_BRIDGE);
208 } else {
209 return Optional.empty();
210 }
211 }
212
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900213 @Override
214 public boolean equals(Object obj) {
215 if (this == obj) {
216 return true;
217 }
218
219 if (obj instanceof OpenstackNode) {
220 OpenstackNode that = (OpenstackNode) obj;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700221 if (Objects.equals(hostname, that.hostname) &&
222 Objects.equals(type, that.type) &&
223 Objects.equals(managementIp, that.managementIp) &&
224 Objects.equals(dataIp, that.dataIp) &&
225 Objects.equals(integrationBridge, that.integrationBridge) &&
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700226 Objects.equals(routerBridge, that.routerBridge) &&
227 Objects.equals(uplink, that.uplink) &&
daniel park917beb42017-03-16 18:07:15 +0900228 Objects.equals(routerController, that.routerController) &&
229 Objects.equals(vlanPort, that.vlanPort)) {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900230 return true;
231 }
232 }
233 return false;
234 }
235
236 @Override
237 public int hashCode() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700238 return Objects.hash(hostname,
239 type,
240 managementIp,
241 dataIp,
242 integrationBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700243 routerBridge,
244 uplink,
daniel park917beb42017-03-16 18:07:15 +0900245 routerController,
246 vlanPort);
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900247 }
248
249 @Override
250 public String toString() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700251 return MoreObjects.toStringHelper(getClass())
252 .add("hostname", hostname)
253 .add("type", type)
254 .add("managementIp", managementIp)
255 .add("dataIp", dataIp)
256 .add("integrationBridge", integrationBridge)
257 .add("routerBridge", routerBridge)
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700258 .add("uplink", uplink)
259 .add("routerController", routerController)
daniel park917beb42017-03-16 18:07:15 +0900260 .add("vlanport", vlanPort)
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700261 .add("state", state)
262 .toString();
263 }
264
265 /**
266 * Returns a new builder instance.
267 *
268 * @return openstack node builder
269 */
270 public static Builder builder() {
271 return new Builder();
272 }
273
274 /**
275 * Builder of OpenStack node entities.
276 */
277 public static final class Builder {
278 private String hostname;
279 private NodeType type;
280 private IpAddress managementIp;
daniel park917beb42017-03-16 18:07:15 +0900281 private Optional<IpAddress> dataIp = Optional.empty();
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700282 private DeviceId integrationBridge;
283 private Optional<DeviceId> routerBridge = Optional.empty();
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700284 private Optional<String> uplink = Optional.empty();
daniel park917beb42017-03-16 18:07:15 +0900285 private Optional<String> vlanPort = Optional.empty();
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700286 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
287 private Optional<IpAddress> routerController = Optional.empty();
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700288 private NodeState state = INIT;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700289
290 private Builder() {
291 }
292
293 public OpenstackNode build() {
294 checkArgument(!Strings.isNullOrEmpty(hostname));
295 checkNotNull(type);
296 checkNotNull(managementIp);
297 checkNotNull(dataIp);
298 checkNotNull(integrationBridge);
299 checkNotNull(routerBridge);
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700300 checkNotNull(uplink);
301 checkNotNull(routerController);
daniel park917beb42017-03-16 18:07:15 +0900302 checkNotNull(vlanPort);
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700303
304 if (type == NodeType.GATEWAY) {
305 checkArgument(routerBridge.isPresent());
306 checkArgument(uplink.isPresent());
307 checkArgument(routerController.isPresent());
308 }
309
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700310 return new OpenstackNode(hostname,
311 type,
312 managementIp,
313 dataIp,
314 integrationBridge,
315 routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700316 uplink,
317 routerController,
daniel park917beb42017-03-16 18:07:15 +0900318 vlanPort,
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700319 state);
320 }
321
322 /**
323 * Returns node builder with the hostname.
324 *
325 * @param hostname hostname
326 * @return openstack node builder
327 */
328 public Builder hostname(String hostname) {
329 this.hostname = hostname;
330 return this;
331 }
332
333 /**
334 * Returns node builder with the node type.
335 *
336 * @param type openstack node type
337 * @return openstack node builder
338 */
339 public Builder type(NodeType type) {
340 this.type = type;
341 return this;
342 }
343
344 /**
345 * Returns node builder with the management network IP address.
346 *
347 * @param managementIp management ip address
348 * @return openstack node builder
349 */
350 public Builder managementIp(IpAddress managementIp) {
351 this.managementIp = managementIp;
352 return this;
353 }
354
355 /**
356 * Returns node builder with the data network IP address.
357 *
358 * @param dataIp data network ip address
359 * @return openstack node builder
360 */
361 public Builder dataIp(IpAddress dataIp) {
daniel park917beb42017-03-16 18:07:15 +0900362 this.dataIp = Optional.ofNullable(dataIp);
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700363 return this;
364 }
365
366 /**
367 * Returns node builder with the integration bridge ID.
368 *
369 * @param integrationBridge integration bridge device id
370 * @return openstack node builder
371 */
372 public Builder integrationBridge(DeviceId integrationBridge) {
373 this.integrationBridge = integrationBridge;
374 return this;
375 }
376
377 /**
378 * Returns node builder with the router bridge ID.
379 *
380 * @param routerBridge router bridge device ID
381 * @return openstack node builder
382 */
383 public Builder routerBridge(DeviceId routerBridge) {
384 this.routerBridge = Optional.ofNullable(routerBridge);
385 return this;
386 }
387
388 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700389 * Returns node builder with the uplink interface name.
390 *
391 * @param uplink uplink interface name
392 * @return openstack node builder
393 */
394 public Builder uplink(String uplink) {
395 this.uplink = Optional.ofNullable(uplink);
396 return this;
397 }
398
399 /**
400 * Returns node builder with the router controller.
401 *
402 * @param routerController router contoller
403 * @return openstack node builder
404 */
405 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
406 public Builder routerController(IpAddress routerController) {
407 this.routerController = Optional.ofNullable(routerController);
408 return this;
409 }
410
411 /**
daniel park917beb42017-03-16 18:07:15 +0900412 * Returns node builder with the vlan interface name.
413 *
414 * @param vlanPort vlan interface name
415 * @return openstack node builder
416 */
417 public Builder vlanPort(String vlanPort) {
418 this.vlanPort = Optional.ofNullable(vlanPort);
419 return this;
420 }
421
422 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700423 * Returns node builder with the init state.
424 *
425 * @param state node init state
426 * @return openstack node builder
427 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700428 public Builder state(NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700429 this.state = state;
430 return this;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900431 }
432 }
433}
434