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