Search in sources :

Example 1 with BPELPlanContext

use of org.opentosca.planbuilder.core.bpel.context.BPELPlanContext in project container by OpenTOSCA.

the class Handler method handleCreate.

public boolean handleCreate(final BPELPlanContext context, final TRelationshipTemplate relationshipTemplate) {
    final String serviceInstanceVarName = context.getServiceInstanceURLVarName();
    if (serviceInstanceVarName == null) {
        return false;
    }
    final String serviceTemplateUrlVarName = context.getServiceTemplateURLVar();
    if (serviceTemplateUrlVarName == null) {
        return false;
    }
    final String serviceInstanceIdVarName = context.getServiceInstanceIDVarName();
    if (serviceInstanceIdVarName == null) {
        return false;
    }
    /*
         * Pre Phase code
         */
    // create variable for all responses
    final String restCallResponseVarName = createRESTResponseVar(context);
    if (restCallResponseVarName == null) {
        return false;
    }
    // create state variable inside scope
    final String stateVarName = createStateVar(context, context.getRelationshipTemplate().getId());
    if (stateVarName == null) {
        return false;
    }
    // based on the relatioships baseType we add the logic into different
    // phases of relations AND nodes
    // connectsTo = own phases
    // else = source node phasesl
    Element injectionPreElement = null;
    Element injectionPostElement = null;
    final String sourceInstanceVarName = context.findInstanceIDVar(ModelUtils.getSource(context.getRelationshipTemplate(), context.getCsar()).getId(), true);
    final String targetInstanceVarName = context.findInstanceIDVar(ModelUtils.getTarget(context.getRelationshipTemplate(), context.getCsar()).getId(), true);
    if (ModelUtils.getRelationshipTypeHierarchy(ModelUtils.findRelationshipType(context.getRelationshipTemplate(), context.getCsar()), context.getCsar()).contains(Types.connectsToRelationType)) {
        injectionPreElement = context.getPrePhaseElement();
        injectionPostElement = context.getPostPhaseElement();
    } else {
        // fetch nodeTemplate
        final TNodeTemplate sourceNodeTemplate = ModelUtils.getSource(context.getRelationshipTemplate(), context.getCsar());
        LOG.debug("Trying to create provisioning plan context for sourceNodeTemplate {} of relationshipTemplate {}", sourceNodeTemplate.toString(), context.getRelationshipTemplate().toString());
        // Right now the knowledge of DEFROST and PROVISIONING activities is to hard of an assumption, if you ask me
        BPELPlanContext sourceContext = context.createContext(sourceNodeTemplate, ActivityType.PROVISIONING, ActivityType.DEFROST);
        if (sourceContext == null) {
            LOG.error("Couldn't create context for sourceNodeTemplate {}", sourceNodeTemplate);
            return false;
        }
        injectionPreElement = sourceContext.getPostPhaseElement();
        injectionPostElement = sourceContext.getPostPhaseElement();
    }
    if (injectionPostElement == null | injectionPreElement == null | sourceInstanceVarName == null | targetInstanceVarName == null) {
        return false;
    }
    /*
         * import request message type and create variable
         */
    final String createRelTInstanceReqVarName = "createRelationshipTemplateRequest" + context.getIdForNames();
    try {
        final Path opentoscaApiSchemaFile = this.bpelFrags.getOpenTOSCAAPISchemaFile();
        QName createRelationshipTemplateInstanceRequestQName = this.bpelFrags.getOpenToscaApiCreateRelationshipTemplateInstanceRequestElementQname();
        context.registerType(createRelationshipTemplateInstanceRequestQName, opentoscaApiSchemaFile);
        createRelationshipTemplateInstanceRequestQName = context.importQName(createRelationshipTemplateInstanceRequestQName);
        context.addGlobalVariable(createRelTInstanceReqVarName, BPELPlan.VariableType.ELEMENT, createRelationshipTemplateInstanceRequestQName);
    } catch (final IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
    try {
        // create bpel extension activity and append
        final String bpelString = this.fragments.generateBPEL4RESTLightRelationInstancePOST(serviceTemplateUrlVarName, context.getRelationshipTemplate().getId(), createRelTInstanceReqVarName, restCallResponseVarName, sourceInstanceVarName, targetInstanceVarName, serviceInstanceIdVarName);
        Node createRelationInstanceExActiv = ModelUtils.string2dom(bpelString);
        createRelationInstanceExActiv = context.importNode(createRelationInstanceExActiv);
        injectionPreElement.appendChild(createRelationInstanceExActiv);
    } catch (final IOException e2) {
        e2.printStackTrace();
    } catch (final SAXException e) {
        e.printStackTrace();
    } catch (final ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // generate String var for relationInstance URL
    String relationInstanceURLVarName = "";
    if (context.findInstanceURLVar(context.getRelationshipTemplate().getId(), false) == null) {
        // generate String var for relationInstance URL
        relationInstanceURLVarName = createInstanceURLVar(context, context.getRelationshipTemplate().getId());
    } else {
        relationInstanceURLVarName = context.findInstanceURLVar(context.getRelationshipTemplate().getId(), false);
    }
    if (relationInstanceURLVarName == null) {
        return false;
    }
    String relationInstanceIDVarName = "";
    if (context.findInstanceIDVar(context.getRelationshipTemplate().getId(), false) == null) {
        // generate String var for nodeInstance URL
        relationInstanceIDVarName = createInstanceIDVar(context, context.getRelationshipTemplate().getId());
    } else {
        relationInstanceIDVarName = context.findInstanceIDVar(context.getRelationshipTemplate().getId(), false);
    }
    if (relationInstanceIDVarName == null) {
        return false;
    }
    try {
        // save relationInstance data from response
        final String bpelString = this.fragments.generateAssignFromRelationInstancePOSTResponseToStringVar(relationInstanceURLVarName, relationInstanceIDVarName, restCallResponseVarName);
        Node assignRelationInstanceUrl = ModelUtils.string2dom(bpelString);
        assignRelationInstanceUrl = context.importNode(assignRelationInstanceUrl);
        injectionPreElement.appendChild(assignRelationInstanceUrl);
    } catch (final IOException e2) {
        e2.printStackTrace();
    } catch (final SAXException e) {
        e.printStackTrace();
    } catch (final ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // we'll use this later when we determine that the handle Node doesn't
    // have lifecycle operations. Without this check all nodes without
    // lifecycle (or cloud prov operations) will be in an uninstalled state
    final String lastSetState = "INITIAL";
    try {
        // update state variable to uninstalled
        final BPELProcessFragments frag = new BPELProcessFragments();
        Node assignNode = frag.createAssignXpathQueryToStringVarFragmentAsNode("assignInitRelationState" + System.currentTimeMillis(), "string('" + lastSetState + "')", stateVarName);
        assignNode = context.importNode(assignNode);
        injectionPreElement.appendChild(assignNode);
        // send state to api
        final String bpelString = this.fragments.generateBPEL4RESTLightPUTInstanceState(relationInstanceURLVarName, stateVarName);
        Node extActiv = ModelUtils.string2dom(bpelString);
        extActiv = context.importNode(extActiv);
        injectionPreElement.appendChild(extActiv);
    } catch (final IOException e2) {
        e2.printStackTrace();
    } catch (final SAXException e) {
        e.printStackTrace();
    } catch (final ParserConfigurationException e) {
        e.printStackTrace();
    }
    try {
        // set state
        final BPELProcessFragments frag = new BPELProcessFragments();
        Node assignNode = frag.createAssignXpathQueryToStringVarFragmentAsNode("assignFinalNodeState" + System.currentTimeMillis(), "string('CREATED')", stateVarName);
        assignNode = context.importNode(assignNode);
        // create PUT activity
        final String bpelString = this.fragments.generateBPEL4RESTLightPUTInstanceState(relationInstanceURLVarName, stateVarName);
        Node extActiv = ModelUtils.string2dom(bpelString);
        extActiv = context.importNode(extActiv);
        injectionPostElement.appendChild(assignNode);
        injectionPostElement.appendChild(extActiv);
    } catch (final IOException e2) {
        e2.printStackTrace();
    } catch (final SAXException e) {
        e.printStackTrace();
    } catch (final ParserConfigurationException e) {
        e.printStackTrace();
    }
    // needs property update only if the relation has properties
    if (checkProperties(relationshipTemplate.getProperties())) {
        try {
            // fetch properties
            Node nodeInstancePropsGETNode = this.fragments.generateInstancePropertiesGETAsNode(relationInstanceURLVarName, restCallResponseVarName);
            nodeInstancePropsGETNode = context.importNode(nodeInstancePropsGETNode);
            injectionPostElement.appendChild(nodeInstancePropsGETNode);
        } catch (final SAXException e1) {
            e1.printStackTrace();
            return false;
        } catch (final IOException e1) {
            e1.printStackTrace();
            return false;
        }
        // assign the values from the property variables into REST/HTTP
        // Request
        // and send
        // first build a mapping from property variable names to dom element
        final Map<String, QName> propertyVarNameToDOMMapping = buildMappingsFromVarNameToDomElement(context, relationshipTemplate);
        try {
            // then generate an assign to have code that writes the runtime
            // values into the instance data db.
            // we use the restCallResponseVarName from the GET before, as it
            // has
            // proper format
            Node assignNode = this.fragments.generateAssignFromPropertyVarToDomMapping(restCallResponseVarName, propertyVarNameToDOMMapping);
            assignNode = context.importNode(assignNode);
            injectionPostElement.appendChild(assignNode);
        } catch (final SAXException e) {
            e.printStackTrace();
            return false;
        } catch (final IOException e) {
            e.printStackTrace();
            return false;
        }
        // generate BPEL4RESTLight PUT request to update the instance data
        try {
            Node bpel4restPUTNode = this.fragments.generateInstancesBPEL4RESTLightPUTAsNode(restCallResponseVarName, relationInstanceURLVarName);
            bpel4restPUTNode = context.importNode(bpel4restPUTNode);
            injectionPostElement.appendChild(bpel4restPUTNode);
        } catch (final IOException e) {
            e.printStackTrace();
            return false;
        } catch (final SAXException e) {
            e.printStackTrace();
            return false;
        }
    }
    appendProgressionUpdateLogMessage(context, relationshipTemplate.getId());
    return true;
}
Also used : XPath(javax.xml.xpath.XPath) Path(java.nio.file.Path) BPELPlanContext(org.opentosca.planbuilder.core.bpel.context.BPELPlanContext) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) BPELProcessFragments(org.opentosca.planbuilder.core.bpel.fragments.BPELProcessFragments) SAXException(org.xml.sax.SAXException)

Example 2 with BPELPlanContext

use of org.opentosca.planbuilder.core.bpel.context.BPELPlanContext in project container by OpenTOSCA.

the class PatternBasedHandler method createDeployDescriptorPropertyToParameterMatching.

/**
 * Try to find a parameter matching in the deployment technology descriptors. For every previously unmatched
 * operation parameters a matching property in the deployment technology descriptors is searched.
 *
 * @param context          The context, providing the deployment technology descriptor variable mapping
 * @param abstractMatching The previous node property matching
 * @param nodesForMatching Set of nodes that can be searched for deployment technology descriptor property matches
 * @return The computed parameter matching
 */
private DeployTechDescriptorOperationMatching createDeployDescriptorPropertyToParameterMatching(final BPELPlanContext context, final OperationMatching abstractMatching, Set<TNodeTemplate> nodesForMatching) {
    final DeployTechDescriptorOperationMatching matching = new DeployTechDescriptorOperationMatching(abstractMatching);
    TOperation operation = matching.operationName;
    if (operation.getInputParameters() != null) {
        Set<TParameter> unmappedInputParams = operation.getInputParameters().stream().filter(tParameter -> !matching.abstractInputMatching.containsKey(tParameter)).collect(Collectors.toSet());
        for (TParameter unmappedInputParam : unmappedInputParams) {
            for (TNodeTemplate nodeToMatch : nodesForMatching) {
                PropertyVariable propVar = context.getPropertyVariable(nodeToMatch, unmappedInputParam.getName());
                if (propVar != null) {
                    matching.concreteInputMatching.put(unmappedInputParam, propVar);
                    matching.matchedNodes.add(nodeToMatch);
                }
            }
        }
    }
    if (operation.getOutputParameters() != null) {
        Set<TParameter> unmappedOutputParams = operation.getOutputParameters().stream().filter(tParameter -> !matching.abstractOutputMatching.containsKey(tParameter)).collect(Collectors.toSet());
        for (TParameter unmappedOutputParam : unmappedOutputParams) {
            for (TNodeTemplate nodeToMatch : nodesForMatching) {
                PropertyVariable propVar = context.getPropertyVariable(nodeToMatch, unmappedOutputParam.getName());
                if (propVar != null) {
                    matching.concreteOutputMatching.put(unmappedOutputParam, propVar);
                    matching.matchedNodes.add(nodeToMatch);
                }
            }
        }
    }
    return matching;
}
Also used : Collection(java.util.Collection) Utils(org.opentosca.container.core.convention.Utils) Variable(org.opentosca.planbuilder.core.plugins.context.Variable) Set(java.util.Set) HashMap(java.util.HashMap) BPELPlanContext(org.opentosca.planbuilder.core.bpel.context.BPELPlanContext) PlanContext(org.opentosca.planbuilder.core.plugins.context.PlanContext) Collectors(java.util.stream.Collectors) TInterface(org.eclipse.winery.model.tosca.TInterface) HashSet(java.util.HashSet) Objects(java.util.Objects) Element(org.w3c.dom.Element) TParameter(org.eclipse.winery.model.tosca.TParameter) BPELInvokerPlugin(org.opentosca.planbuilder.provphase.plugin.invoker.bpel.BPELInvokerPlugin) Map(java.util.Map) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) TOperation(org.eclipse.winery.model.tosca.TOperation) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ModelUtils(org.opentosca.container.core.model.ModelUtils) PropertyVariable(org.opentosca.planbuilder.core.plugins.context.PropertyVariable) TOperation(org.eclipse.winery.model.tosca.TOperation) PropertyVariable(org.opentosca.planbuilder.core.plugins.context.PropertyVariable) TParameter(org.eclipse.winery.model.tosca.TParameter) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 3 with BPELPlanContext

use of org.opentosca.planbuilder.core.bpel.context.BPELPlanContext in project container by OpenTOSCA.

the class RemoteManagerPatternBasedHandler method handleCreate.

public boolean handleCreate(final BPELPlanContext context, final TNodeTemplate nodeTemplate, Element elementToAppendTo) {
    final TInterface iface = ModelUtils.getInterfaceOfNode(nodeTemplate, Interfaces.OPENTOSCA_INTERFACE_REMOTE_MANAGER, context.getCsar());
    final TOperation createOperation = getRemoteManagerInstallOperation(nodeTemplate, context.getCsar());
    final Set<TNodeTemplate> nodesForMatching = calculateNodesForMatching(nodeTemplate, context.getCsar());
    // For the future we should think about integrating the fileupload plugin into the pattern plugin or refactoring it, cause:
    // The fileupload plugin implicitly works according to the lifecycle/container pattern as in that case it can just traverse the the topology downward along the hostedOn relations.
    // The remote manager pattern uses a dependsOn relation to a managing node which used to find the operation for uploading files/DAs
    TNodeTemplate infraNode = this.getRemoteManagerNode(nodeTemplate, context.getCsar());
    nodeTemplate.getDeploymentArtifacts().forEach(da -> ModelUtils.findArtifactTemplate(da.getArtifactRef(), context.getCsar()).getArtifactReferences().forEach(ref -> this.invokeArtifactReferenceUpload(context, ref, infraNode)));
    return invokeWithMatching(context, nodeTemplate, iface, createOperation, nodesForMatching, elementToAppendTo);
}
Also used : Csar(org.opentosca.container.core.model.csar.Csar) Types(org.opentosca.container.core.convention.Types) Collection(java.util.Collection) Utils(org.opentosca.container.core.convention.Utils) Set(java.util.Set) BPELPlanContext(org.opentosca.planbuilder.core.bpel.context.BPELPlanContext) TInterface(org.eclipse.winery.model.tosca.TInterface) HashSet(java.util.HashSet) Element(org.w3c.dom.Element) Interfaces(org.opentosca.container.core.convention.Interfaces) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TOperation(org.eclipse.winery.model.tosca.TOperation) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ModelUtils(org.opentosca.container.core.model.ModelUtils) TInterface(org.eclipse.winery.model.tosca.TInterface) TOperation(org.eclipse.winery.model.tosca.TOperation) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 4 with BPELPlanContext

use of org.opentosca.planbuilder.core.bpel.context.BPELPlanContext in project container by OpenTOSCA.

the class BPELScaleOutProcessBuilder method runProvisioningLogicGeneration.

private void runProvisioningLogicGeneration(final BPELPlan plan, final TRelationshipTemplate relationshipTemplate, final Property2VariableMapping map, Csar csar) {
    // handling relationshiptemplate
    final BPELPlanContext context = this.createContext(relationshipTemplate, plan, map, csar);
    // TODO implement fallback
    if (this.pluginRegistry.findTypePluginForCreation(relationshipTemplate, context.getCsar()) != null) {
        LOG.debug("Handling RelationshipTemplate {} with type plugin", relationshipTemplate.getId());
        IPlanBuilderTypePlugin plugin = this.pluginRegistry.findTypePluginForCreation(relationshipTemplate, context.getCsar());
        this.pluginRegistry.handleCreateWithTypePlugin(context, relationshipTemplate, plugin);
    } else {
        LOG.debug("Couldn't handle RelationshipTemplate {} with type plugin", relationshipTemplate.getId());
    }
    for (final IPlanBuilderPostPhasePlugin postPhasePlugin : this.pluginRegistry.getPostPlugins()) {
        if (postPhasePlugin.canHandleCreate(context, relationshipTemplate)) {
            postPhasePlugin.handleCreate(context, relationshipTemplate);
        }
    }
}
Also used : BPELPlanContext(org.opentosca.planbuilder.core.bpel.context.BPELPlanContext) IPlanBuilderPostPhasePlugin(org.opentosca.planbuilder.core.plugins.typebased.IPlanBuilderPostPhasePlugin) IPlanBuilderTypePlugin(org.opentosca.planbuilder.core.plugins.typebased.IPlanBuilderTypePlugin)

Example 5 with BPELPlanContext

use of org.opentosca.planbuilder.core.bpel.context.BPELPlanContext in project container by OpenTOSCA.

the class BPELScopeBuilder method calculateBestImplementationDACandidates.

/**
 * Calculates correct mappings of the given NodeTypeImplementations, PrePhaseDAPlugins and InfrastructureNodes for
 * the given ProvisioningChain
 *
 * @param impls      a List of NodeTypeImplementations
 * @param plugins    a List of PrePhaseDAPlugins
 * @param infraNodes a List of InfrastructureNode of the NodeTemplate the NodeTypeImplementations belong to
 * @param chain      a ProvisioningChain where the candidates are added to
 */
private void calculateBestImplementationDACandidates(BPELPlanContext context, final TNodeTemplate nodeTemplate, final Collection<TNodeTypeImplementation> impls, final Collection<IPlanBuilderPrePhaseDAPlugin<BPELPlanContext>> plugins, final Collection<TNodeTemplate> infraNodes, final OperationChain chain) {
    final List<DANodeTypeImplCandidate> candidates = new ArrayList<>();
    for (final TNodeTypeImplementation impl : impls) {
        LOG.debug("Checking DAs of NodeTypeImpl {} and NodeTemplate {}", impl.getName(), nodeTemplate.getId());
        final DANodeTypeImplCandidate candidate = new DANodeTypeImplCandidate(nodeTemplate, impl, context.getCsar());
        final List<TDeploymentArtifact> effectiveDAs = ModelUtils.calculateEffectiveDAs(nodeTemplate, impl, context.getCsar());
        for (final TDeploymentArtifact da : effectiveDAs) {
            LOG.debug("Checking whether DA {} can be deployed", da.getName());
            for (final TNodeTemplate infraNode : infraNodes) {
                LOG.debug("Checking if DA {} can be deployed on InfraNode {}", da.getName(), infraNode.getId());
                for (final IPlanBuilderPrePhaseDAPlugin<BPELPlanContext> plugin : plugins) {
                    LOG.debug("Checking with Plugin {}", plugin.getID());
                    if (plugin.canHandle(context, da, ModelUtils.findNodeType(infraNode, context.getCsar()))) {
                        LOG.debug("Adding Plugin, can handle DA on InfraNode");
                        candidate.add(da, infraNode, plugin);
                    }
                }
            }
        }
        if (candidate.isValid()) {
            LOG.debug("Generated Candidate was valid, adding to all Candidates");
            candidates.add(candidate);
        } else {
            LOG.debug("Generated Candidate was invalid, don't add to all Candidates");
        }
    }
    chain.daCandidates = candidates;
}
Also used : BPELPlanContext(org.opentosca.planbuilder.core.bpel.context.BPELPlanContext) ArrayList(java.util.ArrayList) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Aggregations

BPELPlanContext (org.opentosca.planbuilder.core.bpel.context.BPELPlanContext)23 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)16 BPELScope (org.opentosca.planbuilder.model.plan.bpel.BPELScope)14 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)10 QName (javax.xml.namespace.QName)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 BPELScopeBuilder (org.opentosca.planbuilder.core.bpel.artifactbasednodehandler.BPELScopeBuilder)6 IPlanBuilderPostPhasePlugin (org.opentosca.planbuilder.core.plugins.typebased.IPlanBuilderPostPhasePlugin)6 Node (org.w3c.dom.Node)6 SAXException (org.xml.sax.SAXException)6 TOperation (org.eclipse.winery.model.tosca.TOperation)5 TParameter (org.eclipse.winery.model.tosca.TParameter)5 Property2VariableMapping (org.opentosca.planbuilder.core.plugins.context.Property2VariableMapping)5 Variable (org.opentosca.planbuilder.core.plugins.context.Variable)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 ModelUtils (org.opentosca.container.core.model.ModelUtils)4 BPELPlan (org.opentosca.planbuilder.model.plan.bpel.BPELPlan)4 Collection (java.util.Collection)3