a15e0448187237a8d4345e7f9d43bbf937f33488
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / 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.onap.so.client.appc;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Optional;
26
27 import org.onap.appc.client.lcm.model.Action;
28 import org.onap.appc.client.lcm.model.Status;
29 import org.onap.so.bpmn.appc.payload.PayloadClient;
30 import org.onap.so.bpmn.core.json.JsonUtils;
31 import org.onap.so.client.appc.ApplicationControllerSupport.StatusCategory;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Component;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.core.type.TypeReference;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 @Component
41 public class ApplicationControllerAction {
42         protected ApplicationControllerOrchestrator client = new ApplicationControllerOrchestrator();
43         private String errorCode = "1002";
44         private String errorMessage = "Unable to reach App C Servers";
45         private static Logger logger = LoggerFactory.getLogger(ApplicationControllerAction.class);
46         
47         public void runAppCCommand(Action action, String msoRequestId, String vnfId, Optional<String> payload, HashMap<String, String> payloadInfo, String controllerType){             
48                 Status appCStatus = null;
49                 try{
50                         String vnfName = payloadInfo.getOrDefault("vnfName", "");
51                         String aicIdentity = payloadInfo.getOrDefault("vnfName","");
52                         String vnfHostIpAddress = payloadInfo.getOrDefault("vnfHostIpAddress","");
53                         String vmIdList = payloadInfo.getOrDefault("vmIdList", "");
54                         String vserverIdList = payloadInfo.getOrDefault("vserverIdList", "");
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(), 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 vserverIds = JsonUtils.getJsonValue(vserverIdList, "vserverIds");
77                                         String vmId = "";
78                                         String vserverId = "";
79                                         ObjectMapper mapper = new ObjectMapper();
80                                         List<String> vmIdJsonList = mapper.readValue(vmIds, new TypeReference<List<String>>(){});
81                                         List<String> vserverIdJsonList = mapper.readValue(vserverIds, new TypeReference<List<String>>(){});
82                                         int i = 0;
83                                         while(i < vmIdJsonList.size()){
84                                                 vmId = vmIdJsonList.get(i);
85                                                 vserverId = vserverIdJsonList.get(i);
86                                                 Optional<String> vserverIdString = Optional.of(vserverId);
87                                                 appCStatus = snapshotAction(msoRequestId, vnfId, vmId, vserverIdString, identityUrl, controllerType);
88                                                 i++;
89                                         }
90                                         break;
91                                 case ConfigModify:
92                                 case ConfigScaleOut:
93                                 case DistributeTraffic:
94                                 case DistributeTrafficCheck:
95                                         appCStatus = payloadAction(action, msoRequestId, vnfId, payload, controllerType);
96                                         break;
97                                 case UpgradePreCheck:
98                                 case UpgradePostCheck:
99                                 case UpgradeSoftware:
100                                 case UpgradeBackup:
101                                         appCStatus = upgradeAction(action,msoRequestId, vnfId, payload, vnfName, controllerType);
102                                         break;
103                                 default:
104                                         errorMessage = "Unable to idenify Action request for AppCClient";
105                                         break;
106                         }
107                         if(appCStatus != null){
108                                 errorCode = Integer.toString(appCStatus.getCode());
109                                 errorMessage = appCStatus.getMessage();
110  
111                         }
112                         if(ApplicationControllerSupport.getCategoryOf(appCStatus).equals(StatusCategory.NORMAL)){
113                                 errorCode = "0";
114                         }
115                 }
116                 catch(JsonProcessingException e){
117                         logger.error("Incorrect Payload format for action request: {}", action.toString(),e);
118                         errorMessage = e.getMessage();
119                 }
120                 catch(ApplicationControllerOrchestratorException e){
121                         logger.error("Error building Appc request: {}",e.getMessage(), e);
122                         errorCode = "1002";
123                         errorMessage = e.getMessage();
124                 }
125                 catch (NoSuchMethodError e) {
126                         logger.error( "Error building Appc request: {}",  e.getMessage(), e);
127                         errorMessage = e.getMessage();
128                 } 
129                 catch(Exception e){
130                         logger.error("Error building Appc request: {}", e.getMessage(), e);
131                         errorMessage = e.getMessage();
132                 }
133         }
134         
135         private Status payloadAction(Action action, String msoRequestId, String vnfId, Optional<String> payload, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
136                 if(!(payload.isPresent())){
137                         throw new IllegalArgumentException("Payload is not present for " + action.toString());
138                 }
139                 return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
140         }       
141         
142         private Status quiesceTrafficAction(String msoRequestId, String vnfId, Optional<String> payload, String vnfName, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
143                 if(!(payload.isPresent())){
144                         throw new IllegalArgumentException("Payload is not present for " + Action.QuiesceTraffic.toString());
145                 }
146                 payload = PayloadClient.quiesceTrafficFormat(payload, vnfName);
147                 return client.vnfCommand(Action.QuiesceTraffic, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
148         }
149         
150         private Status upgradeAction(Action action, String msoRequestId, String vnfId, Optional<String> payload, String vnfName, String controllerType) throws JsonProcessingException, IllegalArgumentException,ApplicationControllerOrchestratorException{
151                 if(!(payload.isPresent())){
152                         throw new IllegalArgumentException("Payload is not present for " + action.toString());
153                 }
154                 payload = PayloadClient.upgradeFormat(payload, vnfName);
155                 return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
156         }
157         
158         private Status resumeTrafficAction(String msoRequestId, String vnfId, String vnfName, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
159                 Optional<String> payload = PayloadClient.resumeTrafficFormat(vnfName);
160                 return client.vnfCommand(Action.ResumeTraffic, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
161         }
162         
163         private Status startStopAction(Action action, String msoRequestId, String vnfId, String aicIdentity, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
164                 Optional<String> payload = PayloadClient.startStopFormat(aicIdentity);
165                 return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
166         }
167         
168         private Status healthCheckAction(String msoRequestId, String vnfId, String vnfName, String vnfHostIpAddress, String controllerType)throws JsonProcessingException, ApplicationControllerOrchestratorException{
169                 Optional<String> payload = PayloadClient.healthCheckFormat(vnfName, vnfHostIpAddress);
170                 return client.vnfCommand(Action.HealthCheck, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
171         }
172         
173         private Status snapshotAction(String msoRequestId, String vnfId, String vmId, Optional<String> vserverId, String identityUrl, String controllerType) throws JsonProcessingException, ApplicationControllerOrchestratorException{
174                 Optional<String> payload = PayloadClient.snapshotFormat(vmId, identityUrl);
175                 return client.vnfCommand(Action.Snapshot, msoRequestId, vnfId, vserverId, payload, controllerType);
176         }
177         
178         public String getErrorMessage(){
179                 return errorMessage;
180         }
181         
182         public String getErrorCode(){
183                 return errorCode;
184         }
185 }