/** * Set a capabiltiy on the node template (and initialize the capability maps if null). * * @param nodeTemplate The node template in which to add the capability. * @param name The name of the capability to add. * @param capability The capability. */ public static void setCapability(NodeTemplate nodeTemplate, String name, Capability capability) { if (nodeTemplate.getCapabilities() == null) { nodeTemplate.setCapabilities(Maps.newHashMap()); } nodeTemplate.getCapabilities().put(name, capability); }
private void chekCapability(String nodeTemplateName, String capabilityName, NodeTemplate nodeTemplate) { boolean capablityExists = false; if (nodeTemplate.getCapabilities() != null) { for (Map.Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) { if (capaEntry.getKey().equals(capabilityName)) { capablityExists = true; } } } if (!capablityExists) { throw new NotFoundException("A capability with name [" + capabilityName + "] cannot be found in the target node [" + nodeTemplateName + "]."); } } }
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; } }
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 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)); } }
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; }
private Capability getOrFailCapability(NodeTemplate nodeTemplate, String capabilityName) { Capability capability = MapUtils.getObject(nodeTemplate.getCapabilities(), capabilityName); if (capability != null) { return capability; } throw new NotFoundException("Capability [" + capabilityName + "] not found in template."); }
/** * 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 void check(SetNodeCapabilityPropertyAsOutputOperation operation, Topology topology, NodeTemplate nodeTemplate) { if (nodeTemplate.getCapabilities() == null || nodeTemplate.getCapabilities().get(operation.getCapabilityName()) == null) { throw new NotFoundException("Capability " + operation.getCapabilityName() + " not found in node template " + operation.getNodeName()); } Capability capabilityTemplate = nodeTemplate.getCapabilities().get(operation.getCapabilityName()); CapabilityType indexedCapabilityType = EditionContextManager.get().getToscaContext().getElement(CapabilityType.class, capabilityTemplate.getType(), true); if (indexedCapabilityType.getProperties() == null || !indexedCapabilityType.getProperties().containsKey(operation.getPropertyName())) { throw new NotFoundException("Property " + operation.getPropertyName() + " not found in capability " + operation.getCapabilityName() + " of node " + operation.getNodeName()); } }
@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); }
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); }
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); }
public ServiceNodeTemplate(NodeTemplate nodeTemplate, Map<String, String> attributeValues) { super(nodeTemplate.getType(), nodeTemplate.getProperties(), nodeTemplate.getAttributes(), nodeTemplate.getRelationships(), nodeTemplate.getRequirements(), nodeTemplate.getCapabilities(), nodeTemplate.getInterfaces(), nodeTemplate.getArtifacts()); this.attributeValues = attributeValues; }
@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))); }
@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)); }
@Then("^the node \"([^\"]*)\" in the topology dto should not have the capability \"([^\"]*)\"$") public void I_node_in_the_topology_dto_should_not_have_the_capability(String nodeName, String capabilityName) throws IOException { TopologyDTO dto = JsonUtil.read(Context.getInstance().getRestResponse(), TopologyDTO.class, Context.getJsonMapper()).getData(); NodeTemplate template = dto.getTopology().getNodeTemplates().get(nodeName); Assert.assertNotNull(template); assertFalse(template.getCapabilities().containsKey(capabilityName)); }
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)); }
@Given("^I define the capability \"(.*?)\" property \"(.*?)\" of the node \"(.*?)\" as input property$") public void i_define_the_capability_property_of_the_node_of_typeId_as_input_property(String capabilityName, String propertyName, String nodeName) throws Throwable { String url = String.format("/rest/v1/topologies/%s", Context.getInstance().getTopologyId()); String response = Context.getRestClientInstance().get(url); TopologyDTO topologyDTO = JsonUtil.read(response, TopologyDTO.class, Context.getJsonMapper()).getData(); NodeTemplate template = MapUtils.getObject(topologyDTO.getTopology().getNodeTemplates(), nodeName); Capability capability = template.getCapabilities().get(capabilityName); CapabilityType capabilityType = topologyDTO.getCapabilityTypes().get(capability.getType()); PropertyDefinition propertyDefinition = capabilityType.getProperties().get(propertyName); String fullUrl = String.format("/rest/v1/topologies/%s/inputs/%s", Context.getInstance().getTopologyId(), propertyName); String json = JsonUtil.toString(propertyDefinition); Context.getInstance().registerRestResponse(Context.getRestClientInstance().postJSon(fullUrl, json)); }