Search in sources :

Example 6 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 7 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 8 with ItemType

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

the class JdbcManagedItemRepository method getManagedItem.

@Override
@JdbcTransaction
public ItemBase getManagedItem(PlatformLayerKey key, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(key);
    try {
        ServiceProvider serviceProvider = serviceProviderDirectory.getServiceProvider(key.getServiceType());
        if (serviceProvider == null) {
            throw new IllegalStateException();
        }
        ModelClass<?> modelClass = serviceProvider.getModelClass(key.getItemType());
        ServiceType serviceType = key.getServiceType();
        ItemType itemType = key.getItemType();
        ProjectId project = key.getProject();
        ManagedItemId itemId = key.getItemId();
        return fetchItem(db, serviceType, itemType, project, itemId, modelClass.getJavaClass(), secretProvider, fetchTags);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 9 with ItemType

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

the class JdbcManagedItemRepository method mapItemsAndTags.

private <T extends ItemBase> List<T> mapItemsAndTags(ProjectId project, SecretProvider secretProvider, DbHelper db, JoinedQueryResult result) throws RepositoryException, SQLException {
    Multimap<Integer, Tag> itemTags = HashMultimap.create();
    for (TagEntity row : result.getAll(TagEntity.class)) {
        Tag tag = Tag.build(row.key, row.data);
        itemTags.put(row.item, tag);
    }
    List<T> items = Lists.newArrayList();
    for (ItemEntity entity : result.getAll(ItemEntity.class)) {
        if (entity == null) {
            throw new IllegalStateException();
        }
        ServiceType serviceType = db.getServiceType(entity.service);
        ItemType itemType = db.getItemType(entity.model);
        JaxbHelper jaxbHelper = getJaxbHelper(db, serviceType, itemType);
        T item = mapToModel(project, serviceType, itemType, entity, jaxbHelper, secretProvider);
        int itemId = entity.id;
        Collection<Tag> tags = itemTags.get(itemId);
        item.getTags().addAll(tags);
        items.add(item);
    }
    return items;
}
Also used : ItemType(org.platformlayer.ids.ItemType) ServiceType(org.platformlayer.ids.ServiceType) JaxbHelper(org.platformlayer.xml.JaxbHelper) Tag(org.platformlayer.core.model.Tag)

Example 10 with ItemType

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

the class ModelClass method build.

public static <T extends ItemBase> ModelClass<T> build(ServiceProvider serviceProvider, Class<T> clazz) {
    JaxbHelper jaxbHelper = JaxbHelper.get(clazz);
    ItemType itemType = new ItemType(JaxbHelper.getXmlElementName(clazz));
    return new ModelClass<T>(serviceProvider, clazz, itemType);
}
Also used : ItemType(org.platformlayer.ids.ItemType) JaxbHelper(org.platformlayer.xml.JaxbHelper)

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