Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / appc / ApplicationControllerAction.java
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  *
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
21 package org.openecomp.mso.client.appc;
22
23 import java.util.HashMap;
24 import java.util.List;
25
26 import org.openecomp.mso.client.appc.ApplicationControllerSupport.StatusCategory;
27 import org.openecomp.mso.bpmn.appc.payload.PayloadClient;
28 import org.openecomp.mso.bpmn.core.json.JsonUtils;
29 import org.openecomp.mso.client.appc.ApplicationControllerOrchestrator;
30 import java.util.Optional;
31 import org.onap.appc.client.lcm.model.Action;
32 import org.onap.appc.client.lcm.model.Status;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.att.eelf.configuration.EELFLogger.Level;
36 import java.lang.NoSuchMethodError;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.core.type.TypeReference;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42
43 public class ApplicationControllerAction {
44         protected ApplicationControllerOrchestrator client = new ApplicationControllerOrchestrator();
45         private String errorCode = "1002";
46         private String errorMessage = "Unable to reach App C Servers";
47         protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
48         
49         public void runAppCCommand(Action action, String msoRequestId, String vnfId, Optional<String> payload, HashMap<String, String> payloadInfo, String controllerType){             Status appCStatus = null;
50                 try{
51                         String vnfName = payloadInfo.getOrDefault("vnfName", "");
52                         String aicIdentity = payloadInfo.getOrDefault("vnfName","");
53                         String vnfHostIpAddress = payloadInfo.getOrDefault("vnfHostIpAddress","");
54                         String vmIdList = payloadInfo.getOrDefault("vmIdList", "");
55                         String identityUrl = payloadInfo.getOrDefault("identityUrl", "");
56                         switch(action){
57                                 case ResumeTraffic:
58                                         appCStatus = resumeTrafficAction(msoRequestId, vnfId, vnfName, controllerType);
59                                         break;
60                             case Start:
61                             case Stop:
62                                 appCStatus = startStopAction(action, msoRequestId, vnfId, aicIdentity, controllerType);
63                                 break;
64                                 case Unlock:
65                                 case Lock:
66                                         appCStatus = client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), controllerType);
67                                         break;
68                                 case QuiesceTraffic:
69                                         appCStatus = quiesceTrafficAction(msoRequestId, vnfId, payload, vnfName, controllerType);
70                                         break;
71                                 case HealthCheck:
72                                         appCStatus = healthCheckAction(msoRequestId, vnfId, vnfName, vnfHostIpAddress, controllerType);
73                                         break;
74                                 case Snapshot:
75                                         String vmIds = JsonUtils.getJsonValue(vmIdList, "vmIds");
76                                         String vmId = "";
77                                         ObjectMapper mapper = new ObjectMapper();
78                                         List<String> vmIdJsonList = mapper.readValue(vmIds, new TypeReference<List<String>>(){});
79                                         int i = 0;
80                                         while(i < vmIdJsonList.size()){
81                                                 vmId = vmIdJsonList.get(i);
82                                                 appCStatus = snapshotAction(msoRequestId, vnfId, vmId, identityUrl, controllerType);
83                                                 i++;
84                                         }
85                                         break;
86                                 case ConfigModify:
87                                 case ConfigScaleOut:
88                                         appCStatus = payloadAction(action, msoRequestId, vnfId, payload, controllerType);
89                                         break;
90                                 case UpgradePreCheck:
91                                 case UpgradePostCheck:
92                                 case UpgradeSoftware:
93                                 case UpgradeBackup:
94                                         appCStatus = upgradeAction(action,msoRequestId, vnfId, payload, vnfName, controllerType);
95                                         break;
96                                 default:
97                                         errorMessage = "Unable to idenify Action request for AppCClient";
98                                         break;
99                         }
100                         if(appCStatus != null){
101                                 errorCode = Integer.toString(appCStatus.getCode());
102                                 errorMessage = appCStatus.getMessage();
103  
104                         }
105                         if(ApplicationControllerSupport.getCategoryOf(appCStatus).equals(StatusCategory.NORMAL)){
106                                 errorCode = "0";
107                         }
108                 }
109                 catch(JsonProcessingException e){
110                         auditLogger.log(Level.ERROR, "Incorrect Payload format for action request" + action.toString(),e, e.getMessage());
111                         errorMessage = e.getMessage();
112                 }
113                 catch(ApplicationControllerOrchestratorException e){
114                         auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
115                         errorCode = "1002";
116                         errorMessage = e.getMessage();
117                 }
118                 catch (NoSuchMethodError e) {
119                         auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
120                         errorMessage = e.getMessage();
121                 } 
122                 catch(Exception e){
123                         auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
124                         errorMessage = e.getMessage();
125                 }
126         }
127         
128         private Status payloadAction(Action action, String msoRequestId, String vnfId, Optional<String> payload, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
129                 if(!(payload.isPresent())){
130                         throw new IllegalArgumentException("Payload is not present for " + action.toString());
131                 }
132                 return client.vnfCommand(action, msoRequestId, vnfId, payload, controllerType);
133         }
134         
135         private Status quiesceTrafficAction(String msoRequestId, String vnfId, Optional<String> payload, String vnfName, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
136                 if(!(payload.isPresent())){
137                         throw new IllegalArgumentException("Payload is not present for " + Action.QuiesceTraffic.toString());
138                 }
139                 payload = PayloadClient.quiesceTrafficFormat(payload, vnfName);
140                 return client.vnfCommand(Action.QuiesceTraffic, msoRequestId, vnfId, payload, controllerType);
141         }
142         
143         private Status upgradeAction(Action action, String msoRequestId, String vnfId, Optional<String> payload, String vnfName, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
144                 if(!(payload.isPresent())){
145                         throw new IllegalArgumentException("Payload is not present for " + action.toString());
146                 }
147                 payload = PayloadClient.upgradeFormat(payload, vnfName);
148                 return client.vnfCommand(action, msoRequestId, vnfId, payload, controllerType);
149         }
150         
151         private Status resumeTrafficAction(String msoRequestId, String vnfId, String vnfName, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
152                 Optional<String> payload = PayloadClient.resumeTrafficFormat(vnfName);
153                 return client.vnfCommand(Action.ResumeTraffic, msoRequestId, vnfId, payload, controllerType);
154         }
155         
156         private Status startStopAction(Action action, String msoRequestId, String vnfId, String aicIdentity, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
157                 Optional<String> payload = PayloadClient.startStopFormat(aicIdentity);
158                 return client.vnfCommand(action, msoRequestId, vnfId, payload, controllerType);
159         }
160         
161         private Status healthCheckAction(String msoRequestId, String vnfId, String vnfName, String vnfHostIpAddress, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
162                 Optional<String> payload = PayloadClient.healthCheckFormat(vnfName, vnfHostIpAddress);
163                 return client.vnfCommand(Action.HealthCheck, msoRequestId, vnfId, payload, controllerType);
164         }
165         
166         private Status snapshotAction(String msoRequestId, String vnfId, String vmId, String identityUrl, String controllerType) throws JsonProcessingException, ApplicationControllerOrchestratorException{
167                 Optional<String> payload = PayloadClient.snapshotFormat(vmId, identityUrl);
168                 return client.vnfCommand(Action.Snapshot, msoRequestId, vnfId, payload, controllerType);
169         }
170         
171         public String getErrorMessage(){
172                 return errorMessage;
173         }
174         
175         public String getErrorCode(){
176                 return errorCode;
177         }
178 }