Merge "Remove Swagger UI"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCompareModelVersions.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.so.bpmn.infrastructure.scripts;
21
22 import static org.apache.commons.lang3.StringUtils.*;
23
24 import org.onap.so.bpmn.core.domain.ServiceDecomposition
25 import org.onap.so.bpmn.core.domain.Resource
26 import org.onap.so.bpmn.core.json.JsonUtils
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29
30 import org.camunda.bpm.engine.delegate.DelegateExecution
31
32 /**
33  * This groovy class supports the <class>DoCompareModelVersions.bpmn</class> process.
34  *
35  * Inputs:
36  * @param - model-invariant-id-target
37  * @param - model-version-id-target
38  * @param - model-invariant-id-original
39  * @param - model-version-id-original
40  *
41  * Outputs:
42  * @param - addResourceList
43  * @param - delResourceList
44  *
45  */
46 public class DoCompareModelVersions extends AbstractServiceTaskProcessor {
47
48         String Prefix="DCMPMDV_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51
52         public void preProcessRequest (DelegateExecution execution) {
53                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
54                 String msg = ""
55                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
56
57                 try {
58                         execution.setVariable("prefix", Prefix)
59
60                         //Inputs
61                         String modelInvariantUuid_target = execution.getVariable("model-invariant-id-target")
62                         if (isBlank(modelInvariantUuid_target)) {
63                                 msg = "Input model-invariant-id-target is null"
64                                 utils.log("INFO", msg, isDebugEnabled)
65                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
66                         }
67
68             String modelUuid_target = execution.getVariable("model-version-id-target")
69             if (isBlank(modelUuid_target)) {
70                                 msg = "Input model-version-id-target is null"
71                                 utils.log("INFO", msg, isDebugEnabled)
72                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
73                         }
74
75             String modelInvariantUuid_original = execution.getVariable("model-invariant-id-original")
76             if (isBlank(modelInvariantUuid_original)) {
77                                 msg = "Input model-invariant-id-original is null"
78                                 utils.log("INFO", msg, isDebugEnabled)
79                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
80                         }
81
82             String modelUuid_original = execution.getVariable("model-version-id-original")
83             if (isBlank(modelUuid_original)) {
84                                 msg = "Input model-version-id-original is null"
85                                 utils.log("INFO", msg, isDebugEnabled)
86                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
87                         }
88
89                         // Target and original modelInvariantUuid must to be the same
90                         if(modelInvariantUuid_target != modelInvariantUuid_original){
91                                 msg = "Input model-invariant-id-target and model-invariant-id-original must to be the same"
92                                 utils.log("INFO", msg, isDebugEnabled)
93                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
94                         }
95
96                         // Target and original modelUuid must not to be the same
97                         if(modelUuid_target == modelUuid_original){
98                                 msg = "Input model-version-id-target and model-version-id-original must not to be the same"
99                                 utils.log("INFO", msg, isDebugEnabled)
100                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
101                         }
102
103                 } catch (Exception ex){
104                         msg = "Exception in preProcessRequest " + ex.getMessage()
105                         utils.log("INFO", msg, isDebugEnabled)
106                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
107                 }
108                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
109         }
110
111    public void prepareDecomposeService_Target(DelegateExecution execution) {
112         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
113
114         try {
115             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)
116             String modelInvariantUuid = execution.getVariable("model-invariant-id-target")
117             String modelUuid = execution.getVariable("model-version-id-target")
118             //here modelVersion is not set, we use modelUuid to decompose the service.
119             String serviceModelInfo = """{
120             "modelInvariantUuid":"${modelInvariantUuid}",
121             "modelUuid":"${modelUuid}",
122             "modelVersion":""
123              }"""
124
125             execution.setVariable("serviceModelInfo_Target", serviceModelInfo)
126
127             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)
128         } catch (Exception ex) {
129             // try error in method block
130             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Target() - " + ex.getMessage()
131             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
132         }
133      }
134
135     public void processDecomposition_Target(DelegateExecution execution) {
136         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
137
138         utils.log("DEBUG", " ***** Inside processDecomposition_Target() of update generic e2e service flow ***** ", isDebugEnabled)
139         try {
140             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
141             execution.setVariable("serviceDecomposition_Target", serviceDecomposition)
142         } catch (Exception ex) {
143             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method processDecomposition_Target() - " + ex.getMessage()
144             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
145             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
146         }
147     }
148
149    public void prepareDecomposeService_Original(DelegateExecution execution) {
150         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
151
152         try {
153             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)
154             String modelInvariantUuid = execution.getVariable("model-invariant-id-original")
155             String modelUuid = execution.getVariable("model-version-id-original")
156             //here modelVersion is not set, we use modelUuid to decompose the service.
157             String serviceModelInfo = """{
158             "modelInvariantUuid":"${modelInvariantUuid}",
159             "modelUuid":"${modelUuid}",
160             "modelVersion":""
161              }"""
162
163             execution.setVariable("serviceModelInfo_Original", serviceModelInfo)
164
165             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)
166         } catch (Exception ex) {
167             // try error in method block
168             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Original() - " + ex.getMessage()
169             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
170         }
171      }
172
173     public void processDecomposition_Original(DelegateExecution execution) {
174         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
175
176         utils.log("DEBUG", " ***** Inside processDecomposition_Original() of update generic e2e service flow ***** ", isDebugEnabled)
177         try {
178             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
179             execution.setVariable("serviceDecomposition_Original", serviceDecomposition)
180         } catch (Exception ex) {
181             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. processDecomposition_Original() - " + ex.getMessage()
182             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
183             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
184         }
185     }
186
187         public void doCompareModelVersions(DelegateExecution execution){
188             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
189         utils.log("INFO", "======== Start doCompareModelVersions Process ======== ", isDebugEnabled)
190
191         ServiceDecomposition serviceDecomposition_Target = execution.getVariable("serviceDecomposition_Target")
192         ServiceDecomposition serviceDecomposition_Original = execution.getVariable("serviceDecomposition_Original")
193
194         List<Resource> allSR_target = serviceDecomposition_Target.getServiceResources();
195         List<Resource> allSR_original = serviceDecomposition_Original.getServiceResources();
196
197         List<Resource> addResourceList = new ArrayList<String>()
198         List<Resource> delResourceList = new ArrayList<String>()
199
200         addResourceList.addAll(allSR_target)
201         delResourceList.addAll(allSR_original)
202
203         //Compare
204         for (Resource rc_t : allSR_target){
205             String muuid = rc_t.getModelInfo().getModelUuid()
206             String mIuuid = rc_t.getModelInfo().getModelInvariantUuid()
207             String mCuuid = rc_t.getModelInfo().getModelCustomizationUuid()
208             for (Resource rc_o : allSR_original){
209                 if(rc_o.getModelInfo().getModelUuid() == muuid
210                 && rc_o.getModelInfo().getModelInvariantUuid() == mIuuid
211                 && rc_o.getModelInfo().getModelCustomizationUuid() == mCuuid) {
212                     addResourceList.remove(rc_t);
213                     delResourceList.remove(rc_o);
214                 }
215             }
216         }
217
218         execution.setVariable("addResourceList", addResourceList)
219         execution.setVariable("delResourceList", delResourceList)
220         utils.log("INFO", "addResourceList: " + addResourceList, isDebugEnabled)
221         utils.log("INFO", "delResourceList: " + delResourceList, isDebugEnabled)
222
223         utils.log("INFO", "======== COMPLETED doCompareModelVersions Process ======== ", isDebugEnabled)
224         }
225
226 }