Merge "edit activity workflow plan for NS INIT"
[vfc/nfvo/lcm.git] / lcm / workflows / views.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 import traceback
17 import sys
18
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
22
23 from lcm.pub.database.models import WFPlanModel
24 from lcm.pub.utils.syscomm import fun_name
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.pub.msapi import activiti
27
28
29 logger = logging.getLogger(__name__)
30
31
32 @api_view(http_method_names=['POST'])
33 def deploy_workflow(request, *args, **kwargs):
34     logger.debug("Enter %s", fun_name())
35     try:
36         file_path = ignore_case_get(request.data, "filePath")
37         force_deploy = ignore_case_get(request.data, "forceDeploy")
38         logger.debug("file_path is %s, force_deploy is %s", file_path, force_deploy)
39         if force_deploy.upper() == "TRUE":
40             plans = WFPlanModel.objects.filter()
41             if len(plans) > 0:
42                 activiti.undeploy_workflow(plans[0].deployed_id)
43                 plans.delete()
44         else:
45             if WFPlanModel.objects.filter():
46                 logger.warn("Already deployed.")
47                 return Response(data={'msg': 'Already deployed.'}, status=status.HTTP_202_ACCEPTED)
48         deploy_info = activiti.deploy_workflow(file_path)
49         WFPlanModel(
50             deployed_id=deploy_info["deployedId"], 
51             process_id=deploy_info["processId"], 
52             status=deploy_info["status"],
53             message=deploy_info["message"],
54             plan_name="ns_instantiate").save()
55     except:
56         logger.error(traceback.format_exc())
57         return Response(data={'error': str(sys.exc_info())}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
58     logger.debug("Leave %s", fun_name())
59     return Response(data={'msg': 'OK'}, status=status.HTTP_202_ACCEPTED)