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