b346faf4c66ce99bc3d9b9e686c5fc6879726b2c
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. 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 \r
21 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
22 \r
23 import static org.apache.commons.lang3.StringUtils.*;\r
24 import groovy.xml.XmlUtil\r
25 import groovy.json.*\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor \r
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil \r
28 import org.openecomp.mso.bpmn.core.WorkflowException \r
29 import org.openecomp.mso.bpmn.core.json.JsonUtils \r
30 import org.openecomp.mso.rest.APIResponse\r
31 \r
32 import java.util.UUID;\r
33 \r
34 import org.camunda.bpm.engine.delegate.BpmnError \r
35 import org.camunda.bpm.engine.runtime.Execution\r
36 import org.apache.commons.lang3.*\r
37 import org.apache.commons.codec.binary.Base64;\r
38 import org.springframework.web.util.UriUtils \r
39 import org.openecomp.mso.rest.RESTClient \r
40 import org.openecomp.mso.rest.RESTConfig\r
41 import org.openecomp.mso.rest.APIResponse;\r
42 \r
43 /**\r
44  * This groovy class supports the <class>DoCreateVFCNetworkServiceInstance.bpmn</class> process.\r
45  * flow for VFC Network Service Create\r
46  */\r
47 public class DoCreateVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {\r
48 \r
49     String createUrl = "/vfc/vfcadapters/v1/ns"\r
50             \r
51     String instantiateUrl = "/vfcvfcadatpers/v1/ns/{nsInstanceId}/instantiate"\r
52     \r
53     String queryJobUrl = "/vfc/vfcadatpers/v1/jobs/{jobId}"\r
54     \r
55     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
56 \r
57     JsonUtils jsonUtil = new JsonUtils()\r
58 \r
59     /**\r
60      * Pre Process the BPMN Flow Request\r
61      * Inclouds:\r
62      * generate the nsOperationKey\r
63      * generate the nsParameters\r
64      */\r
65     public void preProcessRequest (Execution execution) {\r
66            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
67        String msg = ""\r
68        utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)\r
69        try {\r
70            //deal with nsName and Description\r
71            String nsServiceName = execution.getVariable("nsServiceName")\r
72            String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
73            utils.log("DEBUG", "nsServiceName:" + nsServiceName + " nsServiceDescription:" + nsServiceDescription, isDebugEnabled)\r
74            //deal with operation key\r
75            String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
76            utils.log("DEBUG", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)\r
77            String serviceType = execution.getVariable("serviceType")\r
78            utils.log("DEBUG", "serviceType:" + serviceType, isDebugEnabled)\r
79            String serviceId = execution.getVariable("serviceId")\r
80            utils.log("DEBUG", "serviceId:" + serviceId, isDebugEnabled)\r
81            String operationId = execution.getVariable("operationId")\r
82            utils.log("DEBUG", "serviceType:" + serviceType, isDebugEnabled)\r
83            String nodeTemplateUUID = execution.getVariable("nodeTemplateUUID")\r
84            utils.log("DEBUG", "nodeTemplateUUID:" + nodeTemplateUUID, isDebugEnabled)\r
85            /*\r
86             * segmentInformation needed as a object of segment\r
87             * {\r
88             *     "domain":"",\r
89             *     "nodeTemplateName":"",\r
90             *     "nodeType":"",\r
91             *     "nsParameters":{\r
92             *       //this is the nsParameters sent to VF-C\r
93             *     }\r
94             * }\r
95             */\r
96            String siRequest = execution.getVariable("segmentInformation")\r
97            utils.log("DEBUG", "Input Request:" + siRequest, isDebugEnabled)\r
98            String nsOperationKey = "{\"globalSubscriberId\":\"" + globalSubscriberId + "\",\"serviceType:\""\r
99                  + serviceType + "\",\"serviceId\":\"" + serviceId + "\",\"operationId\":\"" + operationId\r
100                  +"\",\"nodeTemplateUUID\":\"" + nodeTemplateUUID + "\"}";\r
101            execution.setVariable("nsOperationKey", nsOperationKey);\r
102            execution.setVariable("nsParameters", jsonUtil.getJsonValue(siRequest, "nsParameters"))\r
103            \r
104 \r
105        } catch (BpmnError e) {\r
106            throw e;\r
107        } catch (Exception ex){\r
108            msg = "Exception in preProcessRequest " + ex.getMessage()\r
109            utils.log("DEBUG", msg, isDebugEnabled)\r
110            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
111        }\r
112        utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
113         }\r
114 \r
115     /**\r
116      * create NS task\r
117      */\r
118     public void createNetworkService(Execution execution) {\r
119         \r
120         String nsOperationKey = excution.getVariable("nsOperationKey");\r
121         String nsParameters = excution.getVariable("nsParameters");\r
122         String nsServiceName = execution.getVariable("nsServiceName")\r
123         String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
124         String reqBody = "{\"nsServiceName\":\"" + nsServiceName + "\",\"nsServiceDescription\":\"" + nsServiceDescription\r
125               +"\",\"nsOperationKey\":" + nsOperationKey + ",\"nsParameters\":" + nsParameters\r
126         APIResponse apiResponse = postRequest(createUrl, reqBody)\r
127         String returnCode = apiResponse.getStatusCode()\r
128         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
129         String nsInstanceId = "";\r
130         if(returnCode== "200"){\r
131             nsInstanceId =  jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")\r
132         }\r
133         execution.setVariable("nsInstanceId", nsInstanceId)\r
134         \r
135     }\r
136 \r
137     /**\r
138      * instantiate NS task\r
139      */\r
140     public void instantiateNetworkService(Execution execution) {\r
141         String nsOperationKey = excution.getVariable("nsOperationKey");\r
142         String nsParameters = excution.getVariable("nsParameters");\r
143         String nsServiceName = execution.getVariable("nsServiceName")\r
144         String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
145         String reqBody = "{\"nsServiceName\":\"" + nsServiceName + "\",\"nsServiceDescription\":\"" + nsServiceDescription\r
146               +"\",\"nsOperationKey\":" + nsOperationKey + ",\"nsParameters\":" + nsParameters\r
147         String url = instantiateUrl.replaceAll("{nsInstanceId}", execution.getVariable("nsInstanceId")) \r
148         APIResponse apiResponse = postRequest(url, reqBody)\r
149         String returnCode = apiResponse.getStatusCode()\r
150         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
151         String jobId = "";\r
152         if(returnCode== "200"){\r
153             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")\r
154         }\r
155         execution.setVariable("jobId", nsInstanceId)\r
156     }\r
157 \r
158     /**\r
159      * query NS task\r
160      */\r
161     public void queryNSProgress(Execution execution) {\r
162         String jobId = execution.getVariable("jobId")\r
163         String nsOperationKey = excution.getVariable("nsOperationKey");\r
164         String url = queryJobUrl.replaceAll("{jobId}", execution.getVariable("jobId")) \r
165         APIResponse apiResponse = postRequest(url, nsOperationKey)\r
166         String returnCode = apiResponse.getStatusCode()\r
167         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
168         String operationStatus = "error"\r
169         if(returnCode== "200"){\r
170             operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")\r
171         }\r
172         exection.setVariable("operationStatus", operationStatus)\r
173     }\r
174 \r
175     /**\r
176      * delay 5 sec \r
177      */\r
178     public void timeDelay(Execution execution) {\r
179         try {\r
180             Thread.sleep(5000);\r
181         } catch(InterruptedException e) {           \r
182             taskProcessor.utils.log("ERROR", "Time Delay exception" + e , isDebugEnabled)\r
183         }\r
184     }\r
185 \r
186     /**\r
187      * finish NS task\r
188      */\r
189     public void finishNSCreate(Execution execution) {\r
190         //no need to do anything util now\r
191     }\r
192 \r
193     /**\r
194      * post request\r
195      * url: the url of the request\r
196      * requestBody: the body of the request\r
197      */\r
198     private APIResponse postRequest(String url, String requestBody){\r
199         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
200         taskProcessor.logDebug( " ======== Started Execute VFC adapter Post Process ======== ", isDebugEnabled)\r
201         taskProcessor.logDebug( "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)\r
202         APIResponse apiResponse = null\r
203         try{\r
204             RESTConfig config = new RESTConfig(url);\r
205             RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/json");\r
206             apiResponse = client.httpPost(requestBody)\r
207             taskProcessor.logDebug( "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled)\r
208             taskProcessor.logDebug( "======== Completed Execute VF-C adapter Post Process ======== ", isDebugEnabled)\r
209         }catch(Exception e){\r
210             taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
211             throw new BpmnError("MSOWorkflowException")\r
212         }        \r
213         return apiResponse\r
214     }\r
215 }\r