Search in sources :

Example 21 with ArtifactTemplateId

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

the class ScriptPlugin method createDeploymentArtifact.

private String createDeploymentArtifact(ArtifactTemplateId artifactTemplate, IRepository repository, GeneratedArtifacts generatedArtifacts, String packageToInstall) {
    QName packageDaId = new QName(artifactTemplate.getQName().getNamespaceURI(), VersionSupport.getNewComponentVersionId(artifactTemplate, packageToInstall + "-DA"));
    ArtifactTemplateId deployArtId = new ArtifactTemplateId(packageDaId);
    String generatedPackage = packageToInstall + ".tar.gz";
    if (!repository.exists(deployArtId)) {
        String cmd = "/usr/bin/apt download $(/usr/bin/apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts " + "--no-breaks --no-replaces --no-enhances --no-pre-depends " + packageToInstall + " | /bin/grep \"^\\w\")";
        logger.info("Executing command: \"{}\"", cmd);
        Path tempDirectory = null;
        try {
            repository.setElement(deployArtId, new TArtifactTemplate.Builder(deployArtId.getXmlId().getDecoded(), ToscaBaseTypes.archiveArtifactType).build());
            tempDirectory = Files.createTempDirectory(packageDaId.getLocalPart());
            ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(deployArtId);
            try {
                Utils.execute(tempDirectory.toString(), "bash", "-c", cmd);
            } catch (IOException | InterruptedException e) {
                logger.info("Cannot perform download, skipping it! You must add the DA contents yourself!");
                logger.debug("Root cause:", e);
            }
            // Ensure the folder structure exists.
            RepositoryFileReference tarFileRef = new RepositoryFileReference(filesId, generatedPackage);
            if (repository.id2AbsolutePath(filesId).toFile().mkdirs()) {
                compressFolderContents(tempDirectory.toString(), repository.ref2AbsolutePath(tarFileRef).toString());
                BackendUtils.synchronizeReferences(repository, deployArtId);
            } else {
                logger.error("Could not create folders: {}", repository.id2AbsolutePath(filesId).toFile().getAbsolutePath());
            }
        } catch (Exception e) {
            logger.error("Error while downloading artifacts...", e);
        } finally {
            if (tempDirectory != null) {
                FileUtils.forceDelete(tempDirectory);
            }
        }
    }
    generatedArtifacts.deploymentArtifactsToAdd.add(packageDaId);
    return generatedPackage;
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) QName(javax.xml.namespace.QName) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) IOException(java.io.IOException)

Example 22 with ArtifactTemplateId

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

the class ServiceTemplatesResource method createFromArtifact.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response createFromArtifact(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body, @FormDataParam("artifactType") QName artifactType, @FormDataParam("nodeTypes") List<FormDataBodyPart> nodeTypesList, @FormDataParam("infrastructureNodeType") QName infrastructureNodeType, @FormDataParam("tags") List<FormDataBodyPart> sentTagsList, @Context UriInfo uriInfo) throws IllegalArgumentException, JAXBException, IOException {
    Set<String> sentTags = new HashSet<>();
    if (sentTagsList != null) {
        for (FormDataBodyPart tag : sentTagsList) {
            sentTags.add(tag.getValue());
        }
    }
    Set<String> tags = RestUtils.clean(sentTags);
    Set<QName> nodeTypes = new HashSet<>();
    for (FormDataBodyPart nodetype : nodeTypesList) {
        nodeTypes.add(QName.valueOf(nodetype.getValue()));
    }
    nodeTypes = RestUtils.cleanQNameSet(nodeTypes);
    Collection<ServiceTemplateId> xaasPackages = this.getXaaSPackageTemplates(artifactType);
    Collection<ServiceTemplateId> toRemove = new ArrayList<>();
    // check whether the serviceTemplate contains all the given nodeTypes
    for (ServiceTemplateId serviceTemplate : xaasPackages) {
        if (!RestUtils.containsNodeTypes(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), nodeTypes) | !RestUtils.containsTags(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), tags)) {
            toRemove.add(serviceTemplate);
            continue;
        }
        if (infrastructureNodeType != null && !infrastructureNodeType.getLocalPart().equals("undefined")) {
            if (RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageInfrastructure") == null) {
                toRemove.add(serviceTemplate);
            } else {
                String value = RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageInfrastructure");
                if (value != null) {
                    String localName = value.split("}")[1];
                    String namespace = value.split("}")[0].substring(1);
                    if (!infrastructureNodeType.equals(new QName(namespace, localName))) {
                        toRemove.add(serviceTemplate);
                    }
                }
            }
        }
    }
    xaasPackages.removeAll(toRemove);
    if (xaasPackages.size() <= 0) {
        return Response.serverError().entity("No suitable ServiceTemplate found for given artifact and configuration").build();
    }
    // take the first found serviceTemplate
    ServiceTemplateId serviceTemplate = xaasPackages.iterator().next();
    // create new name for the cloned sTemplate
    String newTemplateName = fileDetail.getFileName() + "ServiceTemplate";
    // create artifactTemplate for the uploaded artifact
    ArtifactTemplateId artifactTemplateId = RestUtils.createArtifactTemplate(uploadedInputStream, fileDetail, body, artifactType, uriInfo);
    // clone serviceTemplate
    ServiceTemplateId serviceTemplateId = RestUtils.cloneServiceTemplate(serviceTemplate, newTemplateName, fileDetail.getFileName());
    if (RestUtils.hasDA(serviceTemplateId, RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageNode"), RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageDeploymentArtifact"))) {
        // inject artifact as DA into cloned ServiceTemplate
        BackendUtils.injectArtifactTemplateIntoDeploymentArtifact(serviceTemplateId, RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageNode"), RestUtils.getTagValue(new ServiceTemplateResource(serviceTemplate).getServiceTemplate(), "xaasPackageDeploymentArtifact"), artifactTemplateId, requestRepository);
    } else {
        return Response.serverError().entity("Tagged DeploymentArtifact could not be found on given specified NodeTemplate").build();
    }
    URI absUri = RestUtils.getAbsoluteURI(serviceTemplateId);
    // http://localhost:8080/winery/servicetemplates/winery/servicetemplates/http%253A%252F%252Fopentosca.org%252Fservicetemplates/hs_err_pid13228.logServiceTemplate/
    // http://localhost:8080/winery/servicetemplates/winery/servicetemplates/http%253A%252F%252Fopentosca.org%252Fservicetemplates/java0.logServiceTemplate/
    String absUriString = absUri.toString().replace("/winery/servicetemplates", "");
    absUri = URI.create(absUriString);
    return Response.created(absUri).build();
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) URI(java.net.URI) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) HashSet(java.util.HashSet) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 23 with ArtifactTemplateId

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

the class VersionSupport method getDefinitionInTheGivenVersion.

public static DefinitionsChildId getDefinitionInTheGivenVersion(DefinitionsChildId childId, WineryVersion otherVersion) {
    if (childId.getVersion().compareTo(otherVersion) == 0) {
        return childId;
    }
    String localPart = childId.getNameWithoutVersion() + (otherVersion.toString().length() > 0 ? WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + otherVersion.toString() : "");
    QName qName = new QName(childId.getNamespace().getDecoded(), localPart);
    if (childId instanceof RelationshipTypeImplementationId) {
        return new RelationshipTypeImplementationId(qName);
    } else if (childId instanceof NodeTypeImplementationId) {
        return new NodeTypeImplementationId(qName);
    } else if (childId instanceof RequirementTypeId) {
        return new RequirementTypeId(qName);
    } else if (childId instanceof NodeTypeId) {
        return new NodeTypeId(qName);
    } else if (childId instanceof RelationshipTypeId) {
        return new RelationshipTypeId(qName);
    } else if (childId instanceof CapabilityTypeId) {
        return new CapabilityTypeId(qName);
    } else if (childId instanceof ArtifactTypeId) {
        return new ArtifactTypeId(qName);
    } else if (childId instanceof PolicyTypeId) {
        return new PolicyTypeId(qName);
    } else if (childId instanceof PolicyTemplateId) {
        return new PolicyTemplateId(qName);
    } else if (childId instanceof ServiceTemplateId) {
        return new ServiceTemplateId(qName);
    } else if (childId instanceof ArtifactTemplateId) {
        return new ArtifactTemplateId(qName);
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId)

Example 24 with ArtifactTemplateId

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

the class NodeTemplateResource method createStateElement.

@POST
@Path("state")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response createStateElement(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body, @Context UriInfo uriInfo) {
    LOGGER.debug("Received state artifact for Node Template {} with ID {}", this.nodeTemplate.getName(), this.nodeTemplate.getId());
    LOGGER.debug("Artifact file name is {} and is {} bytes big.", fileDetail.getFileName(), fileDetail.getSize());
    // ensure that the artifact type exists.
    IRepository repo = RepositoryFactory.getRepository();
    repo.getElement(new ArtifactTypeId(OpenToscaBaseTypes.stateArtifactType));
    // create DA
    Optional<TDeploymentArtifact> stateDeploymentArtifact = this.getDeploymentArtifacts().getDeploymentArtifacts().stream().filter(artifact -> artifact.getArtifactType() != null).filter(artifact -> artifact.getArtifactType().equals(OpenToscaBaseTypes.stateArtifactType)).findFirst();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
    TDeploymentArtifact deploymentArtifact = new TDeploymentArtifact.Builder("state", OpenToscaBaseTypes.stateArtifactType).build();
    String componentVersion = dateFormat.format(new Date());
    ArtifactTemplateId newArtifactTemplateId = new ArtifactTemplateId("http://opentosca.org/artifacttemplates", this.getServiceTemplateResource().getServiceTemplate().getName() + "-" + this.nodeTemplate.getId() + "-State" + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + componentVersion + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1", false);
    LOGGER.debug("Created Artifact Template of Type \"State\" called {}", newArtifactTemplateId.getQName());
    // if there is already a state artifact, update the file
    if (stateDeploymentArtifact.isPresent()) {
        LOGGER.debug("Updating the state DA of the Node Template...");
        deploymentArtifact = stateDeploymentArtifact.get();
        // create new ArtifactTemplate version
        ArtifactTemplateId oldArtifactTemplateId = new ArtifactTemplateId(deploymentArtifact.getArtifactRef());
        List<WineryVersion> versions = WineryVersionUtils.getAllVersionsOfOneDefinition(oldArtifactTemplateId, repo);
        WineryVersion newWineryVersion = VersionUtils.getNewWineryVersion(versions);
        newWineryVersion.setWorkInProgressVersion(0);
        newWineryVersion.setComponentVersion(componentVersion);
        newArtifactTemplateId = (ArtifactTemplateId) VersionSupport.getDefinitionInTheGivenVersion(oldArtifactTemplateId, newWineryVersion);
    } else {
        LOGGER.debug("Creating the state DA of the Node Template...");
        List<TDeploymentArtifact> list = this.nodeTemplate.getDeploymentArtifacts();
        if (Objects.isNull(list)) {
            list = new ArrayList<>();
            this.nodeTemplate.setDeploymentArtifacts(list);
        }
        list.add(deploymentArtifact);
    }
    new ArtifactTemplatesResource().onJsonPost(new QNameWithTypeApiData(newArtifactTemplateId.getQName().getLocalPart(), newArtifactTemplateId.getQName().getNamespaceURI(), OpenToscaBaseTypes.stateArtifactType.toString()));
    LOGGER.debug("Attaching the new Artifact...");
    deploymentArtifact.setArtifactRef(newArtifactTemplateId.getQName());
    Response response = new ArtifactTemplateResource(newArtifactTemplateId).getFilesResource().onPost(uploadedInputStream, fileDetail, body, uriInfo, this.nodeTemplate.getId() + ".state");
    if (response.getStatus() != Response.Status.CREATED.getStatusCode()) {
        LOGGER.debug("Could not create artifact file! Response was {}", response);
        return response;
    }
    LOGGER.debug("Persisting now...");
    return RestUtils.persist(this.res);
}
Also used : Produces(javax.ws.rs.Produces) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) Date(java.util.Date) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Namespace(org.eclipse.winery.model.ids.Namespace) ArtifactTemplatesResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplatesResource) OpenToscaBaseTypes(org.eclipse.winery.model.tosca.constants.OpenToscaBaseTypes) WineryVersion(org.eclipse.winery.common.version.WineryVersion) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) IdNames(org.eclipse.winery.model.ids.IdNames) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) Namespaces(org.eclipse.winery.model.tosca.constants.Namespaces) VersionSupport(org.eclipse.winery.model.version.VersionSupport) IPersistable(org.eclipse.winery.repository.rest.resources._support.IPersistable) Map(java.util.Map) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) TEntityTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.TEntityTemplateResource) WineryVersionUtils(org.eclipse.winery.repository.backend.WineryVersionUtils) IIdDetermination(org.eclipse.winery.repository.rest.resources._support.collections.IIdDetermination) Context(javax.ws.rs.core.Context) QNameWithTypeApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameWithTypeApiData) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) DeploymentArtifactsResource(org.eclipse.winery.repository.rest.resources.artifacts.DeploymentArtifactsResource) Objects(java.util.Objects) ArtifactTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource) List(java.util.List) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) Response(javax.ws.rs.core.Response) VersionUtils(org.eclipse.winery.common.version.VersionUtils) IRepository(org.eclipse.winery.repository.backend.IRepository) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) Optional(java.util.Optional) RestUtils(org.eclipse.winery.repository.rest.RestUtils) UriInfo(javax.ws.rs.core.UriInfo) QName(javax.xml.namespace.QName) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) HEAD(javax.ws.rs.HEAD) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) GenericFileResource(org.eclipse.winery.repository.rest.resources._support.GenericFileResource) SimpleDateFormat(java.text.SimpleDateFormat) ArrayList(java.util.ArrayList) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) FormParam(javax.ws.rs.FormParam) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) INodeTemplateResourceOrNodeTypeImplementationResource(org.eclipse.winery.repository.rest.resources._support.INodeTemplateResourceOrNodeTypeImplementationResource) PUT(javax.ws.rs.PUT) InputStream(java.io.InputStream) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) ArtifactTemplatesResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplatesResource) Date(java.util.Date) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) ArtifactTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource) Response(javax.ws.rs.core.Response) QNameWithTypeApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameWithTypeApiData) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) IRepository(org.eclipse.winery.repository.backend.IRepository) SimpleDateFormat(java.text.SimpleDateFormat) WineryVersion(org.eclipse.winery.common.version.WineryVersion) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 25 with ArtifactTemplateId

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

the class YamlRepository method createNewCacheNodeTypeWithArtifact.

/**
 * Creates new cache node type and saves artifact until it's referenced
 *
 * @param ref              repository file reference
 * @param artifactTemplate new artifact template
 * @param artifact         yaml artifact
 * @param imports          imports
 * @return new yaml service template
 */
private YTServiceTemplate createNewCacheNodeTypeWithArtifact(RepositoryFileReference ref, TArtifactTemplate artifactTemplate, YTArtifactDefinition artifact, List<YTMapImportDefinition> imports) {
    YTServiceTemplate serviceTemplate = createEmptyCacheNodeType(((ArtifactTemplateId) ref.getParent()).getQName().getNamespaceURI());
    Map<String, YTArtifactDefinition> artifacts = new LinkedHashMap<>();
    artifacts.put(artifactTemplate.getIdFromIdOrNameField(), artifact);
    serviceTemplate.getNodeTypes().entrySet().iterator().next().getValue().setArtifacts(artifacts);
    serviceTemplate.setImports(imports);
    return serviceTemplate;
}
Also used : YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) LinkedHashMap(java.util.LinkedHashMap)

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