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