Replaced all tabs with spaces in java and pom.xml
[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 import org.onap.appc.client.lcm.model.Action;
27 import org.onap.appc.client.lcm.model.Status;
28 import org.onap.so.bpmn.appc.payload.PayloadClient;
29 import org.onap.so.bpmn.core.json.JsonUtils;
30 import org.onap.so.client.appc.ApplicationControllerSupport.StatusCategory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Component;
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 @Component
39 public class ApplicationControllerAction {
40     protected ApplicationControllerOrchestrator client = new ApplicationControllerOrchestrator();
41     private String errorCode = "1002";
42     private String errorMessage = "Unable to reach App C Servers";
43     private static Logger logger = LoggerFactory.getLogger(ApplicationControllerAction.class);
44
45     public void runAppCCommand(Action action, String msoRequestId, String vnfId, Optional<String> payload,
46             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(),
66                             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 =
88                                 snapshotAction(msoRequestId, vnfId, vmId, vserverIdString, identityUrl, controllerType);
89                         i++;
90                     }
91                     break;
92                 case ConfigModify:
93                 case ConfigScaleOut:
94                 case DistributeTraffic:
95                 case DistributeTrafficCheck:
96                     appCStatus = payloadAction(action, msoRequestId, vnfId, payload, controllerType);
97                     break;
98                 case UpgradePreCheck:
99                 case UpgradePostCheck:
100                 case UpgradeSoftware:
101                 case UpgradeBackup:
102                     appCStatus = upgradeAction(action, msoRequestId, vnfId, payload, vnfName, controllerType);
103                     break;
104                 default:
105                     errorMessage = "Unable to idenify Action request for AppCClient";
106                     break;
107             }
108             if (appCStatus != null) {
109                 errorCode = Integer.toString(appCStatus.getCode());
110                 errorMessage = appCStatus.getMessage();
111
112             }
113             if (ApplicationControllerSupport.getCategoryOf(appCStatus).equals(StatusCategory.NORMAL)) {
114                 errorCode = "0";
115             }
116         } catch (JsonProcessingException e) {
117             logger.error("Incorrect Payload format for action request: {}", action.toString(), e);
118             errorMessage = e.getMessage();
119         } catch (ApplicationControllerOrchestratorException e) {
120             logger.error("Error building Appc request: {}", e.getMessage(), e);
121             errorCode = "1002";
122             errorMessage = e.getMessage();
123         } catch (NoSuchMethodError e) {
124             logger.error("Error building Appc request: {}", e.getMessage(), e);
125             errorMessage = e.getMessage();
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,
133             String controllerType)
134             throws JsonProcessingException, IllegalArgumentException, ApplicationControllerOrchestratorException {
135         if (!(payload.isPresent())) {
136             throw new IllegalArgumentException("Payload is not present for " + action.toString());
137         }
138         return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
139     }
140
141     private Status quiesceTrafficAction(String msoRequestId, String vnfId, Optional<String> payload, String vnfName,
142             String controllerType)
143             throws JsonProcessingException, IllegalArgumentException, ApplicationControllerOrchestratorException {
144         if (!(payload.isPresent())) {
145             throw new IllegalArgumentException("Payload is not present for " + Action.QuiesceTraffic.toString());
146         }
147         payload = PayloadClient.quiesceTrafficFormat(payload, vnfName);
148         return client.vnfCommand(Action.QuiesceTraffic, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
149     }
150
151     private Status upgradeAction(Action action, String msoRequestId, String vnfId, Optional<String> payload,
152             String vnfName, String controllerType)
153             throws JsonProcessingException, IllegalArgumentException, ApplicationControllerOrchestratorException {
154         if (!(payload.isPresent())) {
155             throw new IllegalArgumentException("Payload is not present for " + action.toString());
156         }
157         payload = PayloadClient.upgradeFormat(payload, vnfName);
158         return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
159     }
160
161     private Status resumeTrafficAction(String msoRequestId, String vnfId, String vnfName, String controllerType)
162             throws JsonProcessingException, ApplicationControllerOrchestratorException {
163         Optional<String> payload = PayloadClient.resumeTrafficFormat(vnfName);
164         return client.vnfCommand(Action.ResumeTraffic, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
165     }
166
167     private Status startStopAction(Action action, String msoRequestId, String vnfId, String aicIdentity,
168             String controllerType) throws JsonProcessingException, ApplicationControllerOrchestratorException {
169         Optional<String> payload = PayloadClient.startStopFormat(aicIdentity);
170         return client.vnfCommand(action, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
171     }
172
173     private Status healthCheckAction(String msoRequestId, String vnfId, String vnfName, String vnfHostIpAddress,
174             String controllerType) throws JsonProcessingException, ApplicationControllerOrchestratorException {
175         Optional<String> payload = PayloadClient.healthCheckFormat(vnfName, vnfHostIpAddress);
176         return client.vnfCommand(Action.HealthCheck, msoRequestId, vnfId, Optional.empty(), payload, controllerType);
177     }
178
179     private Status snapshotAction(String msoRequestId, String vnfId, String vmId, Optional<String> vserverId,
180             String identityUrl, String controllerType)
181             throws JsonProcessingException, ApplicationControllerOrchestratorException {
182         Optional<String> payload = PayloadClient.snapshotFormat(vmId, identityUrl);
183         return client.vnfCommand(Action.Snapshot, msoRequestId, vnfId, vserverId, payload, controllerType);
184     }
185
186     public String getErrorMessage() {
187         return errorMessage;
188     }
189
190     public String getErrorCode() {
191         return errorCode;
192     }
193 }