8fc9ce0883d9f6bb307873ad6c1aff581a411735
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import org.json.JSONObject
24 import org.json.XML;
25
26 import static org.apache.commons.lang3.StringUtils.*;
27 import groovy.xml.XmlUtil
28 import groovy.json.*
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.recipe.ResourceInput;
32 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
36 import org.onap.so.logger.MsoLogger
37 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
38
39 import java.util.UUID;
40
41 import org.camunda.bpm.engine.delegate.BpmnError
42 import org.camunda.bpm.engine.delegate.DelegateExecution
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils
46 import org.onap.so.bpmn.common.scripts.AaiUtil
47
48 /**
49  * This groovy class supports the <class>CreateDeviceResource.bpmn</class> process.
50  * flow for Device Resource Create
51  */
52 public class CreateDeviceResource extends AbstractServiceTaskProcessor {
53
54     String Prefix="CREDEVRES_"
55
56     ExceptionUtil exceptionUtil = new ExceptionUtil()
57
58     JsonUtils jsonUtil = new JsonUtils()
59
60     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateDeviceResource.class)
61
62     public void preProcessRequest(DelegateExecution execution){
63         msoLogger.info(" ***** Started preProcessRequest *****")
64         String msg = ""
65         try {
66
67             //get bpmn inputs from resource request.
68             String requestId = execution.getVariable("mso-request-id")
69             String requestAction = execution.getVariable("requestAction")
70             msoLogger.info("The requestAction is: " + requestAction)
71             String recipeParamsFromRequest = execution.getVariable("recipeParams")
72             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
73             String resourceInput = execution.getVariable("resourceInput")
74             msoLogger.info("The resourceInput is: " + resourceInput)
75             //Get ResourceInput Object
76             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
77             execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
78
79             String resourceInputPrameters = resourceInputObj.getResourceParameters()
80             String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
81             JSONObject inputParameters = new JSONObject(inputParametersJson)
82             execution.setVariable(Prefix + "ResourceRequestInputs", inputParameters)
83
84 //            String incomingRequest = resourceInputObj.getRequestsInputs()
85 //            String serviceParameters = JsonUtils.getJsonValue(incomingRequest, "service.parameters")
86 //            String requestInputs = JsonUtils.getJsonValue(serviceParameters, "requestInputs")
87 //            JSONObject serviceInputParameters = new JSONObject(requestInputs)
88 //            execution.setVariable(Prefix + "ServiceParameters", serviceInputParameters)
89
90             //Deal with recipeParams
91             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
92             String resourceName = resourceInputObj.getResourceInstanceName()
93             if (isBlank(resourceName)) {
94                 msg = "Input resourceName is null"
95                 msoLogger.error(msg)
96             }
97             execution.setVariable("resourceName", resourceName)
98             msoLogger.info("resourceName:" + resourceName)
99
100             String resourceModelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid()
101             if (isBlank(resourceModelInvariantUuid)) {
102                 msg = "Input resourceModelInvariantUuid is null"
103                 msoLogger.error(msg)
104             }
105             execution.setVariable(Prefix + "ResourceModelInvariantUuid", resourceModelInvariantUuid)
106             msoLogger.info("resourceModelInvariantUuid:" + resourceModelInvariantUuid)
107
108             String resourceModelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
109             if (isBlank(resourceModelUuid)) {
110                 msg = "Input resourceModelUuid is null"
111                 msoLogger.error(msg)
112             }
113             execution.setVariable(Prefix + "ResourceModelUuid", resourceModelUuid)
114             msoLogger.info("resourceModelUuid:" + resourceModelUuid)
115
116             String resourceModelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
117             if (isBlank(resourceModelCustomizationUuid)) {
118                 msg = "Input resourceModelCustomizationUuid is null"
119                 msoLogger.error(msg)
120             }
121             execution.setVariable(Prefix + "ResourceModelCustomizationUuid", resourceModelCustomizationUuid)
122             msoLogger.info("resourceModelCustomizationUuid:" + resourceModelCustomizationUuid)
123
124             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
125             execution.setVariable("mso-request-id", requestId)
126
127         } catch (BpmnError e) {
128             throw e;
129         } catch (Exception ex){
130             msg = "Exception in preProcessRequest " + ex.getMessage()
131             msoLogger.debug(msg)
132             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
133         }
134     }
135
136         public void checkDevType(DelegateExecution execution){
137                 msoLogger.info(" ***** Started checkDevType *****")
138                 try {
139
140                         JSONObject resourceInputParameters = execution.getVariable(Prefix + "ResourceRequestInputs")
141                         String devType = resourceInputParameters.get("device_class")
142
143                         if(StringUtils.isBlank(devType)) {
144                                 devType = "OTHER"
145                         }
146                         // support VNF as PNF, to modify
147                         else if(StringUtils.equalsIgnoreCase(devType, "VNF")) {
148                                 devType = "PNF"
149                         }
150
151                         execution.setVariable("device_class", devType)
152
153                 } catch (Exception ex){
154                         String msg = "Exception in checkDevType " + ex.getMessage()
155                         msoLogger.debug(msg)
156                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
157                 }
158         }
159
160         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
161                 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
162                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
163                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
164         }
165
166         public void prepareUpdateProgress(DelegateExecution execution) {
167                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
168                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
169                 String operType = resourceInputObj.getOperationType()
170                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
171                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
172                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
173                 String operationId = resourceInputObj.getOperationId()
174                 String progress = execution.getVariable("progress")
175                 String status = execution.getVariable("status")
176                 String statusDescription = execution.getVariable("statusDescription")
177
178                 String body = """
179                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
180                         xmlns:ns="http://org.openecomp.mso/requestsdb">
181                         <soapenv:Header/>
182                 <soapenv:Body>
183                     <ns:updateResourceOperationStatus>
184                                <operType>${operType}</operType>
185                                <operationId>${operationId}</operationId>
186                                <progress>${progress}</progress>
187                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
188                                <serviceId>${ServiceInstanceId}</serviceId>
189                                <status>${status}</status>
190                                <statusDescription>${statusDescription}</statusDescription>
191                     </ns:updateResourceOperationStatus>
192                 </soapenv:Body>
193                 </soapenv:Envelope>"""
194
195                 setProgressUpdateVariables(execution, body)
196                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
197         }
198
199         public void getVNFTemplatefromSDC(DelegateExecution execution){
200                 msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
201                 try {
202                         // To do
203
204
205                 } catch (Exception ex){
206                         String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
207                         msoLogger.debug(msg)
208                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
209                 }
210         }
211
212         public void postVNFInfoProcess(DelegateExecution execution){
213                 msoLogger.info(" ***** Started postVNFInfoProcess *****")
214                 try {
215                         // To do
216
217
218                 } catch (Exception ex){
219                         String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
220                         msoLogger.debug(msg)
221                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
222                 }
223         }
224
225         public void sendSyncResponse (DelegateExecution execution) {
226                 msoLogger.debug(" *** sendSyncResponse *** ")
227
228                 try {
229                         String operationStatus = "finished"
230                         // RESTResponse for main flow
231                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
232                         msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
233                         sendWorkflowResponse(execution, 202, resourceOperationResp)
234                         execution.setVariable("sentSyncResponse", true)
235
236                 } catch (Exception ex) {
237                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
238                         msoLogger.debug(msg)
239                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
240                 }
241                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****")
242         }
243 }