Move Homing Building Block into ONAP
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / Homing.groovy
1 /*\r
2  * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.\r
3  */\r
4 package org.openecomp.mso.bpmn.common.scripts\r
5 \r
6 import org.camunda.bpm.engine.delegate.BpmnError\r
7 import org.camunda.bpm.engine.runtime.Execution\r
8 \r
9 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
10 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
11 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
12 import org.openecomp.mso.bpmn.core.domain.InventoryType\r
13 import org.openecomp.mso.bpmn.core.domain.Resource\r
14 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition\r
15 import org.openecomp.mso.bpmn.core.domain.Subscriber\r
16 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
17 import org.openecomp.mso.rest.APIResponse\r
18 import org.openecomp.mso.rest.RESTClient\r
19 import org.openecomp.mso.rest.RESTConfig\r
20 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
21 \r
22 import org.json.JSONArray\r
23 import org.json.JSONObject\r
24 \r
25 import static org.openecomp.mso.bpmn.common.scripts.GenericUtils.*;\r
26 \r
27 /**\r
28  * This class is contains the scripts used\r
29  * by the Homing Subflow building block. The\r
30  * subflow attempts to home the provided\r
31  * resources by calling sniro.\r
32  *\r
33  * @author cb645j\r
34  *\r
35  */\r
36 class Homing extends AbstractServiceTaskProcessor{\r
37 \r
38         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
39         JsonUtils jsonUtil = new JsonUtils()\r
40         SNIROUtils sniroUtils = new SNIROUtils(this)\r
41 \r
42         /**\r
43          * This method validates the incoming variables.\r
44          * The method then prepares the sniro request\r
45          * and posts it to sniro's rest api.\r
46          *\r
47          * @param execution\r
48          *\r
49          * @author cb645j\r
50          */\r
51         public void callSniro(Execution execution){\r
52                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
53                 execution.setVariable("prefix","HOME_")\r
54                 utils.log("DEBUG", "*** Started Homing Call Sniro ***", isDebugEnabled)\r
55                 try{\r
56                         execution.setVariable("rollbackData", null)\r
57                         execution.setVariable("rolledBack", false)\r
58 \r
59                         String requestId = execution.getVariable("msoRequestId")\r
60                         utils.log("DEBUG", "Incoming Request Id is: "  + requestId, isDebugEnabled)\r
61                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
62                         utils.log("DEBUG", "Incoming Service Instance Id is: "  + serviceInstanceId, isDebugEnabled)\r
63                         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
64                         utils.log("DEBUG", "Incoming Service Decomposition is: "  + serviceDecomposition, isDebugEnabled)\r
65                         String subscriberInfo = execution.getVariable("subscriberInfo")\r
66                         utils.log("DEBUG", "Incoming Subscriber Information is: "  + subscriberInfo, isDebugEnabled)\r
67 \r
68                         if(isBlank(requestId) || isBlank(serviceInstanceId) || isBlank(serviceDecomposition.toString()) || isBlank(subscriberInfo)){\r
69                                 exceptionUtil.buildAndThrowWorkflowException(execution, 4000, "A required input variable is missing or null")\r
70                         }else{\r
71                                 String subId = jsonUtil.getJsonValue(subscriberInfo, "globalSubscriberId")\r
72                                 String subName = jsonUtil.getJsonValue(subscriberInfo, "subscriberName")\r
73                                 String subCommonSiteId = ""\r
74                                 if(jsonUtil.jsonElementExist(subscriberInfo, "subscriberCommonSiteId")){\r
75                                         subCommonSiteId = jsonUtil.getJsonValue(subscriberInfo, "subscriberCommonSiteId")\r
76                                 }\r
77                                 Subscriber subscriber = new Subscriber(subId, subName, subCommonSiteId)\r
78 \r
79                                 String cloudConfiguration = execution.getVariable("cloudConfiguration") // TODO Currently not being used\r
80                                 String homingParameters = execution.getVariable("homingParameters") // (aka. request parameters) Should be json format. TODO confirm its json format\r
81 \r
82                                 //Authentication\r
83                                 def authHeader = ""\r
84                                 String basicAuth = execution.getVariable("URN_mso_sniro_auth")\r
85                                 String msokey = execution.getVariable("URN_mso_msoKey")\r
86                                 String basicAuthValue = utils.encrypt(basicAuth, msokey)\r
87                                 if(basicAuthValue != null){\r
88                                         utils.log("DEBUG", "Obtained BasicAuth username and password for SNIRO Adapter: " + basicAuthValue, isDebugEnabled)\r
89                                         try {\r
90                                                 authHeader = utils.getBasicAuth(basicAuthValue, msokey)\r
91                                                 execution.setVariable("BasicAuthHeaderValue",authHeader)\r
92                                         } catch (Exception ex) {\r
93                                                 utils.log("DEBUG", "Unable to encode username and password string: " + ex, isDebugEnabled)\r
94                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - Unable to encode username and password string")\r
95                                         }\r
96                                 }else{\r
97                                         utils.log("DEBUG", "Unable to obtain BasicAuth - BasicAuth value null" , isDebugEnabled)\r
98                                         exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth value null")\r
99                                 }\r
100 \r
101                                 //Prepare Callback\r
102                                 String timeout = execution.getVariable("timeout")\r
103                                 if(isBlank(timeout)){\r
104                                         timeout = execution.getVariable("URN_mso_sniro_timeout");\r
105                                         if(isBlank(timeout)) {\r
106                                                 timeout = "PT30M";\r
107                                         }\r
108                                 }\r
109                                 utils.log("DEBUG", "Async Callback Timeout will be: " + timeout, isDebugEnabled)\r
110 \r
111                                 execution.setVariable("timeout", timeout);\r
112                                 execution.setVariable("correlator", requestId);\r
113                                 execution.setVariable("messageType", "SNIROResponse");\r
114 \r
115                                 //Build Request & Call Sniro\r
116                                 String sniroRequest = sniroUtils.buildRequest(execution, requestId, serviceDecomposition, subscriber, homingParameters)\r
117                                 execution.setVariable("sniroRequest", sniroRequest)\r
118                                 utils.log("DEBUG", "SNIRO Request is: " + sniroRequest, isDebugEnabled)\r
119 \r
120                                 String endpoint = execution.getVariable("URN_mso_service_agnostic_sniro_endpoint")\r
121                                 String host = execution.getVariable("URN_mso_service_agnostic_sniro_host")\r
122                                 String url = host + endpoint\r
123                                 utils.log("DEBUG", "Posting to Sniro Url: " + url, isDebugEnabled)\r
124 \r
125                                 logDebug( "URL to be used is: " + url, isDebugEnabled)\r
126                                 \r
127                                 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
128                                 \r
129                                 RESTConfig config = new RESTConfig(url);\r
130                                 RESTClient client = new RESTClient(config).addAuthorizationHeader(authHeader).addHeader("Content-Type", "application/json")\r
131                                 if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
132                                         client.addAuthorizationHeader(basicAuthCred)\r
133                                 }\r
134                                 APIResponse response = client.httpPost(sniroRequest)\r
135                                 \r
136                                 int responseCode = response.getStatusCode()\r
137                                 execution.setVariable("syncResponseCode", responseCode);\r
138                                 logDebug("SNIRO sync response code is: " + responseCode, isDebugEnabled)\r
139                                 String syncResponse = response.getResponseBodyAsString()\r
140                                 execution.setVariable("syncResponse", syncResponse);\r
141                                 logDebug("SNIRO sync response is: " + syncResponse, isDebugEnabled)\r
142 \r
143                                 utils.log("DEBUG", "*** Completed Homing Call Sniro ***", isDebugEnabled)\r
144                         }\r
145                 }catch(BpmnError b){\r
146                         throw b\r
147                 }catch(Exception e){\r
148                         utils.log("DEBUG", "Error encountered within Homing CallSniro method: " + e, isDebugEnabled)\r
149                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in Homing CallSniro: " + e.getMessage())\r
150                 }\r
151         }\r
152         \r
153         /**\r
154          * This method processes the callback response\r
155          * and the contained homing solution. It sets\r
156          * homing solution assignment and license\r
157          * information to the corresponding resources\r
158          *\r
159          * @param execution\r
160          *\r
161          * @author cb645j\r
162          */\r
163         public void processHomingSolution(Execution execution){\r
164                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
165                 utils.log("DEBUG", "*** Started Homing Process Homing Solution ***", isDebugEnabled)\r
166                 try{\r
167                         String response = execution.getVariable("asyncCallbackResponse")\r
168                         utils.log("DEBUG", "Sniro Async Callback Response is: " + response, isDebugEnabled)\r
169                         utils.logAudit("Sniro Async Callback Response is: " + response)\r
170 \r
171                         sniroUtils.validateCallbackResponse(execution, response)\r
172                         String placements = jsonUtil.getJsonValue(response, "solutionInfo.placement")\r
173 \r
174                         ServiceDecomposition decomposition = execution.getVariable("serviceDecomposition")\r
175                         utils.log("DEBUG", "Service Decomposition: " + decomposition, isDebugEnabled)\r
176 \r
177                         List<Resource> resourceList = decomposition.getServiceResources()\r
178                         JSONArray arr = new JSONArray(placements)\r
179                         for(int i = 0; i < arr.length(); i++){\r
180                                 JSONObject placement = arr.getJSONObject(i)\r
181                                 String jsonServiceResourceId = placement.getString("serviceResourceId")\r
182                                 for(Resource resource:resourceList){\r
183                                         String serviceResourceId = resource.getResourceId()\r
184                                         if(serviceResourceId.equalsIgnoreCase(jsonServiceResourceId)){\r
185                                                 //match\r
186                                                 String inventoryType = placement.getString("inventoryType")\r
187                                                 resource.getHomingSolution().setInventoryType(InventoryType.valueOf(inventoryType))\r
188                                                 resource.getHomingSolution().setCloudRegionId(placement.getString("cloudRegionId"))\r
189                                                 JSONArray assignmentArr = placement.getJSONArray("assignmentInfo")\r
190                                                 Map<String, String> assignmentMap = jsonUtil.entryArrayToMap(execution, assignmentArr.toString(), "variableName", "variableValue")\r
191                                                 resource.getHomingSolution().setCloudOwner(assignmentMap.get("cloudOwner"))\r
192                                                 resource.getHomingSolution().setAicClli(assignmentMap.get("aicClli"))\r
193                                                 resource.getHomingSolution().setAicVersion(assignmentMap.get("aicVersion"))\r
194                                                 if(inventoryType.equalsIgnoreCase("service")){\r
195                                                         resource.getHomingSolution().setVnfHostname(assignmentMap.get("vnfHostName"));\r
196                                                         resource.getHomingSolution().setServiceInstanceId(placement.getString("serviceInstanceId"));\r
197                                                 }\r
198                                         }\r
199                                 }\r
200                         }\r
201                         if(JsonUtils.jsonElementExist(response, "solutionInfo.licenseInfo")){\r
202                                 String licenseInfo = jsonUtil.getJsonValue(response, "solutionInfo.licenseInfo")\r
203                                 JSONArray licenseArr = new JSONArray(licenseInfo)\r
204                                 for(int l = 0; l < licenseArr.length(); l++){\r
205                                         JSONObject license = licenseArr.getJSONObject(l)\r
206                                         String jsonServiceResourceId = license.getString("serviceResourceId")\r
207                                         for(Resource resource:resourceList){\r
208                                                 String serviceResourceId = resource.getResourceId()\r
209                                                 if(serviceResourceId.equalsIgnoreCase(jsonServiceResourceId)){\r
210                                                         //match\r
211                                                         String jsonEntitlementPoolList = jsonUtil.getJsonValue(license.toString(), "entitlementPoolList")\r
212                                                         List<String> entitlementPoolList = jsonUtil.StringArrayToList(execution, jsonEntitlementPoolList)\r
213                                                         resource.getHomingSolution().setEntitlementPoolList(entitlementPoolList)\r
214 \r
215                                                         String jsonLicenseKeyGroupList = jsonUtil.getJsonValue(license.toString(), "licenseKeyGroupList")\r
216                                                         List<String> licenseKeyGroupList = jsonUtil.StringArrayToList(execution, jsonLicenseKeyGroupList)\r
217                                                         resource.getHomingSolution().setLicenseKeyGroupList(licenseKeyGroupList)\r
218                                                 }\r
219                                         }\r
220                                 }\r
221                         }\r
222                         execution.setVariable("serviceDecomposition", decomposition)\r
223                         execution.setVariable("homingSolution", placements) //TODO - can be removed as output variable\r
224 \r
225                         utils.log("DEBUG", "*** Completed Homing Process Homing Solution ***", isDebugEnabled)\r
226                 }catch(BpmnError b){\r
227                         throw b\r
228                 }catch(Exception e){\r
229                         utils.log("DEBUG", "Error encountered within Homing ProcessHomingSolution method: " + e, isDebugEnabled)\r
230                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in Homing ProcessHomingSolution")\r
231                 }\r
232         }\r
233 \r
234         /**\r
235          * This method logs the start of DHVCreateService\r
236          * to make debugging easier.\r
237          *\r
238          * @param - execution\r
239          * @author cb645j\r
240          */\r
241         public String logStart(Execution execution){\r
242                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
243                 String requestId = execution.getVariable("testReqId")\r
244                 if(isBlank(requestId)){\r
245                         requestId = execution.getVariable("msoRequestId")\r
246                 }\r
247                 execution.setVariable("DHVCS_requestId", requestId)\r
248                 utils.log("DEBUG", "***** STARTED Homing Subflow for request: "  + requestId + " *****", "true")\r
249                 utils.log("DEBUG", "****** Homing Subflow Global Debug Enabled: " + isDebugEnabled  + " *****", "true")\r
250                 utils.logAudit("***** STARTED Homing Subflow for request: "  + requestId + " *****")\r
251         }\r
252 \r
253 \r
254         /**\r
255          * Auto-generated method stub\r
256          */\r
257         public void preProcessRequest(Execution execution){}\r
258 \r
259 }\r