8d855e931110bfec635a6c91cb65408df72b5371
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / DecomposeService.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.bpmn.common.scripts
21
22 import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;
23
24 import static org.apache.commons.lang3.StringUtils.*;
25
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.runtime.Execution
30 import org.json.JSONObject;
31 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
34 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
35 import org.openecomp.mso.bpmn.core.json.JsonUtils
36
37 import com.att.ecomp.mso.bpmn.core.domain.*
38
39 import groovy.json.*
40
41 /**
42  * This groovy class supports the <class>DecomposeService.bpmn</class> process.
43  *
44  * @author
45  *
46  * Inputs:
47  * @param - msoRequestId
48  * @param - isDebugLogEnabled
49  * @param - serviceInstanceId
50  * @param - serviceModelInfo
51  * @param - requestParameters (may be null)
52  *
53  * Outputs:
54  * @param - rollbackData (null)
55  * @param - rolledBack (null)
56  * @param - WorkflowException
57  * @param - serviceDecomposition
58  *
59  */
60 public class DecomposeService extends AbstractServiceTaskProcessor {
61
62         String Prefix="DDS_"
63         ExceptionUtil exceptionUtil = new ExceptionUtil()
64         CatalogDbUtils catalogDbUtils = new CatalogDbUtils()
65         JsonUtils jsonUtils = new JsonUtils()
66
67         public void preProcessRequest (Execution execution) {
68                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
69                 String msg = ""
70                 utils.log("DEBUG"," ***** preProcessRequest of DecomposeService *****",  isDebugEnabled)
71                 setBasicDBAuthHeader(execution, isDebugEnabled)
72
73                 try {
74                         execution.setVariable("prefix", Prefix)
75                         // check for required input
76                         String requestId = execution.getVariable("msoRequestId")
77                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
78                         String serviceModelInfo = execution.getVariable("serviceModelInfo")
79                         execution.setVariable("DDS_serviceModelInvariantId", jsonUtils.getJsonValue(serviceModelInfo, "modelInvariantUuid"))
80                         execution.setVariable("DDS_serviceModelUuid", jsonUtils.getJsonValue(serviceModelInfo, "modelUuid"))
81                         execution.setVariable("DDS_modelVersion", jsonUtils.getJsonValue(serviceModelInfo, "modelVersion"))
82                 } catch (BpmnError e) {
83                         throw e;
84                 } catch (Exception ex){
85                         msg = "Exception in preProcessRequest " + ex.getMessage()
86                         utils.log("DEBUG", msg, isDebugEnabled)
87                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
88                 }
89                 utils.log("DEBUG"," ***** Exit preProcessRequest of DecomposeService *****",  isDebugEnabled)
90         }
91
92         public void queryCatalogDb (Execution execution) {
93                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
94                 String msg = ""
95                 utils.log("DEBUG"," ***** queryCatalogDB of DecomposeService *****",  isDebugEnabled)
96
97                 try {
98
99                         // check for input
100                         String serviceModelInvariantId = execution.getVariable("DDS_serviceModelInvariantId")
101                         String serviceModelUuid = execution.getVariable("DDS_serviceModelUuid")
102                         String modelVersion = execution.getVariable("DDS_modelVersion")
103
104                         utils.log("DEBUG", "serviceModelInvariantId: " + serviceModelInvariantId, isDebugEnabled)
105                         utils.log("DEBUG", "modelVersion: " + modelVersion, isDebugEnabled)
106
107                         JSONObject catalogDbResponse = null
108             if(serviceModelUuid != null && serviceModelUuid.length() > 0)
109                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelUuid(execution, serviceModelUuid, "v2")
110             else if (modelVersion != null && modelVersion.length() > 0)
111                                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(execution, serviceModelInvariantId, modelVersion, "v2")
112                         else
113                                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuid(execution, serviceModelInvariantId, "v2")
114                         String catalogDbResponseString = catalogDbResponse.toString()
115
116                         execution.setVariable("DDS_catalogDbResponse", catalogDbResponseString)
117                         utils.log("DEBUG", "catalog DB response string: "+ catalogDbResponseString, isDebugEnabled)
118
119                 } catch (BpmnError e) {
120                         throw e;
121                 } catch (Exception ex){
122                         msg = "Exception in queryCatalogDb " + ex.getMessage()
123                         utils.log("DEBUG", msg, isDebugEnabled)
124                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
125                 }
126                 utils.log("DEBUG"," ***** Exit queryCatalogDb of DecomposeService *****",  isDebugEnabled)
127         }
128
129
130
131         public void actuallyDecomposeService (Execution execution) {
132                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
133                 String msg = ""
134                 utils.log("DEBUG"," ***** actuallyDecomposeService of DecomposeService *****",  isDebugEnabled)
135
136                 try {
137
138                         // check for input
139                         String requestId = execution.getVariable("msoRequestId")
140                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
141                         String serviceModelInvariantId = execution.getVariable("DDS_serviceModelInvariantId")
142
143                         utils.log("DEBUG", "serviceModelInvariantId: " + serviceModelInvariantId, isDebugEnabled)
144
145                         utils.log("DEBUG", "getting service decomposition", isDebugEnabled)
146
147                         String catalogDbResponse = execution.getVariable("DDS_catalogDbResponse")
148                         ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogDbResponse, serviceInstanceId)
149
150                         execution.setVariable("serviceDecomposition", serviceDecomposition)
151                         execution.setVariable("serviceDecompositionString", serviceDecomposition.toJsonString())
152
153                         utils.log("DEBUG", "service decomposition: "+ serviceDecomposition.toJsonString(), isDebugEnabled)
154
155                 } catch (BpmnError e) {
156                         throw e;
157                 } catch (Exception ex){
158                         msg = "Exception in actuallyDecomposeService " + ex.getMessage()
159                         utils.log("DEBUG", msg, isDebugEnabled)
160                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
161                 }
162                 utils.log("DEBUG"," ***** Exit actuallyDecomposeService of DecomposeService *****",  isDebugEnabled)
163         }
164
165 }