use of org.alien4cloud.alm.events.ManagedServiceUpdatedEvent in project alien4cloud by alien4cloud.
the class ManagedServiceResourceEventService method updateRunningService.
private void updateRunningService(DeploymentTopology topology, Deployment deployment, ServiceResource serviceResource, String serviceState, Map<String, Map<String, InstanceInformation>> instanceInformation) {
// update the state
serviceResource.setState(serviceState);
// update deploymentId, in case it is not yet (when creating the service from an already started deployment)
serviceResource.setDeploymentId(deployment.getId());
// ensure the service is available on all of the deployment locations
updateLocations(serviceResource, deployment.getLocationIds());
// Map input properties from the topology as properties of the service instance
if (serviceResource.getNodeInstance().getNodeTemplate().getProperties() == null) {
serviceResource.getNodeInstance().getNodeTemplate().setProperties(Maps.newHashMap());
}
serviceResource.getNodeInstance().getNodeTemplate().getProperties().putAll(safe(topology.getAllInputProperties()));
// Map attributes from the instances to the actual service resource node.
for (Entry<String, Set<String>> nodeOutputAttrEntry : safe(topology.getOutputAttributes()).entrySet()) {
Map<String, InstanceInformation> instances = instanceInformation.get(nodeOutputAttrEntry.getKey());
if (instances == null) {
log.error("Failed to map attributes from node [ {} ] for service <id: {}, name: {}>. The node cannot be found in deployed topology [ {} ].", nodeOutputAttrEntry.getKey(), serviceResource.getId(), serviceResource.getName(), topology.getId());
} else if (instances.size() > 1) {
log.error("Services substitution does not yet supports the exposure of multiple instances");
} else {
InstanceInformation instance = instances.values().iterator().next();
// let's map attribute
for (String mappedAttribute : nodeOutputAttrEntry.getValue()) {
serviceResource.getNodeInstance().setAttribute(mappedAttribute, instance.getAttributes().get(mappedAttribute));
}
}
}
// Map output properties as attributes of the service instance
for (Entry<String, Set<String>> nodeOutputPropEntry : safe(topology.getOutputProperties()).entrySet()) {
NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputPropEntry.getKey());
for (String prop : nodeOutputPropEntry.getValue()) {
serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(nodeTemplate.getProperties().get(prop)));
}
}
// Map capabilities output properties as attributes of the service instance (that are exposed as node properties)
for (Entry<String, Map<String, Set<String>>> nodeOutputCapaPropEntry : safe(topology.getOutputCapabilityProperties()).entrySet()) {
NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputCapaPropEntry.getKey());
for (Entry<String, Set<String>> outputCapaPropEntry : nodeOutputCapaPropEntry.getValue().entrySet()) {
Capability capability = nodeTemplate.getCapabilities().get(outputCapaPropEntry.getKey());
for (String prop : outputCapaPropEntry.getValue()) {
serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(capability.getProperties().get(prop)));
}
}
}
serviceResource.getNodeInstance().getNodeTemplate().setCapabilities(Maps.newLinkedHashMap());
// Map capabilities exposed as is for the service node.
for (Entry<String, SubstitutionTarget> capabilityMapping : safe(topology.getSubstitutionMapping().getCapabilities()).entrySet()) {
Capability deployedCapability = topology.getNodeTemplates().get(capabilityMapping.getValue().getNodeTemplateName()).getCapabilities().get(capabilityMapping.getValue().getTargetId());
serviceResource.getNodeInstance().getNodeTemplate().getCapabilities().put(capabilityMapping.getKey(), deployedCapability);
// TODO improve while capabilities attributes will be really supported
// Workaround to support capabilities attributes is to use node attributes with keys in format capabilities.capaName.attributeName
mapCapabilityRequirementAttributes(serviceResource, instanceInformation, capabilityMapping.getValue().getNodeTemplateName(), "capabilities", capabilityMapping.getValue().getTargetId());
}
serviceResource.getNodeInstance().getNodeTemplate().setRequirements(Maps.newLinkedHashMap());
// Map requirements exposed as is for the service node.
for (Entry<String, SubstitutionTarget> requirementMapping : safe(topology.getSubstitutionMapping().getRequirements()).entrySet()) {
serviceResource.getNodeInstance().getNodeTemplate().getRequirements().put(requirementMapping.getKey(), topology.getNodeTemplates().get(requirementMapping.getValue().getNodeTemplateName()).getRequirements().get(requirementMapping.getValue().getTargetId()));
// TODO improve while requirements attributes will be really supported
// Workaround to support requirements attributes is to use node attributes with keys in format capabilities.capaName.attributeName
mapCapabilityRequirementAttributes(serviceResource, instanceInformation, requirementMapping.getValue().getNodeTemplateName(), "requirements", requirementMapping.getValue().getTargetId());
}
serviceResourceService.save(serviceResource);
// trigger a ManagedServiceUpdateEvent
publisher.publishEvent(new ManagedServiceUpdatedEvent(this, serviceResource, topology));
}
Aggregations