use of alien4cloud.model.service.ServiceResource in project alien4cloud by alien4cloud.
the class ServiceResourceService method update.
private void update(String resourceId, String name, String version, String description, String nodeTypeStr, String nodeTypeVersion, Map<String, AbstractPropertyValue> nodeProperties, Map<String, Capability> nodeCapabilities, Map<String, String> nodeAttributeValues, String[] locations, Map<String, String> capabilitiesRelationshipTypes, Map<String, String> requirementsRelationshipTypes, boolean patch) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
ServiceResource serviceResource = getOrFail(resourceId);
failUpdateIfManaged(serviceResource, patch, name, version, nodeTypeStr, nodeTypeVersion, nodeProperties, nodeCapabilities, nodeAttributeValues);
boolean ensureUniqueness = false;
boolean isDeployed = !ToscaNodeLifecycleConstants.INITIAL.equals(serviceResource.getNodeInstance().getAttributeValues().get(ToscaNodeLifecycleConstants.ATT_STATE));
String updatedState = nodeAttributeValues == null ? null : nodeAttributeValues.get(ToscaNodeLifecycleConstants.ATT_STATE);
if (!patch || updatedState != null) {
// in case of an update or when patching the state: check that the new state is a valid state
if (!ToscaNodeLifecycleConstants.TOSCA_STATES.contains(updatedState)) {
throw new IllegalArgumentException("State <" + updatedState + "> is not a valid state value must be one of " + ToscaNodeLifecycleConstants.TOSCA_STATES.toString());
}
}
boolean isUpdateDeployed = updatedState == null ? isDeployed : !ToscaNodeLifecycleConstants.INITIAL.equals(nodeAttributeValues.get(ToscaNodeLifecycleConstants.ATT_STATE));
NodeType nodeType = null;
// Updating a running service is not yet authorized
if (isDeployed && isUpdateDeployed) {
// Patch operation is allowed only on the service description or locations authorized for matching.
if (!patch || name != null || version != null || nodeTypeStr != null || nodeTypeVersion != null || nodeProperties != null || nodeCapabilities != null || nodeAttributeValues != null || capabilitiesRelationshipTypes != null || requirementsRelationshipTypes != null) {
throw new UnsupportedOperationException("Update is not allowed on a running service, please use patch if you wish to change locations or authorizations.");
}
} else {
ensureUniqueness = PatchUtil.set(serviceResource, "name", name, patch);
ensureUniqueness = PatchUtil.set(serviceResource, "version", version, patch) || ensureUniqueness;
// Node instance properties update
nodeType = toscaTypeSearchService.findOrFail(NodeType.class, serviceResource.getNodeInstance().getNodeTemplate().getType(), serviceResource.getNodeInstance().getTypeVersion());
// node instance type update
PatchUtil.set(serviceResource.getNodeInstance().getNodeTemplate(), "type", nodeTypeStr, patch);
PatchUtil.set(serviceResource.getNodeInstance(), "typeVersion", nodeTypeVersion, patch);
// update half-relationship type
serviceResource.setCapabilitiesRelationshipTypes(PatchUtil.setMap(serviceResource.getCapabilitiesRelationshipTypes(), capabilitiesRelationshipTypes, patch));
serviceResource.setRequirementsRelationshipTypes(PatchUtil.setMap(serviceResource.getRequirementsRelationshipTypes(), requirementsRelationshipTypes, patch));
// validate the half-relationship types exist
validateRelationshipTypes(serviceResource, nodeType);
// Node instance properties update
nodeType = toscaTypeSearchService.findOrFail(NodeType.class, serviceResource.getNodeInstance().getNodeTemplate().getType(), serviceResource.getNodeInstance().getTypeVersion());
if (patch) {
nodeInstanceService.patch(nodeType, serviceResource.getNodeInstance(), nodeProperties, nodeCapabilities, nodeAttributeValues);
} else {
nodeInstanceService.update(nodeType, serviceResource.getNodeInstance(), nodeProperties, nodeCapabilities, nodeAttributeValues);
}
}
// description, authorized locations and authorizations can be updated even when deployed.
// Note that changing the locations or authorizations won't impact already deployed applications using the service
PatchUtil.set(serviceResource, "description", description, patch);
updateLocations(serviceResource, locations);
if (isDeployed && !isUpdateDeployed) {
// un-deploying a service is authorized only if the service is not used.
failIdUsed(serviceResource.getId());
}
if (!isDeployed && isUpdateDeployed) {
// check that all required properties are defined
if (nodeType == null) {
nodeType = toscaTypeSearchService.findOrFail(NodeType.class, serviceResource.getNodeInstance().getNodeTemplate().getType(), serviceResource.getNodeInstance().getTypeVersion());
}
nodeInstanceService.checkRequired(nodeType, serviceResource.getNodeInstance());
}
save(serviceResource, ensureUniqueness);
}
use of alien4cloud.model.service.ServiceResource in project alien4cloud by alien4cloud.
the class ServiceResourceService method create.
/**
* Create a service.
*
* @param serviceName The unique name that defines the service from user point of view.
* @param serviceVersion The id of the plugin used to communicate with the orchestrator.
* @param serviceNodeType The type of the node type used to create the service.
* @param serviceNodeVersion The version of the node type used to create the service.
* @param environmentId In case the service is created out of an alien environment the id of the environment, null if not.
* @return The generated identifier for the service.
*/
public String create(String serviceName, String serviceVersion, String serviceNodeType, String serviceNodeVersion, String environmentId) {
ServiceResource serviceResource = new ServiceResource();
// generate an unique id
serviceResource.setId(UUID.randomUUID().toString());
serviceResource.setName(serviceName);
serviceResource.setVersion(serviceVersion);
serviceResource.setEnvironmentId(environmentId);
// build a node instance from the given type
NodeType nodeType = toscaTypeSearchService.findOrFail(NodeType.class, serviceNodeType, serviceNodeVersion);
serviceResource.setNodeInstance(nodeInstanceService.create(nodeType, serviceNodeVersion));
serviceResource.setDependency(new CSARDependency(nodeType.getArchiveName(), nodeType.getArchiveVersion()));
// ensure uniqueness and save
save(serviceResource, true);
// TODO: send an event: a service has been created
return serviceResource.getId();
}
use of alien4cloud.model.service.ServiceResource in project alien4cloud by alien4cloud.
the class ServiceResourceService method reportArchiveUsage.
@EventListener
public void reportArchiveUsage(ArchiveUsageRequestEvent event) {
ServiceResource[] serviceResources = alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("dependency.name", event.getArchiveName(), "dependency.version", event.getArchiveVersion())).prepareSearch().search(0, Integer.MAX_VALUE).getData();
for (ServiceResource serviceResource : serviceResources) {
Usage usage = new Usage(serviceResource.getName(), ServiceResource.class.getSimpleName().toLowerCase(), serviceResource.getId(), "");
event.addUsage(usage);
}
}
use of alien4cloud.model.service.ServiceResource in project alien4cloud by alien4cloud.
the class ServiceResourceService method handleLocationDeleted.
@EventListener
public synchronized void handleLocationDeleted(AfterLocationDeleted event) {
// Remove the location in every service that referenced it
GetMultipleDataResult<ServiceResource> serviceResourceResult = alienDAO.buildQuery(ServiceResource.class).setFilters(singleKeyFilter("locationIds", event.getLocationId())).prepareSearch().search(0, Integer.MAX_VALUE);
if (serviceResourceResult.getData() == null) {
return;
}
for (ServiceResource serviceResource : serviceResourceResult.getData()) {
Set<String> locations = CollectionUtils.safeNewHashSet(serviceResource.getLocationIds());
locations.remove(event.getLocationId());
serviceResource.setLocationIds(locations.toArray(new String[locations.size()]));
}
// bulk update
alienDAO.save(serviceResourceResult.getData());
}
use of alien4cloud.model.service.ServiceResource in project alien4cloud by alien4cloud.
the class ApplicationVersionService method failIfAnyEnvironmentExposedAsService.
private void failIfAnyEnvironmentExposedAsService(String applicationId, ApplicationEnvironment[] environments) {
Application application = applicationService.getOrFail(applicationId);
Usage[] usages = Arrays.stream(environments).map(environment -> {
Usage usage = null;
ServiceResource service = alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environment.getId())).prepareSearch().find();
if (service != null) {
String usageName = service.getName() + " [App (" + application.getName() + "), Env (" + environment.getName() + ")]";
usage = new Usage(usageName, "Service", service.getId(), null);
}
return usage;
}).filter(Objects::nonNull).toArray(Usage[]::new);
if (ArrayUtils.isNotEmpty(usages)) {
throw new ReferencedResourceException("Application versions exposed as a service cannot be updated", usages);
}
}
Aggregations