3302210416828a07c238e0c99fd86dca32b44b06
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
22 \r
23 import static org.apache.commons.lang3.StringUtils.*;\r
24 import groovy.xml.XmlUtil\r
25 import groovy.json.*\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor \r
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil \r
28 import org.openecomp.mso.bpmn.common.scripts.VidUtils \r
29 import org.openecomp.mso.bpmn.core.WorkflowException \r
30 import org.openecomp.mso.bpmn.core.json.JsonUtils \r
31 import org.openecomp.mso.rest.APIResponse\r
32 \r
33 import java.util.UUID;\r
34 \r
35 import org.camunda.bpm.engine.delegate.BpmnError \r
36 import org.camunda.bpm.engine.runtime.Execution\r
37 import org.apache.commons.lang3.*\r
38 import org.apache.commons.codec.binary.Base64;\r
39 import org.springframework.web.util.UriUtils\r
40 \r
41 /**\r
42  * This groovy class supports the <class>CreateGenericE2EServiceInstance.bpmn</class> process.\r
43  * flow for E2E ServiceInstance Create\r
44  */\r
45 public class CreateGenericE2EServiceInstance extends AbstractServiceTaskProcessor {\r
46 \r
47     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
48 \r
49     JsonUtils jsonUtil = new JsonUtils()\r
50 \r
51     VidUtils vidUtils = new VidUtils()\r
52 \r
53     /**\r
54      * Pre Process the BPMN Flow Request\r
55      * Inclouds:\r
56      * Deal with the parameters\r
57      * generate the service instance id\r
58      * generate the operation id\r
59      */\r
60     public void preProcessRequest (Execution execution) {\r
61            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
62        String msg = ""\r
63        utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)\r
64        try {\r
65 \r
66            String siRequest = execution.getVariable("bpmnRequest")\r
67            utils.logAudit(siRequest)\r
68 \r
69            String requestId = execution.getVariable("mso-request-id")\r
70            execution.setVariable("msoRequestId", requestId)\r
71            utils.log("DEBUG", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)\r
72 \r
73            String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
74            if (isBlank(serviceInstanceId)) {\r
75                serviceInstanceId = UUID.randomUUID().toString()\r
76            }\r
77            utils.log("DEBUG", "Generated new Service Instance:" + serviceInstanceId, isDebugEnabled)\r
78            serviceInstanceId = UriUtils.encode(serviceInstanceId,"UTF-8")\r
79            execution.setVariable("serviceInstanceId", serviceInstanceId)\r
80            \r
81            String operationId = execution.getVariable("operationId")\r
82            if (isBlank(operationId)) {\r
83                operationId = UUID.randomUUID().toString()\r
84            }\r
85            utils.log("DEBUG", "Generated new Service Instance Operation:" + operationId, isDebugEnabled)\r
86            serviceInstanceId = UriUtils.encode(operationId,"UTF-8")\r
87            execution.setVariable("operationId", operationId)\r
88            //subscriberInfo, TBD , there is no globalSubscriberId in R1 for E2E Service.\r
89            //requestInfo TBD , there is no requestDetails for R1 E2E service\r
90 \r
91            //TBD need to insert operationInfo to RequestDb\r
92            \r
93            //set service Instance Name\r
94            execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "service.name"))\r
95            execution.setVariable("serviceDescription", jsonUtil.getJsonValue(siRequest, "service.description"))\r
96            execution.setVariable("templateId", jsonUtil.getJsonValue(siRequest, "service.templateId"))\r
97      \r
98            //serviceParamters\r
99            String serviceParamters = jsonUtil.getJsonValue(siRequest, "service.parameters")\r
100            if (isBlank(serviceParamters)) {\r
101                msg = "Input service paramters is null"\r
102                utils.log("DEBUG", msg, isDebugEnabled)\r
103                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
104            } else\r
105            {\r
106                execution.setVariable("serviceParamters", serviceParamters)\r
107            }\r
108 \r
109            utils.log("DEBUG", "service parameters:" + serviceParamters,  isDebugEnabled)\r
110        } catch (BpmnError e) {\r
111            throw e;\r
112        } catch (Exception ex){\r
113            msg = "Exception in preProcessRequest " + ex.getMessage()\r
114            utils.log("DEBUG", msg, isDebugEnabled)\r
115            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
116        }\r
117        utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
118         }\r
119 \r
120     /**\r
121      * send the sync response\r
122      * the response incloudes the instance id and the operation id\r
123      */\r
124     public void sendSyncResponse(Execution execution) {\r
125         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
126         utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)\r
127 \r
128         try {\r
129             String requestId = execution.getVariable("msoRequestId")\r
130             String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
131             String operationId = execution.getVariable("operationId")\r
132             // RESTResponse for API Handler (APIH) Reply Task\r
133             String createServiceRestResponse = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()\r
134             utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + createServiceRestResponse, isDebugEnabled)\r
135             sendWorkflowResponse(execution, 202, createServiceRestResponse)\r
136             execution.setVariable("sentSyncResponse", true)\r
137 \r
138         } catch (Exception ex) {\r
139             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()\r
140             utils.log("DEBUG", msg, isDebugEnabled)\r
141             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
142         }\r
143         utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
144     }\r
145 \r
146     public void preCreateRequest(Execution execution) {\r
147     }\r
148 \r
149     public void postConfigRequest(Execution execution) {\r
150     }\r
151 \r
152     public void preVFCRequest(Execution execution) {\r
153     }\r
154 \r
155     public void preAdaptorDataRequest(Execution execution) {\r
156     }\r
157 \r
158     public void preSDNCRequest(Execution execution) {\r
159     }\r
160 }\r