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