Search in sources :

Example 1 with DeploymentArtifact

use of org.alien4cloud.tosca.model.definitions.DeploymentArtifact in project yorc-a4c-plugin by ystia.

the class ToscaExportersTest method testComponentSerialization.

@Test
public void testComponentSerialization() throws Exception {
    Mockito.reset(repositorySearchService);
    String rootDir = "src/test/resources/org/ystia/yorc/alien4cloud/plugin/tosca";
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN20");
    Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(mockedResult.getArchiveName()).thenReturn("tosca-normative-types");
    Mockito.when(mockedResult.getArchiveVersion()).thenReturn("1.0.0-ALIEN20");
    DeploymentArtifact da = Mockito.mock(DeploymentArtifact.class);
    Mockito.when(da.getArtifactPath()).thenReturn("test");
    Mockito.when(da.getArtifactRef()).thenReturn("test");
    Mockito.when(da.getArtifactType()).thenReturn("file");
    Mockito.when(da.getArchiveName()).thenReturn("tosca-normative-types");
    Mockito.when(da.getArchiveVersion()).thenReturn("1.0.0-ALIEN20");
    Mockito.when(mockedResult.getArtifacts()).thenReturn(Collections.singletonMap("SoftwareComponentArtifact", da));
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeCredentialConstant.DATA_TYPE), Mockito.any(Set.class))).thenReturn(Mockito.mock(DataType.class));
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(rootDir, "tosca_component_input.yaml"));
    System.out.println(parsingResult.getContext().getParsingErrors());
    assertNoBlocker(parsingResult);
    String resultYaml = toscaComponentExporter.getYaml(parsingResult.getResult());
    System.out.println(resultYaml);
    String expectedResult = FileUtils.readFileToString(Paths.get(rootDir, "tosca_component_output.yaml").toFile(), "UTF-8");
    // Make some whitespaces change here as IDEs have auto-format features that will overwrite them in the file
    expectedResult = expectedResult.replaceAll("verbose:\\n", "verbose: \n");
    expectedResult = expectedResult.replaceAll("default:\\n", "default: \n");
    Assert.assertEquals(expectedResult, resultYaml);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) DataType(org.alien4cloud.tosca.model.types.DataType) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Test(org.junit.Test) AbstractPluginTest(org.ystia.yorc.alien4cloud.plugin.AbstractPluginTest)

Example 2 with DeploymentArtifact

use of org.alien4cloud.tosca.model.definitions.DeploymentArtifact in project yorc-a4c-plugin by ystia.

the class DeployTask method copyArtifacts.

/**
 * Copy artifacts to archive
 * @param node
 * @param zout
 */
private void copyArtifacts(PaaSNodeTemplate node, ZipOutputStream zout) {
    String name = node.getId();
    // Check if this component has artifacts
    Map<String, DeploymentArtifact> map = node.getTemplate().getArtifacts();
    if (map == null) {
        log.debug("Component with no artifact: " + name);
        return;
    }
    // Process each artifact
    for (Map.Entry<String, DeploymentArtifact> da : map.entrySet()) {
        String aname = name + "/" + da.getKey();
        DeploymentArtifact artifact = da.getValue();
        String artRepo = artifact.getArtifactRepository();
        if (artRepo == null) {
            continue;
        }
        ShowTopology.printArtifact(artifact);
        if (artRepo.equals(ArtifactRepositoryConstants.ALIEN_TOPOLOGY_REPOSITORY)) {
            // Copy artifact from topology repository to the root of archive.
            String from = artifact.getArtifactPath();
            log.debug("Copying local artifact: " + aname + " path=" + from);
            Path artifactPath = Paths.get(from);
            try {
                String filename = artifact.getArtifactRef();
                createZipEntries(filename, zout);
                copy(artifactPath.toFile(), zout);
            } catch (Exception e) {
                log.error("Could not copy local artifact " + aname, e);
            }
        } else {
            // Copy remote artifact
            String from = artifact.getArtifactPath();
            log.debug("Copying remote artifact: " + aname + " path=" + from);
            Path artifactPath = Paths.get(from);
            try {
                String filename = artifact.getArtifactRef();
                createZipEntries(filename, zout);
                copy(artifactPath.toFile(), zout);
            } catch (Exception e) {
                log.error("Could not copy remote artifact " + aname, e);
            }
            // Workaround for a bug in a4c: artifact not added in topology.yml
            // TODO Remove this when a4c bug SUPALIEN-926 is fixed.
            addRemoteArtifactInTopology(name, da.getKey(), artifact);
        }
    }
}
Also used : Path(java.nio.file.Path) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Map(java.util.Map) HashMap(java.util.HashMap) ZipException(java.util.zip.ZipException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException) ParsingException(alien4cloud.tosca.parser.ParsingException) IOException(java.io.IOException)

Example 3 with DeploymentArtifact

use of org.alien4cloud.tosca.model.definitions.DeploymentArtifact in project alien4cloud by alien4cloud.

the class ToscaSerializerTest method simpleTest.

@Ignore
@Test
public void simpleTest() throws IOException, URISyntaxException {
    Topology topology = new Topology();
    topology.setDependencies(new HashSet<CSARDependency>());
    topology.getDependencies().add(new CSARDependency("name1", "1.0"));
    topology.getDependencies().add(new CSARDependency("name2", "2.0"));
    topology.setInputs(new HashMap<String, PropertyDefinition>());
    PropertyDefinition pd1 = new PropertyDefinition();
    pd1.setType("string");
    pd1.setConstraints(getConstraintList());
    pd1.setDescription("A description");
    topology.getInputs().put("input1", pd1);
    PropertyDefinition pd2 = new PropertyDefinition();
    pd2.setType("integer");
    pd2.setRequired(false);
    pd2.setDefault(new ScalarPropertyValue("10"));
    topology.getInputs().put("input2", pd2);
    PropertyDefinition pd3 = new PropertyDefinition();
    pd3.setType("map");
    pd3.setRequired(false);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType("integer");
    pd3.setEntrySchema(entrySchema);
    topology.getInputs().put("input3", pd3);
    topology.setNodeTemplates(new HashMap<String, NodeTemplate>());
    topology.getNodeTemplates().put("node1", new NodeTemplate());
    topology.getNodeTemplates().get("node1").setType("the.node.Type");
    topology.getNodeTemplates().get("node1").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setRelationships(new HashMap<String, RelationshipTemplate>());
    topology.getNodeTemplates().get("node1").getRelationships().put("hostedOn", new RelationshipTemplate());
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setTarget("compute2");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementType("capabilities.Capa");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementName("host");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setType("relationship.Rel");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setCapabilities(new HashMap<String, Capability>());
    Capability capability = new Capability();
    capability.setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa1", capability);
    // this capability should not appear
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa2", new Capability());
    topology.getNodeTemplates().get("node1").setArtifacts(new HashMap<String, DeploymentArtifact>());
    DeploymentArtifact da = new DeploymentArtifact();
    da.setArtifactName("artifact.war");
    da.setArtifactRef("010203904872876723");
    da.setArtifactType("artifacttypes.Artifact");
    topology.getNodeTemplates().get("node1").getArtifacts().put("artifact1", da);
    topology.setOutputProperties(new HashMap<String, Set<String>>());
    topology.getOutputProperties().put("node1", Sets.newHashSet("prop1", "prop2"));
    topology.setOutputAttributes(new HashMap<String, Set<String>>());
    topology.getOutputAttributes().put("node1", Sets.newHashSet("att1", "att2"));
    Map<String, Object> velocityCtx = new HashMap<String, Object>();
    velocityCtx.put("topology", topology);
    velocityCtx.put("template_name", "template-id");
    velocityCtx.put("template_version", "1.0.0-SNAPSHOT");
    velocityCtx.put("template_author", "Foo Bar");
    velocityCtx.put("application_description", "Here is a \nmultiline description");
    StringWriter writer = new StringWriter();
    VelocityUtil.generate("org/alien4cloud/tosca/exporter/topology-alien_dsl_1_4_0.yml.vm", writer, velocityCtx);
    System.out.println(writer.toString());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) HashMap(java.util.HashMap) Topology(org.alien4cloud.tosca.model.templates.Topology) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) StringWriter(java.io.StringWriter) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with DeploymentArtifact

use of org.alien4cloud.tosca.model.definitions.DeploymentArtifact in project alien4cloud by alien4cloud.

the class TemplateBuilder method fillDeploymentArtifactsMap.

private static void fillDeploymentArtifactsMap(Map<String, DeploymentArtifact> deploymentArtifacts, Map<String, DeploymentArtifact> fromTypeArtifacts, Map<String, DeploymentArtifact> mapToMerge) {
    if (MapUtils.isEmpty(fromTypeArtifacts)) {
        fromTypeArtifacts = Maps.newLinkedHashMap();
    }
    // We need to clone objects
    deploymentArtifacts.putAll(fromTypeArtifacts);
    for (Map.Entry<String, DeploymentArtifact> entryArtifact : safe(mapToMerge).entrySet()) {
        deploymentArtifacts.put(entryArtifact.getKey(), entryArtifact.getValue());
        DeploymentArtifact artifactFromType = deploymentArtifacts.get(entryArtifact.getKey());
        if (artifactFromType != null && StringUtils.isBlank(entryArtifact.getValue().getArtifactType())) {
            entryArtifact.getValue().setArtifactType(artifactFromType.getArtifactType());
        }
    }
}
Also used : Map(java.util.Map) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact)

Example 5 with DeploymentArtifact

use of org.alien4cloud.tosca.model.definitions.DeploymentArtifact in project alien4cloud by alien4cloud.

the class RenameInputArtifactProcessor method process.

@Override
public void process(Csar csar, Topology topology, RenameInputArtifactOperation operation) {
    if (operation.getNewInputName() == null || operation.getNewInputName().isEmpty() || !operation.getNewInputName().matches("\\w+")) {
        throw new InvalidNameException("newInputName", operation.getNewInputName(), "\\w+");
    }
    if (safe(topology.getInputArtifacts()).containsKey(operation.getNewInputName())) {
        throw new AlreadyExistException("Input artifact with name <" + operation.getNewInputName() + "> already exists.");
    }
    if (!safe(topology.getInputArtifacts()).containsKey(operation.getInputName())) {
        throw new NotFoundException("Input artifact with name <" + operation.getInputName() + "> does not exists.");
    }
    DeploymentArtifact inputArtifact = topology.getInputArtifacts().remove(operation.getInputName());
    topology.getInputArtifacts().put(operation.getNewInputName(), inputArtifact);
    // change the value of concerned node template artifacts
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        for (DeploymentArtifact dArtifact : safe(nodeTemplate.getArtifacts()).values()) {
            InputArtifactUtil.updateInputArtifactIdIfNeeded(dArtifact, operation.getInputName(), operation.getNewInputName());
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) NotFoundException(alien4cloud.exception.NotFoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact)

Aggregations

DeploymentArtifact (org.alien4cloud.tosca.model.definitions.DeploymentArtifact)25 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)10 NotFoundException (alien4cloud.exception.NotFoundException)6 Map (java.util.Map)5 DeploymentInputs (org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArtifactRepositoryConstants (alien4cloud.component.repository.ArtifactRepositoryConstants)2 IFileRepository (alien4cloud.component.repository.IFileRepository)2 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)2 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)2 Path (java.nio.file.Path)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Resource (javax.annotation.Resource)2 Interface (org.alien4cloud.tosca.model.definitions.Interface)2