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.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;
39 public class AppcOrchestratorPreProcessor {
40 private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
41 public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
44 private ExceptionBuilder exceptionUtil;
46 private ExtractPojosForBB extractPojosForBB;
48 private CatalogDbClient catalogDbClient;
50 private AAIVnfResources aaiVnfResources;
52 public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
54 Action action = Action.valueOf(actionName);
55 ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
56 appcTaskRequest.setAction(action);
57 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
58 GenericVnf vnf = null;
60 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
61 } catch (BBObjectNotFoundException e) {
62 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
65 String vnfName = null;
66 String vnfType = null;
67 String vnfHostIpAddress = null;
70 vnfId = vnf.getVnfId();
71 vnfName = vnf.getVnfName();
72 vnfType = vnf.getVnfType();
73 vnfHostIpAddress = vnf.getIpv4OamAddress();
75 String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
77 String aicIdentity = execution.getVariable("aicIdentity");
78 String identityUrl = execution.getVariable("identityUrl");
79 appcTaskRequest.setIdentityUrl(identityUrl);
81 if (gBBInput.getRequestContext().getRequestParameters() != null) {
82 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
83 if (payload == null) {
86 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing_software_version");
87 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
88 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new_software_version");
89 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
90 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations_timeout");
91 appcTaskRequest.setOperationsTimeout(operationsTimeout);
93 Map<String, String> configMap = new HashMap<>();
94 ObjectMapper objectMapper = new ObjectMapper();
95 String configParamsStr = JsonUtils.getJsonValue(payload, "configuration_parameters");
96 if (configParamsStr != null) {
98 objectMapper.readValue(configParamsStr, new TypeReference<HashMap<String, String>>() {});
100 appcTaskRequest.setConfigParams(configMap);
102 ControllerSelectionReference controllerSelectionReference = catalogDbClient
103 .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
104 String controllerType = null;
105 if (controllerSelectionReference != null) {
106 controllerType = controllerSelectionReference.getControllerName();
108 controllerType = CONTROLLER_TYPE_DEFAULT;
110 appcTaskRequest.setControllerType(controllerType);
112 execution.setVariable("vmIdList", null);
113 execution.setVariable("vserverIdList", null);
114 execution.setVariable("vmIndex", 0);
115 execution.setVariable("vmIdListSize", 0);
117 String vfModuleId = null;
118 VfModule vfModule = null;
120 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
121 } catch (BBObjectNotFoundException e) {
123 if (vfModule != null) {
124 vfModuleId = vfModule.getVfModuleId();
126 if (action.equals(Action.Snapshot)) {
128 getVserversForAppc(execution, vnf);
129 } catch (Exception e) {
130 logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
134 ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
135 applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
136 applicationControllerVnf.setVnfId(vnfId);
137 applicationControllerVnf.setVnfName(vnfName);
138 appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
140 verifyApplicationControllerTaskRequest(execution, appcTaskRequest);
142 execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
143 logger.debug("SET APPC ORCHESTRATOR REQUEST");
144 } catch (Exception e) {
145 logger.error("Error building ApplicationControllerTaskRequest Object", e.getMessage());
146 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
150 public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
152 ApplicationControllerTaskRequest appcTaskRequest =
153 (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
154 ArrayList<String> vmIdList = execution.getVariable("vmIdList");
155 ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
156 Integer vmIndex = (Integer) execution.getVariable("vmIndex");
158 if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
159 execution.setVariable("vmIdListSize", vmIdList.size());
160 if (vmIndex < vmIdList.size()) {
161 ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
162 applicationControllerVm.setVmId(vmIdList.get(vmIndex));
163 applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
164 if (appcTaskRequest.getApplicationControllerVnf() == null) {
165 ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
166 appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
168 appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
169 execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
171 execution.setVariable("vmIndex", vmIndex);
174 } catch (Exception e) {
175 logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
176 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
180 protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
181 AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
183 if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
184 Relationships relationships = aaiRW.getRelationships().get();
185 if (relationships != null) {
186 List<AAIResourceUri> vserverUris = relationships.getRelatedAAIUris(AAIObjectType.VSERVER);
187 ArrayList<String> vserverIds = new ArrayList<String>();
188 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
189 for (AAIResourceUri j : vserverUris) {
190 String vserverId = j.getURIKeys().get(AAIFluentTypeBuilder.Types.VSERVER.getUriParams().vserverId);
191 vserverIds.add(vserverId);
192 Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
193 if (oVserver.isPresent()) {
194 Vserver vserver = oVserver.get();
195 String vserverSelfLink = vserver.getVserverSelflink();
196 vserverSelfLinks.add(vserverSelfLink);
199 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
200 logger.debug("vserverIdsArray is: {}", vserverIds);
201 execution.setVariable("vmIdList", vserverSelfLinks);
202 execution.setVariable("vserverIdList", vserverIds);
207 protected void verifyApplicationControllerTaskRequest(BuildingBlockExecution execution,
208 ApplicationControllerTaskRequest appcTaskRequest) throws ValidationException {
209 String errorMessage = null;
210 switch (appcTaskRequest.getAction()) {
212 if (appcTaskRequest.getOperationsTimeout() == null
213 || appcTaskRequest.getOperationsTimeout().isEmpty()) {
214 errorMessage = "APPC action QuiesceTraffic is missing operations_timeout parameter. ";
217 case UpgradePreCheck:
218 case UpgradePostCheck:
220 case UpgradeSoftware:
221 if (appcTaskRequest.getExistingSoftwareVersion() == null
222 || appcTaskRequest.getExistingSoftwareVersion().isEmpty()) {
224 "APPC action " + appcTaskRequest.getAction() + " is missing existing_software parameter. ";
226 if (appcTaskRequest.getNewSoftwareVersion() == null
227 || appcTaskRequest.getNewSoftwareVersion().isEmpty()) {
229 "APPC action " + appcTaskRequest.getAction() + " is missing new_software parameter. ";
233 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm() != null) {
234 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId() == null
235 || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId()
237 errorMessage = "APPC action Snapshot is missing vmId parameter. ";
239 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm()
240 .getVserverId() == null
241 || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId()
243 errorMessage = "APPC action Snapshot is missing vserverId parameter. ";
248 if (appcTaskRequest.getConfigParams().isEmpty() || appcTaskRequest.getConfigParams() == null) {
249 errorMessage = "APPC action ConfigModify is missing Configuration parameters. ";
255 if (errorMessage != null) {
256 logger.debug("verifyApplicationControllerTaskRequest() failed with " + errorMessage);
257 throw new ValidationException(errorMessage, false);