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.slf4j.Logger;
 
  28 import org.slf4j.LoggerFactory;
 
  29 import org.springframework.beans.factory.annotation.Autowired;
 
  30 import org.springframework.stereotype.Component;
 
  33 public class AppcOrchestratorPreProcessor {
 
  34     private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
 
  35     public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
 
  38     private ExceptionBuilder exceptionUtil;
 
  40     private ExtractPojosForBB extractPojosForBB;
 
  42     private CatalogDbClient catalogDbClient;
 
  44     private AAIVnfResources aaiVnfResources;
 
  46     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
 
  48             Action action = Action.valueOf(actionName);
 
  49             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
 
  50             appcTaskRequest.setAction(action);
 
  51             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
 
  52             GenericVnf vnf = null;
 
  54                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
 
  55             } catch (BBObjectNotFoundException e) {
 
  56                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
 
  59             String vnfName = null;
 
  60             String vnfType = null;
 
  61             String vnfHostIpAddress = null;
 
  64                 vnfId = vnf.getVnfId();
 
  65                 vnfName = vnf.getVnfName();
 
  66                 vnfType = vnf.getVnfType();
 
  67                 vnfHostIpAddress = vnf.getIpv4OamAddress();
 
  69             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
 
  71             String aicIdentity = execution.getVariable("aicIdentity");
 
  72             String identityUrl = execution.getVariable("identityUrl");
 
  73             appcTaskRequest.setIdentityUrl(identityUrl);
 
  75             if (gBBInput.getRequestContext().getRequestParameters() != null) {
 
  76                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
 
  77                 if (payload == null) {
 
  80                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing-software-version");
 
  81                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
 
  82                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new-software-version");
 
  83                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
 
  84                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations-timeout");
 
  85                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
 
  88             ControllerSelectionReference controllerSelectionReference = catalogDbClient
 
  89                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
 
  90             String controllerType = null;
 
  91             if (controllerSelectionReference != null) {
 
  92                 controllerType = controllerSelectionReference.getControllerName();
 
  94                 controllerType = CONTROLLER_TYPE_DEFAULT;
 
  96             appcTaskRequest.setControllerType(controllerType);
 
  98             execution.setVariable("vmIdList", null);
 
  99             execution.setVariable("vserverIdList", null);
 
 100             execution.setVariable("vmIndex", 0);
 
 101             execution.setVariable("vmIdListSize", 0);
 
 103             String vfModuleId = null;
 
 104             VfModule vfModule = null;
 
 106                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
 
 107             } catch (BBObjectNotFoundException e) {
 
 109             if (vfModule != null) {
 
 110                 vfModuleId = vfModule.getVfModuleId();
 
 112             if (action.equals(Action.Snapshot)) {
 
 114                     getVserversForAppc(execution, vnf);
 
 115                 } catch (Exception e) {
 
 116                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
 
 120             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 121             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
 
 122             applicationControllerVnf.setVnfId(vnfId);
 
 123             applicationControllerVnf.setVnfName(vnfName);
 
 124             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 126             execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 127         } catch (Exception e) {
 
 128             logger.error("Error building ApplicationControllerTaskRequest Object", e);
 
 129             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
 
 133     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
 
 135             ApplicationControllerTaskRequest appcTaskRequest =
 
 136                     (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
 
 137             ArrayList<String> vmIdList = execution.getVariable("vmIdList");
 
 138             ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
 
 139             Integer vmIndex = (Integer) execution.getVariable("vmIndex");
 
 141             if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
 
 142                 execution.setVariable("vmIdListSize", vmIdList.size());
 
 143                 if (vmIndex < vmIdList.size()) {
 
 144                     ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
 
 145                     applicationControllerVm.setVmId(vmIdList.get(vmIndex));
 
 146                     applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
 
 147                     if (appcTaskRequest.getApplicationControllerVnf() == null) {
 
 148                         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 149                         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 151                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
 
 152                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 154                     execution.setVariable("vmIndex", vmIndex);
 
 157         } catch (Exception e) {
 
 158             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
 
 159             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
 
 163     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
 
 164         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
 
 166         if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
 
 167             Relationships relationships = aaiRW.getRelationships().get();
 
 168             if (relationships != null) {
 
 169                 List<AAIResourceUri> vserverUris = relationships.getRelatedAAIUris(AAIObjectType.VSERVER);
 
 170                 ArrayList<String> vserverIds = new ArrayList<String>();
 
 171                 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
 
 172                 for (AAIResourceUri j : vserverUris) {
 
 173                     String vserverId = j.getURIKeys().get("vserver-id");
 
 174                     vserverIds.add(vserverId);
 
 175                     Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
 
 176                     if (oVserver.isPresent()) {
 
 177                         Vserver vserver = oVserver.get();
 
 178                         String vserverSelfLink = vserver.getVserverSelflink();
 
 179                         vserverSelfLinks.add(vserverSelfLink);
 
 182                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
 
 183                 logger.debug("vserverIdsArray is: {}", vserverIds);
 
 184                 execution.setVariable("vmIdList", vserverSelfLinks);
 
 185                 execution.setVariable("vserverIdList", vserverIds);