7beb24f6927585dc91a75c7fcf259675cf558b79
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / AppCClient.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
23 package org.onap.so.bpmn.common.scripts
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.so.bpmn.core.json.JsonUtils
28 import org.onap.appc.client.lcm.model.Action
29 import org.onap.so.client.appc.ApplicationControllerAction;
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
34
35 /**
36  * This groovy class supports the <class>AppCClient.bpmn</class> process.
37  *
38  * Inputs:
39  * @param - msoRequestId
40  * @param - mso-request-Id
41  * @param - isDebugLogEnabled
42  * @param - requestId
43  * @param - vnfId
44  * @param - action
45  * @param - payload
46  * 
47  * Outputs:
48  * @param - errorcode
49  * @param - errorText
50  * @param - responsePayload
51  * @param - healthCheckIndex
52  * @param - workstep
53  */
54
55 public class AppCClient extends AbstractServiceTaskProcessor{
56     private static final Logger logger = LoggerFactory.getLogger( AppCClient.class);
57
58         ExceptionUtil exceptionUtil = new ExceptionUtil()
59         JsonUtils jsonUtils = new JsonUtils()
60         def prefix = "UPDVnfI_"
61
62     public void preProcessRequest(DelegateExecution execution){
63
64         }
65
66         public void runAppcCommand(DelegateExecution execution) {
67                 logger.trace("Start runCommand ")
68                 def method = getClass().getSimpleName() + '.runAppcCommand(' +
69                 'execution=' + execution.getId() +
70                 ')'
71                 logger.trace('Entered ' + method)
72                 execution.setVariable("rollbackVnfStop", false)
73                 execution.setVariable("rollbackVnfLock", false)
74                 execution.setVariable("rollbackQuiesceTraffic", false)
75                 String appcCode = "1002"
76                 String responsePayload = ""
77                 String appcMessage = ""
78                 Action action = null
79                 try {
80                         action = (Action) execution.getVariable("action")
81                         String vnfId = execution.getVariable('vnfId')
82                         String msoRequestId = execution.getVariable('msoRequestId')
83                         String vnfName = execution.getVariable('vnfName')
84                         String aicIdentity = execution.getVariable('aicIdentity')
85                         String vnfHostIpAddress = execution.getVariable('vnfHostIpAddress')
86                         String vmIdList = execution.getVariable("vmIdList")
87                         String vserverIdList = execution.getVariable("vserverIdList")
88                         String identityUrl = execution.getVariable("identityUrl")
89                         String controllerType = execution.getVariable("controllerType")                 
90                         String vfModuleId = execution.getVariable("vfModuleId")
91                         HashMap<String, String> payloadInfo = new HashMap<String, String>();
92                         payloadInfo.put("vnfName", vnfName)
93                         payloadInfo.put("aicIdentity", aicIdentity)
94                         payloadInfo.put("vnfHostIpAddress", vnfHostIpAddress)
95                         payloadInfo.put("vmIdList", vmIdList)
96                         payloadInfo.put("vserverIdList", vserverIdList)
97                         payloadInfo.put("identityUrl", identityUrl)
98                         payloadInfo.put("vfModuleId",vfModuleId)
99                         Optional<String> payload
100                         logger.debug("Running APP-C action: " + action.toString())
101                         logger.debug("VNFID: " + vnfId)
102                         execution.setVariable('msoRequestId', msoRequestId)
103                         execution.setVariable("failedActivity", "APP-C")
104                         execution.setVariable('workStep', action.toString())
105                         if(execution.getVariable("payload") != null){
106                                 String pay = execution.getVariable("payload")
107                                 payload =  Optional.of(pay)
108                         }
109                         if(action.equals(Action.HealthCheck)){
110                                 Integer healthCheckIndex = (Integer) execution.getVariable('healthCheckIndex')
111                                 execution.setVariable('workStep', action.toString() + healthCheckIndex)
112                                 execution.setVariable('healthCheckIndex', healthCheckIndex + 1)
113                         }
114                         ApplicationControllerAction client = new ApplicationControllerAction()
115                         logger.debug("Created Application Controller Action Object")
116                         //PayloadInfo contains extra information that adds on to payload before making request to appc
117                         client.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType)
118                         logger.debug("ran through the main method for Application Contoller")
119                         appcCode = client.getErrorCode()
120                         appcMessage = client.getErrorMessage()
121                 }
122                 catch (BpmnError e) {
123                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
124                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
125                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
126                         appcMessage = e.getMessage()
127                 }
128                 execution.setVariable("errorCode", appcCode)
129                 if (appcCode == '0' && action != null) {
130                         if (action.equals(Action.Lock)) {
131                                 execution.setVariable("rollbackVnfLock", true)
132                         }
133                         if (action.equals(Action.Unlock)) {
134                                 execution.setVariable("rollbackVnfLock", false)
135                         }
136                         if (action.equals(Action.Start)) {
137                                 execution.setVariable("rollbackVnfStop", true)
138                         }
139                         if (action.equals(Action.Stop)) {
140                                 execution.setVariable("rollbackVnfStop", false)
141                         }
142                         if (action.equals(Action.QuiesceTraffic)) {
143                                 execution.setVariable("rollbackQuiesceTraffic", true)
144                         }
145                         if (action.equals(Action.ResumeTraffic)) {
146                                 execution.setVariable("rollbackQuiesceTraffic", false)
147                         }
148                 }
149                 execution.setVariable("errorText", appcMessage)
150                 execution.setVariable("responsePayload", responsePayload)
151                 logger.debug("Error Message: " + appcMessage)
152                 logger.debug("ERROR CODE: " + execution.getVariable("errorCode"))
153                 logger.trace("End of runCommand ")
154         }    
155 }