@Override public void process(Map.Entry<String, Capability> instance) { referencePostProcessor.process(new ReferencePostProcessor.TypeReference(instance.getValue(), instance.getValue().getType(), CapabilityType.class)); CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, instance.getValue().getType()); if (capabilityType == null) { return; } propertyValueChecker.checkProperties(capabilityType, instance.getValue().getProperties(), instance.getKey()); } }
if (toAddCapa == null || (StringUtils.isNotBlank(toAddCapa.getType()) && !Objects.equals(toAddCapa.getType(), capa.getType()))) { toAddCapa = new Capability(); toAddCapa.setType(capa.getType()); toAddCapa.setProperties(capaProperties); } else { if (StringUtils.isBlank(toAddCapa.getType())) { toAddCapa.setType(capa.getType()); Map<String, AbstractPropertyValue> nodeCapaProperties = safe(toAddCapa.getProperties()); capaProperties.putAll(nodeCapaProperties); toAddCapa.setProperties(capaProperties); fillProperties(properties, capabilityType != null ? capabilityType.getProperties() : null, toAddCapa.getProperties(), adaptToType); toAddCapa.setProperties(properties); map.put(capa.getId(), toAddCapa);
/** * Set value for a capability property * * @param capability the capability * @param propertyDefinition the definition of the property * @param propertyName the name of the property * @param propertyValue the value of the property */ public void setCapabilityPropertyValue(Capability capability, PropertyDefinition propertyDefinition, String propertyName, Object propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException { if (capability.getProperties() == null) { capability.setProperties(Maps.newLinkedHashMap()); } setPropertyValue(capability.getProperties(), propertyDefinition, propertyName, propertyValue); }
public static void setScalingProperty(String propertyName, int propertyValue, Capability capability) { if (MapUtils.isEmpty(capability.getProperties())) { throw new NotFoundException("The capability scalable has no defined properties, verify your tosca-normative-type archive"); } capability.getProperties().put(propertyName, new ScalarPropertyValue(String.valueOf(propertyValue))); }
private Capability getCapability(NodeTemplate danglingTemplate, String filterCapabilityKey) { for (Entry<String, Capability> capabilityEntry : safe(danglingTemplate.getCapabilities()).entrySet()) { if (filterCapabilityKey.equals(capabilityEntry.getKey()) || filterCapabilityKey.equals(capabilityEntry.getValue().getType())) { return capabilityEntry.getValue(); } } return null; } }
CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, locationCapabilityEntry.getValue().getType()); if (abstractCapability != null && MapUtils.isNotEmpty(abstractCapability.getProperties())) { if (capabilityType != null && !ToscaTypeUtils.isOfType(capabilityType, NormativeCapabilityTypes.SCALABLE)) { locationCapability.setProperties( CollectionUtils.merge(abstractCapability.getProperties(), locationCapability.getProperties(), true, topologyNotMergedProps)); } else { locationCapability.setProperties(abstractCapability.getProperties());
private void addCapabilityToNodeTemplateAndToscaContext(String name, String type, String... derivedFrom) { CapabilityType capabilityType = new CapabilityType(); capabilityType.setDerivedFrom(Arrays.asList(derivedFrom)); capabilityType.setElementId(type); capabilityTypeByTypeName.put(type, capabilityType); Capability capability = new Capability(); capability.setType(type); nodeTemplate.getCapabilities().put(name, capability); }
@Test public void getMissingCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); Capability capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); }
public static int getScalingProperty(String propertyName, Capability capability) { if (MapUtils.isEmpty(capability.getProperties())) { throw new NotFoundException("The capability scalable has no defined properties, verify your tosca-normative-type archive"); } if (!capability.getProperties().containsKey(propertyName)) { throw new NotFoundException(propertyName + " property is not found in the the capability"); } // default value is 1 int propertyValue = 1; String rawPropertyValue = PropertyUtil.getScalarValue(capability.getProperties().get(propertyName)); if (StringUtils.isNotBlank(rawPropertyValue)) { propertyValue = Integer.parseInt(rawPropertyValue); } return propertyValue; }
private boolean isValidRelationship(RelationshipType relationshipType, Capability targetCapability) { if (relationshipType.getValidTargets() == null) { return false; } for (String target : relationshipType.getValidTargets()) { if (targetCapability.getType().equals(target)) { return true; } } return false; }
@Override protected boolean doMergeNode(Topology topology, FlowExecutionContext context, String nodeTemplateId, NodePropsOverride nodePropsOverride) { final ConfigChanged configChanged = new ConfigChanged(); // play the super method first. This will process nodetemplate properties configChanged.changed = super.doMergeNode(topology, context, nodeTemplateId, nodePropsOverride); // Then process capabilities NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeTemplateId); Iterator<Entry<String, NodeCapabilitiesPropsOverride>> capabilitiesOverrideIter = safe(nodePropsOverride.getCapabilities()).entrySet().iterator(); while (capabilitiesOverrideIter.hasNext()) { Entry<String, NodeCapabilitiesPropsOverride> overrideCapabilityProperties = capabilitiesOverrideIter.next(); Capability capability = safe(nodeTemplate.getCapabilities()).get(overrideCapabilityProperties.getKey()); if (capability == null) { // Manage clean logic configChanged.changed = true; capabilitiesOverrideIter.remove(); } else { // Merge logic // When a selected node has changed we may need to cleanup properties that where defined but are not anymore on the capability CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType()); capability.setProperties(mergeProperties(overrideCapabilityProperties.getValue().getProperties(), capability.getProperties(), capabilityType.getProperties(), s -> { configChanged.changed = true; context.log() .info("The property [" + s + "] previously specified to configure capability [" + overrideCapabilityProperties.getKey() + "] of node [" + nodeTemplateId + "] cannot be set anymore as it is already specified by the matched location resource or in the topology."); })); } } return configChanged.changed; }
@Test public void should_not_crash_even_if_node_template_capacities_is_incorrect() throws Exception { nodeTemplate.getCapabilities().clear(); Capability unknownCapacity = new Capability(); unknownCapacity.setType("alien.typo.in.capa"); nodeTemplate.getCapabilities().put("incorrect", unknownCapacity); Map<String, Capability> compatibleCapabilities = service.getCompatibleCapabilityByType(nodeTemplate, "alien.capability.test.Unknown"); assertThat(compatibleCapabilities).hasSize(0); }
@Test(expected = NotFoundException.class) public void getMissingCapabilityByTypeOrFailTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); Capability capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); } }
@Override protected boolean typeSpecificMatching(NodeTemplate abstractTemplate, LocationResourceTemplate candidate, NodeType candidateType, LocationResources locationResources, MatchingConfiguration matchingConfiguration) { for (Entry<String, Capability> candidateCapability : safe(candidate.getTemplate().getCapabilities()).entrySet()) { MatchingFilterDefinition configuredFilterDefinition = matchingConfiguration == null ? null : safe(matchingConfiguration.getCapabilities()).get(candidateCapability.getKey()); Map<String, List<IMatchPropertyConstraint>> configuredFilters = configuredFilterDefinition == null ? null : configuredFilterDefinition.getProperties(); CapabilityType capabilityType = locationResources.getCapabilityTypes().get(candidateCapability.getValue().getType()); // Ignore scalable capabiltiy for matching. if (!ToscaTypeUtils.isOfType(capabilityType, NormativeCapabilityTypes.SCALABLE)) { Capability templateCapability = safe(abstractTemplate.getCapabilities()).get(candidateCapability.getKey()); if (templateCapability != null && !isValidTemplatePropertiesMatch(templateCapability.getProperties(), candidateCapability.getValue().getProperties(), capabilityType.getProperties(), configuredFilters)) { return false; } } } return true; } }
public boolean hasCapabilitiesContainingNotNullProperties(NodeTemplate nodeTemplate) { Map<String, Capability> capabilities = nodeTemplate.getCapabilities(); if (capabilities == null || capabilities.isEmpty()) { return false; } for (Capability capability : capabilities.values()) { if (capability == null) { continue; } if (mapIsNotEmptyAndContainsNotnullValues(capability.getProperties())) { return true; } } return false; }
@Override public Map<String, Capability> getCompatibleCapabilityByType(NodeTemplate nodeTemplate, String type) { Map<String, Capability> capabilities = nodeTemplate.getCapabilities(); if (capabilities == null) { return Collections.emptyMap(); } Map<String, Capability> targetCapabilitiesMatch = Maps.newHashMap(); for (Map.Entry<String, Capability> capabilityEntry : capabilities.entrySet()) { String capabilityTypeName = capabilityEntry.getValue().getType(); CapabilityType capabilityType = toscaContextFinder.find(CapabilityType.class, capabilityTypeName); if (ToscaTypeUtils.isOfType(capabilityType, type)) { targetCapabilitiesMatch.put(capabilityEntry.getKey(), capabilityEntry.getValue()); } } return targetCapabilitiesMatch; }
@Test public void getCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability type exactly equals then no tosca context and request is required Capability capability = getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.SampleCapability"); assertSame(nodeCapability, capability); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertSame(nodeCapability, capability); }
private void setPropertiesFromFilter(NodeTemplate danglingTemplate, NodeType danglingNodeType) { if (danglingTemplate.getNodeFilter() == null) { return; } for (Entry<String, List<PropertyConstraint>> constraintEntry : safe(danglingTemplate.getNodeFilter().getProperties()).entrySet()) { if (constraintEntry.getValue().size() == 1 && constraintEntry.getValue().get(0) instanceof EqualConstraint && ToscaTypes.isSimple(danglingNodeType.getProperties().get(constraintEntry.getKey()).getType())) { danglingTemplate.getProperties().put(constraintEntry.getKey(), new ScalarPropertyValue(((EqualConstraint) constraintEntry.getValue().get(0)).getEqual())); } } for (Entry<String, FilterDefinition> capabilityFilter : safe(danglingTemplate.getNodeFilter().getCapabilities()).entrySet()) { Capability capability = getCapability(danglingTemplate, capabilityFilter.getKey()); CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType()); for (Entry<String, List<PropertyConstraint>> constraintEntry : safe(capabilityFilter.getValue().getProperties()).entrySet()) { if (constraintEntry.getValue().size() == 1 && constraintEntry.getValue().get(0) instanceof EqualConstraint && ToscaTypes.isSimple(capabilityType.getProperties().get(constraintEntry.getKey()).getType())) { capability.getProperties().put(constraintEntry.getKey(), new ScalarPropertyValue(((EqualConstraint) constraintEntry.getValue().get(0)).getEqual())); } } } }
private void updateCapabilitiesProperties(CapabilityType capabilityType, Capability targetCapability, Capability patchCapability) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException { for (Map.Entry<String, AbstractPropertyValue> propertyValueEntry : patchCapability.getProperties().entrySet()) { if (propertyValueEntry.getValue() != null) { AbstractPropertyValue value = PatchUtil.realValue(propertyValueEntry.getValue()); PropertyDefinition propertyDefinition = safe(capabilityType.getProperties()).get(propertyValueEntry.getKey()); if (propertyDefinition == null) { throw new NotFoundException( "No property <" + propertyValueEntry.getKey() + "> can be found for capability <" + capabilityType.getElementId() + ">"); } propertyService.setCapabilityPropertyValue(targetCapability, propertyDefinition, propertyValueEntry.getKey(), value); } } }
/** * Get the possible inputs candidates to be associated with this capability property. */ @ApiOperation(value = "Get the possible inputs candidates to be associated with this capability property.", notes = "Application role required [ APPLICATION_MANAGER | APPLICATION_DEVOPS ]") @RequestMapping(value = "/capability", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public RestResponse<List<String>> getCapabilitiesPropertyInputCandidate( @ApiParam(value = "The topology id.", required = true) @NotBlank @PathVariable final String topologyId, @ApiParam(value = "The node temlate id.", required = true) @NotBlank @RequestParam(value = "nodeTemplateName") final String nodeTemplateName, @ApiParam(value = "The property id.", required = true) @NotBlank @RequestParam(value = "propertyId") final String propertyId, @ApiParam(value = "The capability template id.", required = true) @NotBlank @RequestParam(value = "capabilityId") final String capabilityId) { List<String> inputCandidates = editorService.getInputCandidates(topologyId, nodeTemplateName, nodeTemplate -> { Capability capabilityTemplate = nodeTemplate.getCapabilities().get(capabilityId); return ToscaContext.get(CapabilityType.class, capabilityTemplate.getType()).getProperties().get(propertyId); }); return RestResponseBuilder.<List<String>> builder().data(inputCandidates).build(); }