Refactored SDNCAdapterUtils (groovy)
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.scripts;
23
24 import static org.apache.commons.lang3.StringUtils.*;
25
26 import org.onap.so.bpmn.core.domain.ServiceDecomposition
27 import org.onap.so.bpmn.core.domain.Resource
28 import org.onap.so.bpmn.core.json.JsonUtils
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.slf4j.Logger
32 import org.slf4j.LoggerFactory
33
34 import org.camunda.bpm.engine.delegate.DelegateExecution
35
36 /**
37  * This groovy class supports the <class>DoCompareModelVersions.bpmn</class> process.
38  *
39  * Inputs:
40  * @param - model-invariant-id-target
41  * @param - model-version-id-target
42  * @param - model-invariant-id-original
43  * @param - model-version-id-original
44  *
45  * Outputs:
46  * @param - addResourceList
47  * @param - delResourceList
48  *
49  */
50 public class DoCompareModelVersions extends AbstractServiceTaskProcessor {
51
52         String Prefix="DCMPMDV_"
53         ExceptionUtil exceptionUtil = new ExceptionUtil()
54         JsonUtils jsonUtil = new JsonUtils()
55     private static final Logger logger = LoggerFactory.getLogger( DoCompareModelVersions.class);
56
57         public void preProcessRequest (DelegateExecution execution) {
58                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
59                 String msg = ""
60                 logger.info(" ***** preProcessRequest *****")
61
62                 try {
63                         execution.setVariable("prefix", Prefix)
64
65                         //Inputs
66                         String modelInvariantUuid_target = execution.getVariable("model-invariant-id-target")
67                         if (isBlank(modelInvariantUuid_target)) {
68                                 msg = "Input model-invariant-id-target is null"
69                                 logger.info( msg)
70                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
71                         }
72
73             String modelUuid_target = execution.getVariable("model-version-id-target")
74             if (isBlank(modelUuid_target)) {
75                                 msg = "Input model-version-id-target is null"
76                                 logger.info( msg)
77                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
78                         }
79
80             String modelInvariantUuid_original = execution.getVariable("model-invariant-id-original")
81             if (isBlank(modelInvariantUuid_original)) {
82                                 msg = "Input model-invariant-id-original is null"
83                                 logger.info( msg)
84                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
85                         }
86
87             String modelUuid_original = execution.getVariable("model-version-id-original")
88             if (isBlank(modelUuid_original)) {
89                                 msg = "Input model-version-id-original is null"
90                                 logger.info( msg)
91                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
92                         }
93
94                         // Target and original modelInvariantUuid must to be the same
95                         if(modelInvariantUuid_target != modelInvariantUuid_original){
96                                 msg = "Input model-invariant-id-target and model-invariant-id-original must to be the same"
97                                 logger.info( msg)
98                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
99                         }
100
101                         // Target and original modelUuid must not to be the same
102                         if(modelUuid_target == modelUuid_original){
103                                 msg = "Input model-version-id-target and model-version-id-original must not to be the same"
104                                 logger.info( msg)
105                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
106                         }
107
108                 } catch (Exception ex){
109                         msg = "Exception in preProcessRequest " + ex.getMessage()
110                         logger.info( msg)
111                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
112                 }
113                 logger.info(" ***** Exit preProcessRequest *****")
114         }
115
116    public void prepareDecomposeService_Target(DelegateExecution execution) {
117         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
118
119         try {
120             logger.debug( " ***** Inside prepareDecomposeService_Target of update generic e2e service ***** ")
121             String modelInvariantUuid = execution.getVariable("model-invariant-id-target")
122             String modelUuid = execution.getVariable("model-version-id-target")
123             //here modelVersion is not set, we use modelUuid to decompose the service.
124             String serviceModelInfo = """{
125             "modelInvariantUuid":"${modelInvariantUuid}",
126             "modelUuid":"${modelUuid}",
127             "modelVersion":""
128              }"""
129
130             execution.setVariable("serviceModelInfo_Target", serviceModelInfo)
131
132             logger.debug( " ***** Completed prepareDecomposeService_Target of update generic e2e service ***** ")
133         } catch (Exception ex) {
134             // try error in method block
135             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Target() - " + ex.getMessage()
136             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
137         }
138      }
139
140     public void processDecomposition_Target(DelegateExecution execution) {
141         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
142
143         logger.debug( " ***** Inside processDecomposition_Target() of update generic e2e service flow ***** ")
144         try {
145             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
146             execution.setVariable("serviceDecomposition_Target", serviceDecomposition)
147         } catch (Exception ex) {
148             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method processDecomposition_Target() - " + ex.getMessage()
149             logger.debug( exceptionMessage)
150             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
151         }
152     }
153
154    public void prepareDecomposeService_Original(DelegateExecution execution) {
155         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
156
157         try {
158             logger.debug( " ***** Inside prepareDecomposeService_Original of update generic e2e service ***** ")
159             String modelInvariantUuid = execution.getVariable("model-invariant-id-original")
160             String modelUuid = execution.getVariable("model-version-id-original")
161             //here modelVersion is not set, we use modelUuid to decompose the service.
162             String serviceModelInfo = """{
163             "modelInvariantUuid":"${modelInvariantUuid}",
164             "modelUuid":"${modelUuid}",
165             "modelVersion":""
166              }"""
167
168             execution.setVariable("serviceModelInfo_Original", serviceModelInfo)
169
170             logger.debug( " ***** Completed prepareDecomposeService_Original of update generic e2e service ***** ")
171         } catch (Exception ex) {
172             // try error in method block
173             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Original() - " + ex.getMessage()
174             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
175         }
176      }
177
178     public void processDecomposition_Original(DelegateExecution execution) {
179         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
180
181         logger.debug( " ***** Inside processDecomposition_Original() of update generic e2e service flow ***** ")
182         try {
183             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
184             execution.setVariable("serviceDecomposition_Original", serviceDecomposition)
185         } catch (Exception ex) {
186             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. processDecomposition_Original() - " + ex.getMessage()
187             logger.debug( exceptionMessage)
188             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
189         }
190     }
191
192         public void doCompareModelVersions(DelegateExecution execution){
193             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
194         logger.info( "======== Start doCompareModelVersions Process ======== ")
195
196         ServiceDecomposition serviceDecomposition_Target = execution.getVariable("serviceDecomposition_Target")
197         ServiceDecomposition serviceDecomposition_Original = execution.getVariable("serviceDecomposition_Original")
198
199         List<Resource> allSR_target = serviceDecomposition_Target.getServiceResources();
200         List<Resource> allSR_original = serviceDecomposition_Original.getServiceResources();
201
202         List<Resource> addResourceList = new ArrayList<String>()
203         List<Resource> delResourceList = new ArrayList<String>()
204
205         addResourceList.addAll(allSR_target)
206         delResourceList.addAll(allSR_original)
207
208         //Compare
209         for (Resource rc_t : allSR_target){
210             String muuid = rc_t.getModelInfo().getModelUuid()
211             String mIuuid = rc_t.getModelInfo().getModelInvariantUuid()
212             String mCuuid = rc_t.getModelInfo().getModelCustomizationUuid()
213             for (Resource rc_o : allSR_original){
214                 if(rc_o.getModelInfo().getModelUuid() == muuid
215                 && rc_o.getModelInfo().getModelInvariantUuid() == mIuuid
216                 && rc_o.getModelInfo().getModelCustomizationUuid() == mCuuid) {
217                     addResourceList.remove(rc_t);
218                     delResourceList.remove(rc_o);
219                 }
220             }
221         }
222
223         execution.setVariable("addResourceList", addResourceList)
224         execution.setVariable("delResourceList", delResourceList)
225         logger.info( "addResourceList: " + addResourceList)
226         logger.info( "delResourceList: " + delResourceList)
227
228         logger.info( "======== COMPLETED doCompareModelVersions Process ======== ")
229         }
230
231 }