All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.eureka2.registry.datacenter.AwsDataCenterInfo Maven / Gradle / Ivy

There is a newer version: 2.0.0-DP4
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * 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 com.netflix.eureka2.registry.datacenter;

import com.netflix.eureka2.registry.DataCenterInfo;
import com.netflix.eureka2.registry.NetworkAddress;

import java.util.ArrayList;
import java.util.List;

import static com.netflix.eureka2.registry.NetworkAddress.NetworkAddressBuilder.aNetworkAddress;
import static com.netflix.eureka2.registry.NetworkAddress.PRIVATE_ADDRESS;
import static com.netflix.eureka2.registry.NetworkAddress.PUBLIC_ADDRESS;
import static com.netflix.eureka2.registry.NetworkAddress.ProtocolType;

/**
 * This class represents a location of a server in AWS datacenter.
 *
 * @author Tomasz Bak
 */
public class AwsDataCenterInfo extends DataCenterInfo {

    private final String name;
    private final String region;
    private final String zone;
    private final String placementGroup;
    private final String amiId;
    private final String instanceId;
    private final String instanceType;
    private final NetworkAddress publicAddress;
    private final NetworkAddress privateAddress;

    // For object creation via reflection.
    private AwsDataCenterInfo() {
        name = region = zone = placementGroup = amiId = instanceId = instanceType = null;
        publicAddress = privateAddress = null;
    }

    private AwsDataCenterInfo(Builder builder) {
        region = builder.region;
        zone = builder.zone;
        placementGroup = builder.placementGroup;
        amiId = builder.amiId;
        name = instanceId = builder.instanceId;
        instanceType = builder.instanceType;

        if (builder.privateIP != null || builder.privateHostName != null) {
            privateAddress = aNetworkAddress().withLabel(PRIVATE_ADDRESS).withProtocolType(ProtocolType.IPv4)
                    .withHostName(builder.privateHostName).withIpAddress(builder.privateIP).build();
        } else {
            privateAddress = null;
        }
        if (builder.publicIP != null || builder.publicHostName != null) {
            publicAddress = aNetworkAddress().withLabel(PUBLIC_ADDRESS).withProtocolType(ProtocolType.IPv4)
                    .withHostName(builder.publicHostName).withIpAddress(builder.publicIP).build();
        } else {
            publicAddress = null;
        }
    }

    public String getRegion() {
        return region;
    }

    public String getZone() {
        return zone;
    }

    public String getPlacementGroup() {
        return placementGroup;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public List getAddresses() {
        List addresses = new ArrayList<>(2);
        if (publicAddress != null) {
            addresses.add(publicAddress);
        }
        if (privateAddress != null) {
            addresses.add(privateAddress);
        }
        return addresses;
    }

    /**
     * The order of selection: first public, next private.
     */
    @Override
    public NetworkAddress getDefaultAddress() {
        return publicAddress != null ? publicAddress : privateAddress;
    }

    public NetworkAddress getPublicAddress() {
        return publicAddress;
    }

    public NetworkAddress getPrivateAddress() {
        return privateAddress;
    }

    public String getAmiId() {
        return amiId;
    }

    public String getInstanceId() {
        return instanceId;
    }

    public String getInstanceType() {
        return instanceType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AwsDataCenterInfo that = (AwsDataCenterInfo) o;

        if (amiId != null ? !amiId.equals(that.amiId) : that.amiId != null) {
            return false;
        }
        if (instanceId != null ? !instanceId.equals(that.instanceId) : that.instanceId != null) {
            return false;
        }
        if (instanceType != null ? !instanceType.equals(that.instanceType) : that.instanceType != null) {
            return false;
        }
        if (name != null ? !name.equals(that.name) : that.name != null) {
            return false;
        }
        if (placementGroup != null ? !placementGroup.equals(that.placementGroup) : that.placementGroup != null) {
            return false;
        }
        if (privateAddress != null ? !privateAddress.equals(that.privateAddress) : that.privateAddress != null) {
            return false;
        }
        if (publicAddress != null ? !publicAddress.equals(that.publicAddress) : that.publicAddress != null) {
            return false;
        }
        if (region != null ? !region.equals(that.region) : that.region != null) {
            return false;
        }
        if (zone != null ? !zone.equals(that.zone) : that.zone != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (region != null ? region.hashCode() : 0);
        result = 31 * result + (zone != null ? zone.hashCode() : 0);
        result = 31 * result + (placementGroup != null ? placementGroup.hashCode() : 0);
        result = 31 * result + (amiId != null ? amiId.hashCode() : 0);
        result = 31 * result + (instanceId != null ? instanceId.hashCode() : 0);
        result = 31 * result + (instanceType != null ? instanceType.hashCode() : 0);
        result = 31 * result + (publicAddress != null ? publicAddress.hashCode() : 0);
        result = 31 * result + (privateAddress != null ? privateAddress.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "AwsDataCenterInfo{" +
                "name='" + name + '\'' +
                ", region='" + region + '\'' +
                ", zone='" + zone + '\'' +
                ", placementGroup='" + placementGroup + '\'' +
                ", amiId='" + amiId + '\'' +
                ", instanceId='" + instanceId + '\'' +
                ", instanceType='" + instanceType + '\'' +
                ", publicAddress=" + publicAddress +
                ", privateAddress=" + privateAddress +
                '}';
    }

    public static final class Builder extends DataCenterInfoBuilder {
        private String region;
        private String zone;
        private String placementGroup;
        private String amiId;
        private String instanceId;
        private String instanceType;
        private String privateIP;
        private String privateHostName;
        private String publicIP;
        private String publicHostName;

        public Builder withRegion(String region) {
            this.region = region;
            return this;
        }

        public Builder withZone(String zone) {
            this.zone = zone;
            return this;
        }

        public Builder withPlacementGroup(String placementGroup) {
            this.placementGroup = placementGroup;
            return this;
        }

        public Builder withAmiId(String amiId) {
            this.amiId = amiId;
            return this;
        }

        public Builder withInstanceId(String instanceId) {
            this.instanceId = instanceId;
            return this;
        }

        public Builder withInstanceType(String instanceType) {
            this.instanceType = instanceType;
            return this;
        }

        public Builder withPrivateIPv4(String privateIP) {
            this.privateIP = privateIP;
            return this;
        }

        public Builder withPrivateHostName(String privateHostName) {
            this.privateHostName = privateHostName;
            return this;
        }

        public Builder withPublicIPv4(String publicIP) {
            this.publicIP = publicIP;
            return this;
        }

        public Builder withPublicHostName(String publicHostName) {
            this.publicHostName = publicHostName;
            return this;
        }

        @Override
        public AwsDataCenterInfo build() {
            if (region == null && zone != null && !zone.isEmpty()) { // We will take it from zone name
                region = zone.substring(0, zone.length() - 1);
            }

            return new AwsDataCenterInfo(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy