Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / 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.onap.so.bpmn.infrastructure.scripts
21
22 import static org.apache.commons.lang3.StringUtils.*;
23
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.json.JSONArray;
27 import org.json.JSONObject;
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.scripts.MsoUtils
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
34 import org.springframework.web.util.UriUtils;
35
36
37
38 /**
39  * This groovy class supports the <class>DoScaleServiceInstance.bpmn</class> process.
40  *
41  */
42 public class DoScaleE2EServiceInstance extends AbstractServiceTaskProcessor {
43         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoScaleE2EServiceInstance.class);
44
45
46     String Prefix = "DCRESI_"
47     ExceptionUtil exceptionUtil = new ExceptionUtil()
48     JsonUtils jsonUtil = new JsonUtils()
49
50     public void preProcessRequest(DelegateExecution execution) {
51         String msg = ""
52         msoLogger.trace("preProcessRequest ")
53
54         try {
55             String requestId = execution.getVariable("msoRequestId")
56             execution.setVariable("prefix", Prefix)
57
58             //Inputs
59             String globalSubscriberId = execution.getVariable("globalSubscriberId")
60
61             String serviceType = execution.getVariable("serviceType")
62             String serviceInstanceName = execution.getVariable("serviceInstanceName")
63             String serviceInstanceId = execution.getVariable("serviceInstanceId")
64
65             execution.setVariable("serviceType", serviceType)
66
67             String resourceTemplateUUIDs = ""
68             String scaleNsRequest = execution.getVariable("bpmnRequest")
69             JSONObject jsonObject = new JSONObject(scaleNsRequest).getJSONObject("service")
70             JSONArray jsonArray = jsonObject.getJSONArray("resources")
71
72             for (int i = 0; i < jsonArray.size(); i++) {
73                 JSONObject reqBodyJsonObj = jsonArray.getJSONObject(i)
74                 String nsInstanceId = reqBodyJsonObj.getString("resourceInstanceId")
75                 resourceTemplateUUIDs = resourceTemplateUUIDs + nsInstanceId + ":"
76             }
77
78             execution.setVariable("resourceTemplateUUIDs", resourceTemplateUUIDs)
79
80             if (serviceInstanceName == null) {
81                 execution.setVariable("serviceInstanceName", "")
82             }
83             if (isBlank(serviceInstanceId)) {
84                 msg = "Input serviceInstanceId is null"
85                 msoLogger.debug(msg)
86                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
87             }
88         } catch (BpmnError e) {
89             throw e;
90         } catch (Exception ex) {
91             msg = "Exception in preProcessRequest " + ex.getMessage()
92             msoLogger.debug(msg)
93             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
94         }
95         msoLogger.trace("Exit preProcessRequest ")
96     }
97
98
99     public void preInitResourcesOperStatus(DelegateExecution execution){
100         msoLogger.trace("STARTED preInitResourcesOperStatus Process ")
101         try{
102             String serviceId = execution.getVariable("serviceInstanceId")
103             String operationId = execution.getVariable("operationId")
104             String operationType = "SCALE"
105
106             // resourceTemplateUUIDs should be created ??
107             String resourceTemplateUUIDs = execution.getVariable("resourceTemplateUUIDs")
108             msoLogger.info("Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType)
109             serviceId = UriUtils.encode(serviceId,"UTF-8")
110             execution.setVariable("serviceInstanceId", serviceId)
111             execution.setVariable("operationId", operationId)
112             execution.setVariable("operationType", operationType)
113
114             execution.setVariable("URN_mso_openecomp_adapters_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
115
116             String payload =
117                     """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
118                         xmlns:ns="http://org.onap.so/requestsdb">
119                         <soapenv:Header/>
120                         <soapenv:Body>
121                             <ns:initResourceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
122                             <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
123                             <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
124                             <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
125                             <resourceTemplateUUIDs>${MsoUtils.xmlEscape(resourceTemplateUUIDs)}</resourceTemplateUUIDs>
126                         </ns:initResourceOperationStatus>
127                     </soapenv:Body>
128                 </soapenv:Envelope>"""
129
130             payload = utils.formatXml(payload)
131             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
132             msoLogger.info("Outgoing initResourceOperationStatus: \n" + payload)
133             msoLogger.debug("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
134
135         }catch(Exception e){
136             msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing preInitResourcesOperStatus.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
137             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
138         }
139         msoLogger.trace("COMPLETED preInitResourcesOperStatus Process ")
140     }
141
142 }