@Override public Set<CSARDependency> getDependencies(String name, String version) { Csar csar = csarService.get(name, version); if (csar == null) { throw new NotFoundException("Csar with name [" + name + "] and version [" + version + "] cannot be found"); } if (csar.getDependencies() == null || csar.getDependencies().isEmpty()) { return Sets.newHashSet(); } return Sets.newHashSet(csar.getDependencies()); }
@Override public Topology createTopologyAsTemplate(String name, String description, String version, String workspace, String fromTopologyId) { NameValidationUtils.validate("topologyTemplateName", name); // Every version of a topology template has a Cloud Service Archive Csar csar = new Csar(name, StringUtils.isNotBlank(version) ? version : VersionUtil.DEFAULT_VERSION_NAME); csar.setWorkspace(workspace); csar.setDelegateType(ArchiveDelegateType.CATALOG.toString()); csar.setToscaDefinitionsVersion(ToscaParser.LATEST_DSL); if (description == null) { csar.setDescription("This archive has been created with alien4cloud."); } else { csar.setDescription("Enclosing archive for topology " + description); } Topology topology; if (fromTopologyId != null) { // "cloning" the topology // TODO Currently, the fromTopologyId is always null. If this implementation needed, please think about initializing the workflow topology = alienDAO.findById(Topology.class, fromTopologyId); } else { topology = new Topology(); // Init the workflow if the topology is totally new workflowBuilderService.initWorkflows(workflowBuilderService.buildTopologyContext(topology, csar)); } topology.setDescription(description); topology.setArchiveName(csar.getName()); topology.setArchiveVersion(csar.getVersion()); topology.setWorkspace(csar.getWorkspace()); archiveIndexer.importNewArchive(csar, topology, null); return topology; }
/** * Get a cloud service archive, or fail with {@link NotFoundException} if not found * * @param name The name of the archive. * @param version The version of the archive. * @return The {@link Csar Cloud Service Archive} if found in the repository. */ public Csar getOrFail(String name, String version) { return getOrFail(Csar.createId(name, version)); }
private void checkNotUsedInActiveDeployment(Csar csar) throws CSARUsedInActiveDeployment { if (csar != null && deploymentService.isArchiveDeployed(csar.getName(), csar.getVersion())) { throw new CSARUsedInActiveDeployment("CSAR: " + csar.getName() + ", Version: " + csar.getVersion() + " is used in an active deployment."); } }
/** * Delete the content of the csar from the repository: elements, topologies * * @param csar */ public void deleteCsarContent(Csar csar) { // Delete the topology defined in this archive. csarDAO.delete(Topology.class, csar.getId()); // latest version indicator will be recomputed to match this new reality indexerService.deleteElements(csar.getName(), csar.getVersion()); }
/** * Get an archive from it's id. * * @param name The name of the archive to get. * @param version The version of the archive to get. * @return The archive from it's id. */ public Csar getArchive(String name, String version) { String id = new Csar(name, version).getId(); Csar archive = archivesMap.get(id); log.debug("get archive from map {} {} {}", id, archive); if (archive == null) { archive = csarRepositorySearchService.getArchive(name, version); log.debug("get archive from repo {} {} {}", id, archive, csarRepositorySearchService.getClass().getName()); archivesMap.put(id, archive); } return archive; }
if (archiveRoot.getArchive().getDependencies() == null || archiveRoot.getArchive().getDependencies().isEmpty()) { return; Set<CSARDependency> dependencies = archiveRoot.getArchive().getDependencies(); if (dependencies.contains(new CSARDependency(archive.getName(), archive.getVersion(), archive.getHash()))) { ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.CSAR_IMPORT_ITSELF, AlienUtils.prefixWith(":", archive.getVersion(), archive.getName()), null, "Import itself", null, null)); archiveRoot.getArchive().setDependencies(mergedDependencies);
Csar csar = new Csar(applicationId, version); csar.setWorkspace(APP_WORKSPACE_PREFIX + ":" + applicationId); csar.setDelegateId(applicationId); csar.setDelegateType(delegateType); if (oldArchiveName != null && oldArchiveVersion != null) { csar.setToscaDefinitionsVersion(csarService.getOrFail(new Csar(oldArchiveName, oldArchiveVersion).getId()).getToscaDefinitionsVersion()); } else { csar.setToscaDefinitionsVersion(ToscaParser.LATEST_DSL); topology.setArchiveName(csar.getName()); topology.setArchiveVersion(csar.getVersion()); topology.setWorkspace(csar.getWorkspace()); applicationTopologyVersion.setArchiveId(csar.getId()); applicationTopologyVersion.setQualifier(qualifier); applicationTopologyVersion.setDescription(description);
@Test public void testImportDependency() throws FileNotFoundException, ParsingException { Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03"); Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar); ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-import-dependency.yml")); Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion()); assertNoBlocker(parsingResult); ArchiveRoot archiveRoot = parsingResult.getResult(); assertNotNull(archiveRoot.getArchive()); assertNotNull(archiveRoot.getArchive().getDependencies()); Assert.assertEquals(1, archiveRoot.getArchive().getDependencies().size()); Assert.assertEquals(new CSARDependency(csar.getName(), csar.getVersion()), archiveRoot.getArchive().getDependencies().iterator().next()); }
/** * Utility method to mock the acess to some normative types nodes and capabilities. * * @param repositorySearchService The repositorySearchService to mock. */ public static void mockNormativeTypes(ICSARRepositorySearchService repositorySearchService) { Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN12"); Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar); NodeType mockedNodeRoot = new NodeType(); Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeTypesConstant.ROOT_NODE_TYPE), Mockito.any(Set.class))).thenReturn(mockedNodeRoot); RelationshipType mockedRelationshipRoot = new RelationshipType(); Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE), Mockito.any(Set.class))).thenReturn(mockedRelationshipRoot); CapabilityType mockedCapabilityType = new CapabilityType(); Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(NormativeCapabilityTypes.ROOT), Mockito.any(Set.class))).thenReturn(mockedCapabilityType); DataType mockedDataType = new DataType(); Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeTypesConstant.ROOT_DATA_TYPE), Mockito.any(Set.class))).thenReturn(mockedDataType); ArtifactType mockedArtifactType = new ArtifactType(); Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(ArtifactType.class), Mockito.eq(NormativeTypesConstant.ROOT_ARTIFACT_TYPE), Mockito.any(Set.class))).thenReturn(mockedArtifactType); } }
@SuppressWarnings("unchecked") @Test public void testAttributesConcatValid() throws Throwable { Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03"); Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion()); Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
throws CSARUsedInActiveDeployment, ToscaTypeAlreadyDefinedInOtherCSAR { archiveIndexerAuthorizationFilter.checkAuthorization(archiveRoot); String archiveName = archiveRoot.getArchive().getName(); String archiveVersion = archiveRoot.getArchive().getVersion(); Csar currentIndexedArchive = csarService.get(archiveName, archiveVersion); if (Objects.equals(currentIndexedArchive.getWorkspace(), archiveRoot.getArchive().getWorkspace())) { if (currentIndexedArchive.getHash() != null && currentIndexedArchive.getHash().equals(archiveRoot.getArchive().getHash())) { source = CSARSource.OTHER; archiveRoot.getArchive().setImportSource(source.name()); archiveRoot.getArchive().setHasTopology(archiveRoot.hasToscaTopologyTemplate() && !archiveRoot.getTopology().isEmpty()); archiveRoot.getArchive().setNodeTypesCount(archiveRoot.getNodeTypes().size()); log.debug("Imported archive {}", archiveRoot.getArchive().getId()); indexArchiveTypes(archiveName, archiveVersion, archiveRoot.getArchive().getWorkspace(), archiveRoot, currentIndexedArchive, metapropsNames); indexTopology(archiveRoot, parsingErrors, archiveName, archiveVersion);
/** * Generate resources (application or template) related to a topology list * * @param topologies * @return */ public List<Usage> generateTopologiesInfo(Topology[] topologies) { List<Usage> resourceList = Lists.newArrayList(); List<Csar> topologiesCsar = getTopologiesCsar(topologies); for (Csar csar : topologiesCsar) { if (Objects.equals(csar.getDelegateType(), ArchiveDelegateType.APPLICATION.toString())) { // get the related application Application application = applicationService.checkAndGetApplication(csar.getDelegateId()); resourceList.add(new Usage(application.getName(), csar.getDelegateType(), csar.getDelegateId(), csar.getWorkspace())); } else { resourceList.add(new Usage(csar.getName() + "[" + csar.getVersion() + "]", "topologyTemplate", csar.getId(), csar.getWorkspace())); } } return resourceList; } }
archiveRoot.setArchive(csar); archiveRoot.setTopology(topology); csar.setHasTopology(true); ensureUniqueness(csar.getName(), csar.getVersion()); if (csar.getYamlFilePath() == null) { csar.setYamlFilePath("topology.yml"); csar.setDependencies(topology.getDependencies()); Files.write(topologyPath.resolve(csar.getYamlFilePath()), yaml.getBytes(Charset.forName("UTF-8"))); archiveRepositry.storeCSAR(csar, topologyPath);
@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); } }
@Test public void testRootCategories() throws FileNotFoundException, ParsingException { ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-root-categories.yml")); assertNoBlocker(parsingResult); ArchiveRoot archiveRoot = parsingResult.getResult(); assertNotNull(archiveRoot.getArchive()); Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion()); Assert.assertEquals("Tosca default namespace value", archiveRoot.getArchive().getToscaDefaultNamespace()); Assert.assertEquals("Template name value", archiveRoot.getArchive().getName()); Assert.assertEquals("Temlate author value", archiveRoot.getArchive().getTemplateAuthor()); Assert.assertEquals("1.0.0-SNAPSHOT", archiveRoot.getArchive().getVersion()); Assert.assertEquals("This is an example of a single line description (no folding).", archiveRoot.getArchive().getDescription()); }
velocityCtx.put("template_name", csar.getName()); velocityCtx.put("template_version", csar.getVersion()); velocityCtx.put("hasCustomWorkflows", hasCustomWorkflows(topology)); velocityCtx.put("generateWorkflow", generateWorkflow); if (csar.getDescription() == null) { velocityCtx.put("template_description", ""); } else { velocityCtx.put("template_description", csar.getDescription()); String author = csar.getTemplateAuthor(); if (author == null) { author = loggedUser != null ? loggedUser.getUsername() : null; if (topology.getDescription() == null && ArchiveDelegateType.APPLICATION.toString().equals(csar.getDelegateType())) { Application application = applicationService.getOrFail(csar.getDelegateId()); velocityCtx.put("topology_description", application.getDescription());
if (archiveRoot.getArchive().getTags() != null && !archiveRoot.getArchive().getTags().isEmpty()) { if (topology.getTags() == null) { topology.setTags(Lists.newArrayList()); topology.getTags().addAll(archiveRoot.getArchive().getTags()); if (StringUtils.isEmpty(topology.getDescription()) && StringUtils.isNotEmpty(archiveRoot.getArchive().getDescription())) { topology.setDescription(archiveRoot.getArchive().getDescription()); CSARDependency selfDependency = new CSARDependency(archiveRoot.getArchive().getName(), archiveRoot.getArchive().getVersion(), archiveRoot .getArchive().getHash()); topology.getDependencies().add(selfDependency);
/** * Update a dependency according to what is currently in the repository * * @param initialDependency * @return */ private CSARDependency getUpdatedDependencyIfNeeded(CSARDependency initialDependency) { CSARDependency updatedDependency = null; Csar csar = csarService.getOrFail(initialDependency.getName(), initialDependency.getVersion()); if ((StringUtils.isNotBlank(initialDependency.getHash()) || StringUtils.isNotBlank(csar.getHash())) && !Objects.equals(initialDependency.getHash(), csar.getHash())) { updatedDependency = new CSARDependency(csar.getName(), csar.getVersion(), csar.getHash()); } return updatedDependency; }
dependency.setHash(csar.getHash()); ToscaContext.get().addDependency(dependency); Set<CSARDependency> dependencies = archiveRoot.getArchive().getDependencies(); if (dependencies == null) { dependencies = new HashSet<>(); archiveRoot.getArchive().setDependencies(dependencies);