Search in sources :

Example 41 with TEntityTemplate

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

the class ToscaEngine method getEntityTemplateProperties.

@Nullable
public static Document getEntityTemplateProperties(TEntityTemplate template) {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    Document doc = null;
    try {
        doc = documentBuilderFactory.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        LOG.error("Couldn't create parser", e);
        return null;
    }
    if (template.getProperties() instanceof TEntityTemplate.WineryKVProperties) {
        TEntityTemplate.WineryKVProperties props = (TEntityTemplate.WineryKVProperties) template.getProperties();
        Map<String, String> propMap = props.getKVProperties();
        // So just that people understand:
        /*
                <Properties>
                    <Properties xmlns="http://opentosca.org/nodetypes/propertiesdefinition/winery">
                        <ContainerPort>80</ContainerPort>
                        <Port>get_input: ApplicationPort</Port>
                        <ContainerID/>
                        <ContainerIP/>
                    </Properties>
                </Properties>

                <Properties>
                    <DockerEngine_Properties xmlns="http://opentosca.org/nodetypes/properties">
                        <DockerEngineURL>get_input: DockerEngineURL</DockerEngineURL>
                        <DockerEngineCertificate/>
                        <State>Running</State>
                    </DockerEngine_Properties>
                </Properties>

                In case of the  DockerEngine Properties props.getElementName() returns DockerEngine_Properties

                But

                in case of the MyTinyToDoDocker Container Properties props.getElementName() returns NULL!!

                TODO: FIXME in winery!
                 */
        Element rootElement = doc.createElementNS(props.getNamespace(), props.getElementName() != null ? props.getElementName() : "Properties");
        doc.appendChild(rootElement);
        for (String propName : propMap.keySet()) {
            Element propElement = doc.createElementNS(props.getNamespace(), propName);
            propElement.setTextContent(propMap.get(propName));
            rootElement.appendChild(propElement);
        }
    }
    return doc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 42 with TEntityTemplate

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

the class RulesChecker method checkRules.

private boolean checkRules(final List<TServiceTemplate> stRuleList, final String ruleType, final TServiceTemplate serviceTemplate, final List<TParameterDTO> inputParameters) throws ParserConfigurationException {
    for (final TServiceTemplate stRule : stRuleList) {
        LOG.debug("Checking Rule: " + stRule.getName() + " RuleType: " + ruleType);
        final TTopologyTemplate rule = stRule.getTopologyTemplate();
        final List<TEntityTemplate> templateRuleList = rule.getNodeTemplateOrRelationshipTemplate();
        for (final TEntityTemplate templateRule : templateRuleList) {
            if (!(templateRule instanceof TRelationshipTemplate)) {
                continue;
            }
            final TRelationshipTemplate relationshipRule = (TRelationshipTemplate) templateRule;
            final TNodeTemplate sourceRuleNTemplate = (TNodeTemplate) relationshipRule.getSourceElement().getRef();
            final TNodeTemplate targetRuleNTemplate = (TNodeTemplate) relationshipRule.getTargetElement().getRef();
            boolean ruleCanBeApplied = false;
            // check for types
            if (sourceRuleNTemplate.getId().equals("*")) {
                final List<TNodeTemplate> nodeTemplates = serviceTemplate.getTopologyTemplate().getNodeTemplates();
                for (final TNodeTemplate nodeTemplate : nodeTemplates) {
                    final QName nodeType = nodeTemplate.getType();
                    // found matching nodetemplate
                    if (nodeType.equals(sourceRuleNTemplate.getType())) {
                        LOG.debug("Rule " + stRule.getName() + " can be applied to Service Template: " + serviceTemplate + ". Reason: Matching Source NodeTypes.");
                        ruleCanBeApplied = true;
                    }
                }
            // check for identical IDs
            } else {
                // check source
                if (ToscaEngine.getNodeTemplate(serviceTemplate, sourceRuleNTemplate.getId()).isPresent()) {
                    LOG.debug("Rule " + stRule.getName() + " can be applied to Service Template: " + serviceTemplate + ". Reason: Matching Source NodeTemplateIDs.");
                    ruleCanBeApplied = true;
                }
            }
            if (!ruleCanBeApplied) {
                LOG.debug("Rule " + stRule.getName() + " can not be applied to Service Template: " + serviceTemplate + ".Thus, rule is ignored.");
                continue;
            }
            if (targetRuleNTemplate.getId().equals("*")) {
                targetRuleNTemplate.getType();
                boolean found = false;
                while (!found) {
                    TNodeTemplate targetNT = ToscaEngine.getRelatedNodeTemplate(serviceTemplate, sourceRuleNTemplate, relationshipRule.getType());
                    if (targetNT == null) {
                        switch(ruleType) {
                            case "white":
                                LOG.warn("Target Node Template not found. Rule not fulfilled.");
                                return false;
                            case "black":
                                LOG.debug("Nodes are not matching. Rule is fulfilled.");
                                break;
                        }
                    } else {
                        final QName relatedNodeType = targetNT.getType();
                        if (relatedNodeType.equals(targetRuleNTemplate.getType())) {
                            found = true;
                            LOG.debug("Matching Target Node Type found. Node Template: " + targetNT);
                            // comparing properties
                            if (arePropertiesMatching(sourceRuleNTemplate, inputParameters, targetRuleNTemplate)) {
                                switch(ruleType) {
                                    case "white":
                                        LOG.debug("Properties are matching. Rule is fulfilled.");
                                        break;
                                    case "black":
                                        LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are matching.");
                                        return false;
                                }
                            } else {
                                switch(ruleType) {
                                    case "white":
                                        LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are not matching.");
                                        return false;
                                    case "black":
                                        LOG.debug("Properties are not matching. Rule is fulfilled.");
                                        break;
                                }
                            }
                        }
                    }
                }
            // check target nodetemplateID
            } else {
                Optional<TNodeTemplate> nodeTemplate = ToscaEngine.getNodeTemplate(serviceTemplate, targetRuleNTemplate.getId());
                if (nodeTemplate.isPresent()) {
                    // comparing properties
                    if (arePropertiesMatching(sourceRuleNTemplate, inputParameters, targetRuleNTemplate)) {
                        switch(ruleType) {
                            case "white":
                                LOG.debug("Properties are matching. Rule is fulfilled.");
                                break;
                            case "black":
                                LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are matching.");
                                return false;
                        }
                    } else {
                        switch(ruleType) {
                            case "white":
                                LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are not matching.");
                                return false;
                            case "black":
                                LOG.debug("Properties are not matching. Rule is fulfilled.");
                                break;
                        }
                    }
                // if source is matching but target isn't, abort
                } else {
                    switch(ruleType) {
                        case "white":
                            LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Source is matching, but target isn't.");
                            return false;
                        case "black":
                            LOG.debug("Nodes are not matching. Rule is fulfilled.");
                            break;
                    }
                }
            }
        }
    }
    return true;
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

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