Search in sources :

Example 1 with ItemType

use of org.platformlayer.ids.ItemType in project platformlayer by platformlayer.

the class ServiceProviderBase method getServiceInfo.

@Override
public ServiceInfo getServiceInfo() {
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.serviceType = getServiceType().getKey();
    serviceInfo.description = description;
    for (ModelClass<?> modelClass : getModels().all()) {
        ItemType itemType = modelClass.getItemType();
        if (serviceInfo.getNamespace() == null) {
            serviceInfo.namespace = modelClass.getPrimaryNamespace();
        }
        if (serviceInfo.itemTypes == null) {
            serviceInfo.itemTypes = Lists.newArrayList();
        }
        serviceInfo.itemTypes.add(itemType.getKey());
    }
    return serviceInfo;
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ItemType(org.platformlayer.ids.ItemType)

Example 2 with ItemType

use of org.platformlayer.ids.ItemType in project platformlayer by platformlayer.

the class ServiceProviderHelpers method getModelClass.

public ModelClass<?> getModelClass(PlatformLayerKey platformLayerKey) {
    OpsContext ops = OpsContext.get();
    ServiceProviderDictionary serviceProviderDictionary = ops.getInjector().getInstance(ServiceProviderDictionary.class);
    ServiceType serviceType = platformLayerKey.getServiceType();
    ItemType itemType = platformLayerKey.getItemType();
    ServiceProvider serviceProvider = serviceProviderDictionary.getServiceProvider(serviceType);
    if (serviceProvider == null) {
        throw new IllegalArgumentException();
    }
    ModelClass<?> modelClass = serviceProvider.getModelClass(itemType);
    if (modelClass == null) {
        throw new IllegalArgumentException();
    }
    return modelClass;
}
Also used : ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ServiceProviderDictionary(org.platformlayer.xaas.services.ServiceProviderDictionary) OpsContext(org.platformlayer.ops.OpsContext)

Example 3 with ItemType

use of org.platformlayer.ids.ItemType in project platformlayer by platformlayer.

the class ScoreHostPolicy method choose.

@Override
public DirectCloudHost choose(List<DirectCloudHost> candidates) throws OpsException {
    final String sameGroupId;
    if (Strings.isNullOrEmpty(hostPolicy.groupId)) {
        sameGroupId = DEFAULT_GROUP;
    } else {
        sameGroupId = hostPolicy.groupId;
    }
    final ItemType sameItemType;
    if (hostPolicy.scoreSameItemType != 0) {
        PlatformLayerKey owner = findOwner(newInstance);
        if (owner == null) {
            throw new OpsException();
        }
        sameItemType = owner.getItemType();
    } else {
        sameItemType = null;
    }
    List<HostRecord> records = Lists.newArrayList();
    for (DirectCloudHost candidate : candidates) {
        HostRecord record = new HostRecord();
        record.candidate = candidate;
        record.groups = Maps.newHashMap();
        if (hostPolicy.scoreSameItemType != 0) {
            record.owners = Maps.newHashMap();
        }
        records.add(record);
        for (String assigned : candidate.getModel().getTags().findAll(Tag.ASSIGNED)) {
            PlatformLayerKey instanceKey = PlatformLayerKey.parse(assigned);
            // TODO: Avoid 1+N
            DirectInstance instance = platformLayer.getItem(instanceKey);
            if (instance == null) {
                // TODO: Warn?
                throw new IllegalStateException();
            }
            switch(instance.getState()) {
                case DELETE_REQUESTED:
                case DELETED:
                    continue;
            }
            HostPolicy instanceHostPolicy = instance.hostPolicy;
            String instanceGroupId = instanceHostPolicy.groupId;
            if (Strings.isNullOrEmpty(instanceGroupId)) {
                instanceGroupId = DEFAULT_GROUP;
            }
            record.groups.put(instance, instanceGroupId);
            if (sameItemType != null) {
                PlatformLayerKey owner = findOwner(instance);
                if (owner != null) {
                    record.owners.put(instance, owner);
                }
            }
            record.all.add(instance);
        }
    }
    Function<HostRecord, Float> score = new Function<HostRecord, Float>() {

        @Override
        public Float apply(HostRecord record) {
            float score = 0;
            for (DirectInstance instance : record.all) {
                if (sameGroupId != null) {
                    String instanceGroupId = record.groups.get(instance);
                    if (Objects.equal(instanceGroupId, sameGroupId)) {
                        score += hostPolicy.scoreSameGroup;
                    }
                }
                if (sameItemType != null) {
                    PlatformLayerKey owner = record.owners.get(instance);
                    if (owner != null && owner.getItemType().equals(sameItemType)) {
                        score += hostPolicy.scoreSameItemType;
                    }
                }
            }
            // Break ties using least-loaded
            score -= record.all.size() / 1000.0f;
            return score;
        }
    };
    HostRecord bestRecord = ScoreChooser.chooseMax(score).choose(records);
    return bestRecord.candidate;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemType(org.platformlayer.ids.ItemType) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Function(com.google.common.base.Function) HostPolicy(org.platformlayer.core.model.HostPolicy)

Example 4 with ItemType

use of org.platformlayer.ids.ItemType in project platformlayer by platformlayer.

the class PlatformLayerCliContext method pathToItem.

public PlatformLayerKey pathToItem(ProjectId project, String path) throws PlatformLayerClientException {
    String serviceType = null;
    String itemType = null;
    String itemId = null;
    if (path.contains("://")) {
        return PlatformLayerKey.parse(path);
    }
    List<String> components = Lists.newArrayList(path.split("/"));
    if (components.size() <= 1) {
        throw new IllegalArgumentException("Cannot resolve path: " + path);
    }
    String head = components.get(0);
    {
        Map<String, ServiceInfo> services = listServices();
        if (services.containsKey(head)) {
            serviceType = services.get(head).serviceType;
            components.remove(0);
            if (components.size() <= 1) {
                throw new IllegalArgumentException("Cannot resolve path: " + path);
            }
            head = components.get(0);
        }
    }
    Multimap<String, ServiceInfo> items = listItemTypes();
    if (items.containsKey(head)) {
        Collection<ServiceInfo> services = items.get(head);
        if (services.size() > 1) {
            throw new IllegalArgumentException("Cannot resolve path (ambiguous item type): " + path);
        }
        ServiceInfo serviceInfo = Iterables.getOnlyElement(services);
        itemType = head;
        if (serviceType != null) {
            if (!Objects.equal(serviceType, serviceInfo.serviceType)) {
                throw new IllegalArgumentException("Cannot resolve path (service/item type mismatch): " + path);
            }
        } else {
            serviceType = serviceInfo.serviceType;
        }
        components.remove(0);
    } else {
        throw new IllegalArgumentException("Cannot resolve path (unknown item type): " + path);
    }
    itemId = Joiner.on('/').join(components);
    if (serviceType == null) {
        throw new IllegalArgumentException("Cannot resolve path (service type not resolved): " + path);
    }
    if (itemType == null) {
        throw new IllegalArgumentException("Cannot resolve path (item type not resolved): " + path);
    }
    if (Strings.isNullOrEmpty(itemId)) {
        throw new IllegalArgumentException("Cannot resolve path (item id not resolved): " + path);
    }
    FederationKey host = null;
    return new PlatformLayerKey(host, project, new ServiceType(serviceType), new ItemType(itemType), new ManagedItemId(itemId));
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Map(java.util.Map) ManagedItemId(org.platformlayer.ids.ManagedItemId) FederationKey(org.platformlayer.ids.FederationKey)

Example 5 with ItemType

use of org.platformlayer.ids.ItemType in project platformlayer by platformlayer.

the class PlatformLayerClientBase method toKey.

public static <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, ManagedItemId id, Class<T> itemClass, Collection<ServiceInfo> services) throws PlatformLayerClientException {
    String namespaceURI = jaxbHelper.getPrimaryNamespace();
    String nodeName = jaxbHelper.getXmlElementName();
    if (namespaceURI == null) {
        throw new IllegalArgumentException("Namespace could not be determined");
    }
    ServiceInfo service = getServiceInfo(services, namespaceURI);
    if (service == null) {
        throw new PlatformLayerClientException("Cannot find service for " + namespaceURI);
    }
    ServiceType serviceType = new ServiceType(service.getServiceType());
    ItemType itemType = new ItemType(nodeName);
    FederationKey host = null;
    ProjectId project = null;
    return new PlatformLayerKey(host, project, serviceType, itemType, id);
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) FederationKey(org.platformlayer.ids.FederationKey)

Aggregations

ItemType (org.platformlayer.ids.ItemType)15 ServiceType (org.platformlayer.ids.ServiceType)10 ProjectId (org.platformlayer.ids.ProjectId)6 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)5 FederationKey (org.platformlayer.ids.FederationKey)5 ManagedItemId (org.platformlayer.ids.ManagedItemId)5 ServiceInfo (org.platformlayer.core.model.ServiceInfo)3 OpsException (org.platformlayer.ops.OpsException)2 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)2 JaxbHelper (org.platformlayer.xml.JaxbHelper)2 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)1 Function (com.google.common.base.Function)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 Path (javax.ws.rs.Path)1 RepositoryException (org.platformlayer.RepositoryException)1 HostPolicy (org.platformlayer.core.model.HostPolicy)1 Tag (org.platformlayer.core.model.Tag)1