/** * 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))); }
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; }
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; }
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); } } }
@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()); } }
/** * Find if an image is windows image, defaults to linux if property is not configured. * * @param image The image to check. * @return True if the image is windows. */ private boolean isWindowsImage(LocationResourceTemplate image) { if (image.getTemplate() == null || safe(image.getTemplate().getCapabilities()).get(NormativeComputeConstants.OS_CAPABILITY) == null || safe(image.getTemplate().getCapabilities().get(NormativeComputeConstants.OS_CAPABILITY).getProperties()) .get(NormativeComputeConstants.OS_TYPE) == null) { return false; } return "windows".equals(((ScalarPropertyValue) image.getTemplate().getCapabilities().get(NormativeComputeConstants.OS_CAPABILITY).getProperties() .get(NormativeComputeConstants.OS_TYPE)).getValue().toLowerCase()); }
public static AbstractPropertyValue getNodeCapabilityPropertyValue(NodeTemplate nodeTemplate, String capabilityName, String propertyPath) { Capability capability = safe(nodeTemplate.getCapabilities()).get(capabilityName); if (capability != null) { return PropertyUtil.getPropertyValueFromPath(safe(capability.getProperties()), propertyPath); } return null; }
private static void injectCapabilitiesProperties(NodeTemplate template, Map<String, Interface> interfaces) { if (template.getCapabilities() == null) { return; } template.getCapabilities().forEach((capabilityName, capability) -> { // input name: CAPABILITIES_<capabilityName>_<propertyName> injectPropertiesAsInputs(ToscaFunctionConstants.SELF, capabilityName, capability.getProperties(), interfaces, baseName -> StringUtils.joinWith(AlienUtils.DEFAULT_PREFIX_SEPARATOR, ToscaFunctionConstants.SELF, CAPABILITIES, capabilityName, baseName)); }); }
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())); } } } }
@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; } }
private static void injectTargetedCapabilityProperties(PaaSNodeTemplate target, String capabilityName, Map<String, Interface> interfaces) { if (target.getTemplate().getCapabilities() != null && target.getTemplate().getCapabilities().containsKey(capabilityName)) { Capability capability = target.getTemplate().getCapabilities().get(capabilityName); // input name: TARGET_CAPABILITIES_<capabilityName>_<propertyName> injectPropertiesAsInputs(ToscaFunctionConstants.TARGET, capabilityName, capability.getProperties(), interfaces, baseName -> StringUtils .joinWith(AlienUtils.DEFAULT_PREFIX_SEPARATOR, ToscaFunctionConstants.TARGET, CAPABILITIES, capabilityName, baseName)); } }
protected void setNodeCappabilityPropertyPathValue(Csar csar, Topology topology, NodeTemplate nodeTemplate, String capabilityName, String propertyPath, AbstractPropertyValue propertyValue, boolean lastPropertyIsAList) { Map<String, AbstractPropertyValue> propertyValues = nodeTemplate.getCapabilities().get(capabilityName).getProperties(); String nodePropertyName = feedPropertyValue(propertyValues, propertyPath, propertyValue, lastPropertyIsAList); Object nodePropertyValue = propertyValues.get(nodePropertyName); UpdateCapabilityPropertyValueOperation operation = new UpdateCapabilityPropertyValueOperation(); operation.setCapabilityName(capabilityName); operation.setNodeName(nodeTemplate.getName()); operation.setPropertyName(nodePropertyName); operation.setPropertyValue(propertyValue); // TODO: can be necessary to serialize value before setting it in case of different types operation.setPropertyValue(nodePropertyValue); updateCapabilityPropertyValueProcessor.process(csar, topology, operation); }
@And("^The topology should contain a nodetemplate named \"([^\"]*)\" with property \"([^\"]*)\" of capability \"([^\"]*)\" set to \"([^\"]*)\"$") public void theTopologyShouldContainANodetemplateNamedWithPropertyOfCapabilitySetToNull(String nodeTemplateName, String propertyName, String capabilityName, String propertyValue) throws Throwable { The_topology_should_contain_a_nodetemplate_named(nodeTemplateName); String topologyResponseText = Context.getInstance().getRestResponse(); NodeTemplate nodeTemp = JsonUtil.read(topologyResponseText, TopologyDTO.class, Context.getJsonMapper()).getData().getTopology().getNodeTemplates() .get(nodeTemplateName); assertNotNull(nodeTemp.getCapabilities().get(capabilityName).getProperties()); if (propertyValue != null) { assertNotNull(nodeTemp.getCapabilities().get(capabilityName).getProperties().get(propertyName)); } assertEquals(propertyValue, PropertyUtil.getScalarValue(nodeTemp.getCapabilities().get(capabilityName).getProperties().get(propertyName))); }
@Override protected void processNodeOperation(Csar csar, Topology topology, UnsetNodeCapabilityPropertyAsSecretOperation operation, NodeTemplate nodeTemplate) { Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} do not exist for node {}", operation.getCapabilityName(), operation.getNodeName()); // check if the node property value is a get_secret AbstractPropertyValue currentValue = capabilityTemplate.getProperties().get(operation.getPropertyName()); if (currentValue != null && !isGetSecret(currentValue)) { throw new NotFoundException("Property {} of node {} is not an secret.", operation.getPropertyName(), operation.getNodeName()); } CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType()); PropertyDefinition capabilityPropertyDefinition = getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName()); AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(capabilityPropertyDefinition); capabilityTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue); log.debug("Remove secret property [ {} ] of capability template [ {} ] of node [ {} ] of the topology [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId()); } }
@Override protected void processNodeOperation(Csar csar, Topology topology, UnsetNodeCapabilityPropertyAsInputOperation operation, NodeTemplate nodeTemplate) { Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} do not exist for node {}", operation.getCapabilityName(), operation.getNodeName()); // check if the node property value is a get_input AbstractPropertyValue currentValue = capabilityTemplate.getProperties().get(operation.getPropertyName()); if (!isGetInput(currentValue)) { throw new NotFoundException("Property {} of node {} is not associated to an input.", operation.getPropertyName(), operation.getNodeName()); } CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType()); PropertyDefinition capabilityPropertyDefinition = getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName()); AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(capabilityPropertyDefinition); capabilityTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue); log.debug("Remove association from property [ {} ] of capability template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId()); } }
@And("^The topology should have the property \"([^\"]*)\" of capability \"([^\"]*)\" of a node \"([^\"]*)\" defined as a secret with a secret path \"([^\"]*)\"$") public void theTopologyShouldHaveThePropertyOfCapabilityOfANodeDefinedAsASecretWithASecretPath(String propertyName, String capabilityName, String nodeName, String secretPath) throws Throwable { String response = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId()); JavaType restResponseType = Context.getJsonMapper().getTypeFactory().constructParametricType(RestResponse.class, TopologyDTO.class); TopologyDTO topologyDTO = ((RestResponse<TopologyDTO>) Context.getJsonMapper().readValue(response, restResponseType)).getData(); FunctionPropertyValue functionPropertyValue = (FunctionPropertyValue) topologyDTO.getTopology().getNodeTemplates().get(nodeName).getCapabilities().get(capabilityName).getProperties().get(propertyName); Assert.assertEquals(secretPath, functionPropertyValue.getParameters().get(0)); }
@Override protected void processNodeOperation(Csar csar, Topology topology, SetNodeCapabilityPropertyAsSecretOperation operation, NodeTemplate nodeTemplate) { Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} does not exist for node {}", operation.getCapabilityName(), operation.getNodeName()); CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType()); getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName()); if (operation.getCapabilityName().equals(FORBIDDEN_CAPABILITY)) { throw new UnsupportedSecretException("We cannot set a secret on the capability " + operation.getCapabilityName()); } if ("".equals(operation.getSecretPath())) { throw new InvalidSecretPathException("The secret path to the property " + operation.getPropertyName() + "is null."); } FunctionPropertyValue getSecret = new FunctionPropertyValue(); getSecret.setFunction(ToscaFunctionConstants.GET_SECRET); getSecret.setParameters(Arrays.asList(operation.getSecretPath())); nodeTemplate.getCapabilities().get(operation.getCapabilityName()).getProperties().put(operation.getPropertyName(), getSecret); log.debug("Set the property [ {} ] of capability template [ {} ] of node [ {} ] to the secret path [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId()); } }
@Override protected void processNodeOperation(Csar csar, Topology topology, SetNodeCapabilityPropertyAsInputOperation operation, NodeTemplate nodeTemplate) { Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} does not exist for node {}", operation.getCapabilityName(), operation.getNodeName()); PropertyDefinition inputPropertyDefinition = getOrFail(topology.getInputs(), operation.getInputName(), "Input {} not found in topology", operation.getInputName()); CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType()); PropertyDefinition capabilityPropertyDefinition = getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName()); // Check that the property definition of the input is indeed compatible with the property definition of the capability. inputPropertyDefinition.checkIfCompatibleOrFail(capabilityPropertyDefinition); FunctionPropertyValue getInput = new FunctionPropertyValue(); getInput.setFunction(ToscaFunctionConstants.GET_INPUT); getInput.setParameters(Arrays.asList(operation.getInputName())); capabilityTemplate.getProperties().put(operation.getPropertyName(), getInput); log.debug("Associate the property [ {} ] of capability template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId()); } }
private void assertCapabilityPropertyValueEquals(NodeTemplate node, String capabilityName, String propertyName, String expectedPropertyValue) { assertNotNull(node); Capability capability = MapUtils.getObject(node.getCapabilities(), capabilityName); assertNotNull(capability); AbstractPropertyValue abstractProperty = MapUtils.getObject(capability.getProperties(), propertyName); assertEquals(expectedPropertyValue, PropertyUtil.getScalarValue(abstractProperty)); }