Search in sources :

Example 6 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class BackendUtils method getAllReferencedArtifactTemplates.

@NonNull
private static Collection<QName> getAllReferencedArtifactTemplates(TDeploymentArtifacts tDeploymentArtifacts) {
    if (tDeploymentArtifacts == null) {
        return Collections.emptyList();
    }
    List<TDeploymentArtifact> deploymentArtifacts = tDeploymentArtifacts.getDeploymentArtifact();
    if (deploymentArtifacts == null) {
        return Collections.emptyList();
    }
    Collection<QName> res = new ArrayList<>();
    for (TDeploymentArtifact da : deploymentArtifacts) {
        QName artifactRef = da.getArtifactRef();
        if (artifactRef != null) {
            res.add(artifactRef);
        }
    }
    return res;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 7 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class TOSCATransformer method createTOSCAGraph.

public static TOSCAGraph createTOSCAGraph(TTopologyTemplate topologyTemplate) {
    TOSCAGraph graph = new TOSCAGraph(new TOSCAEdgeFactory());
    @NonNull List<TRelationshipTemplate> relationshipTemplates = topologyTemplate.getRelationshipTemplates();
    @NonNull List<TNodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates();
    Map<TNodeTemplate, TOSCANode> nodes = new HashMap<>();
    for (TNodeTemplate nodeTemplate : nodeTemplates) {
        TOSCANode node = createAndInitializeTOSCANode(nodeTemplate);
        nodes.put(nodeTemplate, node);
        graph.addVertex(node);
    }
    for (TRelationshipTemplate tRelationshipTemplate : relationshipTemplates) {
        TOSCAEdge edge = graph.addEdge(nodes.get(tRelationshipTemplate.getSourceElement().getRef()), nodes.get(tRelationshipTemplate.getTargetElement().getRef()));
        initializeTOSCAEdge(tRelationshipTemplate, edge);
    }
    return graph;
}
Also used : TOSCAEdge(org.eclipse.winery.compliance.model.TOSCAEdge) HashMap(java.util.HashMap) NonNull(org.eclipse.jdt.annotation.NonNull) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TOSCAGraph(org.eclipse.winery.compliance.model.TOSCAGraph) TOSCANode(org.eclipse.winery.compliance.model.TOSCANode) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TOSCAEdgeFactory(org.eclipse.winery.compliance.model.TOSCAEdgeFactory)

Example 8 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class X2YConverter method convert.

/**
 * Converts TOSCA XML Definitions to TOSCA YAML ServiceTemplates
 */
@NonNull
public Map<Path, TServiceTemplate> convert(Definitions node, Path outPath, QName name) throws MultiException {
    if (Objects.isNull(node))
        return new LinkedHashMap<>();
    QName tmpName = name;
    LOGGER.debug("Convert TServiceTemplate: {}", node.getIdFromIdOrNameField());
    TServiceTemplate.Builder builder = new TServiceTemplate.Builder(Defaults.TOSCA_DEFINITIONS_VERSION).setDescription(convertDocumentation(node.getDocumentation())).setArtifactTypes(convert(node.getArtifactTypes())).setCapabilityTypes(convert(node.getCapabilityTypes())).setRelationshipTypes(convert(node.getRelationshipTypes())).setNodeTypes(convert(node.getNodeTypes())).setPolicyTypes(convert(node.getPolicyTypes()));
    if (node.getServiceTemplates().size() == 1) {
        builder.setTopologyTemplate(convert(node.getServiceTemplates().get(0)));
        if (Objects.nonNull(node.getServiceTemplates().get(0).getName())) {
            tmpName = new QName(node.getServiceTemplates().get(0).getName());
        }
        builder.addMetadata("targetNamespace", node.getTargetNamespace());
    }
    MultiException exception = importDefinitions.entrySet().stream().map((entry) -> {
        Path path = this.path.resolve(entry.getKey().getGroup()).resolve(entry.getKey().getNamespace().getEncoded());
        try {
            Map<Path, TServiceTemplate> map = new X2YConverter(repository, this.path).convert(entry.getValue(), path, entry.getKey().getQName());
            Optional.ofNullable(map).orElse(new LinkedHashMap<>()).forEach((Path key, TServiceTemplate value) -> builder.addImports(key.toFile().getName(), new TImportDefinition.Builder(this.path.relativize(key).toString()).setNamespaceUri(entry.getValue().getTargetNamespace()).setNamespacePrefix(getNamespacePrefix(entry.getValue().getTargetNamespace())).build()));
        } catch (MultiException e) {
            return e;
        }
        return null;
    }).filter(Objects::nonNull).reduce(new MultiException(), (a, b) -> a.add(b.getException()));
    if (exception.hasException()) {
        throw exception;
    }
    TServiceTemplate serviceTemplate = builder.build();
    Path filePath = outPath.resolve(tmpName.getLocalPart().concat(".yml"));
    try {
        Files.createDirectories(filePath.getParent());
        Writer writer = new Writer();
        writer.write(serviceTemplate, filePath);
        return Collections.singletonMap(filePath, serviceTemplate);
    } catch (IOException e) {
        LOGGER.error("Could not create file path {}", filePath.getParent());
    }
    return new LinkedHashMap<>();
}
Also used : Path(java.nio.file.Path) QName(javax.xml.namespace.QName) IOException(java.io.IOException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) Writer(org.eclipse.winery.yaml.common.writer.yaml.Writer) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 9 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class Y2XConverter method convert.

/**
 * Converts TOSCA YAML ServiceTemplates to TOSCA XML Definitions
 *
 * @return TOSCA XML Definitions
 */
@NonNull
public Definitions convert(org.eclipse.winery.model.tosca.yaml.TServiceTemplate node, String id, String target_namespace, Path path, Path outPath) {
    if (node == null)
        return new Definitions();
    LOGGER.debug("Converting TServiceTemplate");
    // Reset
    this.reset();
    this.referenceVisitor = new ReferenceVisitor(node, target_namespace, path);
    this.namespace = target_namespace;
    this.path = path;
    this.outPath = outPath;
    init(node);
    Definitions definitions = new Definitions.Builder(id + "_Definitions", target_namespace).setImport(convert(node.getImports())).addTypes(convert(node.getDataTypes())).addTypes(convert(node.getGroupTypes())).addServiceTemplates(convertServiceTemplate(node, id, target_namespace)).addNodeTypes(convert(node.getNodeTypes())).addNodeTypeImplementations(this.nodeTypeImplementations).addRelationshipTypes(convert(node.getRelationshipTypes())).addCapabilityTypes(convert(node.getCapabilityTypes())).addArtifactTypes(convert(node.getArtifactTypes())).addArtifactTemplates(this.artifactTemplates.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList())).addPolicyTypes(convert(node.getPolicyTypes())).setName(id).addImports(this.imports).addRequirementTypes(this.requirementTypes).addPolicyTemplates(this.policyTemplates).build();
    WriterUtils.storeDefinitions(definitions, true, path);
    return definitions;
}
Also used : ReferenceVisitor(org.eclipse.winery.yaml.converter.yaml.visitors.ReferenceVisitor) AssignmentBuilder(org.eclipse.winery.yaml.converter.yaml.support.AssignmentBuilder) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 10 with NonNull

use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.

// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
    Objects.requireNonNull(id);
    Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
    if (!ref.isPresent()) {
        return Collections.emptyList();
    }
    short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
    Date lastUpdate = RepositoryFactory.getRepository().getLastUpdate(ref.get());
    @NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
    @NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
    boolean cacheNeedsUpdate = true;
    if (RepositoryFactory.getRepository().exists(cacheRef)) {
        Date lastUpdateCache = RepositoryFactory.getRepository().getLastUpdate(cacheRef);
        if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
            cacheNeedsUpdate = false;
        }
    }
    List<String> result;
    if (cacheNeedsUpdate) {
        final Optional<XSModel> model = BackendUtils.getXSModel(ref.get());
        if (!model.isPresent()) {
            return Collections.emptyList();
        }
        XSNamedMap components = model.get().getComponents(type);
        // @SuppressWarnings("unchecked")
        int len = components.getLength();
        result = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            XSObject item = components.item(i);
            // We want to return only types defined in the namespace of this resource
            if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
                result.add(item.getName());
            }
        }
        String cacheContent = null;
        try {
            cacheContent = BackendUtils.mapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOGGER.error("Could not generate cache content", e);
        }
        try {
            RepositoryFactory.getRepository().putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } catch (IOException e) {
            LOGGER.error("Could not update cache", e);
        }
    } else {
        // cache should contain most recent information
        try (InputStream is = RepositoryFactory.getRepository().newInputStream(cacheRef)) {
            result = BackendUtils.mapper.readValue(is, java.util.List.class);
        } catch (IOException e) {
            LOGGER.error("Could not read from cache", e);
            result = Collections.emptyList();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) XSObject(org.apache.xerces.xs.XSObject) IOException(java.io.IOException) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) XSModel(org.apache.xerces.xs.XSModel) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

NonNull (org.eclipse.jdt.annotation.NonNull)11 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 IRepository (org.eclipse.winery.repository.backend.IRepository)3 Nullable (org.eclipse.jdt.annotation.Nullable)2 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)2 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ADR (io.github.adr.embedded.ADR)1 ApiOperation (io.swagger.annotations.ApiOperation)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1