Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoScaleE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 CMCC All rights reserved. *
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22 import org.camunda.bpm.engine.delegate.DelegateExecution
23 import org.json.JSONArray;
24 import static org.apache.commons.lang3.StringUtils.*;
25 import org.openecomp.mso.bpmn.core.json.JsonUtils
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.runtime.Execution
30 import org.json.JSONObject;
31 import org.springframework.web.util.UriUtils;
32
33 /**
34  * This groovy class supports the <class>DoScaleServiceInstance.bpmn</class> process.
35  *
36  */
37 public class DoScaleE2EServiceInstance extends AbstractServiceTaskProcessor {
38
39     String Prefix = "DCRESI_"
40     ExceptionUtil exceptionUtil = new ExceptionUtil()
41     JsonUtils jsonUtil = new JsonUtils()
42
43     public void preProcessRequest(DelegateExecution execution) {
44         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
45         String msg = ""
46         utils.log("DEBUG", " ***** preProcessRequest *****", isDebugEnabled)
47
48         try {
49             String requestId = execution.getVariable("msoRequestId")
50             execution.setVariable("prefix", Prefix)
51
52             //Inputs
53             String globalSubscriberId = execution.getVariable("globalSubscriberId")
54
55             String serviceType = execution.getVariable("serviceType")
56             String serviceInstanceName = execution.getVariable("serviceInstanceName")
57             String serviceInstanceId = execution.getVariable("serviceInstanceId")
58
59             execution.setVariable("serviceType", serviceType)
60
61             String resourceTemplateUUIDs = ""
62             String scaleNsRequest = execution.getVariable("bpmnRequest")
63             JSONObject jsonObject = new JSONObject(scaleNsRequest).getJSONObject("service")
64             JSONArray jsonArray = jsonObject.getJSONArray("resources")
65
66             for (int i = 0; i < jsonArray.size(); i++) {
67                 JSONObject reqBodyJsonObj = jsonArray.getJSONObject(i)
68                 String nsInstanceId = reqBodyJsonObj.getString("resourceInstanceId")
69                 resourceTemplateUUIDs = resourceTemplateUUIDs + nsInstanceId + ":"
70             }
71
72             execution.setVariable("resourceTemplateUUIDs", resourceTemplateUUIDs)
73
74             if (serviceInstanceName == null) {
75                 execution.setVariable("serviceInstanceName", "")
76             }
77             if (isBlank(serviceInstanceId)) {
78                 msg = "Input serviceInstanceId is null"
79                 utils.log("DEBUG", msg, isDebugEnabled)
80                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
81             }
82         } catch (BpmnError e) {
83             throw e;
84         } catch (Exception ex) {
85             msg = "Exception in preProcessRequest " + ex.getMessage()
86             utils.log("DEBUG", msg, isDebugEnabled)
87             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
88         }
89         utils.log("DEBUG", " ***** Exit preProcessRequest *****", isDebugEnabled)
90     }
91
92
93     public void preInitResourcesOperStatus(DelegateExecution execution){
94         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
95
96         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
97         try{
98             String serviceId = execution.getVariable("serviceInstanceId")
99             String operationId = execution.getVariable("operationId")
100             String operationType = "SCALE"
101
102             // resourceTemplateUUIDs should be created ??
103             String resourceTemplateUUIDs = execution.getVariable("resourceTemplateUUIDs")
104             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
105             serviceId = UriUtils.encode(serviceId,"UTF-8")
106             execution.setVariable("serviceInstanceId", serviceId)
107             execution.setVariable("operationId", operationId)
108             execution.setVariable("operationType", operationType)
109
110             execution.setVariable("URN_mso_openecomp_adapters_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
111
112             String payload =
113                     """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
114                         xmlns:ns="http://org.openecomp.mso/requestsdb">
115                         <soapenv:Header/>
116                         <soapenv:Body>
117                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
118                             <serviceId>${serviceId}</serviceId>
119                             <operationId>${operationId}</operationId>
120                             <operationType>${operationType}</operationType>
121                             <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>
122                         </ns:initResourceOperationStatus>
123                     </soapenv:Body>
124                 </soapenv:Envelope>"""
125
126             payload = utils.formatXml(payload)
127             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
128             utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
129             utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
130
131         }catch(Exception e){
132             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
133             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
134         }
135         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
136     }
137
138 }