8cd9dee0111fb5d6bc32489db20832f91c05362f
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.scripts;
23
24 import static org.apache.commons.lang3.StringUtils.*;
25
26 import javax.ws.rs.NotFoundException
27
28 import org.apache.commons.lang3.*
29 import org.camunda.bpm.engine.delegate.BpmnError
30 import org.camunda.bpm.engine.delegate.DelegateExecution
31 import org.onap.aai.domain.yang.ServiceInstance
32 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.core.domain.CompareModelsResult
36 import org.onap.so.bpmn.core.domain.ModelInfo
37 import org.onap.so.bpmn.core.domain.Resource
38 import org.onap.so.bpmn.core.domain.ResourceModelInfo
39 import org.onap.so.bpmn.core.json.JsonUtils
40 import org.onap.aaiclient.client.aai.AAIObjectType
41 import org.onap.aaiclient.client.aai.AAIResourcesClient
42 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
43 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
44 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
45 import org.slf4j.Logger
46 import org.slf4j.LoggerFactory
47
48 import groovy.json.*
49
50 /**
51  * This groovy class supports the <class>DoCompareModelofE2EServiceInstance.bpmn</class> process.
52  *
53  * Inputs:
54  * @param - msoRequestId
55  * @param - globalSubscriberId
56  * @param - subscriptionServiceType
57  * @param - serviceInstanceId
58  * @param - modelInvariantIdTarget
59  * @param - modelVersionIdTarget
60  *
61  * Outputs:
62  * @param - compareModelsResult CompareModelsResult
63
64  */
65 public class DoCompareModelofE2EServiceInstance extends AbstractServiceTaskProcessor {
66
67         String Prefix="DCMPMDSI_"
68         private static final String DebugFlag = "isDebugEnabled"
69         private static final Logger logger = LoggerFactory.getLogger( DeleteNetworkInstance.class);
70
71         ExceptionUtil exceptionUtil = new ExceptionUtil()
72         JsonUtils jsonUtil = new JsonUtils()
73
74         public void preProcessRequest (DelegateExecution execution) {
75
76                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
77                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
78                 logger.info("Entered " + method)
79                 String msg = ""
80                 logger.info(" ***** Enter DoCompareModelofE2EServiceInstance preProcessRequest *****")
81
82                 execution.setVariable("prefix", Prefix)
83                 //Inputs
84
85                 //subscriberInfo. for AAI GET
86                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
87                 logger.info(" ***** globalSubscriberId *****" + globalSubscriberId)
88
89                 String serviceType = execution.getVariable("serviceType")
90                 logger.info(" ***** serviceType *****" + serviceType)
91
92                 if (isBlank(globalSubscriberId)) {
93                         msg = "Input globalSubscriberId is null"
94                         logger.info( msg)
95                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
96                 }
97
98                 if (isBlank(serviceType)) {
99                         msg = "Input serviceType is null"
100                         logger.info( msg)
101                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
102                 }
103
104                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
105                 if (isBlank(serviceInstanceId)){
106                         msg = "Input serviceInstanceId is null"
107                         logger.info( msg)
108                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109                 }
110
111                 String modelInvariantUuid = execution.getVariable("modelInvariantIdTarget")
112                 if (isBlank(modelInvariantUuid)){
113                         msg = "Input modelInvariantUuid is null"
114                         logger.info( msg)
115                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
116                 }
117
118                 String modelUuid = execution.getVariable("modelVersionIdTarget")
119                 if (isBlank(modelUuid)){
120                         msg = "Input modelUuid is null"
121                         logger.info( msg)
122                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
123                 }
124
125                 // Set Target Template info
126                 execution.setVariable("model-invariant-id-target", modelInvariantUuid)
127                 execution.setVariable("model-version-id-target", modelUuid)
128
129
130                 logger.info( "Exited " + method)
131         }
132
133         /**
134          * Gets the service instance from aai
135          *
136          * @author cb645j
137          */
138         public void getServiceInstance(DelegateExecution execution) {
139                 try {
140                         String serviceInstanceId = execution.getVariable('serviceInstanceId')
141                         String globalSubscriberId = execution.getVariable('globalSubscriberId')
142                         String serviceType = execution.getVariable('serviceType')
143
144                         AAIResourcesClient resourceClient = new AAIResourcesClient()
145                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId)
146                         AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
147
148                         Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
149                         execution.setVariable("model-invariant-id-original", si.get().getModelInvariantId())
150                         execution.setVariable("model-version-id-original", si.get().getModelVersionId())
151
152                 }catch(BpmnError e) {
153                         throw e;
154                 }catch(NotFoundException e) {
155                         exceptionUtil.buildAndThrowWorkflowException(execution, 404, "Service-instance does not exist AAI")
156                 }catch(Exception ex) {
157                         String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
158                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
159                 }
160         }
161
162         public void postCompareModelVersions(DelegateExecution execution) {
163                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
164
165
166                 List<Resource> addResourceList = execution.getVariable("addResourceList")
167                 List<Resource> delResourceList = execution.getVariable("delResourceList")
168
169                 CompareModelsResult cmpResult = new CompareModelsResult()
170                 List<ResourceModelInfo> addedResourceList = new ArrayList<ResourceModelInfo>()
171                 List<ResourceModelInfo> deletedResourceList = new ArrayList<ResourceModelInfo>()
172
173
174                 String serviceModelUuid = execution.getVariable("model-version-id-target")
175         List<String> requestInputs = new ArrayList<String>()
176                 ModelInfo mi = null;
177                 for(Resource rc : addResourceList) {
178                         mi = rc.getModelInfo()
179                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
180                         ResourceModelInfo rmodel = new ResourceModelInfo()
181                         rmodel.setResourceName(mi.getModelName())
182                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
183                         rmodel.setResourceUuid(mi.getModelUuid())
184                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
185                         addedResourceList.add(rmodel)
186
187                         Map<String, Object> resourceParameters = ResourceRequestBuilder.buildResouceRequest(rc, null, null)
188                         requestInputs.addAll(resourceParameters.keySet())
189                 }
190
191                 for(Resource rc : delResourceList) {
192                         mi = rc.getModelInfo()
193                         String resourceCustomizationUuid = mi.getModelCustomizationUuid()
194                         ResourceModelInfo rmodel = new ResourceModelInfo()
195                         rmodel.setResourceName(mi.getModelName())
196                         rmodel.setResourceInvariantUuid(mi.getModelInvariantUuid())
197                         rmodel.setResourceUuid(mi.getModelUuid())
198                         rmodel.setResourceCustomizationUuid(resourceCustomizationUuid)
199                         deletedResourceList.add(rmodel)
200                 }
201
202                 cmpResult.setAddedResourceList(addedResourceList)
203                 cmpResult.setDeletedResourceList(deletedResourceList)
204                 cmpResult.setRequestInputs(requestInputs)
205
206                 execution.setVariable("compareModelsResult", cmpResult)
207         }
208
209 }
210