Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / 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
21 package org.onap.so.bpmn.common.scripts
22
23 import static org.apache.commons.lang3.StringUtils.*;
24
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.json.JSONObject;
29 import org.onap.so.bpmn.core.domain.ServiceDecomposition
30 import org.onap.so.bpmn.core.json.DecomposeJsonUtil;
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.logger.MsoLogger
33
34 /**
35  * This groovy class supports the <class>DecomposeService.bpmn</class> process.
36  *
37  * @author
38  *
39  * Inputs:
40  * @param - msoRequestId
41  * @param - isDebugLogEnabled
42  * @param - serviceInstanceId
43  * @param - serviceModelInfo
44  * @param - requestParameters (may be null)
45  *
46  * Outputs:
47  * @param - rollbackData (null)
48  * @param - rolledBack (null)
49  * @param - WorkflowException
50  * @param - serviceDecomposition
51  *
52  */
53 public class DecomposeService extends AbstractServiceTaskProcessor {
54         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DecomposeService.class);
55
56
57         String Prefix="DDS_"
58         ExceptionUtil exceptionUtil = new ExceptionUtil()
59         CatalogDbUtils catalogDbUtils = new CatalogDbUtils()
60         JsonUtils jsonUtils = new JsonUtils()
61
62         public void preProcessRequest (DelegateExecution execution) {
63                 String msg = ""
64                 msoLogger.trace("preProcessRequest of DecomposeService ")
65                 setBasicDBAuthHeader(execution, execution.getVariable('isDebugLogEnabled'))
66
67                 try {
68                         execution.setVariable("prefix", Prefix)
69                         // check for required input
70                         String requestId = execution.getVariable("msoRequestId")
71                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
72                         String serviceModelInfo = execution.getVariable("serviceModelInfo")
73                         String invariantId
74                         if(jsonUtils.jsonElementExist(serviceModelInfo, "modelInvariantUuid")){
75                                 invariantId = jsonUtils.getJsonValue(serviceModelInfo, "modelInvariantUuid")
76                         }else if(jsonUtils.jsonElementExist(serviceModelInfo, "modelInvariantId")){
77                                 invariantId = jsonUtils.getJsonValue(serviceModelInfo, "modelInvariantId")
78                         }
79                         execution.setVariable("DDS_serviceModelInvariantId", invariantId)
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                         msoLogger.debug(msg)
87                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
88                 }
89                 msoLogger.trace("Exit preProcessRequest of DecomposeService ")
90         }
91
92         public void queryCatalogDb (DelegateExecution execution) {
93                 String msg = ""
94                 msoLogger.trace("queryCatalogDB of DecomposeService ")
95
96                 try {
97
98                         // check for input
99                         String serviceModelInvariantId = execution.getVariable("DDS_serviceModelInvariantId")
100                         String serviceModelUuid = execution.getVariable("DDS_serviceModelUuid")
101                         String modelVersion = execution.getVariable("DDS_modelVersion")
102
103                         msoLogger.debug("serviceModelInvariantId: " + serviceModelInvariantId)
104                         msoLogger.debug("modelVersion: " + modelVersion)
105
106                         JSONObject catalogDbResponse = null
107             if(serviceModelUuid != null && serviceModelUuid.length() > 0)
108                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelUuid(execution, serviceModelUuid, "v2")
109             else if (modelVersion != null && modelVersion.length() > 0)
110                                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(execution, serviceModelInvariantId, modelVersion, "v2")
111                         else
112                                 catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuid(execution, serviceModelInvariantId, "v2")
113
114                         if (catalogDbResponse == null || catalogDbResponse.toString().equalsIgnoreCase("null")) {
115                                 msg = "No data found in Catalog DB"
116                                 msoLogger.debug(msg)
117                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
118                         }
119
120                         String catalogDbResponseString = catalogDbResponse.toString()
121
122                         execution.setVariable("DDS_catalogDbResponse", catalogDbResponseString)
123                         msoLogger.debug("catalog DB response string: "+ catalogDbResponseString)
124
125                 } catch (BpmnError e) {
126                         throw e;
127                 } catch (Exception ex){
128                         msg = "Exception in queryCatalogDb " + ex.getMessage()
129                         msoLogger.debug(msg)
130                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
131                 }
132                 msoLogger.trace("Exit queryCatalogDb of DecomposeService ")
133         }
134
135
136
137         public void actuallyDecomposeService (DelegateExecution execution) {
138                 String msg = ""
139                 msoLogger.trace("actuallyDecomposeService of DecomposeService ")
140
141                 try {
142
143                         // check for input
144                         String requestId = execution.getVariable("msoRequestId")
145                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
146                         String serviceModelInvariantId = execution.getVariable("DDS_serviceModelInvariantId")
147
148                         msoLogger.debug("serviceModelInvariantId: " + serviceModelInvariantId)
149
150                         msoLogger.debug("getting service decomposition")
151
152                         String catalogDbResponse = execution.getVariable("DDS_catalogDbResponse")
153                         ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogDbResponse, serviceInstanceId)
154
155                         execution.setVariable("serviceDecomposition", serviceDecomposition)
156                         execution.setVariable("serviceDecompositionString", serviceDecomposition.toJsonString())
157
158                         msoLogger.debug("service decomposition: "+ serviceDecomposition.toJsonString())
159
160                 } catch (BpmnError e) {
161                         throw e;
162                 } catch (Exception ex){
163                         msg = "Exception in actuallyDecomposeService " + ex.getMessage()
164                         msoLogger.debug(msg)
165                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
166                 }
167                 msoLogger.trace("Exit actuallyDecomposeService of DecomposeService ")
168         }
169
170 }