15b63fb5ab8ee6ee1c849317d65295aa751c51bb
[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         try {           
69             
70             //get bpmn inputs from resource request.
71             String requestId = execution.getVariable("mso-request-id")
72             String requestAction = execution.getVariable("requestAction")
73             msoLogger.info("The requestAction is: " + requestAction)
74             String recipeParamsFromRequest = execution.getVariable("recipeParams")
75             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
76             String resourceInput = execution.getVariable("resourceInput")
77             msoLogger.info("The resourceInput is: " + resourceInput)
78             //Get ResourceInput Object
79             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
80             execution.setVariable(Prefix + "resourceInput", resourceInputObj)
81                         String resourceInputPrameters = resourceInputObj.getResourceParameters()
82                         String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
83                         JSONObject inputParameters = new JSONObject(customizeResourceParam(inputParametersJson))
84                         execution.setVariable(Prefix + "resourceRequestInputs", inputParameters)
85             
86             //Deal with recipeParams
87             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
88             String resourceName = resourceInputObj.getResourceInstanceName()            
89             //For sdnc requestAction default is "createNetworkInstance"
90             String operationType = "Network"    
91             if(!StringUtils.isBlank(recipeParamsFromRequest)){
92                 //the operationType from worflow(first node) is second priority.
93                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")
94             }
95             if(!StringUtils.isBlank(recipeParamsFromWf)){
96                 //the operationType from worflow(first node) is highest priority.
97                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")
98             }
99
100             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
101             execution.setVariable("mso-request-id", requestId)
102             
103         } catch (BpmnError e) {
104             throw e;
105         } catch (Exception ex){
106             String msg = "Exception in preProcessRequest " + ex.getMessage()
107             msoLogger.debug(msg)
108             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
109         }
110     }
111         
112         String customizeResourceParam(String networkInputParametersJson) {
113         List<Map<String, Object>> paramList = new ArrayList();
114         JSONObject jsonObject = new JSONObject(networkInputParametersJson);
115         Iterator iterator = jsonObject.keys();
116         while (iterator.hasNext()) {
117             String key = iterator.next();
118             HashMap<String, String> hashMap = new HashMap();
119             hashMap.put("name", key);
120             hashMap.put("value", jsonObject.get(key))
121             paramList.add(hashMap)
122         }
123         Map<String, List<Map<String, Object>>> paramMap = new HashMap();
124         paramMap.put("param", paramList);
125
126         return  new JSONObject(paramMap).toString();
127     }
128         
129         public void checkDevType(DelegateExecution execution){
130                 msoLogger.info(" ***** Started checkDevType *****")
131                 try {
132                         
133                         JSONObject inputParameters = execution.getVariable(Prefix + "resourceRequestInputs")
134
135                         String devType = inputParameters.get("device_class")
136                         
137                         if(StringUtils.isBlank(devType)) {
138                                 devType = "OTHER"
139                         }
140                         
141                         execution.setVariable("device_class", devType)
142
143                 } catch (Exception ex){
144                         String msg = "Exception in checkDevType " + ex.getMessage()
145                         msoLogger.debug(msg)
146                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
147                 }
148         }
149         
150         public void getVNFTemplatefromSDC(DelegateExecution execution){
151                 msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
152                 try {
153                         // To do
154
155
156                 } catch (Exception ex){
157                         String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
158                         msoLogger.debug(msg)
159                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
160                 }
161         }
162         
163         public void postVNFInfoProcess(DelegateExecution execution){
164                 msoLogger.info(" ***** Started postVNFInfoProcess *****")
165                 try {
166                         // To do
167
168
169                 } catch (Exception ex){
170                         String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
171                         msoLogger.debug(msg)
172                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
173                 }
174         }
175     
176         public void sendSyncResponse (DelegateExecution execution) {
177                 msoLogger.debug(" *** sendSyncResponse *** ")
178
179                 try {
180                         String operationStatus = "finished"
181                         // RESTResponse for main flow
182                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
183                         msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
184                         sendWorkflowResponse(execution, 202, resourceOperationResp)
185                         execution.setVariable("sentSyncResponse", true)
186
187                 } catch (Exception ex) {
188                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
189                         msoLogger.debug(msg)
190                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
191                 }
192                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****")
193         }
194 }