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