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