bc2d40e11bca40c749f2043a38b20dea327bc28c
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.service.level.impl;
22
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.List;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
42 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_MODEL_INFO;
43
44 /**
45  * This implementation of {@link JavaDelegate} is used to populate the execution object for Service level upgrade
46  */
47 @Component
48 public class ServiceLevelRequestDispatcher implements JavaDelegate {
49
50     private final Logger logger = LoggerFactory.getLogger(getClass());
51
52     @Autowired
53     private ExceptionBuilder exceptionUtil;
54
55     @Autowired
56     private ObjectMapper mapper;
57
58     @Override
59     public void execute(DelegateExecution delegateExecution) throws Exception {
60         logger.debug("Running execute block for activity id: {}, name: {}", delegateExecution.getCurrentActivityId(),
61                 delegateExecution.getCurrentActivityName());
62
63         RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
64
65         final String serviceInstanceId = String.valueOf(delegateExecution.getVariable(SERVICE_INSTANCE_ID));
66         final String serviceType = bpmnRequestDetails.getRequestParameters().getSubscriptionServiceType();
67         final String globalSubscriberId = bpmnRequestDetails.getSubscriberInfo().getGlobalSubscriberId();
68
69         final String modelInvariantId = bpmnRequestDetails.getModelInfo().getModelInvariantId();
70         final String modelId = bpmnRequestDetails.getModelInfo().getModelUuid();
71         final String modelVersion = bpmnRequestDetails.getModelInfo().getModelVersion();
72
73         if (ServiceLevelConstants.PNF.equalsIgnoreCase(serviceType)) {
74             getAndSetPnfNameFromServiceInstance(serviceInstanceId, serviceType, globalSubscriberId, delegateExecution);
75         }
76
77         // TODO : handling for vnf
78
79         logger.trace("Completed dispatcher request for ServiceLevelUpgrade.");
80     }
81
82     private void getAndSetPnfNameFromServiceInstance(final String serviceInstanceId, final String serviceType,
83             final String globalSubscriberId, DelegateExecution delegateExecution) {
84
85         AAIRestClientI restClient = new AAIRestClientImpl();
86
87         Optional<ServiceInstance> optionalSi =
88                 restClient.getServiceInstanceById(serviceInstanceId, serviceType, globalSubscriberId);
89
90         optionalSi.ifPresentOrElse(serviceInstance -> {
91             final List<String> pnfNameList = serviceInstance.getRelationshipList().getRelationship().stream()
92                     .filter(x -> x.getRelatedTo().contains("pnf")).flatMap(x -> x.getRelationshipData().stream())
93                     .filter(data -> data.getRelationshipKey().contains("pnf.pnf-name"))
94                     .map(x -> x.getRelationshipValue()).collect(Collectors.toList());
95             if (pnfNameList == null || pnfNameList.size() == 0) {
96                 logger.warn(
97                         "Unable to find the PNF for service instance id: " + serviceInstance.getServiceInstanceId());
98                 return;
99             }
100             delegateExecution.setVariable(ServiceLevelConstants.PNF_NAME_LIST, pnfNameList);
101             delegateExecution.setVariable(ServiceLevelConstants.PNF_SIZE, pnfNameList.size());
102             delegateExecution.setVariable(ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.PNF);
103         }, () -> {
104             throwExceptionWithWarn(delegateExecution, "Unable to find the service instance: " + serviceInstanceId);
105         });
106     }
107
108     private RequestDetails requestVerification(DelegateExecution delegateExecution) throws IOException {
109         RequestDetails bpmnRequestDetails = mapper.readValue(JsonUtils.getJsonValue(
110                 String.valueOf(delegateExecution.getVariable(ServiceLevelConstants.BPMN_REQUEST)), "requestDetails"),
111                 RequestDetails.class);
112
113         throwIfNull(delegateExecution, bpmnRequestDetails.getModelInfo(), SERVICE_MODEL_INFO);
114         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestInfo(), "RequestInfo");
115         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters(), "RequestParameters");
116         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters().getUserParams(), "UserParams");
117
118         return bpmnRequestDetails;
119     }
120
121     private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
122         if (obj == null) {
123             throwExceptionWithWarn(delegateExecution,
124                     "Unable to find the parameter: " + param + " in the execution context");
125         }
126     }
127
128     private void throwExceptionWithWarn(DelegateExecution delegateExecution, String exceptionMsg) {
129         logger.warn(exceptionMsg);
130         exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ServiceLevelConstants.ERROR_CODE, exceptionMsg);
131     }
132 }