Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCompareModelofE2EServiceInstance.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.CompareModelsResult
31 import org.onap.so.bpmn.core.domain.ResourceModelInfo
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.onap.so.bpmn.common.scripts.AaiUtil
34 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
35 import org.onap.so.bpmn.common.scripts.ExceptionUtil
36 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
37 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
38 import org.onap.so.bpmn.core.RollbackData
39 import org.onap.so.bpmn.core.WorkflowException
40 import org.onap.so.rest.APIResponse;
41 import org.onap.so.rest.RESTClient
42 import org.onap.so.rest.RESTConfig
43
44
45 import java.util.List
46 import java.util.Map
47 import java.util.UUID;
48 import javax.xml.parsers.DocumentBuilder
49 import javax.xml.parsers.DocumentBuilderFactory
50
51 import org.camunda.bpm.engine.delegate.BpmnError
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
59 import org.w3c.dom.Document
60 import org.w3c.dom.Element
61 import org.w3c.dom.Node
62 import org.w3c.dom.NodeList
63 import org.xml.sax.InputSource
64 /**
65  * This groovy class supports the <class>DoCompareModelofE2EServiceInstance.bpmn</class> process.
66  *
67  * Inputs:
68  * @param - msoRequestId
69  * @param - globalSubscriberId
70  * @param - subscriptionServiceType
71  * @param - serviceInstanceId
72  * @param - modelInvariantIdTarget
73  * @param - modelVersionIdTarget
74  *
75  * Outputs:
76  * @param - compareModelsResult CompareModelsResult
77
78  */
79 public class DoCompareModelofE2EServiceInstance extends AbstractServiceTaskProcessor {
80
81         String Prefix="DCMPMDSI_"
82         private static final String DebugFlag = "isDebugEnabled"
83         
84         ExceptionUtil exceptionUtil = new ExceptionUtil()
85         JsonUtils jsonUtil = new JsonUtils()
86
87         public void preProcessRequest (DelegateExecution execution) {
88
89                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
90                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
91                 utils.log("INFO","Entered " + method, isDebugEnabled)
92                 String msg = ""
93                 utils.log("INFO"," ***** Enter DoCompareModelofE2EServiceInstance preProcessRequest *****",  isDebugEnabled)
94
95                 execution.setVariable("prefix", Prefix)
96                 //Inputs
97                 
98                 //subscriberInfo. for AAI GET
99                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
100                 utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
101                 
102                 String serviceType = execution.getVariable("serviceType")
103                 utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
104
105                 if (isBlank(globalSubscriberId)) {
106                         msg = "Input globalSubscriberId is null"
107                         utils.log("INFO", msg, isDebugEnabled)
108                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109                 }
110
111                 if (isBlank(serviceType)) {
112                         msg = "Input serviceType is null"
113                         utils.log("INFO", msg, isDebugEnabled)
114                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
115                 }
116
117                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
118                 if (isBlank(serviceInstanceId)){
119                         msg = "Input serviceInstanceId is null"
120                         utils.log("INFO", msg, isDebugEnabled)
121                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
122                 }
123
124                 String modelInvariantUuid = execution.getVariable("modelInvariantIdTarget")
125                 if (isBlank(modelInvariantUuid)){
126                         msg = "Input modelInvariantUuid is null"
127                         utils.log("INFO", msg, isDebugEnabled)
128                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
129                 }
130
131                 String modelUuid = execution.getVariable("modelVersionIdTarget")
132                 if (isBlank(modelUuid)){
133                         msg = "Input modelUuid is null"
134                         utils.log("INFO", msg, isDebugEnabled)
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
136                 }
137
138                 // Set Target Template info
139                 execution.setVariable("model-invariant-id-target", modelInvariantUuid)
140                 execution.setVariable("model-version-id-target", modelUuid)
141
142
143                 utils.log("INFO", "Exited " + method, isDebugEnabled)
144         }
145         
146         public void postProcessAAIGET(DelegateExecution execution) {
147                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
148                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
149                 String msg = ""
150
151                 try {
152                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
153                         boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
154                         String serviceType = ""
155
156                         if(foundInAAI){
157                                 utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
158
159                                 String siData = execution.getVariable("GENGS_service")
160                                 utils.log("INFO", "SI Data", isDebugEnabled)
161                                 if (isBlank(siData))
162                                 {
163                                         msg = "Could not retrive ServiceInstance data from AAI, Id:" + serviceInstanceId
164                                         utils.log("INFO", msg, isDebugEnabled)
165                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
166                                 }
167                                 else
168                                 {
169                                         utils.log("INFO", "SI Data" + siData, isDebugEnabled)
170                                         
171                                         // Get Template uuid and version
172                                         if (utils.nodeExists(siData, "model-invariant-id") && utils.nodeExists(siData, "model-version-id") ) {
173                                             utils.log("INFO", "SI Data model-invariant-id and model-version-id exist", isDebugEnabled)
174                                                 
175                                             def modelInvariantId  = utils.getNodeText(siData, "model-invariant-id")
176                                             def modelVersionId  = utils.getNodeText(siData, "model-version-id")
177                                             
178                                             // Set Original Template info
179                                             execution.setVariable("model-invariant-id-original", modelInvariantId)
180                                             execution.setVariable("model-version-id-original", modelVersionId)                                  
181                                         }                                       
182                                 }
183                         }else{
184                                 boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
185                                 if(!succInAAI){
186                                         utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)
187                                         WorkflowException workflowException = execution.getVariable("WorkflowException")
188                                         utils.logAudit("workflowException: " + workflowException)
189                                         if(workflowException != null){
190                                                 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
191                                         }
192                                         else
193                                         {
194                                                 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
195                                                 utils.log("INFO", msg, isDebugEnabled)
196                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
197                                         }
198                                 }
199
200                                 utils.log("INFO","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
201                         }
202                 }catch (BpmnError e) {
203                         throw e;
204                 } catch (Exception ex) {
205                         msg = "Exception in DoDeleteE2EServiceInstance.postProcessAAIGET. " + ex.getMessage()
206                         utils.log("INFO", msg, isDebugEnabled)
207                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
208                 }
209                 msoLogger.trace("Exit postProcessAAIGET ")
210         }
211
212         public void postCompareModelVersions(DelegateExecution execution) {
213                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
214
215                                 
216                 List<Resource> addResourceList = execution.getVariable("addResourceList")
217                 List<Resource> delResourceList = execution.getVariable("delResourceList")
218                 
219                 CompareModelsResult cmpResult = new CompareModelsResult()
220                 List<ResourceModelInfo> addedResourceList = new ArrayList<ResourceModelInfo>()
221                 List<ResourceModelInfo> deletedResourceList = new ArrayList<ResourceModelInfo>()
222                 
223                 
224                 String serviceModelUuid = execution.getVariable("model-version-id-target")
225         List<String> requestInputs = new ArrayList<String>()
226                 ModelInfo mi = null;
227                 for(Resource rc : addResourceList) {
228                         mi = rc.getModelInfo()
229                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
230                         ResourceModelInfo rmodel = new ResourceModelInfo()
231                         rmodel.setResourceName(mi.getModelName())
232                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
233                         rmodel.setResourceUuid(mi.getModelUuid())
234                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
235                         addedResourceList.add(rmodel)
236                         
237                         Map<String, Object> resourceParameters = ResourceRequestBuilder.buildResouceRequest(serviceModelUuid, resourceCustomizationUuid, null)
238                         requestInputs.addAll(resourceParameters.keySet())                       
239                 }
240                 
241                 for(Resource rc : delResourceList) {
242                         mi = rc.getModelInfo()
243                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
244                         ResourceModelInfo rmodel = new ResourceModelInfo()
245                         rmodel.setResourceName(mi.getModelName())
246                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
247                         rmodel.setResourceUuid(mi.getModelUuid())
248                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
249                         deletedResourceList.add(rmodel)                 
250                 }
251                 
252                 cmpResult.setAddedResourceList(addedResourceList)
253                 cmpResult.setDeletedResourceList(deletedResourceList)
254                 cmpResult.setRequestInputs(requestInputs)       
255
256                 execution.setVariable("compareModelsResult", cmpResult)
257         }
258         
259 }
260