2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2020 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.service.level.impl;
 
  23 import com.fasterxml.jackson.databind.ObjectMapper;
 
  24 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  25 import org.camunda.bpm.engine.delegate.JavaDelegate;
 
  26 import org.onap.aai.domain.yang.ServiceInstance;
 
  27 import org.onap.aaiclient.client.aai.AAIRestClientI;
 
  28 import org.onap.aaiclient.client.aai.AAIRestClientImpl;
 
  29 import org.onap.so.bpmn.core.json.JsonUtils;
 
  30 import org.onap.so.client.exception.ExceptionBuilder;
 
  31 import org.onap.so.serviceinstancebeans.RequestDetails;
 
  32 import org.slf4j.Logger;
 
  33 import org.slf4j.LoggerFactory;
 
  34 import org.springframework.beans.factory.annotation.Autowired;
 
  35 import org.springframework.stereotype.Component;
 
  36 import java.io.IOException;
 
  37 import java.util.Optional;
 
  38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*;
 
  41  * This implementation of {@link JavaDelegate} is used to populate the execution object for Service level upgrade
 
  44 public class ServiceLevelRequestDispatcher implements JavaDelegate {
 
  46     private final Logger logger = LoggerFactory.getLogger(getClass());
 
  49     private ExceptionBuilder exceptionUtil;
 
  52     private ObjectMapper mapper;
 
  55     public void execute(DelegateExecution delegateExecution) throws Exception {
 
  56         logger.debug("Running execute block for activity id: {}, name: {}", delegateExecution.getCurrentActivityId(),
 
  57                 delegateExecution.getCurrentActivityName());
 
  59         RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
 
  61         final String serviceInstanceId = String.valueOf(delegateExecution.getVariable(SERVICE_INSTANCE_ID));
 
  62         final String serviceType = bpmnRequestDetails.getRequestParameters().getSubscriptionServiceType();
 
  63         final String globalSubscriberId = bpmnRequestDetails.getSubscriberInfo().getGlobalSubscriberId();
 
  65         final String modelInvariantId = bpmnRequestDetails.getModelInfo().getModelInvariantId();
 
  66         final String modelId = bpmnRequestDetails.getModelInfo().getModelUuid();
 
  67         final String modelVersion = bpmnRequestDetails.getModelInfo().getModelVersion();
 
  69         if (ServiceLevelConstants.PNF.equalsIgnoreCase(serviceType)) {
 
  70             getAndSetPnfNameFromServiceInstance(serviceInstanceId, serviceType, globalSubscriberId, delegateExecution);
 
  73         // TODO : handling for vnf
 
  75         logger.trace("Completed dispatcher request for ServiceLevelUpgrade.");
 
  78     private void getAndSetPnfNameFromServiceInstance(final String serviceInstanceId, final String serviceType,
 
  79             final String globalSubscriberId, DelegateExecution delegateExecution) {
 
  81         AAIRestClientI restClient = new AAIRestClientImpl();
 
  83         Optional<ServiceInstance> optionalSi =
 
  84                 restClient.getServiceInstanceById(serviceInstanceId, serviceType, globalSubscriberId);
 
  86         if (!optionalSi.isPresent()) {
 
  90         optionalSi.ifPresentOrElse(serviceInstance -> {
 
  91             final String pnfName = serviceInstance.getRelationshipList().getRelationship().stream()
 
  92                     .filter(x -> x.getRelatedTo().contains("pnf")).findFirst().get().getRelationshipData().stream()
 
  93                     .filter(data -> data.getRelationshipKey().contains("pnf.pnf-name")).findFirst().get()
 
  94                     .getRelationshipValue();
 
  95             if (pnfName == null || pnfName.isEmpty()) {
 
  97                         "Unable to find the PNF for service instance id: " + serviceInstance.getServiceInstanceId());
 
 100             delegateExecution.setVariable(ServiceLevelConstants.PNF_NAME, pnfName);
 
 101             delegateExecution.setVariable(ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.PNF);
 
 103             throwExceptionWithWarn(delegateExecution, "Unable to find the service instance: " + serviceInstanceId);
 
 107     private RequestDetails requestVerification(DelegateExecution delegateExecution) throws IOException {
 
 108         RequestDetails bpmnRequestDetails = mapper.readValue(JsonUtils.getJsonValue(
 
 109                 String.valueOf(delegateExecution.getVariable(ServiceLevelConstants.BPMN_REQUEST)), "requestDetails"),
 
 110                 RequestDetails.class);
 
 112         throwIfNull(delegateExecution, bpmnRequestDetails.getModelInfo(), SERVICE_MODEL_INFO);
 
 113         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestInfo(), "RequestInfo");
 
 114         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters(), "RequestParameters");
 
 115         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters().getUserParams(), "UserParams");
 
 117         return bpmnRequestDetails;
 
 120     private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
 
 122             throwExceptionWithWarn(delegateExecution,
 
 123                     "Unable to find the parameter: " + param + " in the execution context");
 
 127     private void throwExceptionWithWarn(DelegateExecution delegateExecution, String exceptionMsg) {
 
 128         logger.warn(exceptionMsg);
 
 129         exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ServiceLevelConstants.ERROR_CODE, exceptionMsg);