blob: 563dea760741175fa8183827692bc54f9d372e4e [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 Moon34bbe172016-06-28 19:18:40 -070022import org.onosproject.openstacknode.OpenstackNodeService.NodeType;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090023
24import java.util.Comparator;
25import java.util.Objects;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070026import java.util.Optional;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090027
Hyunsun Moon34bbe172016-06-28 19:18:40 -070028import static com.google.common.base.Preconditions.checkArgument;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090029import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Representation of a compute/gateway node for OpenstackSwitching/Routing service.
33 */
34public final class OpenstackNode {
35
Hyunsun Moon34bbe172016-06-28 19:18:40 -070036 private final String hostname;
37 private final NodeType type;
38 private final IpAddress managementIp;
39 private final IpAddress dataIp;
40 private final DeviceId integrationBridge;
41 private final Optional<DeviceId> routerBridge;
42 private final OpenstackNodeState state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090043
44 public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
Hyunsun Moon34bbe172016-06-28 19:18:40 -070045 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
Daniel Parka7d6e9f2016-01-18 17:54:14 +090046
Hyunsun Moon34bbe172016-06-28 19:18:40 -070047 private OpenstackNode(String hostname,
48 NodeType type,
49 IpAddress managementIp,
50 IpAddress dataIp,
51 DeviceId integrationBridge,
52 Optional<DeviceId> routerBridge,
53 OpenstackNodeState state) {
54 this.hostname = hostname;
55 this.type = type;
56 this.managementIp = managementIp;
57 this.dataIp = dataIp;
58 this.integrationBridge = integrationBridge;
59 this.routerBridge = routerBridge;
60 this.state = state;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090061 }
62
63 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070064 * Returns OpenStack node with new state.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090065 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070066 * @param node openstack node
67 * @param state openstack node init state
68 * @return openstack node
Daniel Parka7d6e9f2016-01-18 17:54:14 +090069 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070070 public static OpenstackNode getUpdatedNode(OpenstackNode node, OpenstackNodeState state) {
71 return new OpenstackNode(node.hostname,
72 node.type,
73 node.managementIp,
74 node.dataIp,
75 node.integrationBridge,
76 node.routerBridge,
77 state);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090078 }
79
80 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070081 * Returns hostname of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090082 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070083 * @return hostname
Daniel Parka7d6e9f2016-01-18 17:54:14 +090084 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070085 public String hostname() {
86 return hostname;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090087 }
88
89 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070090 * Returns the type of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +090091 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -070092 * @return node type
Daniel Parka7d6e9f2016-01-18 17:54:14 +090093 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070094 public NodeType type() {
95 return type;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090096 }
97
98 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -070099 * Returns the management network IP address of the node.
100 *
101 * @return management network ip address
102 */
103 public IpAddress managementIp() {
104 return managementIp;
105 }
106
107 /**
108 * Returns the data network IP address of the node.
109 *
110 * @return data network ip address
111 */
112 public IpAddress dataIp() {
113 return dataIp;
114 }
115
116 /**
117 * Returns the integration bridge device ID.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900118 *
119 * @return device id
120 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700121 public DeviceId intBridge() {
122 return integrationBridge;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900123 }
124
125 /**
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700126 * Returns the router bridge device ID.
127 * It returns valid value only if the node type is GATEWAY.
128 *
129 * @return device id; or empty device id
130 */
131 public Optional<DeviceId> routerBridge() {
132 return routerBridge;
133 }
134
135 /**
136 * Returns the init state of the node.
137 *
138 * @return init state
139 */
140 public OpenstackNodeState state() {
141 return state;
142 }
143
144 /**
145 * Returns the device ID of the OVSDB session of the node.
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900146 *
147 * @return device id
148 */
149 public DeviceId ovsdbId() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700150 return DeviceId.deviceId("ovsdb:" + managementIp.toString());
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900151 }
152
153 @Override
154 public boolean equals(Object obj) {
155 if (this == obj) {
156 return true;
157 }
158
159 if (obj instanceof OpenstackNode) {
160 OpenstackNode that = (OpenstackNode) obj;
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700161 if (Objects.equals(hostname, that.hostname) &&
162 Objects.equals(type, that.type) &&
163 Objects.equals(managementIp, that.managementIp) &&
164 Objects.equals(dataIp, that.dataIp) &&
165 Objects.equals(integrationBridge, that.integrationBridge) &&
166 Objects.equals(routerBridge, that.routerBridge)) {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900167 return true;
168 }
169 }
170 return false;
171 }
172
173 @Override
174 public int hashCode() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700175 return Objects.hash(hostname,
176 type,
177 managementIp,
178 dataIp,
179 integrationBridge,
180 routerBridge);
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900181 }
182
183 @Override
184 public String toString() {
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700185 return MoreObjects.toStringHelper(getClass())
186 .add("hostname", hostname)
187 .add("type", type)
188 .add("managementIp", managementIp)
189 .add("dataIp", dataIp)
190 .add("integrationBridge", integrationBridge)
191 .add("routerBridge", routerBridge)
192 .add("state", state)
193 .toString();
194 }
195
196 /**
197 * Returns a new builder instance.
198 *
199 * @return openstack node builder
200 */
201 public static Builder builder() {
202 return new Builder();
203 }
204
205 /**
206 * Builder of OpenStack node entities.
207 */
208 public static final class Builder {
209 private String hostname;
210 private NodeType type;
211 private IpAddress managementIp;
212 private IpAddress dataIp;
213 private DeviceId integrationBridge;
214 private Optional<DeviceId> routerBridge = Optional.empty();
215 private OpenstackNodeState state = OpenstackNodeState.noState();
216
217 private Builder() {
218 }
219
220 public OpenstackNode build() {
221 checkArgument(!Strings.isNullOrEmpty(hostname));
222 checkNotNull(type);
223 checkNotNull(managementIp);
224 checkNotNull(dataIp);
225 checkNotNull(integrationBridge);
226 checkNotNull(routerBridge);
227 return new OpenstackNode(hostname,
228 type,
229 managementIp,
230 dataIp,
231 integrationBridge,
232 routerBridge,
233 state);
234 }
235
236 /**
237 * Returns node builder with the hostname.
238 *
239 * @param hostname hostname
240 * @return openstack node builder
241 */
242 public Builder hostname(String hostname) {
243 this.hostname = hostname;
244 return this;
245 }
246
247 /**
248 * Returns node builder with the node type.
249 *
250 * @param type openstack node type
251 * @return openstack node builder
252 */
253 public Builder type(NodeType type) {
254 this.type = type;
255 return this;
256 }
257
258 /**
259 * Returns node builder with the management network IP address.
260 *
261 * @param managementIp management ip address
262 * @return openstack node builder
263 */
264 public Builder managementIp(IpAddress managementIp) {
265 this.managementIp = managementIp;
266 return this;
267 }
268
269 /**
270 * Returns node builder with the data network IP address.
271 *
272 * @param dataIp data network ip address
273 * @return openstack node builder
274 */
275 public Builder dataIp(IpAddress dataIp) {
276 this.dataIp = dataIp;
277 return this;
278 }
279
280 /**
281 * Returns node builder with the integration bridge ID.
282 *
283 * @param integrationBridge integration bridge device id
284 * @return openstack node builder
285 */
286 public Builder integrationBridge(DeviceId integrationBridge) {
287 this.integrationBridge = integrationBridge;
288 return this;
289 }
290
291 /**
292 * Returns node builder with the router bridge ID.
293 *
294 * @param routerBridge router bridge device ID
295 * @return openstack node builder
296 */
297 public Builder routerBridge(DeviceId routerBridge) {
298 this.routerBridge = Optional.ofNullable(routerBridge);
299 return this;
300 }
301
302 /**
303 * Returns node builder with the init state.
304 *
305 * @param state node init state
306 * @return openstack node builder
307 */
308 public Builder state(OpenstackNodeState state) {
309 this.state = state;
310 return this;
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900311 }
312 }
313}
314