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.Relationship;
27 import org.onap.aai.domain.yang.ServiceInstance;
28 import org.onap.aaiclient.client.aai.AAIRestClientI;
29 import org.onap.aaiclient.client.aai.AAIRestClientImpl;
30 import org.onap.so.bpmn.core.json.JsonUtils;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.onap.so.serviceinstancebeans.RequestDetails;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Optional;
41 import java.util.stream.Collector;
42 import java.util.stream.Collectors;
43 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*;
46 * This implementation of {@link JavaDelegate} is used to populate the execution object for Service level upgrade
49 public class ServiceLevelRequestDispatcher implements JavaDelegate {
51 private final Logger logger = LoggerFactory.getLogger(getClass());
54 private ExceptionBuilder exceptionUtil;
57 private ObjectMapper mapper;
60 public void execute(DelegateExecution delegateExecution) throws Exception {
61 logger.debug("Running execute block for activity id: {}, name: {}", delegateExecution.getCurrentActivityId(),
62 delegateExecution.getCurrentActivityName());
64 RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
66 final String serviceInstanceId = String.valueOf(delegateExecution.getVariable(SERVICE_INSTANCE_ID));
67 final String serviceType = bpmnRequestDetails.getRequestParameters().getSubscriptionServiceType();
68 final String globalSubscriberId = bpmnRequestDetails.getSubscriberInfo().getGlobalSubscriberId();
70 final String modelInvariantId = bpmnRequestDetails.getModelInfo().getModelInvariantId();
71 final String modelId = bpmnRequestDetails.getModelInfo().getModelUuid();
72 final String modelVersion = bpmnRequestDetails.getModelInfo().getModelVersion();
74 if (ServiceLevelConstants.PNF.equalsIgnoreCase(serviceType)) {
75 getAndSetPnfNameFromServiceInstance(serviceInstanceId, serviceType, globalSubscriberId, delegateExecution);
78 // TODO : handling for vnf
80 logger.trace("Completed dispatcher request for ServiceLevelUpgrade.");
83 private void getAndSetPnfNameFromServiceInstance(final String serviceInstanceId, final String serviceType,
84 final String globalSubscriberId, DelegateExecution delegateExecution) {
86 AAIRestClientI restClient = new AAIRestClientImpl();
88 Optional<ServiceInstance> optionalSi =
89 restClient.getServiceInstanceById(serviceInstanceId, serviceType, globalSubscriberId);
91 optionalSi.ifPresentOrElse(serviceInstance -> {
92 final List<String> pnfNameList = serviceInstance.getRelationshipList().getRelationship().stream()
93 .filter(x -> x.getRelatedTo().contains("pnf")).flatMap(x -> x.getRelationshipData().stream())
94 .filter(data -> data.getRelationshipKey().contains("pnf.pnf-name"))
95 .map(x -> x.getRelationshipValue()).collect(Collectors.toList());
96 if (pnfNameList == null || pnfNameList.size() == 0) {
98 "Unable to find the PNF for service instance id: " + serviceInstance.getServiceInstanceId());
101 delegateExecution.setVariable(ServiceLevelConstants.PNF_NAME_LIST, pnfNameList);
102 delegateExecution.setVariable(ServiceLevelConstants.PNF_SIZE, pnfNameList.size());
103 delegateExecution.setVariable(ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.PNF);
105 throwExceptionWithWarn(delegateExecution, "Unable to find the service instance: " + serviceInstanceId);
109 private RequestDetails requestVerification(DelegateExecution delegateExecution) throws IOException {
110 RequestDetails bpmnRequestDetails = mapper.readValue(JsonUtils.getJsonValue(
111 String.valueOf(delegateExecution.getVariable(ServiceLevelConstants.BPMN_REQUEST)), "requestDetails"),
112 RequestDetails.class);
114 throwIfNull(delegateExecution, bpmnRequestDetails.getModelInfo(), SERVICE_MODEL_INFO);
115 throwIfNull(delegateExecution, bpmnRequestDetails.getRequestInfo(), "RequestInfo");
116 throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters(), "RequestParameters");
117 throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters().getUserParams(), "UserParams");
119 return bpmnRequestDetails;
122 private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
124 throwExceptionWithWarn(delegateExecution,
125 "Unable to find the parameter: " + param + " in the execution context");
129 private void throwExceptionWithWarn(DelegateExecution delegateExecution, String exceptionMsg) {
130 logger.warn(exceptionMsg);
131 exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ServiceLevelConstants.ERROR_CODE, exceptionMsg);