Merge "Fix get resource input json string issue"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCompareModelVersions.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved. \r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
21 \r
22 import static org.apache.commons.lang3.StringUtils.*;\r
23 import groovy.xml.XmlUtil\r
24 import groovy.json.*\r
25 \r
26 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition\r
27 import org.openecomp.mso.bpmn.core.domain.ServiceInstance\r
28 import org.openecomp.mso.bpmn.core.domain.ModelInfo\r
29 import org.openecomp.mso.bpmn.core.domain.Resource\r
30 import org.openecomp.mso.bpmn.core.domain.AllottedResource\r
31 import org.openecomp.mso.bpmn.core.domain.NetworkResource\r
32 import org.openecomp.mso.bpmn.core.domain.VnfResource\r
33 import org.openecomp.mso.bpmn.common.recipe.ResourceInput\r
34 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient\r
35 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
36 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
37 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
38 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
39 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
40 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils;\r
41 import org.openecomp.mso.bpmn.core.RollbackData\r
42 import org.openecomp.mso.bpmn.core.WorkflowException\r
43 import org.openecomp.mso.rest.APIResponse;\r
44 import org.openecomp.mso.rest.RESTClient\r
45 import org.openecomp.mso.rest.RESTConfig\r
46 \r
47 import java.util.List;\r
48 import java.util.UUID;\r
49 \r
50 import javax.mail.Quota.Resource;\r
51 \r
52 import org.camunda.bpm.engine.delegate.BpmnError\r
53 import org.camunda.bpm.engine.runtime.Execution\r
54 import org.camunda.bpm.engine.delegate.DelegateExecution\r
55 import org.json.JSONObject;\r
56 import org.json.JSONArray;\r
57 import org.apache.commons.lang3.*\r
58 import org.apache.commons.codec.binary.Base64;\r
59 import org.springframework.web.util.UriUtils;\r
60 \r
61 /**\r
62  * This groovy class supports the <class>DoCompareModelVersions.bpmn</class> process.\r
63  *\r
64  * Inputs:\r
65  * @param - model-invariant-id-target\r
66  * @param - model-version-id-target\r
67  * @param - model-invariant-id-original\r
68  * @param - model-version-id-original\r
69  *\r
70  * Outputs:\r
71  * @param - addResourceList\r
72  * @param - delResourceList\r
73  *\r
74  */\r
75 public class DoCompareModelVersions extends AbstractServiceTaskProcessor {\r
76 \r
77         String Prefix="DCMPMDV_"\r
78         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
79         JsonUtils jsonUtil = new JsonUtils()\r
80         CatalogDbUtils cutils = new CatalogDbUtils()\r
81 \r
82         public void preProcessRequest (DelegateExecution execution) {\r
83                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
84                 String msg = ""\r
85                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)\r
86 \r
87                 try {\r
88                         execution.setVariable("prefix", Prefix)\r
89                         \r
90                         //Inputs\r
91                         String modelInvariantUuid_target = execution.getVariable("model-invariant-id-target")\r
92                         if (isBlank(modelInvariantUuid_target)) {\r
93                                 msg = "Input model-invariant-id-target is null"\r
94                                 utils.log("INFO", msg, isDebugEnabled)\r
95                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
96                         }                       \r
97                         \r
98             String modelUuid_target = execution.getVariable("model-version-id-target")\r
99             if (isBlank(modelUuid_target)) {\r
100                                 msg = "Input model-version-id-target is null"\r
101                                 utils.log("INFO", msg, isDebugEnabled)\r
102                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
103                         }\r
104             \r
105             String modelInvariantUuid_original = execution.getVariable("model-invariant-id-original")\r
106             if (isBlank(modelInvariantUuid_original)) {\r
107                                 msg = "Input model-invariant-id-original is null"\r
108                                 utils.log("INFO", msg, isDebugEnabled)\r
109                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
110                         }\r
111             \r
112             String modelUuid_original = execution.getVariable("model-version-id-original")\r
113             if (isBlank(modelUuid_original)) {\r
114                                 msg = "Input model-version-id-original is null"\r
115                                 utils.log("INFO", msg, isDebugEnabled)\r
116                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
117                         }  \r
118                         \r
119                         // Target and original modelInvariantUuid must to be the same\r
120                         if(modelInvariantUuid_target != modelInvariantUuid_original){\r
121                                 msg = "Input model-invariant-id-target and model-invariant-id-original must to be the same"\r
122                                 utils.log("INFO", msg, isDebugEnabled)\r
123                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)      \r
124                         }\r
125                         \r
126                         // Target and original modelUuid must not to be the same\r
127                         if(modelUuid_target == modelUuid_original){\r
128                                 msg = "Input model-version-id-target and model-version-id-original must not to be the same"\r
129                                 utils.log("INFO", msg, isDebugEnabled)\r
130                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)      \r
131                         }\r
132 \r
133                 } catch (Exception ex){\r
134                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
135                         utils.log("INFO", msg, isDebugEnabled)\r
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
137                 }\r
138                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
139         }\r
140         \r
141    public void prepareDecomposeService_Target(Execution execution) {\r
142         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
143 \r
144         try {\r
145             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)\r
146             String modelInvariantUuid = execution.getVariable("model-invariant-id-target")\r
147             String modelUuid = execution.getVariable("model-version-id-target")\r
148             //here modelVersion is not set, we use modelUuid to decompose the service.\r
149             String serviceModelInfo = """{\r
150             "modelInvariantUuid":"${modelInvariantUuid}",\r
151             "modelUuid":"${modelUuid}",\r
152             "modelVersion":""\r
153              }"""\r
154             execution.setVariable("serviceModelInfo", serviceModelInfo)\r
155             execution.setVariable("serviceModelInfo_Target", serviceModelInfo)\r
156 \r
157             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Target of update generic e2e service ***** ", isDebugEnabled)\r
158         } catch (Exception ex) {\r
159             // try error in method block\r
160             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Target() - " + ex.getMessage()\r
161             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
162         }\r
163      }\r
164 \r
165     public void processDecomposition_Target(Execution execution) {\r
166         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
167     \r
168         utils.log("DEBUG", " ***** Inside processDecomposition_Target() of update generic e2e service flow ***** ", isDebugEnabled)    \r
169         try {\r
170             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
171             execution.setVariable("serviceDecomposition_Target", serviceDecomposition)\r
172         } catch (Exception ex) {\r
173             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method processDecomposition_Target() - " + ex.getMessage()\r
174             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
175             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
176         }\r
177     }\r
178  \r
179    public void prepareDecomposeService_Original(Execution execution) {\r
180         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
181 \r
182         try {\r
183             utils.log("DEBUG", " ***** Inside prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)\r
184             String modelInvariantUuid = execution.getVariable("model-invariant-id-original")\r
185             String modelUuid = execution.getVariable("model-version-id-original")\r
186             //here modelVersion is not set, we use modelUuid to decompose the service.\r
187             String serviceModelInfo = """{\r
188             "modelInvariantUuid":"${modelInvariantUuid}",\r
189             "modelUuid":"${modelUuid}",\r
190             "modelVersion":""\r
191              }"""\r
192             execution.setVariable("serviceModelInfo", serviceModelInfo)\r
193             execution.setVariable("serviceModelInfo_Original", serviceModelInfo)\r
194 \r
195             utils.log("DEBUG", " ***** Completed prepareDecomposeService_Original of update generic e2e service ***** ", isDebugEnabled)\r
196         } catch (Exception ex) {\r
197             // try error in method block\r
198             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. Unexpected Error from method prepareDecomposeService_Original() - " + ex.getMessage()\r
199             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
200         }\r
201      }\r
202 \r
203     public void processDecomposition_Original(Execution execution) {\r
204         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
205     \r
206         utils.log("DEBUG", " ***** Inside processDecomposition_Original() of update generic e2e service flow ***** ", isDebugEnabled)    \r
207         try {\r
208             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
209             execution.setVariable("serviceDecomposition_Original", serviceDecomposition)\r
210         } catch (Exception ex) {\r
211             String exceptionMessage = "Bpmn error encountered in update generic e2e service flow. processDecomposition_Original() - " + ex.getMessage()\r
212             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
213             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
214         }\r
215     }   \r
216 \r
217         public void doCompareModelVersions(execution){\r
218             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
219         utils.log("INFO", "======== Start doCompareModelVersions Process ======== ", isDebugEnabled)          \r
220 \r
221         ServiceDecomposition serviceDecomposition_Target = execution.getVariable("serviceDecomposition_Target")\r
222         ServiceDecomposition serviceDecomposition_Original = execution.getVariable("serviceDecomposition_Original")\r
223         \r
224         List<Resource> allSR_target = serviceDecomposition_Target.getServiceResources();\r
225         List<Resource> allSR_original = serviceDecomposition_Original.getServiceResources();\r
226         \r
227         List<Resource> addedResource = new ArrayList<String>()\r
228         List<Resource> delResource = new ArrayList<String>()\r
229         \r
230         addResourceList.addAll(allSR_target)\r
231         delResourceList.addAll(allSR_original)\r
232         \r
233         //Compare\r
234         for (Resource rc_t : allSR_target){\r
235             String muuid = rc_t.getModelInfo().getModelUuid()\r
236             String mIuuid = rc_t.getModelInfo().getModelInvariantUuid()\r
237             String mCuuid = rc_t.getModelInfo().getModelCustomizationUuid()\r
238             for (Resource rc_o : allSR_original){\r
239                 if(rc_o.getModelInfo().getModelUuid() == muuid \r
240                 && rc_o.getModelInfo().getModelInvariantUuid() == mIuuid \r
241                 && rc_o.getModelInfo().getModelCustomizationUuid() == mCuuid) {\r
242                     addResourceList.remove(rc_t);\r
243                     delResourceList.remove(rc_o);\r
244                 }\r
245             }         
246         }
247 \r
248         execution.setVariable("addResourceList", addResourceList)\r
249         execution.setVariable("delResourceList", delResourceList)\r
250         utils.log("INFO", "addResourceList: " + addResourceList, isDebugEnabled)  \r
251         utils.log("INFO", "delResourceList: " + delResourceList, isDebugEnabled)\r
252         \r
253         utils.log("INFO", "======== COMPLETED doCompareModelVersions Process ======== ", isDebugEnabled)  \r
254         }\r
255 \r
256 }\r