Search in sources :

Example 1 with TArtifact

use of org.eclipse.winery.model.tosca.TArtifact in project winery by eclipse.

the class IRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(NodeTypeId id) {
    Collection<NodeTypeImplementationId> allNodeTypeImplementations = this.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, id.getQName());
    Collection<DefinitionsChildId> ids = new HashSet<>(allNodeTypeImplementations);
    final TNodeType nodeType = this.getElement(id);
    // Add all referenced requirement types, but only in XML mode.
    // For YAML mode add referenced RelationshipType and CapabilityType, if present
    List<TRequirementDefinition> reqDefs = nodeType.getRequirementDefinitions();
    if (reqDefs != null) {
        for (TRequirementDefinition reqDef : reqDefs) {
            // if either of these is set, we're dealing with a type defined in YAML
            if (Objects.nonNull(reqDef.getRelationship()) || Objects.nonNull(reqDef.getCapability()) || Objects.nonNull(reqDef.getNode())) {
                if (Objects.nonNull(reqDef.getRelationship())) {
                    ids.add(new RelationshipTypeId(reqDef.getRelationship()));
                }
                if (Objects.nonNull(reqDef.getCapability())) {
                    ids.add(new CapabilityTypeId(reqDef.getCapability()));
                }
                if (Objects.nonNull(reqDef.getNode())) {
                    ids.add(new NodeTypeId(reqDef.getNode()));
                }
            } else {
                RequirementTypeId reqTypeId = new RequirementTypeId(reqDef.getRequirementType());
                ids.add(reqTypeId);
            }
        }
    }
    // add all referenced capability types
    List<TCapabilityDefinition> capabilityDefinitions = nodeType.getCapabilityDefinitions();
    if (capabilityDefinitions != null) {
        for (TCapabilityDefinition capDef : capabilityDefinitions) {
            CapabilityTypeId capTypeId = new CapabilityTypeId(capDef.getCapabilityType());
            ids.add(capTypeId);
            // Add all types referenced in valid source types
            if (Objects.nonNull(capDef.getValidSourceTypes())) {
                capDef.getValidSourceTypes().forEach(sourceType -> ids.add(new NodeTypeId(sourceType)));
            }
        }
    }
    List<TInterfaceDefinition> interfaceDefinitions = nodeType.getInterfaceDefinitions();
    if (Objects.nonNull(interfaceDefinitions) && !interfaceDefinitions.isEmpty()) {
        for (TInterfaceDefinition intDef : interfaceDefinitions) {
            InterfaceTypeId interfaceTypeId = new InterfaceTypeId(intDef.getType());
            ids.add(interfaceTypeId);
        }
    }
    // Store all referenced artifact types
    List<TArtifact> artifacts = nodeType.getArtifacts();
    if (Objects.nonNull(artifacts)) {
        artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
    }
    getReferencedDefinitionsOfProperties(ids, nodeType.getProperties());
    return ids;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) TArtifact(org.eclipse.winery.model.tosca.TArtifact) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition) TInterfaceDefinition(org.eclipse.winery.model.tosca.TInterfaceDefinition) HashSet(java.util.HashSet)

Example 2 with TArtifact

use of org.eclipse.winery.model.tosca.TArtifact in project winery by eclipse.

the class YamlToscaExportUtil method prepareNodeTypeForExport.

private void prepareNodeTypeForExport(IRepository repository, NodeTypeId id, TDefinitions entryDefinitions) {
    // refMap.put(new CsarContentProperties(BackendUtils.getPathInsideRepo(licenseRef)), new RepositoryRefBasedCsarEntry(licenseRef));
    String nodeTypePath = BackendUtils.getPathInsideRepo(id);
    TNodeType node = repository.getElement(id);
    List<TArtifact> artifacts = node.getArtifacts();
    if (Objects.nonNull(artifacts)) {
        artifacts.forEach(artifact -> {
            UrlValidator customValidator = new UrlValidator();
            if (customValidator.isValid(artifact.getFile())) {
                LOGGER.info("Specified file is a valid URL, start processing the reference");
                try {
                    String pathInsideRepo = putRemoteRefAsReferencedItemInCsar(new URL(artifact.getFile()), nodeTypePath, artifact.getName());
                    updatePathsInNodeArtifacts(entryDefinitions, node, artifact, pathInsideRepo);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            } else {
                Path p = Paths.get("files", artifact.getId());
                RepositoryFileReference ref = new RepositoryFileReference(id, p, artifact.getFile());
                if (repository.exists(ref)) {
                    putRefAsReferencedItemInCsar(repository, ref);
                    String pathInsideRepo = BackendUtils.getPathInsideRepo(ref);
                    updatePathsInNodeArtifacts(entryDefinitions, node, artifact, pathInsideRepo);
                }
            }
        });
    }
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TArtifact(org.eclipse.winery.model.tosca.TArtifact) UrlValidator(org.apache.commons.validator.routines.UrlValidator) URL(java.net.URL) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Example 3 with TArtifact

use of org.eclipse.winery.model.tosca.TArtifact 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 4 with TArtifact

use of org.eclipse.winery.model.tosca.TArtifact in project winery by eclipse.

the class NodeTypeResource method deleteArtifact.

@DELETE
@Path("artifacts/f/{name}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteArtifact(@PathParam("name") String name) {
    TNodeType nodeType = this.getNodeType();
    if (nodeType.getArtifacts() == null) {
        nodeType.setArtifacts(new ArrayList<>());
    }
    TArtifact artifact = null;
    for (TArtifact item : nodeType.getArtifacts()) {
        if (name.equalsIgnoreCase(item.getName())) {
            artifact = item;
        }
    }
    if (artifact == null) {
        return Response.noContent().build();
    }
    List<TArtifact> artifacts = nodeType.getArtifacts();
    artifacts.remove(artifact);
    this.uploadArtifact(name).deleteFile(artifact.getFile(), null);
    return RestUtils.persist(this);
}
Also used : TArtifact(org.eclipse.winery.model.tosca.TArtifact) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes)

Example 5 with TArtifact

use of org.eclipse.winery.model.tosca.TArtifact in project winery by eclipse.

the class YamlToscaExportUtil method prepareServiceTemplateForExport.

/**
 * Prepares artifacts in Service Template
 */
private void prepareServiceTemplateForExport(IRepository repository, ServiceTemplateId id, TDefinitions entryDefinitions) throws IOException {
    BackendUtils.synchronizeReferences(id, repository);
    TServiceTemplate st = repository.getElement(id);
    String serviceTemplatePath = BackendUtils.getPathInsideRepo(id);
    if (Objects.nonNull(st.getTopologyTemplate())) {
        for (TNodeTemplate nodeTemplate : st.getTopologyTemplate().getNodeTemplates()) {
            List<TArtifact> artifacts = nodeTemplate.getArtifacts();
            if (Objects.nonNull(artifacts)) {
                // update file paths in the exported service template
                artifacts.forEach(a -> {
                    UrlValidator customValidator = new UrlValidator();
                    if (customValidator.isValid(a.getFile())) {
                        LOGGER.info("Specified file is a valid URL, start processing the reference");
                        try {
                            String templateArtifactPath = nodeTemplate.getId() + "/" + a.getName();
                            String pathInsideRepo = putRemoteRefAsReferencedItemInCsar(new URL(a.getFile()), serviceTemplatePath, templateArtifactPath);
                            updatePathsInTopologyTemplateArtifacts(entryDefinitions, nodeTemplate, a, pathInsideRepo);
                        } catch (MalformedURLException e) {
                            LOGGER.warn("Supplied URL is invalid: {}", e.getMessage());
                        }
                    } else {
                        Path p = Paths.get("files", nodeTemplate.getId(), a.getId());
                        RepositoryFileReference ref = new RepositoryFileReference(id, p, a.getFile());
                        try {
                            if (repository.exists(ref)) {
                                putRefAsReferencedItemInCsar(repository, ref);
                                String pathInsideRepo = BackendUtils.getPathInsideRepo(ref);
                                updatePathsInTopologyTemplateArtifacts(entryDefinitions, nodeTemplate, a, pathInsideRepo);
                            }
                        } catch (Exception e) {
                            LOGGER.warn("Could not add artifact reference: {}", e.getMessage());
                        }
                    }
                });
            // TODO: update "primary" field in the exported service template
            }
        }
    }
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TArtifact(org.eclipse.winery.model.tosca.TArtifact) UrlValidator(org.apache.commons.validator.routines.UrlValidator) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Aggregations

TArtifact (org.eclipse.winery.model.tosca.TArtifact)5 TNodeType (org.eclipse.winery.model.tosca.TNodeType)4 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 UrlValidator (org.apache.commons.validator.routines.UrlValidator)2 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)2 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)2 InterfaceTypeId (org.eclipse.winery.model.ids.definitions.InterfaceTypeId)2 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)2 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)2 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)2 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)2 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)2 IOException (java.io.IOException)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 QName (javax.xml.namespace.QName)1