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