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;
}
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;
}
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;
}
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));
}
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);
}
Aggregations