private void processSpecificReplacement(Map<String, T> topologyTemplateMap, String nodeId, String locationResourceTemplateId) { // Fetching a new node copy from elasticsearch avoid later issues if the same subtituted node is used in multiple templates (shared maps, shallow copies // etc.) V resourceTemplate = getLocationResourceTemplateCopy(locationResourceTemplateId); T replacingNode = resourceTemplate.getTemplate(); // Substitute the node in the topology with the location provided implementation. T replacedTopologyNode = topologyTemplateMap.put(nodeId, replacingNode); // Merge name, properties and capability properties replacingNode.setName(replacedTopologyNode.getName()); // TODO Log all properties defined in the topology but not merged into the final node Set<String> topologyNotMergedProps = Sets.newHashSet(); // Merge properties from the topology node but prevent any override. replacingNode.setProperties(getMergedProperties(replacedTopologyNode.getProperties(), replacingNode.getProperties(), true, topologyNotMergedProps)); // We need to keep tags (metadata) in the replaced node replacingNode.setTags(replacedTopologyNode.getTags()); processSpecificReplacement(replacingNode, replacedTopologyNode, topologyNotMergedProps); }
private <T extends AbstractInheritableToscaType, V extends AbstractTemplate> void fillTypeMap(Class<T> elementClass, Map<String, T> types, Map<String, V> templateMap, boolean useTemplateNameAsKey, boolean abstractOnly) { if (templateMap == null) { return; } for (Map.Entry<String, V> template : templateMap.entrySet()) { if (!types.containsKey(template.getValue().getType())) { T type = ToscaContext.get(elementClass, template.getValue().getType()); if (!abstractOnly || type.isAbstract()) { String key = useTemplateNameAsKey ? template.getKey() : template.getValue().getType(); types.put(key, type); } } } }
private static void fillAbstractTemplate(AbstractTemplate template, AbstractInheritableToscaType type, AbstractTemplate templateToMerge, boolean mergeUndefinedProps) { template.setType(type.getElementId()); template.setProperties(Maps.newLinkedHashMap()); fillProperties(template.getProperties(), type.getProperties(), templateToMerge != null ? templateToMerge.getProperties() : null, !mergeUndefinedProps); }
/** * Set value for a property * * @param template the template * @param propertyDefinition the definition of the property to be set * @param propertyName the name of the property to set * @param propertyValue the value to be set */ public void setPropertyValue(AbstractTemplate template, PropertyDefinition propertyDefinition, String propertyName, Object propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException { if (template.getProperties() == null) { template.setProperties(Maps.newLinkedHashMap()); } setPropertyValue(template.getProperties(), propertyDefinition, propertyName, propertyValue); }
protected boolean doMergeNode(Topology topology, FlowExecutionContext context, String templateId, NodePropsOverride nodePropsOverride) { final ConfigChanged configChanged = new ConfigChanged(); // This node is still a matched node merge properties U template = getTemplates(topology).get(templateId); T toscaType = ToscaContext.get(getToscaTypeClass(), template.getType()); template.setProperties(mergeProperties(nodePropsOverride.getProperties(), template.getProperties(), toscaType.getProperties(), propertyName -> { configChanged.changed = true; context.log().info("The property [" + propertyName + "] previously specified to configure " + getSubject() + " [" + templateId + "] cannot be set anymore as it is already specified by the matched location resource or in the topology."); })); return configChanged.changed; }
protected void setProperty(FlowExecutionContext context, V resourceTemplate, U template, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException { PropertyDefinition propertyDefinition = ((T) ToscaContext.getOrFail(AbstractToscaType.class, resourceTemplate.getTemplate().getType())).getProperties() .get(propertyName); if (propertyDefinition == null) { throw new NotFoundException("No property of name [" + propertyName + "] can be found on the " + getSubject() + " template [" + templateId + "] of type [" + resourceTemplate.getTemplate().getType() + "]"); } AbstractPropertyValue locationResourcePropertyValue = resourceTemplate.getTemplate().getProperties().get(propertyName); ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue); ensureNotSet(template.getProperties().get(propertyName), "in the portable topology", propertyName, propertyValue); // Update the configuration NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration); // Perform the update of the property if (propertyValue == null) { nodePropsOverride.getProperties().remove(propertyName); } else { AbstractPropertyValue abstractPropertyValue = PropertyService.asPropertyValue(propertyValue); if (! (abstractPropertyValue instanceof FunctionPropertyValue)) { ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition); } nodePropsOverride.getProperties().put(propertyName, abstractPropertyValue); } context.saveConfiguration(matchingConfiguration); }
private boolean validateTemplateMatch(V abstractTemplate, R candidate, T candidateType, LocationResources locationResources, MatchingConfiguration matchingConfiguration) { // check that the node root properties matches the filters defined on the MatchingConfigurations. Map<String, List<IMatchPropertyConstraint>> configuredFilters = matchingConfiguration == null ? null : matchingConfiguration.getProperties(); if (!isValidTemplatePropertiesMatch(abstractTemplate.getProperties(), candidate.getTemplate().getProperties(), candidateType.getProperties(), configuredFilters)) { return false; } // Overridable matching logic. return typeSpecificMatching(abstractTemplate, candidate, candidateType, locationResources, matchingConfiguration); }
public static String getNodeTagValueOrNull(AbstractTemplate template, String name) { List<Tag> tags = template.getTags(); if (tags != null) { Optional<Tag> first = tags.stream().filter(tag -> tag.getName().equals(name)).findFirst(); if (first.isPresent()) { return first.get().getValue(); } } return null; }
/** * Checks if the type of a LocationResourceTemplate is matching the expected type. * * @param abstractTemplate The template to match. * @param candidateType The type of the candidate. * @return True if the candidate type matches the template type, false if not. */ private boolean isCandidateTypeValid(V abstractTemplate, T candidateType) { return candidateType.getElementId().equals(abstractTemplate.getType()) || safe(candidateType.getDerivedFrom()).contains(abstractTemplate.getType()); } }
/** * * @param abstractTemplate * @param type * @param candidates * @param candidateTypes * @param matchingConfigurations */ public List<R> match(V abstractTemplate, T type, List<R> candidates, Map<String, T> candidateTypes, LocationResources locationResources, Map<String, MatchingConfiguration> matchingConfigurations) { List<R> matchingResults = Lists.newArrayList(); for (R candidate : candidates) { T candidateType = candidateTypes.get(candidate.getTemplate().getType()); if (isValidCandidate(abstractTemplate, type, candidate, candidateType, locationResources, matchingConfigurations)) { matchingResults.add(candidate); } } // TODO Sort the matching results to get the best match for the driver. return matchingResults; }
private void fillLocationResourceTypes(Collection<? extends AbstractLocationResourceTemplate> resourceTemplates, IResourceTypeFiller resourceTypeFiller) { Map<String, Set<String>> resourceTypesByLocationId = Maps.newHashMap(); for (AbstractLocationResourceTemplate resourceTemplate : resourceTemplates) { if (!resourceTemplate.isService()) { Set<String> locationResourceTypesSet = resourceTypesByLocationId.computeIfAbsent(resourceTemplate.getLocationId(), k -> Sets.newHashSet()); locationResourceTypesSet.add(resourceTemplate.getTemplate().getType()); } } for (Map.Entry<String, Set<String>> resourceTypeByLocationIdEntry : resourceTypesByLocationId.entrySet()) { String locationId = resourceTypeByLocationIdEntry.getKey(); Set<String> exposedTypes = resourceTypeByLocationIdEntry.getValue(); Location location = locationService.getOrFail(locationId); resourceTypeFiller.process(exposedTypes, location.getDependencies()); } }
private <T extends AbstractLocationResourceTemplate> void loadResourcesTypes(List<T> resources, Location location, ToscaTypeLoader toscaTypeLoader) { for (T resource : resources) { String type = resource.getTemplate().getType(); AbstractToscaType toscaType = csarRepoSearchService.getRequiredElementInDependencies(AbstractToscaType.class, type, location.getDependencies()); toscaTypeLoader.loadType(resource.getTemplate().getType(), csarDependencyLoader.buildDependencyBean(toscaType.getArchiveName(), toscaType.getArchiveVersion())); } }
@Override public void setTemplateProperty(String resourceId, String propertyName, Object propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException { AbstractLocationResourceTemplate resourceTemplate = getOrFail(resourceId); Location location = locationService.getOrFail(resourceTemplate.getLocationId()); AbstractInheritableToscaType resourceType = (AbstractInheritableToscaType) csarRepoSearchService .getRequiredElementInDependencies(AbstractToscaType.class, resourceTemplate.getTemplate().getType(), location.getDependencies()); if (!safe(resourceType.getProperties()).containsKey(propertyName)) { throw new NotFoundException("Property [" + propertyName + "] is not found in type [" + resourceType.getElementId() + "]"); } propertyService.setPropertyValue(location.getDependencies(), resourceTemplate.getTemplate(), resourceType.getProperties().get(propertyName), propertyName, propertyValue); saveResource(resourceTemplate); }
private void checkSubstitutions(List<SubstitutionSetting> expectedSubstitutionSettings, ITemplateSubstitutionInfoAccessor substitutionsAccessor) throws IOException { DeploymentTopologyDTO dto = getDTOAndassertNotNull(); Map<String, String> substitutions = substitutionsAccessor.getSubstitutions(dto); Map<String, ? extends AbstractLocationResourceTemplate> resources = substitutionsAccessor.getSubstitutionTemplates(dto); assertTrue(MapUtils.isNotEmpty(substitutions)); assertTrue(MapUtils.isNotEmpty(resources)); for (SubstitutionSetting substitutionSetting : expectedSubstitutionSettings) { String substituteId = substitutions.get(substitutionSetting.getTemplateNme()); assertNotNull(substituteId); AbstractLocationResourceTemplate substitute = resources.get(substituteId); assertNotNull(substitute); assertEquals(substitutionSetting.getResourceName(), substitute.getName()); assertEquals(substitutionSetting.getResourceType(), substitute.getTemplate().getType()); } }