<commit_before>#########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

__author__ = 'dan'

from dsl_parser import tasks
import json
import uuid
from responses import BlueprintState, Execution, BlueprintValidationStatus, \
    Deployment
from workflow_client import workflow_client


def storage_manager():
    import storage_manager
    return storage_manager.instance()


class DslParseException(Exception):
    pass


class BlueprintsManager(object):

    def blueprints_list(self):
        return storage_manager().blueprints_list()

    def deployments_list(self):
        return storage_manager().deployments_list()

    def get_blueprint(self, blueprint_id):
        return storage_manager().get_blueprint(blueprint_id)

    def get_deployment(self, deployment_id):
        return storage_manager().get_deployment(deployment_id)

    def get_execution(self, execution_id):
        return storage_manager().get_execution(execution_id)

    # TODO: call celery tasks instead of doing this directly here
    # TODO: prepare multi instance plan should be called on workflow execution
    def publish_blueprint(self, dsl_location, alias_mapping_url,
                          resources_base_url):
        # TODO: error code if parsing fails (in one of the 2 tasks)
        try:
            plan = tasks.parse_dsl(dsl_location, alias_mapping_url,
                                   resources_base_url)
        except Exception, ex:
            raise DslParseException(*ex.args)
        new_blueprint = BlueprintState().init(plan=json.loads(plan))
        storage_manager().put_blueprint(new_blueprint.id, new_blueprint)
        return new_blueprint

    # currently validation is split to 2 phases: the first
    # part is during submission (dsl parsing)
    # second part is during call to validate which simply delegates
    # the plan to the workflow service
    # so we can parse all the workflows and see things are ok
    def validate_blueprint(self, blueprint_id):
        blueprint = self.get_blueprint(blueprint_id)
        plan = blueprint.plan
        response = workflow_client().validate_workflows(plan)
        # TODO raise error if error
        return BlueprintValidationStatus().init(blueprint_id=blueprint_id,
                                                status=response['status'])

    def execute_workflow(self, deployment_id, workflow_id):
        #TODO: in the future, take the workflow from the
        # deployment rather than from the blueprint
        deployment = self.get_deployment(deployment_id)
        blueprint = self.get_blueprint(deployment.blueprint_id)
        workflow = blueprint.plan['workflows'][workflow_id]
        plan = blueprint.plan

        execution_id = str(uuid.uuid4())
        response = workflow_client().execute_workflow(
            workflow_id,
            workflow, plan,
            blueprint_id=blueprint.id,
            deployment_id=deployment_id,
            execution_id=execution_id)
        # TODO raise error if there is error in response

        new_execution = Execution().init(
            id=execution_id,
            state=response['state'],
            internal_workflow_id=response['id'],
            created_at=response['created'],
            blueprint_id=blueprint.id,
            workflow_id=workflow_id,
            deployment_id=deployment_id)

        storage_manager().add_execution_to_deployment(deployment.id,
                                                      new_execution)
        storage_manager().put_execution(new_execution.id, new_execution)

        return new_execution

    def get_workflow_state(self, execution_id):
        execution = self.get_execution(execution_id)
        response = workflow_client().get_workflow_status(
            execution.internal_workflow_id)
        execution.status = response['state']
        if execution.status == 'failed':
            execution.error = response['error']
        return execution

    def create_deployment(self, blueprint_id):
        blueprint = self.get_blueprint(blueprint_id)
        plan = blueprint.plan
        deployment_json_plan = tasks.prepare_deployment_plan(plan)
        deployment_id = str(uuid.uuid4())

        new_deployment = Deployment().init(
            deployment_id=deployment_id, plan=json.loads(
                deployment_json_plan), blueprint_id=blueprint_id)
<commit_msg>CLOUDIFY-2392 fixed deployment plan bug<commit_after>#########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

__author__ = 'dan'

from dsl_parser import tasks
import json
import uuid
from responses import BlueprintState, Execution, BlueprintValidationStatus, \
    Deployment
from workflow_client import workflow_client


def storage_manager():
    import storage_manager
    return storage_manager.instance()


class DslParseException(Exception):
    pass


class BlueprintsManager(object):

    def blueprints_list(self):
        return storage_manager().blueprints_list()

    def deployments_list(self):
        return storage_manager().deployments_list()

    def get_blueprint(self, blueprint_id):
        return storage_manager().get_blueprint(blueprint_id)

    def get_deployment(self, deployment_id):
        return storage_manager().get_deployment(deployment_id)

    def get_execution(self, execution_id):
        return storage_manager().get_execution(execution_id)

    # TODO: call celery tasks instead of doing this directly here
    # TODO: prepare multi instance plan should be called on workflow execution
    def publish_blueprint(self, dsl_location, alias_mapping_url,
                          resources_base_url):
        # TODO: error code if parsing fails (in one of the 2 tasks)
        try:
            plan = tasks.parse_dsl(dsl_location, alias_mapping_url,
                                   resources_base_url)
        except Exception, ex:
            raise DslParseException(*ex.args)
        new_blueprint = BlueprintState().init(plan=json.loads(plan))
        storage_manager().put_blueprint(new_blueprint.id, new_blueprint)
        return new_blueprint

    # currently validation is split to 2 phases: the first
    # part is during submission (dsl parsing)
    # second part is during call to validate which simply delegates
    # the plan to the workflow service
    # so we can parse all the workflows and see things are ok
    def validate_blueprint(self, blueprint_id):
        blueprint = self.get_blueprint(blueprint_id)
        plan = blueprint.plan
        response = workflow_client().validate_workflows(plan)
        # TODO raise error if error
        return BlueprintValidationStatus().init(blueprint_id=blueprint_id,
                                                status=response['status'])

    def execute_workflow(self, deployment_id, workflow_id):
        deployment = self.get_deployment(deployment_id)
        workflow = deployment.plan['workflows'][workflow_id]
        plan = deployment.plan

        execution_id = str(uuid.uuid4())
        response = workflow_client().execute_workflow(
            workflow_id,
            workflow, plan,
            blueprint_id=deployment.blueprint_id,
            deployment_id=deployment_id,
            execution_id=execution_id)
        # TODO raise error if there is error in response

        new_execution = Execution().init(
            id=execution_id,
            state=response['state'],
            internal_workflow_id=response['id'],
            created_at=response['created'],
            blueprint_id=deployment.blueprint_id,
            workflow_id=workflow_id,
            deployment_id=deployment_id)

        storage_manager().add_execution_to_deployment(deployment.id,
                                                      new_execution)
        storage_manager().put_execution(new_execution.id, new_execution)

        return new_execution

    def get_workflow_state(self, execution_id):
        execution = self.get_execution(execution_id)
        response = workflow_client().get_workflow_status(
            execution.internal_workflow_id)
        execution.status = response['state']
        if execution.status == 'failed':
            execution.error = response['error']
        return execution

    def create_deployment(self, blueprint_id):
        blueprint = self.get_blueprint(blueprint_id)
        plan = blueprint.plan
        deployment_json_plan = tasks.prepare_deployment_plan(plan)
        deployment_id = str(uuid.uuid4())

        new_deployment = Deployment().init(
            deployment_id=deployment_id, plan=json.loads(
                deployment_json_plan), blueprint_id=blueprint_id)

{'fraction_non_alphanumeric': 0.07323828084931788, 'fraction_numerical': 0.0028720894450712893, 'mean_word_length': 2.8984406237504996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5099741', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2531, 'n_words': 691}