072c3810c244df5be456cfdb52c4eedace98dd1e
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2018 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.common.recipe.ResourceInput;\r
29 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder \r
30 import org.openecomp.mso.bpmn.core.WorkflowException \r
31 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
32 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder\r
33 import org.openecomp.mso.rest.APIResponse\r
34 \r
35 import java.util.UUID;\r
36 \r
37 import org.camunda.bpm.engine.delegate.BpmnError \r
38 import org.camunda.bpm.engine.delegate.DelegateExecution\r
39 import org.apache.commons.lang3.*\r
40 import org.apache.commons.codec.binary.Base64;\r
41 import org.springframework.web.util.UriUtils \r
42 import org.openecomp.mso.rest.RESTClient \r
43 import org.openecomp.mso.rest.RESTConfig\r
44 import org.openecomp.mso.rest.APIResponse;\r
45 \r
46 /**\r
47  * This groovy class supports the <class>CreateSDNCCNetworkResource.bpmn</class> process.\r
48  * flow for SDNC Network Resource Create\r
49  */\r
50 public class CreateSDNCCNetworkResource extends AbstractServiceTaskProcessor {\r
51 \r
52     String Prefix="CRESDNCRES_"\r
53             \r
54     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
55 \r
56     JsonUtils jsonUtil = new JsonUtils()\r
57     \r
58     public void preProcessRequest(DelegateExecution execution){\r
59         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
60         utils.log("INFO"," ***** Started preProcessRequest *****",  isDebugEnabled)\r
61         try {           \r
62             \r
63             //get bpmn inputs from resource request.\r
64             String requestId = execution.getVariable("requestId")\r
65             String requestAction = execution.getVariable("requestAction")\r
66             utils.log("INFO","The requestAction is: " + requestAction,  isDebugEnabled)\r
67             String recipeParamsFromRequest = execution.getVariable("recipeParams")\r
68             utils.log("INFO","The recipeParams is: " + recipeParams,  isDebugEnabled)\r
69             String resourceInput = execution.getVariable("requestInput")\r
70             utils.log("INFO","The resourceInput is: " + resourceInput,  isDebugEnabled)\r
71             //Get ResourceInput Object\r
72             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, resourceInputObj)\r
73             execution.setVariable(Prefix + "resourceInput", resourceInputObj)\r
74             \r
75             //Deal with recipeParams\r
76             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")\r
77             String resourceName = resourceInputObj.getResourceInstanceName()            \r
78             //For sdnc requestAction default is "createNetworkInstance"\r
79             String operationType = "Network"    \r
80             if(!StringUtils.isBlank(recipeParamsFromRequest)){\r
81                 //the operationType from worflow(first node) is second priority.\r
82                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")\r
83             }\r
84             if(!StringUtils.isBlank(recipeParamsFromWf)){\r
85                 //the operationType from worflow(first node) is highest priority.\r
86                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")\r
87             }\r
88             \r
89             \r
90             //For sdnc, generate svc_action and request_action\r
91             String sdnc_svcAction = "create"\r
92             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "overlay")){\r
93                 //This will be resolved in R3.\r
94                 sdnc_svcAction ="activate"\r
95                 operationType = "NCINetwork"        \r
96             }\r
97             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "underlay")){\r
98                 //This will be resolved in R3.\r
99                 operationType ="Network"\r
100             }        \r
101             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"                    \r
102             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)        \r
103             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)\r
104             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())\r
105             execution.setVariable("mso-request-id", requestId)\r
106             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())\r
107             //TODO Here build networkrequest\r
108             \r
109         } catch (BpmnError e) {\r
110             throw e;\r
111         } catch (Exception ex){\r
112             msg = "Exception in preProcessRequest " + ex.getMessage()\r
113             utils.log("DEBUG", msg, isDebugEnabled)\r
114             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
115         }\r
116     }\r
117     \r
118     \r
119     /**\r
120      * Pre Process the BPMN Flow Request\r
121      * Inclouds:\r
122      * generate the nsOperationKey\r
123      * generate the nsParameters\r
124      */\r
125     public void prepareSDNCRequest (DelegateExecution execution) {\r
126         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
127         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
128 \r
129         try {\r
130             // get variables\r
131             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")        \r
132             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")\r
133             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")\r
134             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")\r
135 \r
136             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")\r
137             \r
138             // 1. prepare assign topology via SDNC Adapter SUBFLOW call\r
139             String sndcTopologyCreateRequest = sdncAdapterUtils.sdncTopologyRequestRsrc(execution, createNetworkInput, serviceInstanceId, sdncCallback, sdnc_svcAction, sdnc_requestAction, null, null, null)\r
140 \r
141             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)\r
142             utils.logAudit(sndcTopologyCreateRequesAsString)\r
143             execution.setVariable(Prefix + "createSDNCRequest", sndcTopologyCreateRequesAsString)\r
144             utils.log("DEBUG", Prefix + "createSDNCRequest - " + "\n" +  sndcTopologyCreateRequesAsString, isDebugEnabled)\r
145 \r
146         } catch (Exception ex) {\r
147             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()\r
148             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
149             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
150 \r
151         }\r
152        utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
153         }\r
154 \r
155     public void postCreateSDNCCall(DelegateExecution execution){\r
156         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
157         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
158         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")\r
159         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")\r
160         \r
161         utils.log("INFO","response from sdnc, response code :" + responseCode + "  response object :" + responseObj,  isDebugEnabled)\r
162         utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
163     }\r
164 }\r