blob: 42edd72e4376efbc55f6280d5b5b23faa637896c [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 Moon05d9b262016-07-03 18:38:44 -070031import static org.onosproject.openstacknode.OpenstackNodeEvent.NodeState.INIT;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090032
33/**
34 * Representation of a compute/gateway node for OpenstackSwitching/Routing service.
35 */
36public final class OpenstackNode {
37
Hyunsun Moon34bbe172016-06-28 19:18:40 -070038 private final String hostname;
39 private final NodeType type;
40 private final IpAddress managementIp;
41 private final IpAddress dataIp;
42 private final DeviceId integrationBridge;
43 private final Optional<DeviceId> routerBridge;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070044 private final Optional<String> uplink;
45 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
46 private final Optional<IpAddress> routerController;
Hyunsun Moon05d9b262016-07-03 18:38:44 -070047 private final NodeState state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090048
49 public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
Hyunsun Moon34bbe172016-06-28 19:18:40 -070050 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
Daniel Parka7d6e9f2016-01-18 17:54:14 +090051
Hyunsun Moon34bbe172016-06-28 19:18:40 -070052 private OpenstackNode(String hostname,
53 NodeType type,
54 IpAddress managementIp,
55 IpAddress dataIp,
56 DeviceId integrationBridge,
57 Optional<DeviceId> routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070058 Optional<String> uplink,
59 Optional<IpAddress> routerController,
Hyunsun Moon05d9b262016-07-03 18:38:44 -070060 NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070061 this.hostname = hostname;
62 this.type = type;
63 this.managementIp = managementIp;
64 this.dataIp = dataIp;
65 this.integrationBridge = integrationBridge;
66 this.routerBridge = routerBridge;
Hyunsun Moon052c71f2016-07-11 18:56:18 -070067 this.uplink = uplink;
68 this.routerController = routerController;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070069 this.state = state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090070 }
71
72 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070073 * Returns OpenStack node with new state.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090074 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070075 * @param node openstack node
76 * @param state openstack node init state
77 * @return openstack node
Daniel Parka7d6e9f2016-01-18 17:54:14 +090078 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -070079 public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070080 return new OpenstackNode(node.hostname,
81 node.type,
82 node.managementIp,
83 node.dataIp,
84 node.integrationBridge,
85 node.routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070086 node.uplink,
87 node.routerController,
Hyunsun Moon34bbe172016-06-28 19:18:40 -070088 state);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090089 }
90
91 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070092 * Returns hostname of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090093 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070094 * @return hostname
Daniel Parka7d6e9f2016-01-18 17:54:14 +090095 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070096 public String hostname() {
97 return hostname;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090098 }
99
100 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700101 * Returns the type of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900102 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700103 * @return node type
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900104 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700105 public NodeType type() {
106 return type;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900107 }
108
109 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700110 * Returns the management network IP address of the node.
111 *
112 * @return management network ip address
113 */
114 public IpAddress managementIp() {
115 return managementIp;
116 }
117
118 /**
119 * Returns the data network IP address of the node.
120 *
121 * @return data network ip address
122 */
123 public IpAddress dataIp() {
124 return dataIp;
125 }
126
127 /**
128 * Returns the integration bridge device ID.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900129 *
130 * @return device id
131 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700132 public DeviceId intBridge() {
133 return integrationBridge;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900134 }
135
136 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700137 * Returns the router bridge device ID.
138 * It returns valid value only if the node type is GATEWAY.
139 *
140 * @return device id; or empty device id
141 */
142 public Optional<DeviceId> routerBridge() {
143 return routerBridge;
144 }
145
146 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700147 * Returns the router bridge controller.
148 * It returns valid value only if the node type is GATEWAY.
149 *
150 * @return device id; or empty value
151 */
152 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
153 public Optional<IpAddress> routerController() {
154 return routerController;
155 }
156
157 /**
158 * Returns the uplink interface name.
159 * It returns valid value only if the node type is GATEWAY.
160 *
161 * @return uplink interface name; or empty value
162 */
163 public Optional<String> uplink() {
164 return uplink;
165 }
166
167 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700168 * Returns the init state of the node.
169 *
170 * @return init state
171 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700172 public NodeState state() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700173 return state;
174 }
175
176 /**
177 * Returns the device ID of the OVSDB session of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900178 *
179 * @return device id
180 */
181 public DeviceId ovsdbId() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700182 return DeviceId.deviceId("ovsdb:" + managementIp.toString());
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900183 }
184
185 @Override
186 public boolean equals(Object obj) {
187 if (this == obj) {
188 return true;
189 }
190
191 if (obj instanceof OpenstackNode) {
192 OpenstackNode that = (OpenstackNode) obj;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700193 if (Objects.equals(hostname, that.hostname) &&
194 Objects.equals(type, that.type) &&
195 Objects.equals(managementIp, that.managementIp) &&
196 Objects.equals(dataIp, that.dataIp) &&
197 Objects.equals(integrationBridge, that.integrationBridge) &&
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700198 Objects.equals(routerBridge, that.routerBridge) &&
199 Objects.equals(uplink, that.uplink) &&
200 Objects.equals(routerController, that.routerController)) {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900201 return true;
202 }
203 }
204 return false;
205 }
206
207 @Override
208 public int hashCode() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700209 return Objects.hash(hostname,
210 type,
211 managementIp,
212 dataIp,
213 integrationBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700214 routerBridge,
215 uplink,
216 routerController);
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900217 }
218
219 @Override
220 public String toString() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700221 return MoreObjects.toStringHelper(getClass())
222 .add("hostname", hostname)
223 .add("type", type)
224 .add("managementIp", managementIp)
225 .add("dataIp", dataIp)
226 .add("integrationBridge", integrationBridge)
227 .add("routerBridge", routerBridge)
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700228 .add("uplink", uplink)
229 .add("routerController", routerController)
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700230 .add("state", state)
231 .toString();
232 }
233
234 /**
235 * Returns a new builder instance.
236 *
237 * @return openstack node builder
238 */
239 public static Builder builder() {
240 return new Builder();
241 }
242
243 /**
244 * Builder of OpenStack node entities.
245 */
246 public static final class Builder {
247 private String hostname;
248 private NodeType type;
249 private IpAddress managementIp;
250 private IpAddress dataIp;
251 private DeviceId integrationBridge;
252 private Optional<DeviceId> routerBridge = Optional.empty();
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700253 private Optional<String> uplink = Optional.empty();
254 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
255 private Optional<IpAddress> routerController = Optional.empty();
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700256 private NodeState state = INIT;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700257
258 private Builder() {
259 }
260
261 public OpenstackNode build() {
262 checkArgument(!Strings.isNullOrEmpty(hostname));
263 checkNotNull(type);
264 checkNotNull(managementIp);
265 checkNotNull(dataIp);
266 checkNotNull(integrationBridge);
267 checkNotNull(routerBridge);
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700268 checkNotNull(uplink);
269 checkNotNull(routerController);
270
271 if (type == NodeType.GATEWAY) {
272 checkArgument(routerBridge.isPresent());
273 checkArgument(uplink.isPresent());
274 checkArgument(routerController.isPresent());
275 }
276
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700277 return new OpenstackNode(hostname,
278 type,
279 managementIp,
280 dataIp,
281 integrationBridge,
282 routerBridge,
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700283 uplink,
284 routerController,
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700285 state);
286 }
287
288 /**
289 * Returns node builder with the hostname.
290 *
291 * @param hostname hostname
292 * @return openstack node builder
293 */
294 public Builder hostname(String hostname) {
295 this.hostname = hostname;
296 return this;
297 }
298
299 /**
300 * Returns node builder with the node type.
301 *
302 * @param type openstack node type
303 * @return openstack node builder
304 */
305 public Builder type(NodeType type) {
306 this.type = type;
307 return this;
308 }
309
310 /**
311 * Returns node builder with the management network IP address.
312 *
313 * @param managementIp management ip address
314 * @return openstack node builder
315 */
316 public Builder managementIp(IpAddress managementIp) {
317 this.managementIp = managementIp;
318 return this;
319 }
320
321 /**
322 * Returns node builder with the data network IP address.
323 *
324 * @param dataIp data network ip address
325 * @return openstack node builder
326 */
327 public Builder dataIp(IpAddress dataIp) {
328 this.dataIp = dataIp;
329 return this;
330 }
331
332 /**
333 * Returns node builder with the integration bridge ID.
334 *
335 * @param integrationBridge integration bridge device id
336 * @return openstack node builder
337 */
338 public Builder integrationBridge(DeviceId integrationBridge) {
339 this.integrationBridge = integrationBridge;
340 return this;
341 }
342
343 /**
344 * Returns node builder with the router bridge ID.
345 *
346 * @param routerBridge router bridge device ID
347 * @return openstack node builder
348 */
349 public Builder routerBridge(DeviceId routerBridge) {
350 this.routerBridge = Optional.ofNullable(routerBridge);
351 return this;
352 }
353
354 /**
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700355 * Returns node builder with the uplink interface name.
356 *
357 * @param uplink uplink interface name
358 * @return openstack node builder
359 */
360 public Builder uplink(String uplink) {
361 this.uplink = Optional.ofNullable(uplink);
362 return this;
363 }
364
365 /**
366 * Returns node builder with the router controller.
367 *
368 * @param routerController router contoller
369 * @return openstack node builder
370 */
371 // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
372 public Builder routerController(IpAddress routerController) {
373 this.routerController = Optional.ofNullable(routerController);
374 return this;
375 }
376
377 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700378 * Returns node builder with the init state.
379 *
380 * @param state node init state
381 * @return openstack node builder
382 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700383 public Builder state(NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700384 this.state = state;
385 return this;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900386 }
387 }
388}
389