@Override public void process(final NodeTemplate instance) { referencePostProcessor.process(new ReferencePostProcessor.TypeReference(instance, instance.getType(), NodeType.class)); final NodeType nodeType = ToscaContext.get(NodeType.class, instance.getType()); if (nodeType == null) { return; // error managed by the reference post processor. safe(instance.getArtifacts()).values().forEach(templateDeploymentArtifactPostProcessor); safe(instance.getInterfaces()).values().stream().flatMap(anInterface -> safe(anInterface.getOperations()).values().stream()) .map(Operation::getImplementationArtifact).filter(Objects::nonNull).forEach(implementationArtifactPostProcessor); safe(instance.getCapabilities()).keySet().forEach(s -> { if (!safe(tempObject.getCapabilities()).containsKey(s)) { Node node = ParsingContextExecution.getObjectToNodeMap().get(s); ParsingContextExecution.getParsingErrors() instance.setAttributes(tempObject.getAttributes()); instance.setCapabilities(tempObject.getCapabilities()); instance.setProperties(tempObject.getProperties()); instance.setRequirements(tempObject.getRequirements()); instance.setArtifacts(tempObject.getArtifacts()); instance.setInterfaces(tempObject.getInterfaces()); safe(instance.getCapabilities()).entrySet().forEach(capabilityPostProcessor); safe(instance.getRequirements()).entrySet().forEach(requirementPostProcessor); propertyValueChecker.checkProperties(nodeType, instance.getProperties(), instance.getName());
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; }
/** * Manage node group members when a node name is removed or its name has changed. * * @param newName : the new name of the node or <code>null</code> if the node has been removed. */ public static void updateGroupMembers(Topology topology, NodeTemplate template, String nodeName, String newName) { Map<String, NodeGroup> topologyGroups = topology.getGroups(); if (template.getGroups() != null && !template.getGroups().isEmpty() && topologyGroups != null) { for (String groupId : template.getGroups()) { NodeGroup nodeGroup = topologyGroups.get(groupId); if (nodeGroup != null && nodeGroup.getMembers() != null) { boolean removed = nodeGroup.getMembers().remove(nodeName); if (removed && newName != null) { nodeGroup.getMembers().add(newName); } } } } }
/** * 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); }
/** * Build a node template. Note that a Tosca Context is required. * * @param nodeType the type of the node * @param templateToMerge the template that can be used to merge into the new node template * @param adaptToType This flag allow to know if we should adapt the templateToMerge node to the type. * @return new constructed node template. */ public static NodeTemplate buildNodeTemplate(NodeType nodeType, NodeTemplate templateToMerge, boolean adaptToType) { // clone the type of the node to avoid impacts, this should be improved in the future nodeType = CloneUtil.clone(nodeType); NodeTemplate nodeTemplate = new NodeTemplate(); fillAbstractInstantiableTemplate(nodeTemplate, nodeType, templateToMerge, !adaptToType); nodeTemplate.setCapabilities(Maps.newLinkedHashMap()); nodeTemplate.setRequirements(Maps.newLinkedHashMap()); fillCapabilitiesMap(nodeTemplate.getCapabilities(), nodeType.getCapabilities(), templateToMerge != null ? templateToMerge.getCapabilities() : null, adaptToType); fillRequirementsMap(nodeTemplate.getRequirements(), nodeType.getRequirements(), templateToMerge != null ? templateToMerge.getRequirements() : null, adaptToType); if (templateToMerge != null && templateToMerge.getRelationships() != null) { nodeTemplate.setRelationships(templateToMerge.getRelationships()); } if (templateToMerge != null && templateToMerge.getTags() != null) { nodeTemplate.setTags(templateToMerge.getTags()); } return nodeTemplate; }
if (serviceResource.getNodeInstance().getNodeTemplate().getProperties() == null) { serviceResource.getNodeInstance().getNodeTemplate().setProperties(Maps.newHashMap()); serviceResource.getNodeInstance().getNodeTemplate().getProperties().putAll(safe(topology.getAllInputProperties())); NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputPropEntry.getKey()); for (String prop : nodeOutputPropEntry.getValue()) { serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(nodeTemplate.getProperties().get(prop))); NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputCapaPropEntry.getKey()); for (Entry<String, Set<String>> outputCapaPropEntry : nodeOutputCapaPropEntry.getValue().entrySet()) { Capability capability = nodeTemplate.getCapabilities().get(outputCapaPropEntry.getKey()); for (String prop : outputCapaPropEntry.getValue()) { serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(capability.getProperties().get(prop))); serviceResource.getNodeInstance().getNodeTemplate().setCapabilities(Maps.newLinkedHashMap()); Capability deployedCapability = topology.getNodeTemplates().get(capabilityMapping.getValue().getNodeTemplateName()).getCapabilities() .get(capabilityMapping.getValue().getTargetId()); serviceResource.getNodeInstance().getNodeTemplate().getCapabilities().put(capabilityMapping.getKey(), deployedCapability); serviceResource.getNodeInstance().getNodeTemplate().setRequirements(Maps.newLinkedHashMap()); serviceResource.getNodeInstance().getNodeTemplate().getRequirements().put(requirementMapping.getKey(), topology.getNodeTemplates() .get(requirementMapping.getValue().getNodeTemplateName()).getRequirements().get(requirementMapping.getValue().getTargetId()));
processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties()); if (nodeTemplate.getRelationships() != null) { for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) { RelationshipTemplate relationshipTemplate = relEntry.getValue(); processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties()); if (nodeTemplate.getCapabilities() != null) { for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) { Capability capability = capaEntry.getValue(); processGetInput(evaluatorContext, nodeTemplate, capability.getProperties()); if (nodeTemplate.getRequirements() != null) { for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) { Requirement requirement = requirementEntry.getValue(); processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
private void updateRuntimeTopology(DeploymentTopology runtimeTopo, PaaSInstancePersistentResourceMonitorEvent persistentResourceEvent, Map<String, Object> persistentProperties) { NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(runtimeTopo, persistentResourceEvent.getNodeTemplateId()); log.info("Updating Runtime topology: Storage NodeTemplate <{}.{}> to add a new volumeId", runtimeTopo.getId(), persistentResourceEvent.getNodeTemplateId()); for (String key : persistentProperties.keySet()) { nodeTemplate.getProperties().put(key, getPropertyValue(persistentProperties.get(key))); log.debug("Property [ {} ] to update: [ {} ]. New value is [ {} ]", key, persistentResourceEvent.getPersistentProperties().get(key), persistentProperties.get(key)); } alienMonitorDao.save(runtimeTopo); }
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 boolean areRelationshipsArtifactSupported(LocationMatchNodeFilter.NodeMatchContext matchContext) { if (MapUtils.isNotEmpty(matchContext.getTemplate().getRelationships())) { for (RelationshipTemplate relTemplate : matchContext.getTemplate().getRelationships().values()) { if (!isEligible(relTemplate, matchContext)) { return false; } } } return true; } }
private Map<String, NodeType> getNodeTypes(Topology topology) { Map<String, NodeType> nodeTypes = Maps.newHashMap(); for (NodeTemplate template : safe(topology.getNodeTemplates()).values()) { if (!nodeTypes.containsKey(template.getType())) { NodeType nodeType = ToscaContext.getOrFail(NodeType.class, template.getType()); nodeTypes.put(nodeType.getElementId(), nodeType); } } return nodeTypes; } }
@Override protected void processNodeOperation(Csar csar, Topology topology, UnsetNodePropertyAsSecretOperation operation, NodeTemplate nodeTemplate) { // check if the node property value is a get_secret AbstractPropertyValue currentValue = nodeTemplate.getProperties().get(operation.getPropertyName()); if (currentValue != null && !isGetSecret(currentValue)) { throw new NotFoundException("Property {} of node {} is not associated to an secret.", operation.getPropertyName(), operation.getNodeName()); } NodeType nodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); PropertyDefinition nodePropertyDefinition = getOrFail(nodeType.getProperties(), operation.getPropertyName(), "Property {} do not exist for node {}", operation.getPropertyName(), operation.getNodeName()); AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(nodePropertyDefinition); nodeTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue); log.debug("Remove secret property [ {} ] of the node template [ {} ] of the topology [ {} ].", operation.getPropertyName(), operation.getNodeName(), topology.getId()); } }
/** * Check if the upperBound of a requirement is reached on a node template * * @param nodeTemplate the node to check for requirement bound * @param requirementName the name of the requirement * @param dependencies the dependencies of the topology * @return true if requirement upper bound is reached, false otherwise */ public boolean isRequirementUpperBoundReachedForSource(NodeTemplate nodeTemplate, String requirementName, Set<CSARDependency> dependencies) { NodeType relatedIndexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), dependencies); Requirement requirement = nodeTemplate.getRequirements().get(requirementName); if (nodeTemplate.getRelationships() == null || nodeTemplate.getRelationships().isEmpty()) { return false; } RequirementDefinition requirementDefinition = getRequirementDefinition(relatedIndexedNodeType.getRequirements(), requirementName, requirement.getType()); if (requirementDefinition.getUpperBound() == Integer.MAX_VALUE) { return false; } int count = countRelationshipsForRequirement(nodeTemplate, requirementDefinition); return count >= requirementDefinition.getUpperBound(); }
@Override protected void processNodeOperation(Csar csar, Topology topology, UpdateDockerImageOperation operation, NodeTemplate nodeTemplate) { NodeType nodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); if (!ToscaTypeUtils.isOfType(nodeType, NormativeNodeTypesConstants.DOCKER_TYPE)) { throw new IllegalArgumentException("Updating docker image can only be done on docker nodes. [" + nodeTemplate.getName() + "] does not inherit from [" + NormativeNodeTypesConstants.DOCKER_TYPE + "]."); Interface standard = safe(nodeTemplate.getInterfaces()).get(ToscaNodeLifecycleConstants.STANDARD); if (standard == null) { standard = new Interface(ToscaNodeLifecycleConstants.STANDARD); if (nodeTemplate.getInterfaces() == null) { nodeTemplate.setInterfaces(Maps.newHashMap()); nodeTemplate.getInterfaces().put(ToscaNodeLifecycleConstants.STANDARD, standard);
@Override public void process(Csar csar, Topology topology, ResetNodeDeploymentArtifactOperation operation) { // Get the node template's artifacts to reset Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology); NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates); DeploymentArtifact currentArtifact = nodeTemplate.getArtifacts() == null ? null : nodeTemplate.getArtifacts().get(operation.getArtifactName()); if (currentArtifact == null) { throw new NotFoundException( "Artifact with key [" + operation.getArtifactName() + "] do not exist in node template [" + nodeTemplate.getName() + "]."); } // Get the node type's artifact Map<String, NodeType> nodeTypes = topologyServiceCore.getIndexedNodeTypesFromTopology(topology, false, false, true); NodeType nodeType = nodeTypes.get(nodeTemplate.getType()); DeploymentArtifact artifactFromNodeType = nodeType.getArtifacts() == null ? null : nodeType.getArtifacts().get(operation.getArtifactName()); if (artifactFromNodeType == null) { throw new NotFoundException("Artifact with key [" + operation.getArtifactName() + "] do not exist in node type [" + nodeType.getId() + "]."); } currentArtifact.setArtifactRef(artifactFromNodeType.getArtifactRef()); currentArtifact.setArtifactName(artifactFromNodeType.getArtifactName()); currentArtifact.setArtifactType(artifactFromNodeType.getArtifactType()); currentArtifact.setArtifactRepository(artifactFromNodeType.getArtifactRepository()); currentArtifact.setRepositoryName(artifactFromNodeType.getRepositoryName()); currentArtifact.setRepositoryURL(artifactFromNodeType.getRepositoryURL()); currentArtifact.setRepositoryCredential(artifactFromNodeType.getRepositoryCredential()); } }
if (AlienUtils.safe(sourceNode.getRelationships()).containsKey(operation.getRelationshipName())) { throw new AlreadyExistException("Relationship " + operation.getRelationshipName() + " already exist on node " + operation.getNodeName()); if (sourceNode.getRequirements() == null || sourceNode.getRequirements().get(operation.getRequirementName()) == null) { throw new NotFoundException( "Unable to find requirement with name <" + operation.getRequirementName() + "> on the source node" + operation.getNodeName()); NodeTemplate newSourceNode = topology.getNodeTemplates().get(sourceNode.getName()); if (sourceNode != newSourceNode) { Map<String, RelationshipTemplate> relationships = sourceNode.getRelationships(); if (relationships == null) { relationships = Maps.newHashMap(); sourceNode.setRelationships(relationships); relationshipTemplate.setTargetedCapabilityName(operation.getTargetedCapabilityName()); relationshipTemplate.setRequirementName(operation.getRequirementName()); relationshipTemplate.setRequirementType(sourceNode.getRequirements().get(operation.getRequirementName()).getType()); relationshipTemplate.setType(indexedRelationshipType.getElementId()); relationshipTemplate.setArtifacts(newLinkedHashMap(indexedRelationshipType.getArtifacts()));
@Override public void process(Csar csar, Topology topology, DeleteFileOperation operation) { // Topology topology = EditionContextManager.get().getTopology(); if (csar.getYamlFilePath().equals(operation.getPath())) { throw new InvalidPathException("Topology yaml file cannot be removed."); } TreeNode target = FileProcessorHelper.getFileTreeNode(operation.getPath()); target.getParent().getChildren().remove(target); for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) { for (DeploymentArtifact artifact : safe(nodeTemplate.getArtifacts()).values()) { resetRemovedArtifact(artifact, operation.getPath()); } cleanupInterfaces(nodeTemplate.getInterfaces(), operation.getPath()); for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) { cleanupInterfaces(relationshipTemplate.getInterfaces(), operation.getPath()); } } }
public static void doWithTopologyArtifacts(Topology topology, DoWithArtifact doWithArtifact) { safe(topology.getInputArtifacts()).values().forEach(doWithArtifact::doWithArtifact); safe(topology.getNodeTemplates()).values().forEach(nodeTemplate -> { safe(nodeTemplate.getArtifacts()).values().forEach(doWithArtifact::doWithArtifact); safe(nodeTemplate.getRelationships()).values() .forEach(relationshipTemplate -> safe(relationshipTemplate.getArtifacts()).values().forEach(doWithArtifact::doWithArtifact)); }); }
@Override protected void processNodeOperation(Csar csar, Topology topology, RebuildNodeOperation operation, NodeTemplate nodeTemplate) { log.debug("Rebuilding the node template [ {} ] of topology [ {} ] .", operation.getNodeName(), topology.getId()); NodeType type = ToscaContext.getOrFail(NodeType.class, nodeTemplate.getType()); // Artifacts are copied from the type to the template // In case of an update of version, we must remove old artifacts copied from old types // FIXME This is very tricky, we must think about stopping copying artifact from types to templates nodeTemplate.getArtifacts().entrySet().removeIf(artifactEntry -> Objects.equals(type.getArchiveName(), artifactEntry.getValue().getArchiveName())); // We need to do this on the whole hierarchy for (String typeName : type.getDerivedFrom()) { NodeType subType = ToscaContext.getOrFail(NodeType.class, typeName); nodeTemplate.getArtifacts().entrySet().removeIf(artifactEntry -> Objects.equals(subType.getArchiveName(), artifactEntry.getValue().getArchiveName())); } NodeTemplate rebuiltNodeTemplate = TemplateBuilder.buildNodeTemplate(type, nodeTemplate); rebuiltNodeTemplate.setName(operation.getNodeName()); topology.getNodeTemplates().put(operation.getNodeName(), rebuiltNodeTemplate); } }
/** * Change policies that target the sourceTemplate and make them target the targetTemplate. * * TODO: move elsewhere ? */ public static void changePolicyTarget(Topology topology, NodeTemplate sourceTemplate, NodeTemplate targetTemplate) { Set<PolicyTemplate> policies = TopologyNavigationUtil.getTargetedPolicies(topology, sourceTemplate); policies.forEach(policyTemplate -> { policyTemplate.getTargets().remove(sourceTemplate.getName()); policyTemplate.getTargets().add(targetTemplate.getName()); }); }