use of org.alien4cloud.alm.deployment.configuration.events.OnMatchedLocationChangedEvent in project alien4cloud by alien4cloud.
the class LocationMatchService method setLocationPolicy.
public void setLocationPolicy(ApplicationEnvironment environment, String orchestratorId, Map<String, String> groupsLocationsMapping) {
if (MapUtils.isEmpty(groupsLocationsMapping)) {
return;
}
DeploymentMatchingConfiguration configuration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(environment.getTopologyVersion(), environment.getId()));
if (configuration == null) {
configuration = new DeploymentMatchingConfiguration(environment.getTopologyVersion(), environment.getId());
} else if (configuration.getLocationGroups() == null) {
configuration.setLocationGroups(Maps.newHashMap());
}
Map<String, String> currentConfiguration = configuration.getLocationIds();
// TODO For now, we only support one location policy for all nodes. So we have a group _A4C_ALL that represents all compute nodes in the topology
// To improve later on for multiple groups support
// throw an exception if multiple location policies provided: not yet supported
// throw an exception if group name is not _A4C_ALL
checkGroups(groupsLocationsMapping);
boolean updated = false;
for (Entry<String, String> matchEntry : groupsLocationsMapping.entrySet()) {
String current = currentConfiguration.get(matchEntry.getKey());
if (current != null && current.equals(matchEntry.getValue())) {
continue;
}
updated = true;
String locationId = matchEntry.getValue();
Location location = locationService.getOrFail(locationId);
locationSecurityService.checkAuthorisation(location, environment.getId());
LocationPlacementPolicy locationPolicy = new LocationPlacementPolicy(locationId);
locationPolicy.setName("Location policy");
Map<String, NodeGroup> groups = configuration.getLocationGroups();
NodeGroup group = new NodeGroup();
group.setName(matchEntry.getKey());
group.setPolicies(Lists.<AbstractPolicy>newArrayList());
group.getPolicies().add(locationPolicy);
groups.put(matchEntry.getKey(), group);
}
if (!updated) {
// nothing has changed.
return;
}
configuration.setOrchestratorId(orchestratorId);
configuration.setMatchedLocationResources(Maps.newHashMap());
configuration.setMatchedNodesConfiguration(Maps.newHashMap());
publisher.publishEvent(new OnMatchedLocationChangedEvent(this, environment, orchestratorId, groupsLocationsMapping));
deploymentConfigurationDao.save(configuration);
}
Aggregations