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

org.dasein.cloud.cloudstack.CSTopology Maven / Gradle / Ivy

Go to download

Implements the Dasein Cloud API for Cloud.com Cloudstack-based public and private clouds.

There is a newer version: 2015.10.5
Show newest version
/**
 * Copyright (C) 2009-2015 Dell, 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 org.dasein.cloud.cloudstack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.dc.AbstractDataCenterServices;
import org.dasein.cloud.dc.DataCenter;
import org.dasein.cloud.dc.DataCenterCapabilities;
import org.dasein.cloud.dc.DataCenterServices;
import org.dasein.cloud.dc.Folder;
import org.dasein.cloud.dc.Region;
import org.dasein.cloud.dc.ResourcePool;
import org.dasein.cloud.dc.StoragePool;
import org.dasein.cloud.util.APITrace;
import org.dasein.cloud.util.Cache;
import org.dasein.cloud.util.CacheLevel;
import org.dasein.util.uom.time.Day;
import org.dasein.util.uom.time.Minute;
import org.dasein.util.uom.time.TimePeriod;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CSTopology extends AbstractDataCenterServices {
    static public final String LIST_ZONES = "listZones";

    private transient volatile CSTopologyCapabilities capabilities;

    CSTopology(CSCloud provider) {
        super(provider);
    }

    @Nonnull
    @Override
    public DataCenterCapabilities getCapabilities() throws InternalException, CloudException {
        if( capabilities == null ) {
            capabilities = new CSTopologyCapabilities(getProvider());
        }
        return capabilities;
    }

    public @Nullable DataCenter getDataCenter(@Nonnull String zoneId) throws InternalException, CloudException {
        APITrace.begin(getProvider(), "DC.getDataCenter");
        try {
            for( Region region : listRegions() ) {
                for( DataCenter dc : listDataCenters(region.getProviderRegionId()) ) {
                    if( dc.getProviderDataCenterId().equals(zoneId) ) {
                        return dc;
                    }
                }
            }
            return null;
        }
        finally {
            APITrace.end();
        }
    }

    public @Nullable Region getRegion(@Nonnull String regionId) throws InternalException, CloudException {
        APITrace.begin(getProvider(), "DC.getRegion");
        try {
            for( Region region : listRegions() ) {
                if( region.getProviderRegionId().equals(regionId) ) {
                    return region;
                }
            }
            return null;
        }
        finally {
            APITrace.end();
        }
    }

    public boolean zoneSupportsVlans(@Nonnull String zoneId) throws InternalException, CloudException {
        Cache cache = Cache.getInstance(getProvider(), "advanced.vlan."+zoneId, Boolean.class, CacheLevel.CLOUD_ACCOUNT,
                new TimePeriod(1,
                TimePeriod.DAY));
        Iterable cachedValues = cache.get(getContext());
        if( cachedValues != null && cachedValues.iterator().hasNext() ) {
            return cachedValues.iterator().next();
        }

        APITrace.begin(getProvider(), "DC.zoneSupportsVlans");
        try {
            Document doc = new CSMethod(getProvider()).get(LIST_ZONES, new Param("available", "true"));

            NodeList matches = doc.getElementsByTagName("zone");
            for( int i=0; i listDataCenters(@Nonnull String regionId) throws InternalException, CloudException {
        APITrace.begin(getProvider(), "DC.listDataCenters");
        try {
            Cache cache = Cache.getInstance(getProvider(), "dataCenters", DataCenter.class, CacheLevel.REGION_ACCOUNT, new TimePeriod(15, TimePeriod.MINUTE));
            Iterable dcs = cache.get(getContext());

            if( dcs == null ) {
                Region region = getRegion(regionId);

                if( region == null ) {
                    throw new CloudException("No such region: " + regionId);
                }
                DataCenter zone = new DataCenter();

                zone.setActive(true);
                zone.setAvailable(true);
                zone.setName(region.getName() + " (DC)");
                zone.setProviderDataCenterId(regionId);
                zone.setRegionId(regionId);
                dcs = Collections.singletonList(zone);
                cache.put(getContext(), dcs);
            }
            return dcs;
        }
        finally {
            APITrace.end();
        }
    }

    public @Nonnull Iterable listRegions() throws InternalException, CloudException {
        APITrace.begin(getProvider(), "DC.listRegions");
        try {
            Cache cache = Cache.getInstance(getProvider(), "regions", Region.class, CacheLevel.CLOUD_ACCOUNT, new TimePeriod(15, TimePeriod.MINUTE));
            Iterable regions = cache.get(getContext());

            if( regions == null ) {
                Document doc = new CSMethod(getProvider()).get(LIST_ZONES, new Param("available", "true"));

                regions = new ArrayList();
                NodeList matches = doc.getElementsByTagName("zone");
                for( int i=0; i 0 ) {
                value = n.getFirstChild().getNodeValue();
            }
            else {
                value = null;
            }
            if( name.equals("id") ) {
                region.setProviderRegionId(value);
            }
            else if( name.equals("name") ) {
                region.setName(value);
            }
        }
        if( region.getProviderRegionId() == null ) {
            return null;
        }
        if( region.getName() == null ) {
            region.setName(region.getProviderRegionId());
        }
        region.setActive(true);
        region.setAvailable(true);
        region.setJurisdiction(getJurisdiction(region.getName()));
        return region;
    }
    
    private String getJurisdiction(String name) {
        if( name.contains("New York") ) {
            return "US";
        }
        else if( name.contains("Hong Kong") ) {
            return "HK";
        }
        else if( name.contains("India") ) {
            return "IN";
        }
        else if( name.contains("London") ) {
            return "EU";
        }
        ProviderContext ctx = getProvider().getContext();

        Properties p = (ctx == null ? null : ctx.getCustomProperties());

        return (p == null ? "US" : p.getProperty("locale." + name, "US"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy