Search in sources :

Example 16 with ArtifactTemplateId

use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class IRepository method getReferencedTOSCAComponentImplementationArtifactIds.

/**
 * Helper method
 *
 * @param ids                     the list of ids to add the new ids to
 * @param implementationArtifacts the implementation artifacts belonging to the given id
 * @param id                      the id to handle
 */
default Collection<DefinitionsChildId> getReferencedTOSCAComponentImplementationArtifactIds(Collection<DefinitionsChildId> ids, List<TImplementationArtifact> implementationArtifacts, DefinitionsChildId id) {
    if (implementationArtifacts != null) {
        for (TImplementationArtifact ia : implementationArtifacts) {
            QName qname;
            if ((qname = ia.getArtifactRef()) != null) {
                ids.add(new ArtifactTemplateId(qname));
            }
            ids.add(new ArtifactTypeId(ia.getArtifactType()));
        }
    }
    return ids;
}
Also used : TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)

Example 17 with ArtifactTemplateId

use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class IRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId id) {
    // We have to use a HashSet to ensure that no duplicate ids are added<
    // E.g., there may be multiple relationship templates having the same type
    Collection<DefinitionsChildId> ids = new HashSet<>();
    TServiceTemplate serviceTemplate = this.getElement(id);
    // add included things to export queue
    TBoundaryDefinitions boundaryDefs;
    if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
        List<TPolicy> policies = boundaryDefs.getPolicies();
        if (policies != null) {
            for (TPolicy policy : policies) {
                PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
                ids.add(policyTypeId);
                PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
                ids.add(policyTemplateId);
            }
        }
    // reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
    }
    final TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
    if (topology != null) {
        if (Objects.nonNull(topology.getPolicies())) {
            topology.getPolicies().stream().filter(Objects::nonNull).forEach(p -> {
                QName type = p.getPolicyType();
                PolicyTypeId policyTypeIdId = new PolicyTypeId(type);
                ids.add(policyTypeIdId);
            });
        }
        for (TEntityTemplate entityTemplate : topology.getNodeTemplateOrRelationshipTemplate()) {
            QName qname = entityTemplate.getType();
            if (entityTemplate instanceof TNodeTemplate) {
                ids.add(new NodeTypeId(qname));
                TNodeTemplate n = (TNodeTemplate) entityTemplate;
                // crawl through policies
                List<TPolicy> policies = n.getPolicies();
                if (policies != null) {
                    for (TPolicy pol : policies) {
                        QName type = pol.getPolicyType();
                        PolicyTypeId ctId = new PolicyTypeId(type);
                        ids.add(ctId);
                        QName template = pol.getPolicyRef();
                        if (template != null) {
                            PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
                            ids.add(policyTemplateId);
                        }
                    }
                }
                // Crawl RequirementTypes and Capabilities for their references
                getReferencedRequirementTypeIds(ids, n);
                getCapabilitiesReferences(ids, n);
                // TODO: this information is collected differently for YAML and XML modes
                // crawl through deployment artifacts
                List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
                if (deploymentArtifacts != null) {
                    for (TDeploymentArtifact da : deploymentArtifacts) {
                        if (da.getArtifactType() != null) {
                            // This is considered Nullable, because the test case ConsistencyCheckerTest#hasError
                            // expects an empty artifactType and thus it may be null.
                            ids.add(new ArtifactTypeId(da.getArtifactType()));
                        }
                        if (da.getArtifactRef() != null) {
                            ids.add(new ArtifactTemplateId(da.getArtifactRef()));
                        }
                    }
                }
                // Store all referenced artifact types
                List<TArtifact> artifacts = n.getArtifacts();
                if (Objects.nonNull(artifacts)) {
                    artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
                }
                TNodeType nodeType = this.getElement(new NodeTypeId(qname));
                if (Objects.nonNull(nodeType.getInterfaceDefinitions())) {
                    nodeType.getInterfaceDefinitions().stream().filter(Objects::nonNull).forEach(iDef -> {
                        if (Objects.nonNull(iDef.getType())) {
                            ids.add(new InterfaceTypeId(iDef.getType()));
                        }
                    });
                }
            } else {
                assert (entityTemplate instanceof TRelationshipTemplate);
                ids.add(new RelationshipTypeId(qname));
            }
        }
    }
    return ids;
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) TArtifact(org.eclipse.winery.model.tosca.TArtifact) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TPolicy(org.eclipse.winery.model.tosca.TPolicy) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 18 with ArtifactTemplateId

use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class DockerPlugin method createDockerImage.

private QName createDockerImage(Path tempDirectory, ArtifactTemplateId artifactTemplate, IRepository repository) {
    String dockerImageName = artifactTemplate.getQName().getLocalPart().toLowerCase().replaceAll("(\\s)|(_)|(-)", "");
    QName selfContainedVersion = VersionSupport.getSelfContainedVersion(artifactTemplate);
    ArtifactTemplateId generatedArtifactTemplateId = new ArtifactTemplateId(selfContainedVersion);
    if (!repository.exists(generatedArtifactTemplateId)) {
        try {
            Utils.execute(tempDirectory.toString(), "docker", "build", "-t", dockerImageName, ".");
            String tarball = dockerImageName + ".tar";
            Utils.execute(tempDirectory.toString(), "docker", "save", "-o", tarball, dockerImageName + ":latest");
            Utils.compressTarBallAndAddToArtifact(tempDirectory, repository, generatedArtifactTemplateId, tarball);
            FileUtils.forceDelete(tempDirectory);
        } catch (InterruptedException | IOException e) {
            logger.error("Error while creating Dockerfile...", e);
            return null;
        }
    }
    return selfContainedVersion;
}
Also used : QName(javax.xml.namespace.QName) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)

Example 19 with ArtifactTemplateId

use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class SelfContainmentPackager method createSelfContainedNodeTypeImplementation.

private void createSelfContainedNodeTypeImplementation(TNodeTypeImplementation impl) {
    if (impl.getImplementationArtifacts() != null) {
        List<SelfContainmentPlugin.GeneratedArtifacts> generatedArtifacts = impl.getImplementationArtifacts().stream().filter(ia -> Objects.nonNull(ia.getArtifactRef())).filter(ia -> !new ArtifactTemplateId(ia.getArtifactRef()).isSelfContained()).map(ia -> this.downloadArtifacts(ia.getArtifactRef(), ia.getArtifactType())).filter(Objects::nonNull).filter(SelfContainmentPlugin.GeneratedArtifacts::containsNewElements).collect(Collectors.toList());
        if (!generatedArtifacts.isEmpty()) {
            NodeTypeImplementationId nodeTypeImplementationId = getSelfContainedNodeTypeImplId(impl);
            try {
                this.repository.duplicate(new NodeTypeImplementationId(impl.getQName()), nodeTypeImplementationId);
                TNodeTypeImplementation implementation = this.repository.getElement(nodeTypeImplementationId);
                generatedArtifacts.forEach(generatedArtifact -> {
                    if (implementation.getImplementationArtifacts() != null) {
                        implementation.getImplementationArtifacts().forEach(ia -> {
                            if (generatedArtifact.artifactToReplaceQName.equals(ia.getArtifactRef())) {
                                ia.setArtifactRef(generatedArtifact.selfContainedArtifactQName);
                                TArtifactTemplate artifactTemplate = generatedArtifact.selfContainedArtifactTemplate;
                                if (artifactTemplate == null) {
                                    artifactTemplate = repository.getElement(new ArtifactTemplateId(generatedArtifact.selfContainedArtifactQName));
                                }
                                if (artifactTemplate.getType() == null) {
                                    logger.error("Artifact Template does not have a type assigned! {}", generatedArtifact.selfContainedArtifactQName);
                                }
                                ia.setArtifactType(artifactTemplate.getType());
                            }
                        });
                        List<TDeploymentArtifact> deploymentArtifacts = implementation.getDeploymentArtifacts();
                        if (deploymentArtifacts == null) {
                            deploymentArtifacts = new ArrayList<>();
                            implementation.setDeploymentArtifacts(deploymentArtifacts);
                        } else {
                            deploymentArtifacts.removeIf(da -> da.getArtifactRef() != null && generatedArtifact.deploymentArtifactsToRemove.contains(da.getArtifactRef()));
                        }
                        for (QName artifactTemplate : generatedArtifact.deploymentArtifactsToAdd) {
                            TArtifactTemplate generatedAT = repository.getElement(new ArtifactTemplateId(artifactTemplate));
                            deploymentArtifacts.add(new TDeploymentArtifact.Builder(artifactTemplate.getLocalPart(), generatedAT.getType()).setArtifactRef(artifactTemplate).build());
                        }
                    }
                });
                repository.setElement(nodeTypeImplementationId, implementation);
            } catch (IOException e) {
                logger.error("Error while creating new self-contained NodeTypeImplementation of {}", impl, e);
            }
        }
    } else {
        logger.info("No processable IAs found in Node Type Implementation {}", impl.getQName());
    }
}
Also used : TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Collection(java.util.Collection) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Objects(java.util.Objects) List(java.util.List) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) VersionSupport(org.eclipse.winery.model.version.VersionSupport) IRepository(org.eclipse.winery.repository.backend.IRepository) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) Optional(java.util.Optional) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) QName(javax.xml.namespace.QName) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) QName(javax.xml.namespace.QName) IOException(java.io.IOException) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Objects(java.util.Objects) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact)

Example 20 with ArtifactTemplateId

use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class UbuntuVMPlugin method downloadDependenciesBasedOnNodeType.

@Override
public void downloadDependenciesBasedOnNodeType(TNodeTypeImplementation nodeTypeImplementation, IRepository repository) {
    QName nodeType = nodeTypeImplementation.getNodeType();
    WineryVersion nodeTypeVersion = VersionUtils.getVersion(nodeType.getLocalPart());
    String componentVersion = nodeTypeVersion.getComponentVersion();
    if (componentVersion != null) {
        String codeName = getCodeName(componentVersion);
        if (codeName != null) {
            logger.info("Found code name '{}' for Ubuntu Node Type {}", codeName, nodeType);
            String nameWithoutVersion = VersionUtils.getNameWithoutVersion(nodeType.getLocalPart());
            WineryVersion artifactVersion = new WineryVersion(nodeTypeVersion.getComponentVersion() + "-CloudImage", 1, 1);
            ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId(OpenToscaBaseTypes.artifactTemplateNamespace, nameWithoutVersion + "-DA" + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + artifactVersion, false);
            TArtifactTemplate element = repository.getElement(artifactTemplateId);
            element.setType(OpenToscaBaseTypes.cloudImageArtifactType);
            logger.info("Generated ArtifactTemplate {}", artifactTemplateId.getQName());
            if (!repository.exists(artifactTemplateId)) {
                logger.info("Trying to download image file...");
                String baseUrl = "https://cloud-images.ubuntu.com/" + codeName + "/current/" + codeName + "-server-cloudimg-amd64";
                try {
                    URL url = new URL(baseUrl + imageFileType);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("HEAD");
                    int responseCode = connection.getResponseCode();
                    if (responseCode != 200) {
                        connection.disconnect();
                        logger.info("Image not found, trying with '-disk' suffix...");
                        url = new URL(baseUrl + imageDiskType);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("HEAD");
                        responseCode = connection.getResponseCode();
                    }
                    if (responseCode == 200) {
                        repository.setElement(artifactTemplateId, element);
                        ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
                        String fileName = url.getFile().substring(url.getFile().lastIndexOf("/") + 1);
                        RepositoryFileReference repositoryFileReference = new RepositoryFileReference(filesId, fileName);
                        try (InputStream inputStream = url.openStream()) {
                            repository.putContentToFile(repositoryFileReference, inputStream, MediaType.parse("application/x-image"));
                        }
                        BackendUtils.synchronizeReferences(repository, artifactTemplateId);
                        TDeploymentArtifact imageDa = new TDeploymentArtifact.Builder("CloudImage", OpenToscaBaseTypes.cloudImageArtifactType).setArtifactRef(artifactTemplateId.getQName()).build();
                        List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
                        if (deploymentArtifacts == null) {
                            deploymentArtifacts = new ArrayList<>();
                            nodeTypeImplementation.setDeploymentArtifacts(deploymentArtifacts);
                        }
                        deploymentArtifacts.add(imageDa);
                    } else {
                        logger.info("Could not download image -- the URLs do not exist: \n\t{}\n\t{}", baseUrl + imageFileType, baseUrl + imageDiskType);
                    }
                } catch (IOException e) {
                    logger.info("Error while downloading image file!", e);
                }
            }
        } else {
            logger.info("Could not identify code name of given Ubuntu Node Type! {}", nodeType);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) InputStream(java.io.InputStream) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) URL(java.net.URL) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) HttpURLConnection(java.net.HttpURLConnection) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Aggregations

ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)36 QName (javax.xml.namespace.QName)20 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)13 IOException (java.io.IOException)10 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)10 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)9 TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)9 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)9 Test (org.junit.jupiter.api.Test)9 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)8 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)8 ArrayList (java.util.ArrayList)7 NodeTypeImplementationId (org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId)7 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)7 Path (java.nio.file.Path)6 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)6 PolicyTypeId (org.eclipse.winery.model.ids.definitions.PolicyTypeId)6 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)6 InputStream (java.io.InputStream)5 RelationshipTypeImplementationId (org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId)5