Merge "Add deploy workflow logic"
[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             WFPlanModel.objects.filter().delete()
41         else:
42             if WFPlanModel.objects.filter():
43                 logger.warn("Already deployed.")
44                 return Response(data={'msg': 'Already deployed.'}, status=status.HTTP_202_ACCEPTED)
45         deploy_info = activiti.deploy_workflow(file_path)
46         WFPlanModel(
47             deployed_id=deploy_info["deployedId"], 
48             process_id=deploy_info["processId"], 
49             status=deploy_info["status"],
50             message=deploy_info["message"],
51             plan_name="ns_instantiate").save()
52     except:
53         logger.error(traceback.format_exc())
54         return Response(data={'error': str(sys.exc_info())}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
55     logger.debug("Leave %s", fun_name())
56     return Response(data={'msg': 'OK'}, status=status.HTTP_202_ACCEPTED)
57
58
59
60
61