blob: 2d73a85f717b690995a0a8a1d682e86b8adb6921 [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;
42 private final IpAddress dataIp;
43 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;
Hyunsun Moon05d9b262016-07-03 18:38:44 -070048 private final NodeState state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090049
50 public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
Hyunsun Moon34bbe172016-06-28 19:18:40 -070051 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
Daniel Parka7d6e9f2016-01-18 17:54:14 +090052
Hyunsun Moon34bbe172016-06-28 19:18:40 -070053 private OpenstackNode(String hostname,
54 NodeType type,
55 IpAddress managementIp,
56 IpAddress dataIp,
57 DeviceId integrationBridge,
58 Optional<DeviceId> routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070059 Optional<String> uplink,
60 Optional<IpAddress> routerController,
Hyunsun Moon05d9b262016-07-03 18:38:44 -070061 NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070062 this.hostname = hostname;
63 this.type = type;
64 this.managementIp = managementIp;
65 this.dataIp = dataIp;
66 this.integrationBridge = integrationBridge;
67 this.routerBridge = routerBridge;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070068 this.uplink = uplink;
69 this.routerController = routerController;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070070 this.state = state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090071 }
72
73 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070074 * Returns OpenStack node with new state.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090075 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070076 * @param node openstack node
77 * @param state openstack node init state
78 * @return openstack node
Daniel Parka7d6e9f2016-01-18 17:54:14 +090079 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -070080 public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070081 return new OpenstackNode(node.hostname,
82 node.type,
83 node.managementIp,
84 node.dataIp,
85 node.integrationBridge,
86 node.routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070087 node.uplink,
88 node.routerController,
Hyunsun Moon34bbe172016-06-28 19:18:40 -070089 state);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090090 }
91
92 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070093 * Returns hostname of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090094 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070095 * @return hostname
Daniel Parka7d6e9f2016-01-18 17:54:14 +090096 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070097 public String hostname() {
98 return hostname;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090099 }
100
101 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700102 * Returns the type of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900103 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700104 * @return node type
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900105 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700106 public NodeType type() {
107 return type;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900108 }
109
110 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700111 * Returns the management network IP address of the node.
112 *
113 * @return management network ip address
114 */
115 public IpAddress managementIp() {
116 return managementIp;
117 }
118
119 /**
120 * Returns the data network IP address of the node.
121 *
122 * @return data network ip address
123 */
124 public IpAddress dataIp() {
125 return dataIp;
126 }
127
128 /**
129 * Returns the integration bridge device ID.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900130 *
131 * @return device id
132 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700133 public DeviceId intBridge() {
134 return integrationBridge;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900135 }
136
137 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700138 * Returns the router bridge device ID.
139 * It returns valid value only if the node type is GATEWAY.
140 *
141 * @return device id; or empty device id
142 */
143 public Optional<DeviceId> routerBridge() {
144 return routerBridge;
145 }
146
147 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700148 * Returns the router bridge controller.
149 * It returns valid value only if the node type is GATEWAY.
150 *
151 * @return device id; or empty value
152 */
153 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
154 public Optional<IpAddress> routerController() {
155 return routerController;
156 }
157
158 /**
159 * Returns the uplink interface name.
160 * It returns valid value only if the node type is GATEWAY.
161 *
162 * @return uplink interface name; or empty value
163 */
164 public Optional<String> uplink() {
165 return uplink;
166 }
167
168 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700169 * Returns the init state of the node.
170 *
171 * @return init state
172 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700173 public NodeState state() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700174 return state;
175 }
176
177 /**
178 * Returns the device ID of the OVSDB session of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900179 *
180 * @return device id
181 */
182 public DeviceId ovsdbId() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700183 return DeviceId.deviceId("ovsdb:" + managementIp.toString());
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900184 }
185
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700186 /**
187 * Returns the name of the port connected to the external network.
188 * It returns valid value only if the node is gateway node.
189 *
190 * @return external port name
191 */
192 public Optional<String> externalPortName() {
193 if (type == NodeType.GATEWAY) {
194 return Optional.of(PATCH_INTG_BRIDGE);
195 } else {
196 return Optional.empty();
197 }
198 }
199
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900200 @Override
201 public boolean equals(Object obj) {
202 if (this == obj) {
203 return true;
204 }
205
206 if (obj instanceof OpenstackNode) {
207 OpenstackNode that = (OpenstackNode) obj;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700208 if (Objects.equals(hostname, that.hostname) &&
209 Objects.equals(type, that.type) &&
210 Objects.equals(managementIp, that.managementIp) &&
211 Objects.equals(dataIp, that.dataIp) &&
212 Objects.equals(integrationBridge, that.integrationBridge) &&
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700213 Objects.equals(routerBridge, that.routerBridge) &&
214 Objects.equals(uplink, that.uplink) &&
215 Objects.equals(routerController, that.routerController)) {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900216 return true;
217 }
218 }
219 return false;
220 }
221
222 @Override
223 public int hashCode() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700224 return Objects.hash(hostname,
225 type,
226 managementIp,
227 dataIp,
228 integrationBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700229 routerBridge,
230 uplink,
231 routerController);
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900232 }
233
234 @Override
235 public String toString() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700236 return MoreObjects.toStringHelper(getClass())
237 .add("hostname", hostname)
238 .add("type", type)
239 .add("managementIp", managementIp)
240 .add("dataIp", dataIp)
241 .add("integrationBridge", integrationBridge)
242 .add("routerBridge", routerBridge)
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700243 .add("uplink", uplink)
244 .add("routerController", routerController)
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700245 .add("state", state)
246 .toString();
247 }
248
249 /**
250 * Returns a new builder instance.
251 *
252 * @return openstack node builder
253 */
254 public static Builder builder() {
255 return new Builder();
256 }
257
258 /**
259 * Builder of OpenStack node entities.
260 */
261 public static final class Builder {
262 private String hostname;
263 private NodeType type;
264 private IpAddress managementIp;
265 private IpAddress dataIp;
266 private DeviceId integrationBridge;
267 private Optional<DeviceId> routerBridge = Optional.empty();
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700268 private Optional<String> uplink = Optional.empty();
269 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
270 private Optional<IpAddress> routerController = Optional.empty();
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700271 private NodeState state = INIT;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700272
273 private Builder() {
274 }
275
276 public OpenstackNode build() {
277 checkArgument(!Strings.isNullOrEmpty(hostname));
278 checkNotNull(type);
279 checkNotNull(managementIp);
280 checkNotNull(dataIp);
281 checkNotNull(integrationBridge);
282 checkNotNull(routerBridge);
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700283 checkNotNull(uplink);
284 checkNotNull(routerController);
285
286 if (type == NodeType.GATEWAY) {
287 checkArgument(routerBridge.isPresent());
288 checkArgument(uplink.isPresent());
289 checkArgument(routerController.isPresent());
290 }
291
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700292 return new OpenstackNode(hostname,
293 type,
294 managementIp,
295 dataIp,
296 integrationBridge,
297 routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700298 uplink,
299 routerController,
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700300 state);
301 }
302
303 /**
304 * Returns node builder with the hostname.
305 *
306 * @param hostname hostname
307 * @return openstack node builder
308 */
309 public Builder hostname(String hostname) {
310 this.hostname = hostname;
311 return this;
312 }
313
314 /**
315 * Returns node builder with the node type.
316 *
317 * @param type openstack node type
318 * @return openstack node builder
319 */
320 public Builder type(NodeType type) {
321 this.type = type;
322 return this;
323 }
324
325 /**
326 * Returns node builder with the management network IP address.
327 *
328 * @param managementIp management ip address
329 * @return openstack node builder
330 */
331 public Builder managementIp(IpAddress managementIp) {
332 this.managementIp = managementIp;
333 return this;
334 }
335
336 /**
337 * Returns node builder with the data network IP address.
338 *
339 * @param dataIp data network ip address
340 * @return openstack node builder
341 */
342 public Builder dataIp(IpAddress dataIp) {
343 this.dataIp = dataIp;
344 return this;
345 }
346
347 /**
348 * Returns node builder with the integration bridge ID.
349 *
350 * @param integrationBridge integration bridge device id
351 * @return openstack node builder
352 */
353 public Builder integrationBridge(DeviceId integrationBridge) {
354 this.integrationBridge = integrationBridge;
355 return this;
356 }
357
358 /**
359 * Returns node builder with the router bridge ID.
360 *
361 * @param routerBridge router bridge device ID
362 * @return openstack node builder
363 */
364 public Builder routerBridge(DeviceId routerBridge) {
365 this.routerBridge = Optional.ofNullable(routerBridge);
366 return this;
367 }
368
369 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700370 * Returns node builder with the uplink interface name.
371 *
372 * @param uplink uplink interface name
373 * @return openstack node builder
374 */
375 public Builder uplink(String uplink) {
376 this.uplink = Optional.ofNullable(uplink);
377 return this;
378 }
379
380 /**
381 * Returns node builder with the router controller.
382 *
383 * @param routerController router contoller
384 * @return openstack node builder
385 */
386 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
387 public Builder routerController(IpAddress routerController) {
388 this.routerController = Optional.ofNullable(routerController);
389 return this;
390 }
391
392 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700393 * Returns node builder with the init state.
394 *
395 * @param state node init state
396 * @return openstack node builder
397 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700398 public Builder state(NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700399 this.state = state;
400 return this;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900401 }
402 }
403}
404