private String buildRelationShipTemplateName(RelationshipTemplate relationshipTemplate) { String value = relationshipTemplate.getType(); if (value == null) { return null; } if (value.contains(".")) { value = value.substring(value.lastIndexOf(".") + 1); } value = StringUtils.uncapitalize(value); value = value + StringUtils.capitalize(relationshipTemplate.getTarget()); return value; }
@Override protected void processRelationshipOperation(Csar csar, Topology topology, RebuildRelationshipOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) { // rebuild a relationship template based on the current relationship type log.debug("Rebuilding the relationship [ {} ] in the node template [ {} ] of topology [ {} ] .", operation.getRelationshipName(), operation.getNodeName(), topology.getId()); RelationshipType relType = ToscaContext.getOrFail(RelationshipType.class, relationshipTemplate.getType()); Map<String, AbstractPropertyValue> properties = Maps.newHashMap(); TemplateBuilder.fillProperties(properties, relType.getProperties(), relationshipTemplate.getProperties()); relationshipTemplate.setProperties(properties); relationshipTemplate.setAttributes(relType.getAttributes()); } }
public static boolean hasRelationship(NodeTemplate sourceNode, String targetNodeName, String requirementName, String capabilityName) { for (RelationshipTemplate relationshipTemplate : safe(sourceNode.getRelationships()).values()) { if (relationshipTemplate.getTarget().equals(targetNodeName) && relationshipTemplate.getRequirementName().equals(requirementName) && relationshipTemplate.getTargetedCapabilityName().equals(capabilityName)) { return true; } } return false; }
private static void refreshNodeTemplateNameInRelationships(String oldNodeTemplateName, String newNodeTemplateName, Map<String, RelationshipTemplate> relationshipTemplates) { Map<String, String> updatedKeys = Maps.newHashMap(); for (Map.Entry<String, RelationshipTemplate> relationshipTemplateEntry : relationshipTemplates.entrySet()) { String relationshipTemplateId = relationshipTemplateEntry.getKey(); RelationshipTemplate relationshipTemplate = relationshipTemplateEntry.getValue(); if (relationshipTemplate.getTarget().equals(oldNodeTemplateName)) { relationshipTemplate.setTarget(newNodeTemplateName); String formatedOldNodeName = getRelationShipName(relationshipTemplate.getType(), oldNodeTemplateName); // if the id/name of the relationship is auto-generated we should update it also as auto-generation is <typeName+targetId> if (relationshipTemplateId.equals(formatedOldNodeName)) { // check that the new name is not already used (so we won't override another relationship)... String validNewRelationshipTemplateId = getNexAvailableName(getRelationShipName(relationshipTemplate.getType(), newNodeTemplateName), "", relationshipTemplates.keySet()); updatedKeys.put(relationshipTemplateId, validNewRelationshipTemplateId); } } } // update the relationship keys if any has been impacted for (Map.Entry<String, String> updateKeyEntry : updatedKeys.entrySet()) { RelationshipTemplate relationshipTemplate = relationshipTemplates.remove(updateKeyEntry.getKey()); relationshipTemplates.put(updateKeyEntry.getValue(), relationshipTemplate); } }
public void process(NodeType nodeTemplateType, Map.Entry<String, RelationshipTemplate> instance) { RelationshipTemplate relationshipTemplate = instance.getValue(); if (relationshipTemplate.getTarget() == null) { Node node = ParsingContextExecution.getObjectToNodeMap().get(instance); RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType()); propertyValueChecker.checkProperties(relationshipType, relationshipTemplate.getProperties(), instance.getKey()); RequirementDefinition rd = getRequirementDefinitionByName(nodeTemplateType, relationshipTemplate.getRequirementName()); if (rd == null) { Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getRequirementName()); ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName())); return; if (relationshipTemplate.getType() == null) { relationshipTemplate.setType(rd.getRelationshipType()); referencePostProcessor.process(new ReferencePostProcessor.TypeReference(relationshipTemplate, relationshipTemplate.getType(), RelationshipType.class)); relationshipTemplate.setRequirementType(rd.getType()); NodeTemplate targetNodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(relationshipTemplate.getTarget()); if (targetNodeTemplate == null) { Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getTarget()); ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getTarget())); return; String capabilityStr = relationshipTemplate.getTargetedCapabilityName(); // alien actually supports a capability type in the TOSCA yaml
nodeTemplate.getRelationships().keySet()); RelationshipTemplate relationshipTemplate = new RelationshipTemplate(); relationshipTemplate.setName(danglingRelationshipTemplateName); relationshipTemplate.setTarget(danglingTemplateName); relationshipTemplate.setTargetedCapabilityName(targetCapabilityName); relationshipTemplate.setRequirementName(requirementDefinition.getId()); relationshipTemplate.setRequirementType(requirementDefinition.getType()); relationshipTemplate.setType(danglingRelationshipType.getElementId()); relationshipTemplate.setArtifacts(newLinkedHashMap(safe(danglingRelationshipType.getArtifacts()))); relationshipTemplate.setAttributes(newLinkedHashMap(safe(danglingRelationshipType.getAttributes()))); Map<String, AbstractPropertyValue> properties = new LinkedHashMap(); TemplateBuilder.fillProperties(properties, danglingRelationshipType.getProperties(), null); relationshipTemplate.setProperties(properties);
private void updateRelationshipsCapabilitiesRelationships(Topology topology, NodeTemplate nodeTemplate) { List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTemplate.getName(), topology.getNodeTemplates()); for (RelationshipEntry targetRelationshipEntry : targetRelationships) { RelationshipTemplate targetRelationship = targetRelationshipEntry.getRelationship(); Capability capability = safe(nodeTemplate.getCapabilities()).get(targetRelationship.getTargetedCapabilityName()); if (capability == null || isCapabilityNotOfType(capability, targetRelationship.getRequirementType())) { Entry<String, Capability> capabilityEntry = NodeTemplateUtils.getCapabilitEntryyByType(nodeTemplate, targetRelationship.getRequirementType()); targetRelationship.setTargetedCapabilityName(capabilityEntry.getKey()); // check that the relationship type is still valid with the new capability RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, targetRelationship.getType()); if (!isValidRelationship(relationshipType, capabilityEntry.getValue())) { NodeType sourceNodeType = ToscaContext.get(NodeType.class, targetRelationshipEntry.getSource().getType()); RequirementDefinition requirementDefinition = NodeTypeUtils.getRequirementById(sourceNodeType, targetRelationshipEntry.getRelationship().getRequirementName()); NodeType targetNodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); CapabilityDefinition capabilityDefinition = NodeTypeUtils.getCapabilityById(targetNodeType, capabilityEntry.getKey()); RelationshipType validRelationshipType = danglingRequirementService.fetchValidRelationshipType(requirementDefinition, capabilityDefinition); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setArtifacts(newLinkedHashMap(safe(validRelationshipType.getArtifacts()))); targetRelationship.setAttributes(newLinkedHashMap(safe(validRelationshipType.getAttributes()))); Map<String, AbstractPropertyValue> properties = new LinkedHashMap(); TemplateBuilder.fillProperties(properties, validRelationshipType.getProperties(), null); targetRelationship.setProperties(properties); } } } }
RelationshipTemplate relationshipTemplate = new RelationshipTemplate(); String relationshipId = scalarParser.parse(assignmentNode.getValue().get(0).getKeyNode(), context); relationshipTemplate.setName(relationshipId); relationshipTemplate.setRequirementName(relationshipId); relationshipTemplate.setTarget(scalarParser.parse(node, context)); } else if (node instanceof MappingNode) { switch (key) { case "node": relationshipTemplate.setTarget(scalarParser.parse(nodeTuple.getValueNode(), context)); break; case "capability": relationshipTemplate.setTargetedCapabilityName(scalarParser.parse(nodeTuple.getValueNode(), context)); break; case "type_requirement": relationshipTemplate.setRequirementName(scalarParser.parse(nodeTuple.getValueNode(), context)); break; case "relationship": relationshipTemplate.setType(scalarParser.parse(nodeTuple.getValueNode(), context)); break; case "properties": INodeParser<AbstractPropertyValue> propertyValueParser = context.getRegistry().get("node_template_property"); MapParser<AbstractPropertyValue> mapParser = baseParserFactory.getMapParser(propertyValueParser, "node_template_property"); relationshipTemplate.setProperties(mapParser.parse(nodeTuple.getValueNode(), context)); break; case "interfaces":
@Override protected void processRelationshipOperation(Csar csar, Topology topology, UnsetRelationshipPropertyAsSecretOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) { RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType()); PropertyDefinition relationshipPropertyDefinition = getOrFail(relationshipType.getProperties(), operation.getPropertyName(), "Property {} do not exist for relationship {} of node {}", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName()); AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(relationshipPropertyDefinition); relationshipTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue); log.debug("Remove secret from property [ {} ] of relationship template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName(), topology.getId()); } }
String relationShipKey = e.getKey(); RelationshipTemplate proxyRelationShip = e.getValue(); String requirementName = proxyRelationShip.getRequirementName(); SubstitutionTarget substitutionTarget = compositionCouple.child.getSubstitutionMapping().getRequirements().get(requirementName); NodeTemplate nodeTemplate = compositionCouple.child.getNodeTemplates().get(substitutionTarget.getNodeTemplateName()); proxyRelationShip.setRequirementName(substitutionTarget.getTargetId()); if (otherNodes.getRelationships() != null) { for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) { if (relationshipTemplate.getTarget().equals(compositionCouple.nodeName)) { SubstitutionTarget st = compositionCouple.child.getSubstitutionMapping().getCapabilities() .get(relationshipTemplate.getTargetedCapabilityName()); relationshipTemplate.setTarget(st.getNodeTemplateName()); relationshipTemplate.setTargetedCapabilityName(st.getTargetId());
@Then("^I should have a relationship \"([^\"]*)\" with type \"([^\"]*)\" from \"([^\"]*)\" to \"([^\"]*)\" in ALIEN$") public void I_should_have_a_relationship_with_type_from_to_in_ALIEN(String relName, String relType, String source, String target) throws Throwable { // I should have a relationship with type String topologyJson = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId()); RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyJson, TopologyDTO.class, Context.getJsonMapper()); NodeTemplate sourceNode = topologyResponse.getData().getTopology().getNodeTemplates().get(source); relName = relName == null || relName.isEmpty() ? getRelationShipName(relType, target) : relName; RelationshipTemplate rel = sourceNode.getRelationships().get(relName); assertNotNull(rel); assertEquals(relType, rel.getType()); assertEquals(target, rel.getTarget()); assertNotNull(rel.getRequirementName()); assertNotNull(rel.getRequirementType()); }
typesTobeUnloaded.add(relationshipTemplateEntry.getValue().getType()); workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplateEntry.getKey(), relationshipTemplateEntry.getValue()); if (removedNode.equals(relationshipTemplate.getTarget())) { relationshipsToRemove.add(relationshipTemplate.getName()); typesTobeUnloaded.add(relationshipTemplate.getType()); workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplate.getName(), relationshipTemplate);
/** * Returns all the nodes that this node template targets with the given requirement. */ public static Set<NodeTemplate> getTargetNodes(Topology topology, NodeTemplate nodeTemplate, String requirementName) { Set<NodeTemplate> result = Sets.newHashSet(); for (RelationshipTemplate relationshipTemplate : getTargetRelationships(nodeTemplate, requirementName)) { result.add(topology.getNodeTemplates().get(relationshipTemplate.getTarget())); } return result; }
/** * Get the possible inputs candidates to be associated with this relationship property. */ @ApiOperation(value = "Get the possible inputs candidates to be associated with this relationship property.", notes = "Application role required [ APPLICATION_MANAGER | APPLICATION_DEVOPS ]") @RequestMapping(value = "/relationship", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public RestResponse<List<String>> getRelationshipPropertyInputCandidate( @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 relationship template id.", required = true) @NotBlank @RequestParam(value = "relationshipId") final String relationshipId) { List<String> inputCandidates = editorService.getInputCandidates(topologyId, nodeTemplateName, nodeTemplate -> { RelationshipTemplate relationshipTemplate = nodeTemplate.getRelationships().get(relationshipId); return ToscaContext.get(RelationshipType.class, relationshipTemplate.getType()).getProperties().get(propertyId); }); return RestResponseBuilder.<List<String>> builder().data(inputCandidates).build(); } }
/** * Discard all relationship targeting an "external" node. External here in terms of the hostedOn hierarchy * Copy the valid ones * * @param nodeName * @param validTargets A map of oldNodeName -> duplicatedNodeName, we should keep relationships targeting one of these nodes. */ private void copyAndCleanRelationships(String nodeName, Map<String, String> validTargets, Map<String, NodeTemplate> nodeTemplates, TopologyContext topologyContext) { NodeTemplate nodeTemplate = nodeTemplates.get(nodeName); if (MapUtils.isNotEmpty(nodeTemplate.getRelationships())) { Map<String, RelationshipTemplate> relationships = nodeTemplate.getRelationships(); Set<String> keys = Sets.newHashSet(relationships.keySet()); for (String key : keys) { RelationshipTemplate rel = relationships.remove(key); // check if the target is from the valids one // If so, then rename it, its target and keep it if (validTargets.containsKey(rel.getTarget())) { rel.setName(copyName(rel.getName(), relationships.keySet())); rel.setTarget(validTargets.get(rel.getTarget())); relationships.put(rel.getName(), rel); workflowBuilderService.addRelationship(topologyContext, nodeName, rel.getName()); } } if (relationships.isEmpty()) { nodeTemplate.setRelationships(null); } } }
if (relationshipTemplate.getProperties() == null) { relationshipTemplate.setProperties(Maps.newHashMap()); if (relationshipTemplate.getProperties().containsKey(propertyEntry.getKey())) { throw new IllegalArgumentException( "The service relationship requires to override a property already used/defined by the template relationship. This association cannot be done."); relationshipTemplate.getProperties().put(propertyEntry.getKey(), propertyEntry.getValue().getDefault()); if (relationshipTemplate.getArtifacts() == null) { relationshipTemplate.setArtifacts(Maps.newHashMap()); if (relationshipTemplate.getArtifacts().containsKey(artifactEntry.getKey())) { throw new IllegalArgumentException( "The service relationship requires to override an artifact already used/defined by the template relationship. This association cannot be done."); relationshipTemplate.getArtifacts().put(artifactEntry.getKey(), artifactEntry.getValue());
RelationshipTemplate template = paaSRelationshipTemplate.getTemplate(); injectPropertiesAsInputs(ToscaFunctionConstants.SELF, null, template.getProperties(), paaSRelationshipTemplate.getInterfaces(), baseName -> StringUtils.joinWith(AlienUtils.DEFAULT_PREFIX_SEPARATOR, ToscaFunctionConstants.SELF, baseName)); injectTargetPropertiesAsInputs(paaSNodeTemplates.get(template.getTarget()), paaSRelationshipTemplate.getInterfaces()); injectTargetedCapabilityProperties(paaSNodeTemplates.get(template.getTarget()), template.getTargetedCapabilityName(), paaSRelationshipTemplate.getInterfaces());
/** * validate the requirement:<br> * <ul> * <li>Check that a node still has the requirement, by checking directly from the related node type</li> * <li>Check that the requirement type of the node hasn't changed</li> * </ul> * * @param relationshipTemplate The name of the requirement to check * @param nodeTemplate The node template in which to check for the requirement * @param nodeType * @throws InvalidStateException when something is wrong */ private void checkRequirement(RelationshipTemplate relationshipTemplate, NodeTemplate nodeTemplate, NodeType nodeType) { Map<String, RequirementDefinition> requirementMap = AlienUtils.fromListToMap(nodeType.getRequirements(), "id", true); if (!AlienUtils.safe(requirementMap).containsKey(relationshipTemplate.getRequirementName())) { throw new InvalidStateException( "A requirement with name [" + relationshipTemplate.getName() + "] cannot be found in the node [" + nodeTemplate.getName() + "]."); } // check that the requirement type hasn't changed RequirementDefinition requirement = AlienUtils.safe(requirementMap).get(relationshipTemplate.getRequirementName()); if (!Objects.equals(requirement.getType(), relationshipTemplate.getRequirementType())) { // the requirementType has changed. delete the relationship // TODO check if this requirement is compatible with the previous one, and mabey try to rebuild the relationship throw new InvalidStateException( "The type of the requirement [" + relationshipTemplate.getRequirementName() + "], node [" + nodeTemplate.getName() + "] has changed"); } }
/** * Get the number of relationships from a node template that are actually linked to the given requirement. * * @param nodeTemplate The node template for which to count relationships * @param requirementDefinition The requirement definition from the node template's type for which to count related relationships. * @return The number of relationships connected to the given requirement. */ public static int countRelationshipsForRequirement(NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition) { int count = 0; for (Map.Entry<String, RelationshipTemplate> relEntry : safe(nodeTemplate.getRelationships()).entrySet()) { if (relEntry.getValue().getRequirementName().equals(requirementDefinition.getId()) && relEntry.getValue().getRequirementType().equals(requirementDefinition.getType())) { count++; } } return count; } }
/** * Returns all the nodes that target this node template with the given requirement. */ public static Set<NodeTemplate> getSourceNodes(Topology topology, NodeTemplate nodeTemplate, String capabilityName) { Set<NodeTemplate> result = Sets.newHashSet(); for (NodeTemplate node : topology.getNodeTemplates().values()) { for (RelationshipTemplate relationshipTemplate : safe(node.getRelationships()).values()) { if (relationshipTemplate.getTargetedCapabilityName().equals(capabilityName) && relationshipTemplate.getTarget().equals(nodeTemplate.getName())) { result.add(node); } } } return result; }