@Override public <T extends AbstractToscaType> T findElement(Class<T> clazz, String id) { return csarRepoSearchService.getElementInDependencies(clazz, id, topology.getDependencies()); } }
/** * Return a Set of updated {@link CSARDependency} that have changed since last added in a given topology * * @param topology * @return */ public Set<CSARDependency> getUpdatedDependencies(Topology topology) { Set<CSARDependency> dependencies = topology.getDependencies(); Set<CSARDependency> updatedDependencies = Sets.newHashSet(); for (CSARDependency csarDependency : dependencies) { CSARDependency updatedDependency = getUpdatedDependencyIfNeeded(csarDependency); if (updatedDependency != null) { updatedDependencies.add(updatedDependency); } } return updatedDependencies; }
/** * Add locations dependencies */ @Override @SuppressWarnings("unchecked") protected void init(Topology topology, FlowExecutionContext context) { Location location = ((Map<String, Location>) context.getExecutionCache().get(DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY)).get(GROUP_ALL); topology.getDependencies().addAll(location.getDependencies()); ToscaContext.get().resetDependencies(topology.getDependencies()); }
/** * Add locations dependencies */ @Override @SuppressWarnings("unchecked") protected void init(Topology topology, FlowExecutionContext context) { Location location = ((Map<String, Location>) context.getExecutionCache().get(DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY)).get(GROUP_ALL); topology.getDependencies().addAll(location.getDependencies()); ToscaContext.get().resetDependencies(topology.getDependencies()); }
/** * Get an element from the local-cache or from ES. * * @param elementClass The class of the element to look for. * @param elementId The id of the element to look for. * @param <T> The type of element. * @return The requested element. */ public <T extends AbstractToscaType> T getElement(Class<T> elementClass, String elementId) { String elementType = elementClass.getSimpleName(); Map<String, AbstractToscaType> typeElements = toscaTypesCache.get(elementType); if (typeElements == null) { typeElements = new HashMap<>(); toscaTypesCache.put(elementType, typeElements); } else { // find in local-cache T element = (T) typeElements.get(elementId); if (element != null) { return element; } } T element = toscaTypeSearchService.getRequiredElementInDependencies(elementClass, elementId, topology.getDependencies()); typeElements.put(elementId, element); return element; }
private String getRelationshipId(Map<String, RelationshipType> relationshipTypeMap, Topology topology, String relationshipTypeName) { RelationshipType relationshipType = relationshipTypeMap.computeIfAbsent(relationshipTypeName, k -> toscaTypeSearchService.getElementInDependencies(RelationshipType.class, relationshipTypeName, topology.getDependencies())); return relationshipType.getId(); } }
/** * Reset the topology context to it's initial state. * * @param editionClone The clone of the initial topology. // TODO better use an inner java cloning. * @throws IOException In case we wait to initialize the archive content tree. */ public void reset(Topology editionClone) throws IOException { this.topology = editionClone; this.toscaContext = new ToscaContext.Context(topology.getDependencies()); this.archiveContentTree = DirectoryJSonWalker.getDirectoryTree(this.localGitPath); }
/** * Create a new instance of a topology edition context from an existing topology. * * @param csar The archive under edition. * @param topology The topology for which to create the context. * @param localGitPath The git location associated with the topology. */ public EditionContext(Csar csar, Topology topology, Path localGitPath) throws IOException { this.csar = csar; this.topology = topology; this.toscaContext = new ToscaContext.Context(topology.getDependencies()); this.localGitPath = localGitPath; // initialize the file tree based on the git repository location this.archiveContentTree = DirectoryJSonWalker.getDirectoryTree(this.localGitPath); }
/** * Check that the topology can be associated to a release version, actually : check that the topology doesn't reference SNAPSHOT * dependencies. * * @throws @{@link ReleaseReferencingSnapshotException} if the topology references SNAPSHOT dependencies * version. */ private void checkTopologyReleasable(Topology topology) { if (topology.getDependencies() != null) { for (CSARDependency dep : topology.getDependencies()) { // we allow SNAPSHOTS only for tosca-normative-types (we don't expect to have a release soon !) if (VersionUtil.isSnapshot(dep.getVersion()) && !dep.getName().equals("tosca-normative-types")) { throw new ReleaseReferencingSnapshotException(String.format("Can not release: %s dependency is a snapshot", dep.getName())); } } } }
public Map<String, PolicyType> getPolicyTypesFromTopology(Topology topology, boolean failOnTypeNotFound) { Map<String, PolicyType> types = Maps.newHashMap(); for (PolicyTemplate template : safe(topology.getPolicies()).values()) { types.put(template.getType(), getFromContextIfDefined(PolicyType.class, template.getType(), topology.getDependencies(), failOnTypeNotFound)); } return types; }
private void addRelationshipTypeFromSubstitutionTarget(Topology topology, Map<String, RelationshipType> relationshipTypes, SubstitutionTarget substitutionTarget, boolean failOnTypeNotFound) { if (substitutionTarget.getServiceRelationshipType() != null) { RelationshipType relationshipType = getFromContextIfDefined(RelationshipType.class, substitutionTarget.getServiceRelationshipType(), topology.getDependencies(), failOnTypeNotFound); relationshipTypes.put(substitutionTarget.getServiceRelationshipType(), relationshipType); } }
/** * Find replacements nodes for a node template * * @param nodeTemplateName the node to search for replacements * @param topology the topology * @return all possible replacement types for this node */ @SneakyThrows(IOException.class) public NodeType[] findReplacementForNode(String nodeTemplateName, Topology topology) { NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeTemplateName); Map<String, Map<String, Set<String>>> nodeTemplatesToFilters = Maps.newHashMap(); Entry<String, NodeTemplate> nodeTempEntry = Maps.immutableEntry(nodeTemplateName, nodeTemplate); NodeType indexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), topology.getDependencies()); processNodeTemplate(topology, nodeTempEntry, nodeTemplatesToFilters); List<SuggestionsTask> topoTasks = searchForNodeTypes(topology.getWorkspace(), nodeTemplatesToFilters, MapUtil.newHashMap(new String[] { nodeTemplateName }, new NodeType[] { indexedNodeType }), false); if (CollectionUtils.isEmpty(topoTasks)) { return null; } return removeDuplicatedNodeTypeForReplacement(indexedNodeType.getElementId(), topoTasks.get(0).getSuggestedNodeTypes()); }
/** * Deeply explore composition in order to detect cyclic reference: if a descendant references the mainTopologyId. */ public void recursivelyDetectTopologyCompositionCyclicReference(String mainTopologyId, String substitutionTopologyId) { Topology child = topologyServiceCore.getOrFail(substitutionTopologyId); if (child == null || child.getNodeTemplates() == null || child.getNodeTemplates().isEmpty()) { return; } for (Entry<String, NodeTemplate> nodeEntry : child.getNodeTemplates().entrySet()) { String type = nodeEntry.getValue().getType(); NodeType nodeType = csarRepoSearchService.getElementInDependencies(NodeType.class, type, child.getDependencies()); if (nodeType.getSubstitutionTopologyId() != null) { if (nodeType.getSubstitutionTopologyId().equals(mainTopologyId)) { throw new CyclicReferenceException("Cyclic reference : a topology template can not reference itself (even indirectly)"); } recursivelyDetectTopologyCompositionCyclicReference(mainTopologyId, nodeType.getSubstitutionTopologyId()); } } }
/** * Get the indexed node types used in a topology. * * @param topology The topology for which to get indexed node types. * @param abstractOnly If true, only abstract types will be retrieved. * @param useTemplateNameAsKey If true the name of the node template will be used as key for the type in the returned map, if not the type will be used as * key. * @param failOnTypeNotFound * @return A map of indexed node types. */ public Map<String, NodeType> getIndexedNodeTypesFromTopology(Topology topology, boolean abstractOnly, boolean useTemplateNameAsKey, boolean failOnTypeNotFound) { Map<String, NodeType> nodeTypeMap = getIndexedNodeTypesFromDependencies(topology.getNodeTemplates(), topology.getDependencies(), abstractOnly, useTemplateNameAsKey, failOnTypeNotFound); if (!useTemplateNameAsKey && topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) { NodeType nodeType = getFromContextIfDefined(NodeType.class, topology.getSubstitutionMapping().getSubstitutionType(), topology.getDependencies(), failOnTypeNotFound); nodeTypeMap.put(topology.getSubstitutionMapping().getSubstitutionType(), nodeType); } return nodeTypeMap; }
private void validateCommand(OperationExecRequest operationRequest, Topology topology) throws ConstraintFunctionalException { NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology.getId(), operationRequest.getNodeTemplateName(), TopologyUtils.getNodeTemplates(topology)); NodeType indexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), topology.getDependencies()); Map<String, Interface> interfaces = IndexedModelUtils.mergeInterfaces(indexedNodeType.getInterfaces(), nodeTemplate.getInterfaces()); if (interfaces == null || interfaces.get(operationRequest.getInterfaceName()) == null) { throw new NotFoundException("Interface [" + operationRequest.getInterfaceName() + "] not found in the node template [" + operationRequest.getNodeTemplateName() + "] related to [" + indexedNodeType.getId() + "]"); } Interface interfass = interfaces.get(operationRequest.getInterfaceName()); validateOperation(interfass, operationRequest, topology.getDependencies()); }
/** * Deeply explore this topology to detect if some type must be substituted by the corresponding topology template content and feed the {@link Deque}. <br> * BTW, rename the nodes by prefixing all the node names. */ private void recursivelyBuildSubstitutionStack(Topology topology, Deque<CompositionCouple> stack, String prefix) { if (topology == null || topology.getNodeTemplates() == null || topology.getNodeTemplates().isEmpty()) { return; } for (Entry<String, NodeTemplate> nodeEntry : topology.getNodeTemplates().entrySet()) { String nodeName = nodeEntry.getKey(); String type = nodeEntry.getValue().getType(); // FIXME use tosca context, beware of child topologies (dependencies to use ? conflicts ?) NodeType nodeType = csarRepoSearchService.getRequiredElementInDependencies(NodeType.class, type, topology.getDependencies()); if (nodeType.getSubstitutionTopologyId() != null) { // this node type is a proxy for a topology template Topology child = topologyServiceCore.getOrFail(nodeType.getSubstitutionTopologyId()); CompositionCouple couple = new CompositionCouple(topology, child, nodeName, nodeName + "_"); renameNodes(couple); stack.offer(couple); recursivelyBuildSubstitutionStack(child, stack, nodeName + "_"); } } }
@And("^The topology should have as dependencies$") public void The_topology_should_have_as_dependencies(DataTable dependencies) throws Throwable { Set<CSARDependency> expectedDependencies = Sets.newHashSet(); for (List<String> row : dependencies.raw()) { expectedDependencies.add(new CSARDependency(row.get(0), row.get(1))); } String topologyResponseText = Context.getInstance().getRestResponse(); Set<CSARDependency> actualDependencies = JsonUtil.read(topologyResponseText, TopologyDTO.class, Context.getJsonMapper()).getData().getTopology() .getDependencies(); Assert.assertEquals(expectedDependencies, actualDependencies); }
public void updateDependencies(EditionContext context, CSARDependency newDependency) { final Set<CSARDependency> oldDependencies = new HashSet<>(context.getTopology().getDependencies()); final Set<CSARDependency> newDependencies = csarDependencyLoader.getDependencies(newDependency.getName(), newDependency.getVersion()); newDependencies.add(newDependency); // Update context with the new dependencies. newDependencies.forEach(csarDependency -> context.getToscaContext().updateDependency(csarDependency)); // Validate that the dependency change does not induce missing types. try { this.checkForMissingTypes(context.getTopology()); } catch (NotFoundException e) { // Revert changes made to the Context then throw. context.getToscaContext().resetDependencies(oldDependencies); context.getTopology().setDependencies(oldDependencies); throw new VersionConflictException("Changing the dependency [" + newDependency.getName() + "] to version [" + newDependency.getVersion() + "] induces missing types in the topology. Not found : [" + e.getMessage() + "].", e); } // Perform the dependency update on the topology. context.getTopology().setDependencies(new HashSet<>(context.getToscaContext().getDependencies())); }
@Override protected void processServiceResourceReplacement(Topology topology, Map<String, NodeTemplate> topologyTemplateMap, String nodeId, String serviceResourceId) { ServiceResource serviceResource = getServiceResourceService().getOrFail(serviceResourceId); NodeTemplate serviceNodeTemplate = serviceResource.getNodeInstance().getNodeTemplate(); ServiceNodeTemplate substitutionNodeTemplate = new ServiceNodeTemplate(serviceNodeTemplate.getType(), serviceNodeTemplate.getProperties(), serviceNodeTemplate.getAttributes(), serviceNodeTemplate.getRelationships(), serviceNodeTemplate.getRequirements(), serviceNodeTemplate.getCapabilities(), serviceNodeTemplate.getInterfaces(), serviceNodeTemplate.getArtifacts()); substitutionNodeTemplate.setServiceResourceId(serviceResource.getId()); substitutionNodeTemplate.setAttributeValues(serviceResource.getNodeInstance().getAttributeValues()); NodeTemplate abstractTopologyNode = topologyTemplateMap.put(nodeId, substitutionNodeTemplate); substitutionNodeTemplate.setName(abstractTopologyNode.getName()); substitutionNodeTemplate.setRelationships(abstractTopologyNode.getRelationships()); // add all the necessary dependencies to the topology Csar csar = getToscaTypeSearchService().getArchive(serviceResource.getDependency().getName(), serviceResource.getDependency().getVersion()); Set<CSARDependency> dependencies = Sets.newHashSet(); if (csar.getDependencies() != null) { dependencies.addAll(csar.getDependencies()); } dependencies.add(new CSARDependency(csar.getName(), csar.getVersion())); topology.getDependencies().addAll(dependencies); } }
@Override public void process(Csar csar, Topology topology, AddSubstitutionTypeOperation operation) { if (topology.getSubstitutionMapping() == null) { topology.setSubstitutionMapping(new SubstitutionMapping()); } NodeType nodeType = toscaTypeSearchService.getElementInDependencies(NodeType.class, operation.getElementId(), topology.getDependencies()); // if not null the node type exists in the dependencies, there is no choices for this type version if (nodeType == null) { // the node type does'nt exist in this topology dependencies // we need to find the latest version of this component and use it as default nodeType = toscaTypeSearchService.findMostRecent(NodeType.class, operation.getElementId()); if (nodeType == null) { throw new NotFoundException("Node type with name <" + operation.getElementId() + "> cannot be found in the catalog."); } topologyService.loadType(topology, nodeType); } topology.getSubstitutionMapping().setSubstitutionType(operation.getElementId()); } }