blob: 4508bdd5cd8a3e3af7c70e5e837e397e055eb376 [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 Moon05d9b262016-07-03 18:38:44 -070044 private final NodeState state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090045
46 public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
Hyunsun Moon34bbe172016-06-28 19:18:40 -070047 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
Daniel Parka7d6e9f2016-01-18 17:54:14 +090048
Hyunsun Moon34bbe172016-06-28 19:18:40 -070049 private OpenstackNode(String hostname,
50 NodeType type,
51 IpAddress managementIp,
52 IpAddress dataIp,
53 DeviceId integrationBridge,
54 Optional<DeviceId> routerBridge,
Hyunsun Moon05d9b262016-07-03 18:38:44 -070055 NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070056 this.hostname = hostname;
57 this.type = type;
58 this.managementIp = managementIp;
59 this.dataIp = dataIp;
60 this.integrationBridge = integrationBridge;
61 this.routerBridge = routerBridge;
62 this.state = state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090063 }
64
65 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070066 * Returns OpenStack node with new state.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090067 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070068 * @param node openstack node
69 * @param state openstack node init state
70 * @return openstack node
Daniel Parka7d6e9f2016-01-18 17:54:14 +090071 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -070072 public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070073 return new OpenstackNode(node.hostname,
74 node.type,
75 node.managementIp,
76 node.dataIp,
77 node.integrationBridge,
78 node.routerBridge,
79 state);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090080 }
81
82 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070083 * Returns hostname of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090084 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070085 * @return hostname
Daniel Parka7d6e9f2016-01-18 17:54:14 +090086 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070087 public String hostname() {
88 return hostname;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090089 }
90
91 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070092 * Returns the type of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090093 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070094 * @return node type
Daniel Parka7d6e9f2016-01-18 17:54:14 +090095 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070096 public NodeType type() {
97 return type;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090098 }
99
100 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700101 * Returns the management network IP address of the node.
102 *
103 * @return management network ip address
104 */
105 public IpAddress managementIp() {
106 return managementIp;
107 }
108
109 /**
110 * Returns the data network IP address of the node.
111 *
112 * @return data network ip address
113 */
114 public IpAddress dataIp() {
115 return dataIp;
116 }
117
118 /**
119 * Returns the integration bridge device ID.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900120 *
121 * @return device id
122 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700123 public DeviceId intBridge() {
124 return integrationBridge;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900125 }
126
127 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700128 * Returns the router bridge device ID.
129 * It returns valid value only if the node type is GATEWAY.
130 *
131 * @return device id; or empty device id
132 */
133 public Optional<DeviceId> routerBridge() {
134 return routerBridge;
135 }
136
137 /**
138 * Returns the init state of the node.
139 *
140 * @return init state
141 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700142 public NodeState state() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700143 return state;
144 }
145
146 /**
147 * Returns the device ID of the OVSDB session of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900148 *
149 * @return device id
150 */
151 public DeviceId ovsdbId() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700152 return DeviceId.deviceId("ovsdb:" + managementIp.toString());
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900153 }
154
155 @Override
156 public boolean equals(Object obj) {
157 if (this == obj) {
158 return true;
159 }
160
161 if (obj instanceof OpenstackNode) {
162 OpenstackNode that = (OpenstackNode) obj;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700163 if (Objects.equals(hostname, that.hostname) &&
164 Objects.equals(type, that.type) &&
165 Objects.equals(managementIp, that.managementIp) &&
166 Objects.equals(dataIp, that.dataIp) &&
167 Objects.equals(integrationBridge, that.integrationBridge) &&
168 Objects.equals(routerBridge, that.routerBridge)) {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900169 return true;
170 }
171 }
172 return false;
173 }
174
175 @Override
176 public int hashCode() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700177 return Objects.hash(hostname,
178 type,
179 managementIp,
180 dataIp,
181 integrationBridge,
182 routerBridge);
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900183 }
184
185 @Override
186 public String toString() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700187 return MoreObjects.toStringHelper(getClass())
188 .add("hostname", hostname)
189 .add("type", type)
190 .add("managementIp", managementIp)
191 .add("dataIp", dataIp)
192 .add("integrationBridge", integrationBridge)
193 .add("routerBridge", routerBridge)
194 .add("state", state)
195 .toString();
196 }
197
198 /**
199 * Returns a new builder instance.
200 *
201 * @return openstack node builder
202 */
203 public static Builder builder() {
204 return new Builder();
205 }
206
207 /**
208 * Builder of OpenStack node entities.
209 */
210 public static final class Builder {
211 private String hostname;
212 private NodeType type;
213 private IpAddress managementIp;
214 private IpAddress dataIp;
215 private DeviceId integrationBridge;
216 private Optional<DeviceId> routerBridge = Optional.empty();
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700217 private NodeState state = INIT;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700218
219 private Builder() {
220 }
221
222 public OpenstackNode build() {
223 checkArgument(!Strings.isNullOrEmpty(hostname));
224 checkNotNull(type);
225 checkNotNull(managementIp);
226 checkNotNull(dataIp);
227 checkNotNull(integrationBridge);
228 checkNotNull(routerBridge);
229 return new OpenstackNode(hostname,
230 type,
231 managementIp,
232 dataIp,
233 integrationBridge,
234 routerBridge,
235 state);
236 }
237
238 /**
239 * Returns node builder with the hostname.
240 *
241 * @param hostname hostname
242 * @return openstack node builder
243 */
244 public Builder hostname(String hostname) {
245 this.hostname = hostname;
246 return this;
247 }
248
249 /**
250 * Returns node builder with the node type.
251 *
252 * @param type openstack node type
253 * @return openstack node builder
254 */
255 public Builder type(NodeType type) {
256 this.type = type;
257 return this;
258 }
259
260 /**
261 * Returns node builder with the management network IP address.
262 *
263 * @param managementIp management ip address
264 * @return openstack node builder
265 */
266 public Builder managementIp(IpAddress managementIp) {
267 this.managementIp = managementIp;
268 return this;
269 }
270
271 /**
272 * Returns node builder with the data network IP address.
273 *
274 * @param dataIp data network ip address
275 * @return openstack node builder
276 */
277 public Builder dataIp(IpAddress dataIp) {
278 this.dataIp = dataIp;
279 return this;
280 }
281
282 /**
283 * Returns node builder with the integration bridge ID.
284 *
285 * @param integrationBridge integration bridge device id
286 * @return openstack node builder
287 */
288 public Builder integrationBridge(DeviceId integrationBridge) {
289 this.integrationBridge = integrationBridge;
290 return this;
291 }
292
293 /**
294 * Returns node builder with the router bridge ID.
295 *
296 * @param routerBridge router bridge device ID
297 * @return openstack node builder
298 */
299 public Builder routerBridge(DeviceId routerBridge) {
300 this.routerBridge = Optional.ofNullable(routerBridge);
301 return this;
302 }
303
304 /**
305 * Returns node builder with the init state.
306 *
307 * @param state node init state
308 * @return openstack node builder
309 */
Hyunsun Moon05d9b262016-07-03 18:38:44 -0700310 public Builder state(NodeState state) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700311 this.state = state;
312 return this;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900313 }
314 }
315}
316