Search in sources :

Example 11 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.

the class DataFlowResource method createNodeTemplate.

/**
 * Create a NodeTemplate corresponding to the given filter with the given type, properties and artifacts and add it
 * to the topology of the incomplete deployment model.
 */
private TTopologyTemplate createNodeTemplate(TTopologyTemplate topology, NodeTypeId nodeTypeId, String templateName, Map<String, String> properties, List<QName> artifacts) {
    // get NodeType to access Requirements for the completion and available properties
    IRepository repo = RepositoryFactory.getRepository();
    TNodeType nodeType = repo.getElement(nodeTypeId);
    if (Objects.isNull(nodeType)) {
        return null;
    }
    TNodeTemplate.Builder templateBuilder = new TNodeTemplate.Builder(templateName, nodeTypeId.getQName());
    // add the defined properties to the NodeTemplate
    if (Objects.nonNull(properties)) {
        LinkedHashMap<String, String> propertyList = new LinkedHashMap<>();
        if (Objects.nonNull(nodeType.getWinerysPropertiesDefinition())) {
            // add empty property for NodeType properties to avoid errors due to missing properties
            WinerysPropertiesDefinition def = nodeType.getWinerysPropertiesDefinition();
            for (PropertyDefinitionKV prop : def.getPropertyDefinitions()) {
                propertyList.put(prop.getKey(), "");
            }
        }
        // add all properties which are defined at the filter
        propertyList.putAll(properties);
        TEntityTemplate.WineryKVProperties nodeProperties = new TEntityTemplate.WineryKVProperties();
        nodeProperties.setKVProperties(propertyList);
        templateBuilder.setProperties(nodeProperties);
    }
    // add all requirements which are defined by the corresponding NodeType
    if (nodeType.getRequirementDefinitions() != null) {
        for (TRequirementDefinition requirementDef : nodeType.getRequirementDefinitions()) {
            String requirementId = templateName + "-" + requirementDef.getName();
            templateBuilder.addRequirement(new TRequirement.Builder(requirementId, requirementDef.getName(), requirementDef.getRequirementType()).build());
        }
    }
    // add the DAs to the NodeTemplate
    if (Objects.nonNull(artifacts) && !artifacts.isEmpty()) {
        LOGGER.debug("{} artifacts specified for filter {}", artifacts.size(), templateName);
        // get the IDs of all available ArtifactTemplates
        List<ArtifactTemplateId> artifactTemplateIds = repo.getAllDefinitionsChildIds().stream().filter(id -> id.getGroup().equals(ARTIFACT_TEMPLATE_GROUP) && id instanceof ArtifactTemplateId).map(id -> (ArtifactTemplateId) id).collect(Collectors.toList());
        for (QName artifactName : artifacts) {
            Optional<ArtifactTemplateId> idOptional = artifactTemplateIds.stream().filter(id -> id.getQName().equals(artifactName)).findFirst();
            if (idOptional.isPresent()) {
                ArtifactTemplateId artifactTemplateId = idOptional.get();
                TArtifactTemplate artifactTemplate = repo.getElement(artifactTemplateId);
                templateBuilder.addDeploymentArtifact(new TDeploymentArtifact.Builder(artifactName.toString(), artifactTemplate.getType()).setArtifactRef(artifactName).build());
            } else {
                LOGGER.warn("Filter '{}' specifies DA with name '{}' but no such artifact available in repository!", templateName, artifactName);
            }
        }
    }
    topology.addNodeTemplate(templateBuilder.build());
    return topology;
}
Also used : URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) TRequirement(org.eclipse.winery.model.tosca.TRequirement) URI(java.net.URI) TAG_NAME_PROVIDER(org.eclipse.winery.model.adaptation.placement.Constants.TAG_NAME_PROVIDER) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL(org.eclipse.winery.model.adaptation.placement.Constants.RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL) TNodeType(org.eclipse.winery.model.tosca.TNodeType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Collectors(java.util.stream.Collectors) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) Objects(java.util.Objects) List(java.util.List) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) Response(javax.ws.rs.core.Response) 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) QName(javax.xml.namespace.QName) ToscaBaseTypes(org.eclipse.winery.model.tosca.constants.ToscaBaseTypes) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) LinkedHashMap(java.util.LinkedHashMap) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) SERVICE_TEMPLATE_GROUP(org.eclipse.winery.model.adaptation.placement.Constants.SERVICE_TEMPLATE_GROUP) IOException(java.io.IOException) TTag(org.eclipse.winery.model.tosca.TTag) TAG_NAME_LOCATION(org.eclipse.winery.model.adaptation.placement.Constants.TAG_NAME_LOCATION) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) DataFlowModel(org.eclipse.winery.repository.rest.resources.apiData.DataFlowModel) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) ARTIFACT_TEMPLATE_GROUP(org.eclipse.winery.model.adaptation.placement.Constants.ARTIFACT_TEMPLATE_GROUP) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) DATA_TRANSFER_TYPE_PULL(org.eclipse.winery.model.adaptation.placement.Constants.DATA_TRANSFER_TYPE_PULL) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) LinkedHashMap(java.util.LinkedHashMap) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) IRepository(org.eclipse.winery.repository.backend.IRepository) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 12 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.

the class DataFlowResource method createRelationshipTemplate.

private TRelationshipTemplate createRelationshipTemplate(RelationshipTypeId relationshipTypeId, TNodeTemplate source, TNodeTemplate target, String dataTransferType) {
    // get ConnectsTo RelationshipType to access available properties
    IRepository repo = RepositoryFactory.getRepository();
    TRelationshipType relationshipType = repo.getElement(relationshipTypeId);
    if (Objects.isNull(relationshipType)) {
        return null;
    }
    String relationId = source.getId() + "-connectsTo-" + target.getId();
    TRelationshipTemplate.Builder builder = new TRelationshipTemplate.Builder(relationId, relationshipTypeId.getQName(), source, target).setName(relationId);
    // add empty properties to avoid errors due to missing properties
    if (Objects.nonNull(relationshipType.getWinerysPropertiesDefinition())) {
        LinkedHashMap<String, String> propertyList = new LinkedHashMap<>();
        WinerysPropertiesDefinition def = relationshipType.getWinerysPropertiesDefinition();
        for (PropertyDefinitionKV prop : def.getPropertyDefinitions()) {
            propertyList.put(prop.getKey(), "");
        }
        TEntityTemplate.WineryKVProperties relationProperties = new TEntityTemplate.WineryKVProperties();
        relationProperties.setElementName(def.getElementName());
        relationProperties.setNamespace(def.getNamespace());
        relationProperties.setKVProperties(propertyList);
        builder.setProperties(relationProperties);
    }
    TRelationshipTemplate relation = builder.build();
    // evaluate pipe attribute to determine connectsTo direction
    if (Objects.nonNull(dataTransferType) && dataTransferType.equalsIgnoreCase(DATA_TRANSFER_TYPE_PULL)) {
        // change connectsTo direction in case the corresponding target filter is pulling
        relation.setSourceNodeTemplate(target);
        relation.setTargetNodeTemplate(source);
        relation.getOtherAttributes().put(ModelUtilities.RELATIONSHIP_TEMPLATE_TRANSFER_TYPE, RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL);
    }
    return relation;
}
Also used : TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) IRepository(org.eclipse.winery.repository.backend.IRepository) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.

the class EdmmDependantTest method setup.

@BeforeEach
void setup() {
    // region *** NodeType setup ***
    QName nodeType1QName = QName.valueOf("{" + NAMESPACE + "}" + "test_node_type");
    TNodeType nodeType1 = new TNodeType();
    nodeType1.setName(nodeType1QName.getLocalPart());
    nodeType1.setTargetNamespace(nodeType1QName.getNamespaceURI());
    nodeTypes.put(nodeType1QName, nodeType1);
    QName nodeType2QName = QName.valueOf("{" + NAMESPACE + "}" + "test_node_type_2");
    TNodeType nodeType2 = new TNodeType();
    nodeType2.setName(nodeType2QName.getLocalPart());
    nodeType2.setTargetNamespace(nodeType2QName.getNamespaceURI());
    TEntityType.DerivedFrom derivedFrom = new TNodeType.DerivedFrom();
    derivedFrom.setTypeRef(nodeType1QName);
    nodeType2.setDerivedFrom(derivedFrom);
    nodeTypes.put(nodeType2QName, nodeType2);
    QName nodeType3QName = QName.valueOf("{" + NAMESPACE + "}" + "test_node_type_3");
    TNodeType nodeType3 = new TNodeType();
    nodeType3.setName(nodeType3QName.getLocalPart());
    nodeType3.setTargetNamespace(nodeType3QName.getNamespaceURI());
    List<PropertyDefinitionKV> kvList = new ArrayList<>(Arrays.asList(new PropertyDefinitionKV("os_family", "xsd:string"), new PropertyDefinitionKV("public_key", "xsd:string"), new PropertyDefinitionKV("ssh_port", "number")));
    WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
    wpd.setPropertyDefinitions(kvList);
    ModelUtilities.replaceWinerysPropertiesDefinition(nodeType3, wpd);
    nodeType3.setProperties(wpd);
    nodeTypes.put(nodeType3QName, nodeType3);
    QName nodeType4QName = QName.valueOf("{" + NAMESPACE + "}" + "test_node_type_4");
    TNodeType nodeType4 = new TNodeType();
    nodeType4.setName(nodeType4QName.getLocalPart());
    nodeType4.setTargetNamespace(nodeType4QName.getNamespaceURI());
    TOperation start = new TOperation();
    start.setName("start");
    TOperation stop = new TOperation();
    stop.setName("stop");
    TInterface lifecycle = new TInterface();
    lifecycle.setName("lifecycle_interface");
    lifecycle.getOperations().add(start);
    lifecycle.getOperations().add(stop);
    List<TInterface> tInterfaces = new ArrayList<>();
    tInterfaces.add(lifecycle);
    nodeType4.setInterfaces(tInterfaces);
    nodeTypes.put(nodeType4QName, nodeType4);
    // endregion
    // region *** ArtifactTemplates setup ***
    QName startIaQName = QName.valueOf("{" + NAMESPACE + "}" + "Start_IA");
    artifactTemplates.put(startIaQName, new TArtifactTemplate.Builder("Start_IA", startIaQName).addArtifactReference(new TArtifactReference.Builder("/artifacttemplates/" + NAMESPACE_DOUBLE_ENCODED + "/startTestNode4/files/script.sh").build()).build());
    QName stopIaQName = QName.valueOf("{" + NAMESPACE + "}" + "Stop_IA");
    TArtifactTemplate stopIa = new TArtifactTemplate.Builder("Stop_IA", stopIaQName).addArtifactReference(new TArtifactReference.Builder("/artifacttemplates/" + NAMESPACE_DOUBLE_ENCODED + "/startTestNode4/files/script.sh").build()).build();
    artifactTemplates.put(stopIaQName, stopIa);
    QName deploymentArtifactIAQName = QName.valueOf("{" + NAMESPACE + "}" + "TestNode1-DA");
    TArtifactTemplate deploymentArtifactTemplate = new TArtifactTemplate.Builder("TestNode1-DA", deploymentArtifactIAQName).addArtifactReference(new TArtifactReference.Builder("/artifacttemplates/" + NAMESPACE_DOUBLE_ENCODED + "/testNode1-DA/files/da.war").build()).build();
    artifactTemplates.put(deploymentArtifactIAQName, deploymentArtifactTemplate);
    // endregion
    // region *** NodeTypeImplementations setup ***
    QName nodeTypeImpl4QName = QName.valueOf("{" + NAMESPACE + "}" + "test_node_type_Impl_4");
    TImplementationArtifact startArtifact = new TImplementationArtifact.Builder(QName.valueOf("{ex.org}test")).setArtifactRef(startIaQName).setInterfaceName("lifecycle_interface").setOperationName("start").build();
    TImplementationArtifact stopArtifact = new TImplementationArtifact.Builder(QName.valueOf("{ex.org}test")).setArtifactRef(stopIaQName).setInterfaceName("lifecycle_interface").setOperationName("stop").build();
    nodeTypeImplementations.put(nodeTypeImpl4QName, new TNodeTypeImplementation.Builder(nodeTypeImpl4QName.getLocalPart(), nodeType4QName).addImplementationArtifact(startArtifact).addImplementationArtifact(stopArtifact).build());
    // endregion
    // region *** RelationType setup ***
    QName hostedOnQName = QName.valueOf("{" + NAMESPACE + "}" + "hostedOn");
    TRelationshipType hostedOnType = new TRelationshipType();
    hostedOnType.setName(hostedOnQName.getLocalPart());
    hostedOnType.setTargetNamespace(hostedOnQName.getNamespaceURI());
    relationshipTypes.put(hostedOnQName, hostedOnType);
    QName connectsToQName = QName.valueOf("{" + NAMESPACE + "}" + "connectsTo");
    TRelationshipType connectsToType = new TRelationshipType();
    connectsToType.setName(connectsToQName.getLocalPart());
    connectsToType.setTargetNamespace(connectsToQName.getNamespaceURI());
    relationshipTypes.put(connectsToQName, connectsToType);
    // endregion
    // region *** create NodeTemplates ***
    TDeploymentArtifact artifact = new TDeploymentArtifact.Builder("test_artifact", QName.valueOf("{" + NAMESPACE + "}" + "WAR")).setArtifactRef(deploymentArtifactIAQName).build();
    TNodeTemplate nt1 = new TNodeTemplate.Builder("test_node_1", nodeType1QName).setName("test_node_1").addDeploymentArtifact(artifact).build();
    nodeTemplates.put(nt1.getId(), nt1);
    TNodeTemplate nt2 = new TNodeTemplate();
    nt2.setType(nodeType2QName);
    nt2.setId("test_node_2");
    nt2.setName("test_node_2");
    nodeTemplates.put(nt2.getId(), nt2);
    TNodeTemplate nt3 = new TNodeTemplate();
    nt3.setType(nodeType3QName);
    nt3.setId("test_node_3");
    nt3.setName("test_node_3");
    TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
    LinkedHashMap<String, String> nt3Properties = new LinkedHashMap<>();
    nt3Properties.put("os_family", "ubuntu");
    nt3Properties.put("public_key", "-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----");
    nt3Properties.put("ssh_port", "22");
    properties.setKVProperties(nt3Properties);
    nt3.setProperties(properties);
    nodeTemplates.put(nt3.getId(), nt3);
    TNodeTemplate nt4 = new TNodeTemplate();
    nt4.setType(nodeType4QName);
    nt4.setId("test_node_4");
    nt4.setName("test_node_4");
    nodeTemplates.put(nt4.getId(), nt4);
    // endregion
    // region *** create RelationshipTemplate ***
    TRelationshipTemplate rt13 = new TRelationshipTemplate();
    rt13.setType(hostedOnQName);
    rt13.setId("1_hosted_on_3");
    rt13.setName("1_hosted_on_3");
    rt13.setSourceNodeTemplate(nt1);
    rt13.setTargetNodeTemplate(nt3);
    relationshipTemplates.put(rt13.getId(), rt13);
    TRelationshipTemplate rt23 = new TRelationshipTemplate();
    rt23.setType(hostedOnQName);
    rt23.setId("2_hosted_on_3");
    rt23.setName("2_hosted_on_3");
    rt23.setSourceNodeTemplate(nt2);
    rt23.setTargetNodeTemplate(nt3);
    relationshipTemplates.put(rt23.getId(), rt23);
    TRelationshipTemplate rt41 = new TRelationshipTemplate();
    rt41.setType(hostedOnQName);
    rt41.setId("4_hosted_on_1");
    rt41.setName("4_hosted_on_1");
    rt41.setSourceNodeTemplate(nt4);
    rt41.setTargetNodeTemplate(nt1);
    relationshipTemplates.put(rt41.getId(), rt41);
    TRelationshipTemplate rt12 = new TRelationshipTemplate();
    rt12.setType(connectsToQName);
    rt12.setId("1_connects_to_2");
    rt12.setName("1_connects_to_2");
    rt12.setSourceNodeTemplate(nt1);
    rt12.setTargetNodeTemplate(nt2);
    relationshipTemplates.put(rt12.getId(), rt12);
    // endregion
    // region *** create edmm type mapping ***
    edmm1to1Mapping.put(nodeType1QName, EdmmType.SOFTWARE_COMPONENT);
    // edmmTypeMapping.put(nodeType2QName, EdmmType.SOFTWARE_COMPONENT);
    edmmTypeExtendsMapping.put(nodeType3QName, EdmmType.COMPUTE);
    edmmTypeExtendsMapping.put(nodeType4QName, EdmmType.WEB_APPLICATION);
    edmm1to1Mapping.put(hostedOnQName, EdmmType.HOSTED_ON);
    edmm1to1Mapping.put(connectsToQName, EdmmType.CONNECTS_TO);
// endregion
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TInterface(org.eclipse.winery.model.tosca.TInterface) TEntityType(org.eclipse.winery.model.tosca.TEntityType) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) LinkedHashMap(java.util.LinkedHashMap) TOperation(org.eclipse.winery.model.tosca.TOperation) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.

the class BackendUtils method initializeProperties.

/**
 * Properties need to be initialized in the case of K/V Properties
 *
 * @param repository     The repository to work on
 * @param entityTemplate the entity template to update
 */
public static void initializeProperties(IRepository repository, TEntityTemplate entityTemplate) {
    Objects.requireNonNull(repository);
    Objects.requireNonNull(entityTemplate);
    Objects.requireNonNull(entityTemplate.getType());
    final TEntityType entityType = repository.getTypeForTemplate(entityTemplate);
    final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
    if (winerysPropertiesDefinition == null) {
        return;
    }
    final LinkedHashMap<String, String> emptyKVProperties = new LinkedHashMap<>();
    for (PropertyDefinitionKV definitionKV : winerysPropertiesDefinition.getPropertyDefinitions()) {
        emptyKVProperties.put(definitionKV.getKey(), "");
    }
    TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
    properties.setNamespace(winerysPropertiesDefinition.getNamespace());
    properties.setElementName(winerysPropertiesDefinition.getElementName());
    properties.setKVProperties(emptyKVProperties);
    entityTemplate.setProperties(properties);
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.

the class BackendUtils method deriveWPD.

/**
 * Derives Winery's Properties Definition from an existing properties definition
 *
 * @param ci     the entity type to try to modify the WPDs
 * @param errors the list to add errors to
 */
// FIXME this is specifically for xml backends and therefore broken under the new canonical model
public static void deriveWPD(TEntityType ci, List<String> errors, IRepository repository) {
    BackendUtils.LOGGER.trace("deriveWPD");
    TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
    if (propertiesDefinition == null) {
        // if there's no properties definition, there's nothing to derive because we're in YAML mode
        return;
    }
    if (!(propertiesDefinition instanceof TEntityType.XmlElementDefinition)) {
        BackendUtils.LOGGER.debug("only works for an element definition, not for types");
        return;
    }
    final QName element = ((TEntityType.XmlElementDefinition) propertiesDefinition).getElement();
    BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
    // fetch the XSD defining the element
    final XsdImportManager xsdImportManager = repository.getXsdImportManager();
    Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
    RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
    if (ref == null) {
        String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
        BackendUtils.LOGGER.debug(msg);
        errors.add(msg);
        return;
    }
    final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref, repository);
    if (!xsModelOptional.isPresent()) {
        LOGGER.error("no XSModel found");
    }
    XSModel xsModel = xsModelOptional.get();
    XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
    if (elementDeclaration == null) {
        String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
        BackendUtils.LOGGER.debug(msg);
        errors.add(msg);
        return;
    }
    // go through the XSD definition and
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    if (!(typeDefinition instanceof XSComplexTypeDefinition)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
        return;
    }
    XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
    XSParticle particle = cTypeDefinition.getParticle();
    if (particle == null) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
        return;
    }
    XSTerm term = particle.getTerm();
    if (!(term instanceof XSModelGroup)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
        return;
    }
    XSModelGroup modelGroup = (XSModelGroup) term;
    if (modelGroup.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
        return;
    }
    XSObjectList particles = modelGroup.getParticles();
    int len = particles.getLength();
    boolean everyThingIsASimpleType = true;
    List<PropertyDefinitionKV> list = new ArrayList<>();
    if (len != 0) {
        for (int i = 0; i < len; i++) {
            XSParticle innerParticle = (XSParticle) particles.item(i);
            XSTerm innerTerm = innerParticle.getTerm();
            if (innerTerm instanceof XSElementDeclaration) {
                XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
                String name = innerElementDeclaration.getName();
                XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
                if (innerTypeDefinition instanceof XSSimpleType) {
                    XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
                    String typeNS = xsSimpleType.getNamespace();
                    String typeName = xsSimpleType.getName();
                    if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                        PropertyDefinitionKV def = new PropertyDefinitionKV();
                        def.setKey(name);
                        // convention at WPD: use "xsd" as prefix for XML Schema Definition
                        def.setType("xsd:" + typeName);
                        list.add(def);
                    } else {
                        everyThingIsASimpleType = false;
                        break;
                    }
                } else {
                    everyThingIsASimpleType = false;
                    break;
                }
            } else {
                everyThingIsASimpleType = false;
                break;
            }
        }
    }
    if (!everyThingIsASimpleType) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
        return;
    }
    // everything went alright, we can add a WPD
    WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
    wpd.setIsDerivedFromXSD(Boolean.TRUE);
    wpd.setElementName(element.getLocalPart());
    wpd.setNamespace(element.getNamespaceURI());
    wpd.setPropertyDefinitions(list);
    ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
    BackendUtils.LOGGER.debug("Successfully generated WPD");
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) XSParticle(org.apache.xerces.xs.XSParticle) TEntityType(org.eclipse.winery.model.tosca.TEntityType) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleType(org.apache.xerces.impl.dv.XSSimpleType) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) XSModelGroup(org.apache.xerces.xs.XSModelGroup) XSTerm(org.apache.xerces.xs.XSTerm) QName(javax.xml.namespace.QName) HasTargetNamespace(org.eclipse.winery.model.tosca.HasTargetNamespace) Namespace(org.eclipse.winery.model.ids.Namespace) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Aggregations

WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)22 PropertyDefinitionKV (org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV)15 TEntityType (org.eclipse.winery.model.tosca.TEntityType)13 QName (javax.xml.namespace.QName)8 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)8 ArrayList (java.util.ArrayList)7 TNodeType (org.eclipse.winery.model.tosca.TNodeType)7 LinkedHashMap (java.util.LinkedHashMap)6 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)6 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)5 IOException (java.io.IOException)4 Produces (javax.ws.rs.Produces)4 TInterface (org.eclipse.winery.model.tosca.TInterface)4 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)4 TRelationshipType (org.eclipse.winery.model.tosca.TRelationshipType)4 TRequirement (org.eclipse.winery.model.tosca.TRequirement)4 IRepository (org.eclipse.winery.repository.backend.IRepository)4 Consumes (javax.ws.rs.Consumes)3 GET (javax.ws.rs.GET)3 POST (javax.ws.rs.POST)3