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