1 package org.onap.so.bpmn.infrastructure.appc.tasks;
 
   3 import java.util.ArrayList;
 
   5 import java.util.Optional;
 
   6 import org.onap.aai.domain.yang.Vserver;
 
   7 import org.onap.appc.client.lcm.model.Action;
 
   8 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerTaskRequest;
 
   9 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVm;
 
  10 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVnf;
 
  11 import org.onap.so.bpmn.common.BuildingBlockExecution;
 
  12 import org.onap.so.bpmn.core.json.JsonUtils;
 
  13 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
 
  14 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
 
  15 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
 
  16 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
 
  17 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
 
  18 import org.onap.so.client.aai.AAIObjectType;
 
  19 import org.onap.so.client.aai.entities.AAIResultWrapper;
 
  20 import org.onap.so.client.aai.entities.Relationships;
 
  21 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
 
  22 import org.onap.so.client.exception.BBObjectNotFoundException;
 
  23 import org.onap.so.client.exception.ExceptionBuilder;
 
  24 import org.onap.so.client.orchestration.AAIVnfResources;
 
  25 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
 
  26 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  27 import org.onap.so.exceptions.ValidationException;
 
  28 import org.slf4j.Logger;
 
  29 import org.slf4j.LoggerFactory;
 
  30 import org.springframework.beans.factory.annotation.Autowired;
 
  31 import org.springframework.stereotype.Component;
 
  34 public class AppcOrchestratorPreProcessor {
 
  35     private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
 
  36     public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
 
  39     private ExceptionBuilder exceptionUtil;
 
  41     private ExtractPojosForBB extractPojosForBB;
 
  43     private CatalogDbClient catalogDbClient;
 
  45     private AAIVnfResources aaiVnfResources;
 
  47     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
 
  49             Action action = Action.valueOf(actionName);
 
  50             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
 
  51             appcTaskRequest.setAction(action);
 
  52             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
 
  53             GenericVnf vnf = null;
 
  55                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
 
  56             } catch (BBObjectNotFoundException e) {
 
  57                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
 
  60             String vnfName = null;
 
  61             String vnfType = null;
 
  62             String vnfHostIpAddress = null;
 
  65                 vnfId = vnf.getVnfId();
 
  66                 vnfName = vnf.getVnfName();
 
  67                 vnfType = vnf.getVnfType();
 
  68                 vnfHostIpAddress = vnf.getIpv4OamAddress();
 
  70             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
 
  72             String aicIdentity = execution.getVariable("aicIdentity");
 
  73             String identityUrl = execution.getVariable("identityUrl");
 
  74             appcTaskRequest.setIdentityUrl(identityUrl);
 
  76             if (gBBInput.getRequestContext().getRequestParameters() != null) {
 
  77                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
 
  78                 if (payload == null) {
 
  81                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing_software_version");
 
  82                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
 
  83                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new_software_version");
 
  84                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
 
  85                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations_timeout");
 
  86                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
 
  89             ControllerSelectionReference controllerSelectionReference = catalogDbClient
 
  90                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
 
  91             String controllerType = null;
 
  92             if (controllerSelectionReference != null) {
 
  93                 controllerType = controllerSelectionReference.getControllerName();
 
  95                 controllerType = CONTROLLER_TYPE_DEFAULT;
 
  97             appcTaskRequest.setControllerType(controllerType);
 
  99             execution.setVariable("vmIdList", null);
 
 100             execution.setVariable("vserverIdList", null);
 
 101             execution.setVariable("vmIndex", 0);
 
 102             execution.setVariable("vmIdListSize", 0);
 
 104             String vfModuleId = null;
 
 105             VfModule vfModule = null;
 
 107                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
 
 108             } catch (BBObjectNotFoundException e) {
 
 110             if (vfModule != null) {
 
 111                 vfModuleId = vfModule.getVfModuleId();
 
 113             if (action.equals(Action.Snapshot)) {
 
 115                     getVserversForAppc(execution, vnf);
 
 116                 } catch (Exception e) {
 
 117                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
 
 121             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 122             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
 
 123             applicationControllerVnf.setVnfId(vnfId);
 
 124             applicationControllerVnf.setVnfName(vnfName);
 
 125             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 127             verifyApplicationControllerTaskRequest(execution, appcTaskRequest);
 
 129             execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 130             logger.debug("SET APPC ORCHESTRATOR REQUEST");
 
 131         } catch (Exception e) {
 
 132             logger.error("Error building ApplicationControllerTaskRequest Object", e.getMessage());
 
 133             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
 
 137     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
 
 139             ApplicationControllerTaskRequest appcTaskRequest =
 
 140                     (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
 
 141             ArrayList<String> vmIdList = execution.getVariable("vmIdList");
 
 142             ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
 
 143             Integer vmIndex = (Integer) execution.getVariable("vmIndex");
 
 145             if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
 
 146                 execution.setVariable("vmIdListSize", vmIdList.size());
 
 147                 if (vmIndex < vmIdList.size()) {
 
 148                     ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
 
 149                     applicationControllerVm.setVmId(vmIdList.get(vmIndex));
 
 150                     applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
 
 151                     if (appcTaskRequest.getApplicationControllerVnf() == null) {
 
 152                         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 153                         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 155                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
 
 156                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 158                     execution.setVariable("vmIndex", vmIndex);
 
 161         } catch (Exception e) {
 
 162             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
 
 163             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
 
 167     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
 
 168         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
 
 170         if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
 
 171             Relationships relationships = aaiRW.getRelationships().get();
 
 172             if (relationships != null) {
 
 173                 List<AAIResourceUri> vserverUris = relationships.getRelatedAAIUris(AAIObjectType.VSERVER);
 
 174                 ArrayList<String> vserverIds = new ArrayList<String>();
 
 175                 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
 
 176                 for (AAIResourceUri j : vserverUris) {
 
 177                     String vserverId = j.getURIKeys().get("vserver-id");
 
 178                     vserverIds.add(vserverId);
 
 179                     Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
 
 180                     if (oVserver.isPresent()) {
 
 181                         Vserver vserver = oVserver.get();
 
 182                         String vserverSelfLink = vserver.getVserverSelflink();
 
 183                         vserverSelfLinks.add(vserverSelfLink);
 
 186                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
 
 187                 logger.debug("vserverIdsArray is: {}", vserverIds);
 
 188                 execution.setVariable("vmIdList", vserverSelfLinks);
 
 189                 execution.setVariable("vserverIdList", vserverIds);
 
 194     protected void verifyApplicationControllerTaskRequest(BuildingBlockExecution execution,
 
 195             ApplicationControllerTaskRequest appcTaskRequest) throws ValidationException {
 
 196         String errorMessage = null;
 
 197         switch (appcTaskRequest.getAction()) {
 
 199                 if (appcTaskRequest.getOperationsTimeout() == null
 
 200                         || appcTaskRequest.getOperationsTimeout().isEmpty()) {
 
 201                     errorMessage = "APPC action QuiesceTraffic is missing operations_timeout parameter. ";
 
 204             case UpgradePreCheck:
 
 205             case UpgradePostCheck:
 
 207             case UpgradeSoftware:
 
 208                 if (appcTaskRequest.getExistingSoftwareVersion() == null
 
 209                         || appcTaskRequest.getExistingSoftwareVersion().isEmpty()) {
 
 211                             "APPC action " + appcTaskRequest.getAction() + " is missing existing_software parameter. ";
 
 213                 if (appcTaskRequest.getNewSoftwareVersion() == null
 
 214                         || appcTaskRequest.getNewSoftwareVersion().isEmpty()) {
 
 216                             "APPC action " + appcTaskRequest.getAction() + " is missing new_software parameter. ";
 
 220                 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm() != null) {
 
 221                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId() == null
 
 222                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId()
 
 224                         errorMessage = "APPC action Snapshot is missing vmId parameter. ";
 
 226                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm()
 
 227                             .getVserverId() == null
 
 228                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId()
 
 230                         errorMessage = "APPC action Snapshot is missing vserverId parameter. ";
 
 237         if (errorMessage != null) {
 
 238             logger.debug("verifyApplicationControllerTaskRequest() failed with " + errorMessage);
 
 239             throw new ValidationException(errorMessage, false);