Search in sources :

Example 46 with TDeploymentArtifact

use of org.eclipse.winery.model.tosca.TDeploymentArtifact in project container by OpenTOSCA.

the class ManagementBusInvocationPluginScript method createDANamePathMapEnvVar.

/**
 * @return mapping with DeploymentArtifact names and their paths.
 */
private String createDANamePathMapEnvVar(final Csar csar, final TNodeType nodeType, final TNodeTemplate nodeTemplate) {
    LOG.debug("Checking if NodeTemplate {} has DAs...", nodeTemplate.getName());
    List<String> daArtifactReferences;
    final Map<String, List<String>> daNameReferenceMapping = new HashMap<>();
    final ResolvedArtifacts resolvedArtifacts = containerEngine.resolvedDeploymentArtifacts(csar, nodeTemplate);
    for (final ResolvedDeploymentArtifact resolvedDA : resolvedArtifacts.getDeploymentArtifacts()) {
        daArtifactReferences = resolvedDA.getReferences();
        for (final String daArtifactReference : daArtifactReferences) {
            LOG.debug("Artifact reference for DA: {} found: {} .", resolvedDA.getName(), daArtifactReference);
            List<String> currentValue = daNameReferenceMapping.computeIfAbsent(resolvedDA.getName(), k -> new ArrayList<>());
            currentValue.add(daArtifactReference);
        }
    }
    final List<TNodeTypeImplementation> nodeTypeImpls = ToscaEngine.getNodeTypeImplementations(csar, nodeType);
    for (final TNodeTypeImplementation nodeTypeImpl : nodeTypeImpls) {
        List<TDeploymentArtifact> das = nodeTypeImpl.getDeploymentArtifacts();
        if (das != null) {
            for (final TDeploymentArtifact da : das) {
                final TArtifactTemplate daArtifactTemplate;
                try {
                    daArtifactTemplate = ToscaEngine.resolveArtifactTemplate(csar, da.getArtifactRef());
                } catch (NotFoundException e) {
                    LOG.warn("Failed to find ArtifactTemplate with reference [{}] for DeploymentArtifact {}", da.getArtifactRef(), da.getName());
                    continue;
                }
                if (daArtifactTemplate.getArtifactReferences() == null) {
                    continue;
                }
                for (final TArtifactReference daArtifactReference : daArtifactTemplate.getArtifactReferences()) {
                    LOG.debug("Artifact reference for DA: {} found: {} .", da.getName(), daArtifactReference);
                    List<String> currentValue = daNameReferenceMapping.computeIfAbsent(da.getName(), k -> new ArrayList<>());
                    currentValue.add(daArtifactReference.getReference());
                }
            }
        }
    }
    StringBuilder daEnvMap = new StringBuilder();
    if (!daNameReferenceMapping.isEmpty()) {
        LOG.debug("NodeTemplate {} has {} DAs.", nodeTemplate.getName(), daNameReferenceMapping.size());
        daEnvMap.append("DAs=\"");
        for (final Entry<String, List<String>> da : daNameReferenceMapping.entrySet()) {
            final String daName = da.getKey();
            final List<String> daRefs = da.getValue();
            for (String daRef : daRefs) {
                // FIXME / is a brutal assumption
                if (!daRef.startsWith("/")) {
                    daRef = "/" + daRef;
                }
                daEnvMap.append(daName).append(",").append(daRef).append(";");
            }
        }
        daEnvMap.append("\" ");
        LOG.debug("Created DA-DANamePathMapEnvVar for NodeTemplate {} : {}", nodeTemplate.getName(), daEnvMap);
    }
    return daEnvMap.toString();
}
Also used : HashMap(java.util.HashMap) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) NotFoundException(org.opentosca.container.core.common.NotFoundException) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) ResolvedDeploymentArtifact(org.opentosca.container.core.engine.ResolvedArtifacts.ResolvedDeploymentArtifact) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) List(java.util.List) ArrayList(java.util.ArrayList) ResolvedArtifacts(org.opentosca.container.core.engine.ResolvedArtifacts)

Example 47 with TDeploymentArtifact

use of org.eclipse.winery.model.tosca.TDeploymentArtifact in project container by OpenTOSCA.

the class ModelUtils method calculateEffectiveDAs.

private static List<TDeploymentArtifact> calculateEffectiveDAs(TNodeTemplate nodeTemplate, TNodeTypeImplementation nodeTypeImplementation, Csar csar, boolean traceHierarchy) {
    List<TDeploymentArtifact> nodeImplementationDAs = nodeTypeImplementation == null || nodeTypeImplementation.getDeploymentArtifacts() == null ? new ArrayList<>() : nodeTypeImplementation.getDeploymentArtifacts();
    List<TDeploymentArtifact> nodeTemplateDAs = nodeTemplate.getDeploymentArtifacts() == null ? new ArrayList<>() : nodeTemplate.getDeploymentArtifacts();
    // DAs at the Node Template override the Node Type Implementation, if the name is equal.
    List<TDeploymentArtifact> effectiveDAs = new ArrayList<>(nodeTemplateDAs);
    nodeImplementationDAs.forEach(nodeTypeImplementationDA -> {
        if (effectiveDAs.stream().noneMatch(nodeTemplateDa -> nodeTemplateDa.getName().equals(nodeTypeImplementationDA.getName()))) {
            effectiveDAs.add(nodeTypeImplementationDA);
        }
    });
    if (traceHierarchy) {
        TNodeType nodeType = csar.nodeTypesMap().get(nodeTemplate.getType());
        List<QName> nodeTypeHierarchy = ModelUtils.getNodeTypeHierarchy(nodeType, csar);
        // >1 since the hierarchy always contains the type itself at place 0.
        for (int index = 1; index < nodeTypeHierarchy.size(); index++) {
            QName parentType = nodeTypeHierarchy.get(index);
            csar.nodeTypeImplementations().stream().filter(implementation -> implementation.getNodeType().equals(parentType)).forEach(parentImplementation -> calculateEffectiveDAs(nodeTemplate, parentImplementation, csar, false).forEach(da -> {
                if (!effectiveDAs.contains(da)) {
                    effectiveDAs.add(da);
                }
            }));
        }
    }
    return effectiveDAs;
}
Also used : Transformer(javax.xml.transform.Transformer) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions) StreamResult(javax.xml.transform.stream.StreamResult) PropertyParser(org.opentosca.container.core.next.xml.PropertyParser) LoggerFactory(org.slf4j.LoggerFactory) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) ToscaEngine(org.opentosca.container.core.engine.ToscaEngine) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) Document(org.w3c.dom.Document) Map(java.util.Map) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Collection(java.util.Collection) Set(java.util.Set) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Collectors(java.util.stream.Collectors) TInterface(org.eclipse.winery.model.tosca.TInterface) Sets(com.google.common.collect.Sets) DocumentConverter(org.opentosca.container.core.common.jpa.DocumentConverter) Objects(java.util.Objects) List(java.util.List) TCapability(org.eclipse.winery.model.tosca.TCapability) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) SAXException(org.xml.sax.SAXException) QName(javax.xml.namespace.QName) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMSource(javax.xml.transform.dom.DOMSource) TransformerException(javax.xml.transform.TransformerException) Types(org.opentosca.container.core.convention.Types) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TExportedInterface(org.eclipse.winery.model.tosca.TExportedInterface) Node(org.w3c.dom.Node) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Csar(org.opentosca.container.core.model.csar.Csar) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) NotFoundException(org.opentosca.container.core.common.NotFoundException) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) IOException(java.io.IOException) OutputKeys(javax.xml.transform.OutputKeys) Property(org.opentosca.container.core.next.model.Property) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TOperation(org.eclipse.winery.model.tosca.TOperation) TransformerFactory(javax.xml.transform.TransformerFactory) TExportedOperation(org.eclipse.winery.model.tosca.TExportedOperation) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Example 48 with TDeploymentArtifact

use of org.eclipse.winery.model.tosca.TDeploymentArtifact in project container by OpenTOSCA.

the class CsarImpl method addArtifactTemplate.

public void addArtifactTemplate(InputStream inputStream, ServiceTemplateId serviceTemplateId, String nodeTemplateId) throws IOException {
    final String artifactTypeNamespace = "http://opentosca.org/artifacttypes";
    final String artifactTypeName = "State";
    // final QName artifactTypeQName = new QName (artifactTypeNamespace, artifactTypeName);
    final String artifactTemplateNamespace = "http://opentosca.org/stateartifacttemplates";
    final String artifactTemplateName = serviceTemplateId.getQName().getLocalPart() + "_" + nodeTemplateId + "_StateArtifactTemplate";
    final QName artifactTemplateQName = new QName(artifactTemplateNamespace, artifactTemplateName);
    // ArtifactType handling
    TArtifactType artifactType = new TArtifactType.Builder(artifactTypeName).setTargetNamespace(artifactTypeNamespace).build();
    ArtifactTypeId artTypeId = new ArtifactTypeId(artifactType.getQName());
    this.wineryRepo.setElement(artTypeId, artifactType);
    // ArtifactTemplate handling
    TArtifactTemplate artifactTemplate = new TArtifactTemplate.Builder(artifactTemplateName, artifactType.getQName()).setName(artifactTemplateName).build();
    ArtifactTemplateId artTemplateId = new ArtifactTemplateId(artifactTemplateQName);
    this.wineryRepo.setElement(artTemplateId, artifactTemplate);
    ArtifactTemplateFilesDirectoryId artFileId = new ArtifactTemplateFilesDirectoryId(artTemplateId);
    RepositoryFileReference fileRef = new RepositoryFileReference(artFileId, "stateArtifact.state");
    this.wineryRepo.putContentToFile(fileRef, inputStream, MediaType.parse("application/x-state"));
    BackendUtils.synchronizeReferences(this.wineryRepo, artTemplateId);
    TServiceTemplate servTemp = this.wineryRepo.getElement(serviceTemplateId);
    for (TNodeTemplate nestedNodeTemplate : BackendUtils.getAllNestedNodeTemplates(servTemp)) {
        if (nestedNodeTemplate.getId().equals(nodeTemplateId)) {
            TDeploymentArtifact deploymentArtifact = new TDeploymentArtifact.Builder(nodeTemplateId + "_StateArtifact", artifactType.getQName()).setArtifactRef(artTemplateId.getQName()).setArtifactRef(artTemplateId.getQName()).build();
            if (nestedNodeTemplate.getDeploymentArtifacts() == null) {
                nestedNodeTemplate.setDeploymentArtifacts(new ArrayList<>());
            }
            nestedNodeTemplate.getDeploymentArtifacts().add(deploymentArtifact);
            this.wineryRepo.setElement(serviceTemplateId, servTemp);
            break;
        }
    }
    // update the ArtifactTemplates list
    this.artifactTemplates.putAll(this.wineryRepo.getQNameToElementMapping(ArtifactTemplateId.class));
}
Also used : ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Aggregations

TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)48 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)28 QName (javax.xml.namespace.QName)23 ArrayList (java.util.ArrayList)14 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)13 TNodeTypeImplementation (org.eclipse.winery.model.tosca.TNodeTypeImplementation)12 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)12 HashSet (java.util.HashSet)11 List (java.util.List)9 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)9 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)9 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)9 IOException (java.io.IOException)8 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)8 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)7 Test (org.junit.jupiter.api.Test)7 TArtifactType (org.eclipse.winery.model.tosca.TArtifactType)6 TImplementationArtifact (org.eclipse.winery.model.tosca.TImplementationArtifact)6 Node (org.w3c.dom.Node)6 Map (java.util.Map)5