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             String requestorId = gBBInput.getRequestContext().getRequestorId();
 
  82             appcTaskRequest.setRequestorId(requestorId);
 
  84             if (gBBInput.getRequestContext().getRequestParameters() != null) {
 
  85                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
 
  86                 if (payload == null) {
 
  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);
 
  96                 Map<String, String> configMap = new HashMap<>();
 
  97                 ObjectMapper objectMapper = new ObjectMapper();
 
  98                 String configParamsStr = JsonUtils.getJsonValue(payload, "configuration_parameters");
 
  99                 if (configParamsStr != null) {
 
 101                             objectMapper.readValue(configParamsStr, new TypeReference<HashMap<String, String>>() {});
 
 103                 appcTaskRequest.setConfigParams(configMap);
 
 105             ControllerSelectionReference controllerSelectionReference = catalogDbClient
 
 106                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
 
 107             String controllerType = null;
 
 108             if (controllerSelectionReference != null) {
 
 109                 controllerType = controllerSelectionReference.getControllerName();
 
 111                 controllerType = CONTROLLER_TYPE_DEFAULT;
 
 113             appcTaskRequest.setControllerType(controllerType);
 
 115             execution.setVariable("vmIdList", null);
 
 116             execution.setVariable("vserverIdList", null);
 
 117             execution.setVariable("vmIndex", 0);
 
 118             execution.setVariable("vmIdListSize", 0);
 
 120             String vfModuleId = null;
 
 121             VfModule vfModule = null;
 
 123                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
 
 124             } catch (BBObjectNotFoundException e) {
 
 126             if (vfModule != null) {
 
 127                 vfModuleId = vfModule.getVfModuleId();
 
 129             if (action.equals(Action.Snapshot)) {
 
 131                     getVserversForAppc(execution, vnf);
 
 132                 } catch (Exception e) {
 
 133                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
 
 137             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 138             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
 
 139             applicationControllerVnf.setVnfId(vnfId);
 
 140             applicationControllerVnf.setVnfName(vnfName);
 
 141             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 143             verifyApplicationControllerTaskRequest(execution, appcTaskRequest);
 
 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);
 
 153     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
 
 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");
 
 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);
 
 171                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
 
 172                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 174                     execution.setVariable("vmIndex", vmIndex);
 
 177         } catch (Exception e) {
 
 178             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
 
 179             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
 
 183     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
 
 184         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
 
 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);
 
 202                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
 
 203                 logger.debug("vserverIdsArray is: {}", vserverIds);
 
 204                 execution.setVariable("vmIdList", vserverSelfLinks);
 
 205                 execution.setVariable("vserverIdList", vserverIds);
 
 210     protected void verifyApplicationControllerTaskRequest(BuildingBlockExecution execution,
 
 211             ApplicationControllerTaskRequest appcTaskRequest) throws ValidationException {
 
 212         String errorMessage = null;
 
 213         switch (appcTaskRequest.getAction()) {
 
 215                 if (appcTaskRequest.getOperationsTimeout() == null
 
 216                         || appcTaskRequest.getOperationsTimeout().isEmpty()) {
 
 217                     errorMessage = "APPC action QuiesceTraffic is missing operations_timeout parameter. ";
 
 220             case UpgradePreCheck:
 
 221             case UpgradePostCheck:
 
 223             case UpgradeSoftware:
 
 224                 if (appcTaskRequest.getExistingSoftwareVersion() == null
 
 225                         || appcTaskRequest.getExistingSoftwareVersion().isEmpty()) {
 
 227                             "APPC action " + appcTaskRequest.getAction() + " is missing existing_software parameter. ";
 
 229                 if (appcTaskRequest.getNewSoftwareVersion() == null
 
 230                         || appcTaskRequest.getNewSoftwareVersion().isEmpty()) {
 
 232                             "APPC action " + appcTaskRequest.getAction() + " is missing new_software parameter. ";
 
 236                 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm() != null) {
 
 237                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId() == null
 
 238                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId()
 
 240                         errorMessage = "APPC action Snapshot is missing vmId parameter. ";
 
 242                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm()
 
 243                             .getVserverId() == null
 
 244                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId()
 
 246                         errorMessage = "APPC action Snapshot is missing vserverId parameter. ";
 
 251                 if (appcTaskRequest.getConfigParams().isEmpty() || appcTaskRequest.getConfigParams() == null) {
 
 252                     errorMessage = "APPC action ConfigModify is missing Configuration parameters. ";
 
 258         if (errorMessage != null) {
 
 259             logger.debug("verifyApplicationControllerTaskRequest() failed with " + errorMessage);
 
 260             throw new ValidationException(errorMessage, false);