Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / 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 import groovy.xml.XmlUtil
24 import groovy.json.*
25
26 import org.onap.so.bpmn.core.domain.ServiceDecomposition
27 import org.onap.so.bpmn.core.domain.ServiceInstance
28 import org.onap.so.bpmn.core.domain.ModelInfo
29 import org.onap.so.bpmn.core.domain.Resource
30 import org.onap.so.bpmn.core.domain.AllottedResource
31 import org.onap.so.bpmn.core.domain.NetworkResource
32 import org.onap.so.bpmn.core.domain.VnfResource
33 import org.onap.so.bpmn.common.recipe.ResourceInput
34 import org.onap.so.bpmn.common.recipe.BpmnRestClient
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.bpmn.common.scripts.AaiUtil
37 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
38 import org.onap.so.bpmn.common.scripts.ExceptionUtil
39 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
40 import org.onap.so.bpmn.common.scripts.CatalogDbUtils;
41 import org.onap.so.bpmn.core.RollbackData
42 import org.onap.so.bpmn.core.WorkflowException
43 import org.onap.so.rest.APIResponse;
44 import org.onap.so.rest.RESTClient
45 import org.onap.so.rest.RESTConfig
46
47 import java.util.List;
48 import java.util.UUID;
49
50 import org.camunda.bpm.engine.delegate.BpmnError
51 import org.camunda.bpm.engine.runtime.Execution
52 import org.camunda.bpm.engine.delegate.DelegateExecution
53 import org.json.JSONObject;
54 import org.json.JSONArray;
55 import org.apache.commons.lang3.*
56 import org.apache.commons.codec.binary.Base64;
57 import org.springframework.web.util.UriUtils;
58 import org.onap.so.logger.MessageEnum
59 import org.onap.so.logger.MsoLogger
60
61
62
63 /**
64  * This groovy class supports the <class>DoCompareModelVersions.bpmn</class> process.
65  *
66  * Inputs:
67  * @param - model-invariant-id-target
68  * @param - model-version-id-target
69  * @param - model-invariant-id-original
70  * @param - model-version-id-original
71  *
72  * Outputs:
73  * @param - addResourceList
74  * @param - delResourceList
75  *
76  */
77 public class DoCompareModelVersions extends AbstractServiceTaskProcessor {
78
79         String Prefix="DCMPMDV_"
80         ExceptionUtil exceptionUtil = new ExceptionUtil()
81         JsonUtils jsonUtil = new JsonUtils()
82         CatalogDbUtils cutils = new CatalogDbUtils()
83
84         public void preProcessRequest (DelegateExecution execution) {
85                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
86                 String msg = ""
87                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
88
89                 try {
90                         execution.setVariable("prefix", Prefix)
91                         
92                         //Inputs
93                         String modelInvariantUuid_target = execution.getVariable("model-invariant-id-target")
94                         if (isBlank(modelInvariantUuid_target)) {
95                                 msg = "Input model-invariant-id-target is null"
96                                 utils.log("INFO", msg, isDebugEnabled)
97                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
98                         }                       
99                         
100             String modelUuid_target = execution.getVariable("model-version-id-target")
101             if (isBlank(modelUuid_target)) {
102                                 msg = "Input model-version-id-target is null"
103                                 utils.log("INFO", msg, isDebugEnabled)
104                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105                         }
106             
107             String modelInvariantUuid_original = execution.getVariable("model-invariant-id-original")
108             if (isBlank(modelInvariantUuid_original)) {
109                                 msg = "Input model-invariant-id-original is null"
110                                 utils.log("INFO", msg, isDebugEnabled)
111                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
112                         }
113             
114             String modelUuid_original = execution.getVariable("model-version-id-original")
115             if (isBlank(modelUuid_original)) {
116                                 msg = "Input model-version-id-original is null"
117                                 utils.log("INFO", msg, isDebugEnabled)
118                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
119                         }  
120                         
121                         // Target and original modelInvariantUuid must to be the same
122                         if(modelInvariantUuid_target != modelInvariantUuid_original){
123                                 msg = "Input model-invariant-id-target and model-invariant-id-original must to be the same"
124                                 utils.log("INFO", msg, isDebugEnabled)
125                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)      
126                         }
127                         
128                         // Target and original modelUuid must not to be the same
129                         if(modelUuid_target == modelUuid_original){
130                                 msg = "Input model-version-id-target and model-version-id-original must not to be the same"
131                                 utils.log("INFO", msg, isDebugEnabled)
132                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)      
133                         }
134
135                 } catch (Exception ex){
136                         msg = "Exception in preProcessRequest " + ex.getMessage()
137                         utils.log("INFO", msg, isDebugEnabled)
138                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139                 }
140                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
141         }
142         
143    public void prepareDecomposeService_Target(DelegateExecution execution) {
144         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
145
146         try {
147             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)
148             String modelInvariantUuid = execution.getVariable("model-invariant-id-target")
149             String modelUuid = execution.getVariable("model-version-id-target")
150             //here modelVersion is not set, we use modelUuid to decompose the service.
151             String serviceModelInfo = """{
152             "modelInvariantUuid":"${modelInvariantUuid}",
153             "modelUuid":"${modelUuid}",
154             "modelVersion":""
155              }"""
156             
157             execution.setVariable("serviceModelInfo_Target", serviceModelInfo)
158
159             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)
160         } catch (Exception ex) {
161             // try error in method block
162             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Target() - " + ex.getMessage()
163             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
164         }
165      }
166
167     public void processDecomposition_Target(DelegateExecution execution) {
168         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
169     
170         utils.log("DEBUG", " ***** Inside processDecomposition_Target() of update generic e2e service flow ***** ", isDebugEnabled)    
171         try {
172             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
173             execution.setVariable("serviceDecomposition_Target", serviceDecomposition)
174         } catch (Exception ex) {
175             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method processDecomposition_Target() - " + ex.getMessage()
176             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
177             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
178         }
179     }
180  
181    public void prepareDecomposeService_Original(DelegateExecution execution) {
182         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
183
184         try {
185             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)
186             String modelInvariantUuid = execution.getVariable("model-invariant-id-original")
187             String modelUuid = execution.getVariable("model-version-id-original")
188             //here modelVersion is not set, we use modelUuid to decompose the service.
189             String serviceModelInfo = """{
190             "modelInvariantUuid":"${modelInvariantUuid}",
191             "modelUuid":"${modelUuid}",
192             "modelVersion":""
193              }"""
194             
195             execution.setVariable("serviceModelInfo_Original", serviceModelInfo)
196
197             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)
198         } catch (Exception ex) {
199             // try error in method block
200             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Original() - " + ex.getMessage()
201             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
202         }
203      }
204
205     public void processDecomposition_Original(DelegateExecution execution) {
206         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
207     
208         utils.log("DEBUG", " ***** Inside processDecomposition_Original() of update generic e2e service flow ***** ", isDebugEnabled)    
209         try {
210             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
211             execution.setVariable("serviceDecomposition_Original", serviceDecomposition)
212         } catch (Exception ex) {
213             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. processDecomposition_Original() - " + ex.getMessage()
214             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
215             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
216         }
217     }   
218
219         public void doCompareModelVersions(DelegateExecution execution){
220             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
221         utils.log("INFO", "======== Start doCompareModelVersions Process ======== ", isDebugEnabled)          
222
223         ServiceDecomposition serviceDecomposition_Target = execution.getVariable("serviceDecomposition_Target")
224         ServiceDecomposition serviceDecomposition_Original = execution.getVariable("serviceDecomposition_Original")
225         
226         List<Resource> allSR_target = serviceDecomposition_Target.getServiceResources();
227         List<Resource> allSR_original = serviceDecomposition_Original.getServiceResources();
228         
229         List<Resource> addResourceList = new ArrayList<String>()
230         List<Resource> delResourceList = new ArrayList<String>()
231         
232         addResourceList.addAll(allSR_target)
233         delResourceList.addAll(allSR_original)
234         
235         //Compare
236         for (Resource rc_t : allSR_target){
237             String muuid = rc_t.getModelInfo().getModelUuid()
238             String mIuuid = rc_t.getModelInfo().getModelInvariantUuid()
239             String mCuuid = rc_t.getModelInfo().getModelCustomizationUuid()
240             for (Resource rc_o : allSR_original){
241                 if(rc_o.getModelInfo().getModelUuid() == muuid 
242                 && rc_o.getModelInfo().getModelInvariantUuid() == mIuuid 
243                 && rc_o.getModelInfo().getModelCustomizationUuid() == mCuuid) {
244                     addResourceList.remove(rc_t);
245                     delResourceList.remove(rc_o);
246                 }
247             }         
248         }
249
250         execution.setVariable("addResourceList", addResourceList)
251         execution.setVariable("delResourceList", delResourceList)
252         utils.log("INFO", "addResourceList: " + addResourceList, isDebugEnabled)  
253         utils.log("INFO", "delResourceList: " + delResourceList, isDebugEnabled)
254         
255         utils.log("INFO", "======== COMPLETED doCompareModelVersions Process ======== ", isDebugEnabled)  
256         }
257
258 }