Bugfixes for December 2018
[so.git] / bpmn / so-bpmn-infrastructure-common / 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
24 import javax.ws.rs.NotFoundException
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.onap.aai.domain.yang.ServiceInstance
30 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.core.domain.CompareModelsResult
34 import org.onap.so.bpmn.core.domain.ModelInfo
35 import org.onap.so.bpmn.core.domain.Resource
36 import org.onap.so.bpmn.core.domain.ResourceModelInfo
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.client.aai.AAIObjectType
39 import org.onap.so.client.aai.AAIResourcesClient
40 import org.onap.so.client.aai.entities.AAIResultWrapper
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43
44 import groovy.json.*
45
46 /**
47  * This groovy class supports the <class>DoCompareModelofE2EServiceInstance.bpmn</class> process.
48  *
49  * Inputs:
50  * @param - msoRequestId
51  * @param - globalSubscriberId
52  * @param - subscriptionServiceType
53  * @param - serviceInstanceId
54  * @param - modelInvariantIdTarget
55  * @param - modelVersionIdTarget
56  *
57  * Outputs:
58  * @param - compareModelsResult CompareModelsResult
59
60  */
61 public class DoCompareModelofE2EServiceInstance extends AbstractServiceTaskProcessor {
62
63         String Prefix="DCMPMDSI_"
64         private static final String DebugFlag = "isDebugEnabled"
65
66         ExceptionUtil exceptionUtil = new ExceptionUtil()
67         JsonUtils jsonUtil = new JsonUtils()
68
69         public void preProcessRequest (DelegateExecution execution) {
70
71                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
72                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
73                 utils.log("INFO","Entered " + method, isDebugEnabled)
74                 String msg = ""
75                 utils.log("INFO"," ***** Enter DoCompareModelofE2EServiceInstance preProcessRequest *****",  isDebugEnabled)
76
77                 execution.setVariable("prefix", Prefix)
78                 //Inputs
79
80                 //subscriberInfo. for AAI GET
81                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
82                 utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
83
84                 String serviceType = execution.getVariable("serviceType")
85                 utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
86
87                 if (isBlank(globalSubscriberId)) {
88                         msg = "Input globalSubscriberId is null"
89                         utils.log("INFO", msg, isDebugEnabled)
90                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
91                 }
92
93                 if (isBlank(serviceType)) {
94                         msg = "Input serviceType is null"
95                         utils.log("INFO", msg, isDebugEnabled)
96                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
97                 }
98
99                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
100                 if (isBlank(serviceInstanceId)){
101                         msg = "Input serviceInstanceId is null"
102                         utils.log("INFO", msg, isDebugEnabled)
103                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
104                 }
105
106                 String modelInvariantUuid = execution.getVariable("modelInvariantIdTarget")
107                 if (isBlank(modelInvariantUuid)){
108                         msg = "Input modelInvariantUuid is null"
109                         utils.log("INFO", msg, isDebugEnabled)
110                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
111                 }
112
113                 String modelUuid = execution.getVariable("modelVersionIdTarget")
114                 if (isBlank(modelUuid)){
115                         msg = "Input modelUuid is null"
116                         utils.log("INFO", msg, isDebugEnabled)
117                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
118                 }
119
120                 // Set Target Template info
121                 execution.setVariable("model-invariant-id-target", modelInvariantUuid)
122                 execution.setVariable("model-version-id-target", modelUuid)
123
124
125                 utils.log("INFO", "Exited " + method, isDebugEnabled)
126         }
127
128         /**
129          * Gets the service instance from aai
130          *
131          * @author cb645j
132          */
133         public void getServiceInstance(DelegateExecution execution) {
134                 try {
135                         String serviceInstanceId = execution.getVariable('serviceInstanceId')
136                         String globalSubscriberId = execution.getVariable('globalSubscriberId')
137                         String serviceType = execution.getVariable('serviceType')
138
139                         AAIResourcesClient resourceClient = new AAIResourcesClient()
140                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId)
141                         AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
142
143                         Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
144                         execution.setVariable("model-invariant-id-original", si.get().getModelInvariantId())
145                         execution.setVariable("model-version-id-original", si.get().getModelVersionId())
146
147                 }catch(BpmnError e) {
148                         throw e;
149                 }catch(NotFoundException e) {
150                         exceptionUtil.buildAndThrowWorkflowException(execution, 404, "Service-instance does not exist AAI")
151                 }catch(Exception ex) {
152                         String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
154                 }
155         }
156
157         public void postCompareModelVersions(DelegateExecution execution) {
158                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
159
160
161                 List<Resource> addResourceList = execution.getVariable("addResourceList")
162                 List<Resource> delResourceList = execution.getVariable("delResourceList")
163
164                 CompareModelsResult cmpResult = new CompareModelsResult()
165                 List<ResourceModelInfo> addedResourceList = new ArrayList<ResourceModelInfo>()
166                 List<ResourceModelInfo> deletedResourceList = new ArrayList<ResourceModelInfo>()
167
168
169                 String serviceModelUuid = execution.getVariable("model-version-id-target")
170         List<String> requestInputs = new ArrayList<String>()
171                 ModelInfo mi = null;
172                 for(Resource rc : addResourceList) {
173                         mi = rc.getModelInfo()
174                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
175                         ResourceModelInfo rmodel = new ResourceModelInfo()
176                         rmodel.setResourceName(mi.getModelName())
177                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
178                         rmodel.setResourceUuid(mi.getModelUuid())
179                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
180                         addedResourceList.add(rmodel)
181
182                         Map<String, Object> resourceParameters = ResourceRequestBuilder.buildResouceRequest(serviceModelUuid, resourceCustomizationUuid, null)
183                         requestInputs.addAll(resourceParameters.keySet())
184                 }
185
186                 for(Resource rc : delResourceList) {
187                         mi = rc.getModelInfo()
188                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
189                         ResourceModelInfo rmodel = new ResourceModelInfo()
190                         rmodel.setResourceName(mi.getModelName())
191                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
192                         rmodel.setResourceUuid(mi.getModelUuid())
193                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
194                         deletedResourceList.add(rmodel)
195                 }
196
197                 cmpResult.setAddedResourceList(addedResourceList)
198                 cmpResult.setDeletedResourceList(deletedResourceList)
199                 cmpResult.setRequestInputs(requestInputs)
200
201                 execution.setVariable("compareModelsResult", cmpResult)
202         }
203
204 }
205