OpenstackNode.java 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/*
 * Copyright 2016-present Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.openstacknode;

import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import org.onlab.packet.IpAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.openstacknode.OpenstackNodeEvent.NodeState;
import org.onosproject.openstacknode.OpenstackNodeService.NodeType;

import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.openstacknode.Constants.PATCH_INTG_BRIDGE;
import static org.onosproject.openstacknode.OpenstackNodeEvent.NodeState.INIT;

/**
 * Representation of a compute/gateway node for OpenstackSwitching/Routing service.
 */
public final class OpenstackNode {

    private final String hostname;
    private final NodeType type;
    private final IpAddress managementIp;
    private final IpAddress dataIp;
    private final DeviceId integrationBridge;
    private final Optional<DeviceId> routerBridge;
    private final Optional<String> uplink;
    // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
    private final Optional<IpAddress> routerController;
    private final NodeState state;

    public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR =
            (node1, node2) -> node1.hostname().compareTo(node2.hostname());

    private OpenstackNode(String hostname,
                          NodeType type,
                          IpAddress managementIp,
                          IpAddress dataIp,
                          DeviceId integrationBridge,
                          Optional<DeviceId> routerBridge,
                          Optional<String> uplink,
                          Optional<IpAddress> routerController,
                          NodeState state) {
        this.hostname = hostname;
        this.type = type;
        this.managementIp = managementIp;
        this.dataIp = dataIp;
        this.integrationBridge = integrationBridge;
        this.routerBridge = routerBridge;
        this.uplink = uplink;
        this.routerController = routerController;
        this.state = state;
    }

    /**
     * Returns OpenStack node with new state.
     *
     * @param node openstack node
     * @param state openstack node init state
     * @return openstack node
     */
    public static OpenstackNode getUpdatedNode(OpenstackNode node, NodeState state) {
        return new OpenstackNode(node.hostname,
                node.type,
                node.managementIp,
                node.dataIp,
                node.integrationBridge,
                node.routerBridge,
                node.uplink,
                node.routerController,
                state);
    }

    /**
     * Returns hostname of the node.
     *
     * @return hostname
     */
    public String hostname() {
        return hostname;
    }

    /**
     * Returns the type of the node.
     *
     * @return node type
     */
    public NodeType type() {
        return type;
    }

    /**
     * Returns the management network IP address of the node.
     *
     * @return management network ip address
     */
    public IpAddress managementIp() {
        return managementIp;
    }

    /**
     * Returns the data network IP address of the node.
     *
     * @return data network ip address
     */
    public IpAddress dataIp() {
        return dataIp;
    }

    /**
     * Returns the integration bridge device ID.
     *
     * @return device id
     */
    public DeviceId intBridge() {
        return integrationBridge;
    }

    /**
     * Returns the router bridge device ID.
     * It returns valid value only if the node type is GATEWAY.
     *
     * @return device id; or empty device id
     */
    public Optional<DeviceId> routerBridge() {
        return routerBridge;
    }

    /**
     * Returns the router bridge controller.
     * It returns valid value only if the node type is GATEWAY.
     *
     * @return device id; or empty value
     */
    // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
    public Optional<IpAddress> routerController() {
        return routerController;
    }

    /**
     * Returns the uplink interface name.
     * It returns valid value only if the node type is GATEWAY.
     *
     * @return uplink interface name; or empty value
     */
    public Optional<String> uplink() {
        return uplink;
    }

    /**
     * Returns the init state of the node.
     *
     * @return init state
     */
    public NodeState state() {
        return state;
    }

    /**
     * Returns the device ID of the OVSDB session of the node.
     *
     * @return device id
     */
    public DeviceId ovsdbId() {
        return DeviceId.deviceId("ovsdb:" + managementIp.toString());
    }

    /**
     * Returns the name of the port connected to the external network.
     * It returns valid value only if the node is gateway node.
     *
     * @return external port name
     */
    public Optional<String> externalPortName() {
        if (type == NodeType.GATEWAY) {
            return Optional.of(PATCH_INTG_BRIDGE);
        } else {
            return Optional.empty();
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj instanceof OpenstackNode) {
            OpenstackNode that = (OpenstackNode) obj;
            if (Objects.equals(hostname, that.hostname) &&
                    Objects.equals(type, that.type) &&
                    Objects.equals(managementIp, that.managementIp) &&
                    Objects.equals(dataIp, that.dataIp) &&
                    Objects.equals(integrationBridge, that.integrationBridge) &&
                    Objects.equals(routerBridge, that.routerBridge) &&
                    Objects.equals(uplink, that.uplink) &&
                    Objects.equals(routerController, that.routerController)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(hostname,
                type,
                managementIp,
                dataIp,
                integrationBridge,
                routerBridge,
                uplink,
                routerController);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(getClass())
                .add("hostname", hostname)
                .add("type", type)
                .add("managementIp", managementIp)
                .add("dataIp", dataIp)
                .add("integrationBridge", integrationBridge)
                .add("routerBridge", routerBridge)
                .add("uplink", uplink)
                .add("routerController", routerController)
                .add("state", state)
                .toString();
    }

    /**
     * Returns a new builder instance.
     *
     * @return openstack node builder
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder of OpenStack node entities.
     */
    public static final class Builder {
        private String hostname;
        private NodeType type;
        private IpAddress managementIp;
        private IpAddress dataIp;
        private DeviceId integrationBridge;
        private Optional<DeviceId> routerBridge = Optional.empty();
        private Optional<String> uplink = Optional.empty();
        // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
        private Optional<IpAddress> routerController = Optional.empty();
        private NodeState state = INIT;

        private Builder() {
        }

        public OpenstackNode build() {
            checkArgument(!Strings.isNullOrEmpty(hostname));
            checkNotNull(type);
            checkNotNull(managementIp);
            checkNotNull(dataIp);
            checkNotNull(integrationBridge);
            checkNotNull(routerBridge);
            checkNotNull(uplink);
            checkNotNull(routerController);

            if (type == NodeType.GATEWAY) {
                checkArgument(routerBridge.isPresent());
                checkArgument(uplink.isPresent());
                checkArgument(routerController.isPresent());
            }

            return new OpenstackNode(hostname,
                    type,
                    managementIp,
                    dataIp,
                    integrationBridge,
                    routerBridge,
                    uplink,
                    routerController,
                    state);
        }

        /**
         * Returns node builder with the hostname.
         *
         * @param hostname hostname
         * @return openstack node builder
         */
        public Builder hostname(String hostname) {
            this.hostname = hostname;
            return this;
        }

        /**
         * Returns node builder with the node type.
         *
         * @param type openstack node type
         * @return openstack node builder
         */
        public Builder type(NodeType type) {
            this.type = type;
            return this;
        }

        /**
         * Returns node builder with the management network IP address.
         *
         * @param managementIp management ip address
         * @return openstack node builder
         */
        public Builder managementIp(IpAddress managementIp) {
            this.managementIp = managementIp;
            return this;
        }

        /**
         * Returns node builder with the data network IP address.
         *
         * @param dataIp data network ip address
         * @return openstack node builder
         */
        public Builder dataIp(IpAddress dataIp) {
            this.dataIp = dataIp;
            return this;
        }

        /**
         * Returns node builder with the integration bridge ID.
         *
         * @param integrationBridge integration bridge device id
         * @return openstack node builder
         */
        public Builder integrationBridge(DeviceId integrationBridge) {
            this.integrationBridge = integrationBridge;
            return this;
        }

        /**
         * Returns node builder with the router bridge ID.
         *
         * @param routerBridge router bridge device ID
         * @return openstack node builder
         */
        public Builder routerBridge(DeviceId routerBridge) {
            this.routerBridge = Optional.ofNullable(routerBridge);
            return this;
        }

        /**
         * Returns node builder with the uplink interface name.
         *
         * @param uplink uplink interface name
         * @return openstack node builder
         */
        public Builder uplink(String uplink) {
            this.uplink = Optional.ofNullable(uplink);
            return this;
        }

        /**
         * Returns node builder with the router controller.
         *
         * @param routerController router contoller
         * @return openstack node builder
         */
        // TODO remove this when we use single ONOS cluster for both openstackNode and vRouter
        public Builder routerController(IpAddress routerController) {
            this.routerController = Optional.ofNullable(routerController);
            return this;
        }

        /**
         * Returns node builder with the init state.
         *
         * @param state node init state
         * @return openstack node builder
         */
        public Builder state(NodeState state) {
            this.state = state;
            return this;
        }
    }
}