Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.openstacknode; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 22 | import org.onosproject.openstacknode.OpenstackNodeEvent.NodeState; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 23 | import org.onosproject.openstacknode.OpenstackNodeService.NodeType; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.Comparator; |
| 26 | import java.util.Objects; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 27 | import java.util.Optional; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 28 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkArgument; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 31 | import static org.onosproject.openstacknode.Constants.PATCH_INTG_BRIDGE; |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 32 | import static org.onosproject.openstacknode.OpenstackNodeEvent.NodeState.INIT; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Representation of a compute/gateway node for OpenstackSwitching/Routing service. |
| 36 | */ |
| 37 | public final class OpenstackNode { |
| 38 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 39 | 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 45 | 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 48 | private final NodeState state; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 49 | |
| 50 | public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR = |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 51 | (node1, node2) -> node1.hostname().compareTo(node2.hostname()); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 52 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 53 | private OpenstackNode(String hostname, |
| 54 | NodeType type, |
| 55 | IpAddress managementIp, |
| 56 | IpAddress dataIp, |
| 57 | DeviceId integrationBridge, |
| 58 | Optional<DeviceId> routerBridge, |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 59 | Optional<String> uplink, |
| 60 | Optional<IpAddress> routerController, |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 61 | NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 62 | this.hostname = hostname; |
| 63 | this.type = type; |
| 64 | this.managementIp = managementIp; |
| 65 | this.dataIp = dataIp; |
| 66 | this.integrationBridge = integrationBridge; |
| 67 | this.routerBridge = routerBridge; |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 68 | this.uplink = uplink; |
| 69 | this.routerController = routerController; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 70 | this.state = state; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 74 | * Returns OpenStack node with new state. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 75 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 76 | * @param node openstack node |
| 77 | * @param state openstack node init state |
| 78 | * @return openstack node |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 79 | */ |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 80 | public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 81 | return new OpenstackNode(node.hostname, |
| 82 | node.type, |
| 83 | node.managementIp, |
| 84 | node.dataIp, |
| 85 | node.integrationBridge, |
| 86 | node.routerBridge, |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 87 | node.uplink, |
| 88 | node.routerController, |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 89 | state); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 93 | * Returns hostname of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 94 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 95 | * @return hostname |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 96 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 97 | public String hostname() { |
| 98 | return hostname; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 102 | * Returns the type of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 103 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 104 | * @return node type |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 105 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 106 | public NodeType type() { |
| 107 | return type; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 111 | * 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 Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 130 | * |
| 131 | * @return device id |
| 132 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 133 | public DeviceId intBridge() { |
| 134 | return integrationBridge; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 138 | * 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 148 | * 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 169 | * Returns the init state of the node. |
| 170 | * |
| 171 | * @return init state |
| 172 | */ |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 173 | public NodeState state() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 174 | return state; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the device ID of the OVSDB session of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 179 | * |
| 180 | * @return device id |
| 181 | */ |
| 182 | public DeviceId ovsdbId() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 183 | return DeviceId.deviceId("ovsdb:" + managementIp.toString()); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 184 | } |
| 185 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 186 | /** |
| 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 Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 200 | @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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 208 | 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 213 | Objects.equals(routerBridge, that.routerBridge) && |
| 214 | Objects.equals(uplink, that.uplink) && |
| 215 | Objects.equals(routerController, that.routerController)) { |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int hashCode() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 224 | return Objects.hash(hostname, |
| 225 | type, |
| 226 | managementIp, |
| 227 | dataIp, |
| 228 | integrationBridge, |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 229 | routerBridge, |
| 230 | uplink, |
| 231 | routerController); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public String toString() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 236 | 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 243 | .add("uplink", uplink) |
| 244 | .add("routerController", routerController) |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 245 | .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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 268 | 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 271 | private NodeState state = INIT; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 272 | |
| 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 283 | 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 292 | return new OpenstackNode(hostname, |
| 293 | type, |
| 294 | managementIp, |
| 295 | dataIp, |
| 296 | integrationBridge, |
| 297 | routerBridge, |
Hyunsun Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 298 | uplink, |
| 299 | routerController, |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 300 | 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 Moon | 052c71f | 2016-07-11 18:56:18 -0700 | [diff] [blame] | 370 | * 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 393 | * Returns node builder with the init state. |
| 394 | * |
| 395 | * @param state node init state |
| 396 | * @return openstack node builder |
| 397 | */ |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 398 | public Builder state(NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 399 | this.state = state; |
| 400 | return this; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |