Search in sources :

Example 31 with TEntityTemplate

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

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

the class PatternDetectionUtilsTest method swapDetectorWithRefinement.

@Test
public void swapDetectorWithRefinement() {
    TTopologyTemplate detector = new TTopologyTemplate();
    TTopologyTemplate refinementStructure = new TTopologyTemplate();
    TEntityTemplate detectorElement = new TNodeTemplate();
    TEntityTemplate refinementElement = new TNodeTemplate();
    OTRelationMapping relationMapping = new OTRelationMapping(new OTRelationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDirection(OTRelationDirection.INGOING).setRelationType("relationType").setValidSourceOrTarget(QName.valueOf("validSourceOrTarget")));
    OTPermutationMapping permutationMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
    OTAttributeMapping attributeMapping = new OTAttributeMapping(new OTAttributeMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDetectorProperty("detectorProperty").setRefinementProperty("refinementProperty"));
    OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
    OTDeploymentArtifactMapping deploymentArtifactMapping = new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setArtifactType(QName.valueOf("artifactType")));
    OTBehaviorPatternMapping behaviorPatternMapping = new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("behaviorPattern").setProperty(new OTPropertyKV("key", "value")));
    OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinementStructure).setRelationMappings(Collections.singletonList(relationMapping)).setPermutationMappings(Collections.singletonList(permutationMapping)).setAttributeMappings(Collections.singletonList(attributeMapping)).setStayMappings(Collections.singletonList(stayMapping)).setDeploymentArtifactMappings(Collections.singletonList(deploymentArtifactMapping)).setBehaviorPatternMappings(Collections.singletonList(behaviorPatternMapping)));
    PatternDetectionUtils.swapDetectorWithRefinement(prm);
    assertEquals(prm.getDetector(), refinementStructure);
    assertEquals(prm.getRefinementStructure(), detector);
    assertEquals(relationMapping.getDetectorElement(), refinementElement);
    assertEquals(relationMapping.getRefinementElement(), detectorElement);
    assertEquals(relationMapping.getDirection(), OTRelationDirection.INGOING);
    assertEquals(relationMapping.getRelationType(), QName.valueOf("relationType"));
    assertEquals(relationMapping.getValidSourceOrTarget(), QName.valueOf("validSourceOrTarget"));
    assertEquals(permutationMapping.getDetectorElement(), refinementElement);
    assertEquals(permutationMapping.getRefinementElement(), detectorElement);
    assertEquals(attributeMapping.getDetectorElement(), refinementElement);
    assertEquals(attributeMapping.getRefinementElement(), detectorElement);
    assertEquals(attributeMapping.getDetectorProperty(), "refinementProperty");
    assertEquals(attributeMapping.getRefinementProperty(), "detectorProperty");
    assertEquals(stayMapping.getDetectorElement(), refinementElement);
    assertEquals(stayMapping.getRefinementElement(), detectorElement);
    assertEquals(deploymentArtifactMapping.getDetectorElement(), refinementElement);
    assertEquals(deploymentArtifactMapping.getRefinementElement(), detectorElement);
    assertEquals(deploymentArtifactMapping.getArtifactType(), QName.valueOf("artifactType"));
    assertEquals(behaviorPatternMapping.getDetectorElement(), refinementElement);
    assertEquals(behaviorPatternMapping.getRefinementElement(), detectorElement);
    assertEquals(behaviorPatternMapping.getBehaviorPattern(), "behaviorPattern");
    assertEquals(behaviorPatternMapping.getProperty(), new OTPropertyKV("key", "value"));
}
Also used : OTDeploymentArtifactMapping(org.eclipse.winery.model.tosca.extensions.OTDeploymentArtifactMapping) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) OTAttributeMapping(org.eclipse.winery.model.tosca.extensions.OTAttributeMapping) OTRelationMapping(org.eclipse.winery.model.tosca.extensions.OTRelationMapping) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) Test(org.junit.jupiter.api.Test)

Example 33 with TEntityTemplate

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

the class ToscaPropertyMatcher method propertiesCompatible.

public boolean propertiesCompatible(ToscaEntity left, ToscaEntity right) {
    // By convention, the left node is always the element to search in right.
    TEntityTemplate detectorElement = left.getTemplate();
    TEntityTemplate candidate = right.getTemplate();
    // TODO the implementation (currently) works for KV properties only
    Map<String, String> detectorProperties = ModelUtilities.getPropertiesKV(detectorElement);
    Map<String, String> candidateProperties = ModelUtilities.getPropertiesKV(candidate);
    if (detectorProperties == null || detectorProperties.isEmpty()) {
        // detector and candidate are always compatible if detector specifies no properties
        return true;
    }
    if (candidateProperties == null || candidateProperties.isEmpty()) {
        // detector and candidate cannot be compatible if detector requires properties, but candidate has none
        return false;
    }
    return detectorProperties.entrySet().stream().allMatch(entry -> {
        String val = entry.getValue();
        if (StringUtils.isEmpty(val)) {
            // always match if detector value is empty
            return true;
        }
        // Assumption: properties are simple KV Properties
        String refProp = candidateProperties.get(entry.getKey());
        if (StringUtils.isEmpty(refProp)) {
            // cannot match if candidate value is empty but detector value is not
            return false;
        }
        if (val.equalsIgnoreCase("*")) {
            // if the detector defines a wildcard, the property must be set in the candidate
            return StringUtils.isNotEmpty(refProp);
        } else {
            // if the detector defines a specific value, the candidate's property must match
            if (matchPropertiesByRegex) {
                return Pattern.matches(val, refProp);
            } else {
                return val.equalsIgnoreCase(refProp);
            }
        }
    });
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate)

Example 34 with TEntityTemplate

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

the class ToscaBehaviorPatternMatcher method propertiesCompatible.

@Override
public boolean propertiesCompatible(ToscaEntity left, ToscaEntity right) {
    // By convention, the left node is always the element to search in right.
    TEntityTemplate detectorElement = left.getTemplate();
    TEntityTemplate candidateElement = right.getTemplate();
    return propertiesCompatible(detectorElement, candidateElement);
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate)

Example 35 with TEntityTemplate

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

the class ToscaComponentPatternMatcher method componentPatternsCompatible.

protected boolean componentPatternsCompatible(ToscaEntity left, ToscaEntity right) {
    // By convention, the left node is always the element to search in right.
    TEntityTemplate detectorElement = left.getTemplate();
    TEntityTemplate candidateElement = right.getTemplate();
    if (!(detectorElement instanceof TNodeTemplate) || !(candidateElement instanceof TNodeTemplate)) {
        return false;
    }
    if (prm.getPermutationMappings() == null || prm.getPermutationMappings().isEmpty()) {
        return false;
    }
    List<OTPermutationMapping> detectorMappings = prm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, prm) && mapping.getDetectorElement().getId().equals(detectorElement.getId())).collect(Collectors.toList());
    if (detectorMappings.isEmpty()) {
        return false;
    }
    TEntityTemplate refinementElement = detectorMappings.get(0).getRefinementElement();
    return refinementModels.stream().map(OTPatternRefinementModel.class::cast).filter(otherPrm -> otherPrm.getPermutationMappings() != null && !otherPrm.getPermutationMappings().isEmpty()).anyMatch(otherPrm -> {
        List<OTPermutationMapping> matchingDetectorMappings = otherPrm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, otherPrm) && super.isCompatible(mapping.getDetectorElement(), candidateElement) && mapping.getRefinementElement().getType().equals(refinementElement.getType())).collect(Collectors.toList());
        if (!matchingDetectorMappings.isEmpty()) {
            componentPatternCandidates.get(prm).add(new ComponentPatternCandidate((TNodeTemplate) detectorElement, (TNodeTemplate) refinementElement, otherPrm, (TNodeTemplate) matchingDetectorMappings.get(0).getDetectorElement()));
            return true;
        }
        return false;
    });
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) Set(java.util.Set) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) List(java.util.List) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) Map(java.util.Map) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) QName(javax.xml.namespace.QName) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Aggregations

TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)42 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)19 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)14 QName (javax.xml.namespace.QName)13 ArrayList (java.util.ArrayList)9 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)9 Map (java.util.Map)8 HashSet (java.util.HashSet)7 List (java.util.List)7 Consumes (javax.ws.rs.Consumes)7 Produces (javax.ws.rs.Produces)7 TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)7 TPolicy (org.eclipse.winery.model.tosca.TPolicy)7 PUT (javax.ws.rs.PUT)6 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)6 TEntityType (org.eclipse.winery.model.tosca.TEntityType)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Objects (java.util.Objects)4 Collectors (java.util.stream.Collectors)4