89f40ed680ed4e88fc05f95a3cbab3a58e96a6f0
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.bpmn.infrastructure.scripts
21
22 import org.camunda.bpm.engine.delegate.BpmnError
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.appc.client.lcm.model.Action;
25 import org.onap.so.bpmn.common.scripts.ExceptionUtil
26 import org.onap.so.bpmn.common.scripts.MsoUtils
27 import org.onap.so.bpmn.core.json.JsonUtils
28 import org.onap.so.client.aai.*
29 import org.onap.so.client.aai.entities.AAIResultWrapper
30 import org.onap.so.client.aai.entities.uri.AAIUri
31 import org.onap.so.client.aai.entities.uri.AAIUriFactory
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
34
35 import groovy.json.JsonOutput
36 import groovy.json.JsonSlurper
37
38 public class VnfConfigUpdate extends VnfCmBase {
39         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfConfigUpdate.class);
40
41         ExceptionUtil exceptionUtil = new ExceptionUtil()
42         JsonUtils jsonUtils = new JsonUtils()   
43         def prefix = "VnfIPU_"
44
45         /**
46          * Initialize the flow's variables.
47          *
48          * @param execution The flow's execution instance.
49          */
50         public void initProcessVariables(DelegateExecution execution) {
51                 execution.setVariable('prefix', 'VnfCU_')
52                 execution.setVariable('Request', null)                  
53                 execution.setVariable('source', null)                   
54                 execution.setVariable('controllerType', null)                   
55                 execution.setVariable('UpdateVnfSuccessIndicator', false)
56                 execution.setVariable('serviceType', null)
57                 execution.setVariable('nfRole', null)
58                 execution.setVariable('currentActivity', 'VnfCU')
59                 execution.setVariable('workStep', null)
60                 execution.setVariable('failedActivity', null)
61                 execution.setVariable('errorCode', "0")
62                 execution.setVariable('errorText', null)
63                 execution.setVariable('healthCheckIndex0', 0)
64                 execution.setVariable('healthCheckIndex1', 1)
65                 execution.setVariable('maxRetryCount', 3)
66                 execution.setVariable('retryCount', 0)
67                 execution.setVariable("lcpCloudRegionId", null)
68                 execution.setVariable("rollbackSetClosedLoopDisabledFlag", false)
69                 execution.setVariable("rollbackVnfStop", false)
70                 execution.setVariable("rollbackVnfLock", false)
71                 execution.setVariable("rollbackQuiesceTraffic", false)
72                 execution.setVariable("rollbackSetVnfInMaintenanceFlag", false)
73         }
74
75         /**
76          * Check for missing elements in the received request.
77          *
78          * @param execution The flow's execution instance.
79          */
80         public void preProcessRequest(DelegateExecution execution) {
81                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
82                 'execution=' + execution.getId() +
83                 ')'
84                 initProcessVariables(execution)
85
86                 msoLogger.trace('Entered ' + method)
87
88                 initProcessVariables(execution)         
89
90                 def incomingRequest = execution.getVariable('bpmnRequest')
91
92                 msoLogger.debug("Incoming Infra Request: " + incomingRequest)
93                 try {
94                         def jsonSlurper = new JsonSlurper()
95                         def jsonOutput = new JsonOutput()
96                         Map reqMap = jsonSlurper.parseText(incomingRequest)
97                         msoLogger.debug(" Request is in JSON format.")
98
99                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
100                         def vnfId = execution.getVariable('vnfId')
101                         
102                         execution.setVariable('serviceInstanceId', serviceInstanceId)
103                         execution.setVariable('vnfId', vnfId)                   
104                         execution.setVariable('serviceType', 'Mobility')
105                         execution.setVariable('payload', "")
106                         execution.setVariable('actionHealthCheck', Action.HealthCheck)
107                         execution.setVariable('actionConfigModify', Action.ConfigModify)                        
108                         
109
110                         def controllerType = reqMap.requestDetails?.requestParameters?.controllerType
111                         execution.setVariable('controllerType', controllerType)
112                         
113                         msoLogger.debug('Controller Type: ' + controllerType)                   
114                         
115                         def payload = reqMap.requestDetails?.requestParameters?.payload
116                         execution.setVariable('payload', payload)
117                         
118                         msoLogger.debug('Processed payload: ' + payload)
119                         
120                         def requestId = execution.getVariable("mso-request-id")
121                         execution.setVariable('requestId', requestId)
122                         execution.setVariable('msoRequestId', requestId)                        
123                         
124                         def requestorId = reqMap.requestDetails?.requestInfo?.requestorId ?: null
125                         execution.setVariable('requestorId', requestorId)
126                         
127                         execution.setVariable('sdncVersion', '1702')
128
129                         execution.setVariable("UpdateVnfInfraSuccessIndicator", false)
130                                                 
131
132                         
133                         def source = reqMap.requestDetails?.requestInfo?.source
134                         execution.setVariable("source", source)
135                         
136                         //For Completion Handler & Fallout Handler
137                         String requestInfo =
138                         """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
139                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
140                                         <action>UPDATE</action>
141                                         <source>${MsoUtils.xmlEscape(source)}</source>
142                                    </request-info>"""
143                         
144                         execution.setVariable("requestInfo", requestInfo)                       
145                         
146                         msoLogger.debug('RequestInfo: ' + execution.getVariable("requestInfo"))         
147                         
148                         msoLogger.trace('Exited ' + method)
149
150                 }
151                 catch(groovy.json.JsonException je) {
152                         msoLogger.debug(" Request is not in JSON format.")
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Invalid request format")
154
155                 }
156                 catch(Exception e) {
157                         String restFaultMessage = e.getMessage()
158                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Exception Encountered - " + "\n" + restFaultMessage, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
159                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, restFaultMessage)
160                 }       
161         }
162
163         /**
164          * Prepare and send the sychronous response for this flow.
165          *
166          * @param execution The flow's execution instance.
167          */
168         public void sendSynchResponse(DelegateExecution execution) {
169                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
170                         'execution=' + execution.getId() +
171                         ')'
172
173                 msoLogger.trace('Entered ' + method)
174
175
176                 try {
177                         def requestInfo = execution.getVariable('requestInfo')
178                         def requestId = execution.getVariable('requestId')
179                         def source = execution.getVariable('source')
180                         def progress = getNodeTextForce(requestInfo, 'progress')
181                         if (progress.isEmpty()) {
182                                 progress = '0'
183                         }
184                         def startTime = getNodeTextForce(requestInfo, 'start-time')
185                         if (startTime.isEmpty()) {
186                                 startTime = System.currentTimeMillis()
187                         }
188
189                         // RESTResponse (for API Handler (APIH) Reply Task)
190                         def vnfId = execution.getVariable("vnfId")
191                         String synchResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
192
193                         sendWorkflowResponse(execution, 200, synchResponse)
194
195                         msoLogger.trace('Exited ' + method)
196                 } catch (BpmnError e) {
197                         throw e;
198                 } catch (Exception e) {
199                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
200                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
201                 }
202         }       
203         
204         
205         /**
206          * Check if this VNF is already in maintenance in A&AI.
207          *
208          *
209          * @param execution The flow's execution instance.
210          */
211         public void checkIfVnfInMaintInAAI(DelegateExecution execution) {
212                 def method = getClass().getSimpleName() + '.checkIfVnfInMaintInAAI(' +
213                         'execution=' + execution.getId() +
214                         ')'
215
216                 execution.setVariable('errorCode', "0")
217                 execution.setVariable("workStep", "checkIfVnfInMaintInAAI")
218                 execution.setVariable("failedActivity", "AAI")
219                 msoLogger.trace('Entered ' + method)
220
221                 try {
222                         AAIRestClientImpl client = new AAIRestClientImpl()
223                         AAIValidatorImpl aaiValidator = new AAIValidatorImpl()
224                         aaiValidator.setClient(client)
225                         def vnfId = execution.getVariable("vnfId")
226                         boolean isInMaint = aaiValidator.isVNFLocked(vnfId)
227                         msoLogger.debug("isInMaint result: " + isInMaint)
228                         execution.setVariable('isVnfInMaintenance', isInMaint)
229                         
230                         if (isInMaint) {
231                                 execution.setVariable("errorCode", "1003")
232                                 execution.setVariable("errorText", "VNF is in maintenance in A&AI")
233                         }
234
235                         msoLogger.trace('Exited ' + method)
236                 } catch (BpmnError e) {
237                         throw e;
238                 } catch (Exception e) {
239                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);                  
240                         execution.setVariable("errorCode", "1002")
241                         execution.setVariable("errorText", e.getMessage())
242                         //exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in checkIfVnfInMaintInAAI(): ' + e.getMessage())
243                 }
244         }
245         
246         
247         /**
248          * Check if this VNF's pservers are locked in A&AI.
249          *
250          *
251          * @param execution The flow's execution instance.
252          */
253         public void checkIfPserversInMaintInAAI(DelegateExecution execution) {
254                 def method = getClass().getSimpleName() + '.checkIfPserversInMaintInAAI(' +
255                         'execution=' + execution.getId() +
256                         ')'
257
258                 execution.setVariable('errorCode', "0")
259                 msoLogger.trace('Entered ' + method)
260                 execution.setVariable("workStep", "checkIfPserversInMaintInAAI")
261                 execution.setVariable("failedActivity", "AAI")
262
263                 try {
264                         AAIRestClientImpl client = new AAIRestClientImpl()
265                         AAIValidatorImpl aaiValidator = new AAIValidatorImpl()
266                         aaiValidator.setClient(client)
267                         def vnfId = execution.getVariable("vnfId")                      
268                         boolean areLocked = aaiValidator.isPhysicalServerLocked(vnfId)
269                         msoLogger.debug("areLocked result: " + areLocked)
270                         execution.setVariable('arePserversLocked', areLocked)
271                         
272                         if (areLocked) {
273                                 execution.setVariable("errorCode", "1003")
274                                 execution.setVariable("errorText", "pServers are locked in A&AI")
275                         }
276
277
278                         msoLogger.trace('Exited ' + method)
279                 } catch (BpmnError e) {
280                         throw e;
281                 } catch (Exception e) {
282                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
283                         execution.setVariable("errorCode", "1002")
284                         execution.setVariable("errorText", e.getMessage())
285                         //exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in checkIfPserversInMaintInAAI(): ' + e.getMessage())
286                 }
287         }
288         
289         /**
290          * Set inMaint flag for this VNF to the specified value in A&AI.
291          *
292          *
293          * @param execution The flow's execution instance.
294          * @param inMaint The boolean value of the flag to set
295          */
296         public void setVnfInMaintFlagInAAI(DelegateExecution execution, boolean inMaint) {
297                 def method = getClass().getSimpleName() + '.setVnfInMaintFlagInAAI(' +
298                         'execution=' + execution.getId() +
299                         ')'
300
301                 execution.setVariable('errorCode', "0")
302                 msoLogger.trace('Entered ' + method)
303                 if (inMaint) {
304                         execution.setVariable("workStep", "setVnfInMaintFlagInAAI")
305                         execution.setVariable("rollbackSetVnfInMaintenanceFlag", true)
306                 }
307                 else {
308                         execution.setVariable("workStep", "unsetVnfInMaintFlagInAAI")
309                 }
310                 execution.setVariable("failedActivity", "AAI")
311
312                 try {
313                         AAIRestClientImpl client = new AAIRestClientImpl()
314                         AAIUpdatorImpl aaiUpdator = new AAIUpdatorImpl()
315                         aaiUpdator.setClient(client)
316                         def vnfId = execution.getVariable("vnfId")
317                         if (inMaint) {
318                                 aaiUpdator.updateVnfToLocked(vnfId)
319                                 execution.setVariable("rollbackSetVnfInMaintenanceFlag", true)
320                         }
321                         else {
322                                 aaiUpdator.updateVnfToUnLocked(vnfId)
323                         }
324                                                         
325                         msoLogger.trace('Exited ' + method)
326                 } catch (BpmnError e) {
327                         throw e;
328                 } catch (Exception e) {
329                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
330                         execution.setVariable("errorCode", "1002")
331                         execution.setVariable("errorText", e.getMessage())
332                 }
333         }
334         
335         /**
336          * Check if VF Closed Loop Disabled in A&AI.
337          *
338          *
339          * @param execution The flow's execution instance.
340          */
341         public void checkIfClosedLoopDisabledInAAI(DelegateExecution execution) {
342                 def method = getClass().getSimpleName() + '.checkIfClosedLoopDisabledInAAI(' +
343                         'execution=' + execution.getId() +
344                         ')'
345
346                 execution.setVariable('errorCode', "0")
347                 execution.setVariable("workStep", "checkClosedLoopDisabledFlagInAAI")
348                 execution.setVariable("failedActivity", "AAI")
349                 msoLogger.trace('Entered ' + method)
350
351                 try {
352                         def transactionLoggingUuid = UUID.randomUUID().toString()
353                         def vnfId = execution.getVariable("vnfId")
354                         msoLogger.debug("vnfId is: " + vnfId)
355                         AAIResourcesClient client = new AAIResourcesClient()                    
356                         AAIUri genericVnfUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
357                         AAIResultWrapper aaiRW = client.get(genericVnfUri)
358                         Map<String, Object> result = aaiRW.asMap()
359                         boolean isClosedLoopDisabled = result.getOrDefault("is-closed-loop-disabled", false)
360                 
361                         msoLogger.debug("isClosedLoopDisabled result: " + isClosedLoopDisabled)
362                         execution.setVariable('isClosedLoopDisabled', isClosedLoopDisabled)
363                         
364                         if (isClosedLoopDisabled) {
365                                 execution.setVariable("errorCode", "1004")
366                                 execution.setVariable("errorText", "closedLoop is disabled in A&AI")
367                         }
368
369                         msoLogger.trace('Exited ' + method)
370                 } catch (BpmnError e) {
371                         throw e;
372                 } catch (Exception e) {
373                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
374                         execution.setVariable("errorCode", "1002")
375                         execution.setVariable("errorText", e.getMessage())
376                         //exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in checkIfVnfInMaintInAAI(): ' + e.getMessage())
377                 }
378         }
379         
380         /**
381          * Set VF Closed Loop Disabled Flag in A&AI.
382          *
383          *
384          * @param execution The flow's execution instance.
385          */
386         public void setClosedLoopDisabledInAAI(DelegateExecution execution, boolean setDisabled) {
387                 def method = getClass().getSimpleName() + '.setClosedLoopDisabledInAAI(' +
388                         'execution=' + execution.getId() +
389                         ')'
390
391                 execution.setVariable('errorCode', "0")
392                 if (setDisabled) {
393                         execution.setVariable("workStep", "setClosedLoopDisabledFlagInAAI")
394                         execution.setVariable("rollbackSetClosedLoopDisabledFlag", true)
395                 }
396                 else {
397                         execution.setVariable("workStep", "unsetClosedLoopDisabledFlagInAAI")
398                 }
399                 
400                 execution.setVariable("failedActivity", "AAI")
401                 msoLogger.trace('Entered ' + method)
402
403                 try {
404                         def transactionLoggingUuid = UUID.randomUUID().toString()
405                         def vnfId = execution.getVariable("vnfId")
406                         AAIResourcesClient client = new AAIResourcesClient()                    
407                         AAIUri genericVnfUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
408                         
409                         Map<String, Boolean> request = new HashMap<>()
410                         request.put("is-closed-loop-disabled", setDisabled)
411                         client.update(genericVnfUri, request)
412                         msoLogger.debug("set isClosedLoop to: " + setDisabled)          
413
414
415                         msoLogger.trace('Exited ' + method)
416                 } catch (BpmnError e) {
417                         throw e;
418                 } catch (Exception e) {
419                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
420                         execution.setVariable("errorCode", "1002")
421                         execution.setVariable("errorText", e.getMessage())
422                         //exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in checkIfVnfInMaintInAAI(): ' + e.getMessage())
423                 }
424         }       
425
426         
427         /**
428          * Handle Abort disposition from RainyDayHandler
429          *
430          * @param execution The flow's execution instance.       
431          */
432         public void abortProcessing(DelegateExecution execution) {
433                 def method = getClass().getSimpleName() + '.abortProcessing(' +
434                         'execution=' + execution.getId() +
435                         ')'
436
437                 msoLogger.trace('Entered ' + method)
438                 
439                 def errorText = execution.getVariable("errorText")
440                 def errorCode = execution.getVariable("errorCode")
441                 
442                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode as Integer, errorText)
443         }
444         
445         /**
446          * Increment Retry Count for Current Work Step
447          *
448          * @param execution The flow's execution instance.
449          */
450         public void incrementRetryCount(DelegateExecution execution) {
451                 def method = getClass().getSimpleName() + '.incrementRetryCount(' +
452                         'execution=' + execution.getId() +
453                         ')'
454
455                 msoLogger.trace('Entered ' + method)
456                 
457                 String retryCountVariableName = execution.getVariable("workStep") + "RetryCount"
458                 execution.setVariable("retryCountVariableName", retryCountVariableName)
459                 
460                 def retryCountVariable = execution.getVariable(retryCountVariableName)
461                 int retryCount = 0
462                 
463                 if (retryCountVariable != null) {
464                         retryCount = (int) retryCountVariable
465                 }               
466                 
467                 retryCount += 1
468                 
469                 execution.setVariable(retryCountVariableName, retryCount)
470                 
471                 msoLogger.debug("value of " + retryCountVariableName + " is " + retryCount)
472                 msoLogger.trace('Exited ' + method)
473                         
474                 
475         }
476         
477         
478         
479 }