8354798345cd60e7eaf959d587a1376caa6c6d25
[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 vfcUrl = "/vfc/rest/v1/vfcadapter"\r
50     \r
51     String host = "http://mso.mso.testlab.openecomp.org:8080"\r
52     \r
53     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
54 \r
55     JsonUtils jsonUtil = new JsonUtils()\r
56 \r
57     /**\r
58      * Pre Process the BPMN Flow Request\r
59      * Inclouds:\r
60      * generate the nsOperationKey\r
61      * generate the nsParameters\r
62      */\r
63     public void preProcessRequest (Execution execution) {\r
64            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
65        String msg = ""\r
66        utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)\r
67        try {\r
68            //deal with nsName and Description\r
69            String nsServiceName = execution.getVariable("nsServiceName")\r
70            String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
71            utils.log("INFO", "nsServiceName:" + nsServiceName + " nsServiceDescription:" + nsServiceDescription, isDebugEnabled)\r
72            //deal with operation key\r
73            String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
74            utils.log("INFO", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)\r
75            String serviceType = execution.getVariable("serviceType")\r
76            utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
77            String serviceId = execution.getVariable("serviceId")\r
78            utils.log("INFO", "serviceId:" + serviceId, isDebugEnabled)\r
79            String operationId = execution.getVariable("operationId")\r
80            utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
81            String nodeTemplateUUID = execution.getVariable("resourceUUID")\r
82            utils.log("INFO", "nodeTemplateUUID:" + nodeTemplateUUID, isDebugEnabled)\r
83            /*\r
84             * segmentInformation needed as a object of segment\r
85             * {\r
86             *     "domain":"",\r
87             *     "nodeTemplateName":"",\r
88             *     "nodeType":"",\r
89             *     "nsParameters":{\r
90             *       //this is the nsParameters sent to VF-C\r
91             *     }\r
92             * }\r
93             */\r
94            String nsParameters = execution.getVariable("resourceParameters")\r
95            utils.log("INFO", "nsParameters:" + nsParameters, isDebugEnabled)\r
96            String nsOperationKey = """{\r
97                    "globalSubscriberId":"${globalSubscriberId}",\r
98                    "serviceType":"${serviceType}",\r
99                    "serviceId":"${serviceId}",\r
100                    "operationId":"${operationId}",\r
101                    "nodeTemplateUUID":"${nodeTemplateUUID}"\r
102                     }"""\r
103            execution.setVariable("nsOperationKey", nsOperationKey);\r
104            execution.setVariable("nsParameters", nsParameters)\r
105            \r
106 \r
107        } catch (BpmnError e) {\r
108            throw e;\r
109        } catch (Exception ex){\r
110            msg = "Exception in preProcessRequest " + ex.getMessage()\r
111            utils.log("INFO", msg, isDebugEnabled)\r
112            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
113        }\r
114        utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
115         }\r
116 \r
117     /**\r
118      * create NS task\r
119      */\r
120     public void createNetworkService(Execution execution) {\r
121         \r
122         String nsOperationKey = execution.getVariable("nsOperationKey");\r
123         String nsParameters = execution.getVariable("nsParameters");\r
124         String nsServiceName = execution.getVariable("nsServiceName")\r
125         String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
126         String reqBody ="""{\r
127                 "nsServiceName":"${nsServiceName}",\r
128                 "nsServiceDescription":"${nsServiceDescription}",\r
129                 "nsOperationKey":${nsOperationKey},\r
130                 "nsParameters":${nsParameters}\r
131                }"""\r
132         APIResponse apiResponse = postRequest(execution, host + vfcUrl + "/ns", reqBody)\r
133         String returnCode = apiResponse.getStatusCode()\r
134         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
135         String nsInstanceId = "";\r
136         if(returnCode== "200"){\r
137             nsInstanceId =  jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")\r
138         }\r
139         execution.setVariable("nsInstanceId", nsInstanceId)\r
140         \r
141     }\r
142 \r
143     /**\r
144      * instantiate NS task\r
145      */\r
146     public void instantiateNetworkService(Execution execution) {\r
147         String nsOperationKey = execution.getVariable("nsOperationKey");\r
148         String nsParameters = execution.getVariable("nsParameters");\r
149         String nsServiceName = execution.getVariable("nsServiceName")\r
150         String nsServiceDescription = execution.getVariable("nsServiceDescription")\r
151         String reqBody ="""{\r
152         "nsServiceName":"${nsServiceName}",\r
153         "nsServiceDescription":"${nsServiceDescription}",\r
154         "nsOperationKey":${nsOperationKey},\r
155         "nsParameters":${nsParameters}\r
156        }"""\r
157         String nsInstanceId = execution.getVariable("nsInstanceId")\r
158         String url = host + vfcUrl + "/ns/" +nsInstanceId + "/instantiate"\r
159         APIResponse apiResponse = postRequest(execution, url, reqBody)\r
160         String returnCode = apiResponse.getStatusCode()\r
161         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
162         String jobId = "";\r
163         if(returnCode== "200"){\r
164             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")\r
165         }\r
166         execution.setVariable("jobId", nsInstanceId)\r
167     }\r
168 \r
169     /**\r
170      * query NS task\r
171      */\r
172     public void queryNSProgress(Execution execution) {\r
173         String jobId = execution.getVariable("jobId")\r
174         String nsOperationKey = execution.getVariable("nsOperationKey");\r
175         String url = host + vfcUrl + "/jobs/" + jobId\r
176         APIResponse apiResponse = postRequest(execution, url, nsOperationKey)\r
177         String returnCode = apiResponse.getStatusCode()\r
178         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
179         String operationStatus = "error"\r
180         if(returnCode== "200"){\r
181             operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")\r
182         }\r
183         execution.setVariable("operationStatus", operationStatus)\r
184     }\r
185 \r
186     /**\r
187      * delay 5 sec \r
188      */\r
189     public void timeDelay(Execution execution) {\r
190         try {\r
191             Thread.sleep(5000);\r
192         } catch(InterruptedException e) {           \r
193             taskProcessor.utils.log("ERROR", "Time Delay exception" + e , isDebugEnabled)\r
194         }\r
195     }\r
196 \r
197     /**\r
198      * finish NS task\r
199      */\r
200     public void finishNSCreate(Execution execution) {\r
201         //no need to do anything util now\r
202     }\r
203     \r
204     /**\r
205      * post request\r
206      * url: the url of the request\r
207      * requestBody: the body of the request\r
208      */\r
209     private APIResponse postRequest(Execution execution, String url, String requestBody){\r
210         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
211         utils.log("INFO"," ***** Started Execute VFC adapter Post Process *****",  isDebugEnabled)\r
212         utils.log("INFO","url:"+url +"\nrequestBody:"+ requestBody,  isDebugEnabled)\r
213         APIResponse apiResponse = null\r
214         try{\r
215             RESTConfig config = new RESTConfig(url);\r
216             RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");\r
217             apiResponse = client.httpPost(requestBody)\r
218             utils.log("INFO","response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(),  isDebugEnabled)    \r
219             utils.log("INFO","======== Completed Execute VF-C adapter Post Process ======== ",  isDebugEnabled)\r
220         }catch(Exception e){\r
221             utils.log("ERROR","Exception occured while executing AAI Post Call. Exception is: \n" + e,  isDebugEnabled)\r
222             throw new BpmnError("MSOWorkflowException")\r
223         }        \r
224         return apiResponse\r
225     }\r
226 }\r