Fix pep8 error for vfc-nfvo-lcm/ns/sfcs
[vfc/nfvo/lcm.git] / lcm / ns / sfcs / views.py
1 # Copyright 2016 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
16 import json
17 import logging
18 import traceback
19 import uuid
20
21 import time
22 from rest_framework import status
23 from rest_framework.response import Response
24 from rest_framework.views import APIView
25
26 from lcm.ns.sfcs.create_flowcla import CreateFlowClassifier
27 from lcm.ns.sfcs.create_port_chain import CreatePortChain
28 from lcm.ns.sfcs.create_portpairgp import CreatePortPairGroup
29 from lcm.ns.sfcs.create_sfc_worker import CreateSfcWorker
30 from lcm.ns.sfcs.sfc_instance import SfcInstance
31 from lcm.ns.sfcs.utils import get_fp_id, ignorcase_get
32
33 logger = logging.getLogger(__name__)
34
35
36 class SfcInstanceView(APIView):
37     def post(self, request):
38         data = {
39             'nsinstid': request.data['nsinstanceid'],
40             "ns_model_data": json.loads(request.data['context']),
41             'fpindex': request.data['fpindex'],
42             'fpinstid': str(uuid.uuid4()),
43             'sdncontrollerid': request.data["sdncontrollerid"]}
44         rsp = SfcInstance(data).do_biz()
45         return Response(data=rsp, status=status.HTTP_200_OK)
46
47
48 class PortPairGpView(APIView):
49     def post(self, request):
50         data = {
51             'fpinstid': request.data["fpinstid"],
52             "ns_model_data": json.loads(request.data['context']),
53             'nsinstid': request.data["nsinstanceid"]}
54         CreatePortPairGroup(data).do_biz()
55         return Response(status=status.HTTP_200_OK)
56
57
58 class FlowClaView(APIView):
59     def post(self, request):
60         data = {
61             'fpinstid': request.data["fpinstid"],
62             "ns_model_data": json.loads(request.data['context'])}
63         CreateFlowClassifier(data).do_biz()
64         return Response(status=status.HTTP_200_OK)
65
66
67 class PortChainView(APIView):
68     def post(self, request):
69         data = {
70             'fpinstid': request.data["fpinstid"],
71             "ns_model_data": json.loads(request.data['context'])}
72         CreatePortChain(data).do_biz()
73         return Response(status=status.HTTP_200_OK)
74
75
76 class SfcView(APIView):
77     def post(self, request):
78         try:
79             logger.info("Create Service Function Chain start")
80             logger.info("service_function_chain_request: %s" % json.dumps(request.data))
81             logger.info("service_function_chain_context  : %s" % json.dumps(request.data['context']))
82             logger.info("service_function_chain_context  : %s" % request.data['context'])
83             logger.info("service_function_chain_instanceid : %s" % ignorcase_get(request.data, 'nsinstanceid'))
84             logger.info("service_function_chain_sdncontrollerid : %s" % ignorcase_get(request.data, 'sdncontrollerid'))
85             logger.info("service_function_chain_fpindex : %s" % ignorcase_get(request.data, 'fpindex'))
86             ns_model_data = request.data['context']
87         except Exception as e:
88             logger.error("Exception occurs: %s", e.message)
89             logger.error(traceback.format_exc())
90         data = {
91             'nsinstid': ignorcase_get(request.data, 'nsinstanceid'),
92             "ns_model_data": ns_model_data,
93             'fpindex': get_fp_id(ignorcase_get(request.data, 'fpindex'), ns_model_data),
94             'fpinstid': str(uuid.uuid4()),
95             'sdncontrollerid': ignorcase_get(request.data, 'sdncontrollerid')
96         }
97         logger.info("Save FPInstModel start: ")
98         SfcInstance(data).do_biz()
99         logger.info("Save FPInstModel end: ")
100         worker = CreateSfcWorker(data)
101         job_id = worker.init_data()
102         worker.start()
103         logger.info("Service Function Chain Thread Sleep start : %s" % time.ctime())
104         time.sleep(2)
105         logger.info("Service Function Chain Thread Sleep end: %s" % time.ctime())
106         logger.info("Create Service Function Chain end")
107         return Response(data={"jobId": job_id,
108                               "sfcInstId": data["fpinstid"]},
109                         status=status.HTTP_200_OK)