1f055220117845510bfc9935270b8d85fe22db1c
[so.git] /
1 package org.onap.so.bpmn.infrastructure.appc.tasks;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Optional;
8 import org.onap.aai.domain.yang.Vserver;
9 import org.onap.aaiclient.client.aai.AAIObjectType;
10 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
11 import org.onap.aaiclient.client.aai.entities.Relationships;
12 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
13 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
14 import org.onap.appc.client.lcm.model.Action;
15 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerTaskRequest;
16 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVm;
17 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVnf;
18 import org.onap.so.bpmn.common.BuildingBlockExecution;
19 import org.onap.so.bpmn.core.json.JsonUtils;
20 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
21 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
22 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
23 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
24 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
25 import org.onap.so.client.exception.BBObjectNotFoundException;
26 import org.onap.so.client.exception.ExceptionBuilder;
27 import org.onap.so.client.orchestration.AAIVnfResources;
28 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
29 import org.onap.so.db.catalog.client.CatalogDbClient;
30 import org.onap.so.exceptions.ValidationException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 @Component
39 public class AppcOrchestratorPreProcessor {
40     private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
41     public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
42
43     @Autowired
44     private ExceptionBuilder exceptionUtil;
45     @Autowired
46     private ExtractPojosForBB extractPojosForBB;
47     @Autowired
48     private CatalogDbClient catalogDbClient;
49     @Autowired
50     private AAIVnfResources aaiVnfResources;
51
52     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
53         try {
54             Action action = Action.valueOf(actionName);
55             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
56             appcTaskRequest.setAction(action);
57             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
58             GenericVnf vnf = null;
59             try {
60                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
61             } catch (BBObjectNotFoundException e) {
62                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
63             }
64             String vnfId = null;
65             String vnfName = null;
66             String vnfType = null;
67             String vnfHostIpAddress = null;
68
69             if (vnf != null) {
70                 vnfId = vnf.getVnfId();
71                 vnfName = vnf.getVnfName();
72                 vnfType = vnf.getVnfType();
73                 vnfHostIpAddress = vnf.getIpv4OamAddress();
74             }
75             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
76
77             String aicIdentity = execution.getVariable("aicIdentity");
78             String identityUrl = execution.getVariable("identityUrl");
79             appcTaskRequest.setIdentityUrl(identityUrl);
80
81             String requestorId = gBBInput.getRequestContext().getRequestorId();
82             appcTaskRequest.setRequestorId(requestorId);
83
84             if (gBBInput.getRequestContext().getRequestParameters() != null) {
85                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
86                 if (payload == null) {
87                     payload = "";
88                 }
89                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing_software_version");
90                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
91                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new_software_version");
92                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
93                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations_timeout");
94                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
95
96                 Map<String, String> configMap = new HashMap<>();
97                 ObjectMapper objectMapper = new ObjectMapper();
98                 String configParamsStr = JsonUtils.getJsonValue(payload, "configuration_parameters");
99                 if (configParamsStr != null) {
100                     configMap =
101                             objectMapper.readValue(configParamsStr, new TypeReference<HashMap<String, String>>() {});
102                 }
103                 appcTaskRequest.setConfigParams(configMap);
104             }
105             ControllerSelectionReference controllerSelectionReference = catalogDbClient
106                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
107             String controllerType = null;
108             if (controllerSelectionReference != null) {
109                 controllerType = controllerSelectionReference.getControllerName();
110             } else {
111                 controllerType = CONTROLLER_TYPE_DEFAULT;
112             }
113             appcTaskRequest.setControllerType(controllerType);
114
115             execution.setVariable("vmIdList", null);
116             execution.setVariable("vserverIdList", null);
117             execution.setVariable("vmIndex", 0);
118             execution.setVariable("vmIdListSize", 0);
119
120             String vfModuleId = null;
121             VfModule vfModule = null;
122             try {
123                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
124             } catch (BBObjectNotFoundException e) {
125             }
126             if (vfModule != null) {
127                 vfModuleId = vfModule.getVfModuleId();
128             }
129             if (action.equals(Action.Snapshot)) {
130                 try {
131                     getVserversForAppc(execution, vnf);
132                 } catch (Exception e) {
133                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
134                 }
135             }
136
137             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
138             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
139             applicationControllerVnf.setVnfId(vnfId);
140             applicationControllerVnf.setVnfName(vnfName);
141             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
142
143             verifyApplicationControllerTaskRequest(execution, appcTaskRequest);
144
145             execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
146             logger.debug("SET APPC ORCHESTRATOR REQUEST");
147         } catch (Exception e) {
148             logger.error("Error building ApplicationControllerTaskRequest Object", e.getMessage());
149             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
150         }
151     }
152
153     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
154         try {
155             ApplicationControllerTaskRequest appcTaskRequest =
156                     (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
157             ArrayList<String> vmIdList = execution.getVariable("vmIdList");
158             ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
159             Integer vmIndex = (Integer) execution.getVariable("vmIndex");
160
161             if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
162                 execution.setVariable("vmIdListSize", vmIdList.size());
163                 if (vmIndex < vmIdList.size()) {
164                     ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
165                     applicationControllerVm.setVmId(vmIdList.get(vmIndex));
166                     applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
167                     if (appcTaskRequest.getApplicationControllerVnf() == null) {
168                         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
169                         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
170                     }
171                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
172                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
173                     vmIndex++;
174                     execution.setVariable("vmIndex", vmIndex);
175                 }
176             }
177         } catch (Exception e) {
178             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
179             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
180         }
181     }
182
183     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
184         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
185
186         if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
187             Relationships relationships = aaiRW.getRelationships().get();
188             if (relationships != null) {
189                 List<AAIResourceUri> vserverUris = relationships.getRelatedAAIUris(AAIObjectType.VSERVER);
190                 ArrayList<String> vserverIds = new ArrayList<String>();
191                 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
192                 for (AAIResourceUri j : vserverUris) {
193                     String vserverId = j.getURIKeys().get(AAIFluentTypeBuilder.Types.VSERVER.getUriParams().vserverId);
194                     vserverIds.add(vserverId);
195                     Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
196                     if (oVserver.isPresent()) {
197                         Vserver vserver = oVserver.get();
198                         String vserverSelfLink = vserver.getVserverSelflink();
199                         vserverSelfLinks.add(vserverSelfLink);
200                     }
201                 }
202                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
203                 logger.debug("vserverIdsArray is: {}", vserverIds);
204                 execution.setVariable("vmIdList", vserverSelfLinks);
205                 execution.setVariable("vserverIdList", vserverIds);
206             }
207         }
208     }
209
210     protected void verifyApplicationControllerTaskRequest(BuildingBlockExecution execution,
211             ApplicationControllerTaskRequest appcTaskRequest) throws ValidationException {
212         String errorMessage = null;
213         switch (appcTaskRequest.getAction()) {
214             case QuiesceTraffic:
215                 if (appcTaskRequest.getOperationsTimeout() == null
216                         || appcTaskRequest.getOperationsTimeout().isEmpty()) {
217                     errorMessage = "APPC action QuiesceTraffic is missing operations_timeout parameter. ";
218                 }
219                 break;
220             case UpgradePreCheck:
221             case UpgradePostCheck:
222             case UpgradeBackup:
223             case UpgradeSoftware:
224                 if (appcTaskRequest.getExistingSoftwareVersion() == null
225                         || appcTaskRequest.getExistingSoftwareVersion().isEmpty()) {
226                     errorMessage =
227                             "APPC action " + appcTaskRequest.getAction() + " is missing existing_software parameter. ";
228                 }
229                 if (appcTaskRequest.getNewSoftwareVersion() == null
230                         || appcTaskRequest.getNewSoftwareVersion().isEmpty()) {
231                     errorMessage =
232                             "APPC action " + appcTaskRequest.getAction() + " is missing new_software parameter. ";
233                 }
234                 break;
235             case Snapshot:
236                 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm() != null) {
237                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId() == null
238                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId()
239                                     .isEmpty()) {
240                         errorMessage = "APPC action Snapshot is missing vmId parameter. ";
241                     }
242                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm()
243                             .getVserverId() == null
244                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId()
245                                     .isEmpty()) {
246                         errorMessage = "APPC action Snapshot is missing vserverId parameter. ";
247                     }
248                 }
249                 break;
250             case ConfigModify:
251                 if (appcTaskRequest.getConfigParams().isEmpty() || appcTaskRequest.getConfigParams() == null) {
252                     errorMessage = "APPC action ConfigModify is missing Configuration parameters. ";
253                 }
254                 break;
255             default:
256                 break;
257         }
258         if (errorMessage != null) {
259             logger.debug("verifyApplicationControllerTaskRequest() failed with " + errorMessage);
260             throw new ValidationException(errorMessage, false);
261         }
262         return;
263     }
264 }