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 | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 31 | import static org.onosproject.openstacknode.OpenstackNodeEvent.NodeState.INIT; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Representation of a compute/gateway node for OpenstackSwitching/Routing service. |
| 35 | */ |
| 36 | public final class OpenstackNode { |
| 37 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 38 | 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 44 | private final NodeState state; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 45 | |
| 46 | public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR = |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 47 | (node1, node2) -> node1.hostname().compareTo(node2.hostname()); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 48 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 49 | private OpenstackNode(String hostname, |
| 50 | NodeType type, |
| 51 | IpAddress managementIp, |
| 52 | IpAddress dataIp, |
| 53 | DeviceId integrationBridge, |
| 54 | Optional<DeviceId> routerBridge, |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 55 | NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 56 | 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 Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 66 | * Returns OpenStack node with new state. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 67 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 68 | * @param node openstack node |
| 69 | * @param state openstack node init state |
| 70 | * @return openstack node |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 71 | */ |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 72 | public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 73 | return new OpenstackNode(node.hostname, |
| 74 | node.type, |
| 75 | node.managementIp, |
| 76 | node.dataIp, |
| 77 | node.integrationBridge, |
| 78 | node.routerBridge, |
| 79 | state); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 83 | * Returns hostname of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 84 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 85 | * @return hostname |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 86 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 87 | public String hostname() { |
| 88 | return hostname; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 92 | * Returns the type of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 93 | * |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 94 | * @return node type |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 95 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 96 | public NodeType type() { |
| 97 | return type; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 101 | * 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 Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 120 | * |
| 121 | * @return device id |
| 122 | */ |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 123 | public DeviceId intBridge() { |
| 124 | return integrationBridge; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 128 | * 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 142 | public NodeState state() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 143 | return state; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the device ID of the OVSDB session of the node. |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 148 | * |
| 149 | * @return device id |
| 150 | */ |
| 151 | public DeviceId ovsdbId() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 152 | return DeviceId.deviceId("ovsdb:" + managementIp.toString()); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 153 | } |
| 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 Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 163 | 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 Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public int hashCode() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 177 | return Objects.hash(hostname, |
| 178 | type, |
| 179 | managementIp, |
| 180 | dataIp, |
| 181 | integrationBridge, |
| 182 | routerBridge); |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String toString() { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 187 | 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 217 | private NodeState state = INIT; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 218 | |
| 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 Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 310 | public Builder state(NodeState state) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 311 | this.state = state; |
| 312 | return this; |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |