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