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