5d9295dc95ee5efd04d091da0f624b83e6327bff
[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.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.*;
44
45 /**
46  * This implementation of {@link JavaDelegate} is used to populate the execution object for Service level upgrade
47  */
48 @Component
49 public class ServiceLevelRequestDispatcher implements JavaDelegate {
50
51     private final Logger logger = LoggerFactory.getLogger(getClass());
52
53     @Autowired
54     private ExceptionBuilder exceptionUtil;
55
56     @Autowired
57     private ObjectMapper mapper;
58
59     @Override
60     public void execute(DelegateExecution delegateExecution) throws Exception {
61         logger.debug("Running execute block for activity id: {}, name: {}", delegateExecution.getCurrentActivityId(),
62                 delegateExecution.getCurrentActivityName());
63
64         RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
65
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();
69
70         final String modelInvariantId = bpmnRequestDetails.getModelInfo().getModelInvariantId();
71         final String modelId = bpmnRequestDetails.getModelInfo().getModelUuid();
72         final String modelVersion = bpmnRequestDetails.getModelInfo().getModelVersion();
73
74         if (ServiceLevelConstants.PNF.equalsIgnoreCase(serviceType)) {
75             getAndSetPnfNameFromServiceInstance(serviceInstanceId, serviceType, globalSubscriberId, delegateExecution);
76         }
77
78         // TODO : handling for vnf
79
80         logger.trace("Completed dispatcher request for ServiceLevelUpgrade.");
81     }
82
83     private void getAndSetPnfNameFromServiceInstance(final String serviceInstanceId, final String serviceType,
84             final String globalSubscriberId, DelegateExecution delegateExecution) {
85
86         AAIRestClientI restClient = new AAIRestClientImpl();
87
88         Optional<ServiceInstance> optionalSi =
89                 restClient.getServiceInstanceById(serviceInstanceId, serviceType, globalSubscriberId);
90
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) {
97                 logger.warn(
98                         "Unable to find the PNF for service instance id: " + serviceInstance.getServiceInstanceId());
99                 return;
100             }
101             delegateExecution.setVariable(ServiceLevelConstants.PNF_NAME_LIST, pnfNameList);
102             delegateExecution.setVariable(ServiceLevelConstants.PNF_SIZE, pnfNameList.size());
103             delegateExecution.setVariable(ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.PNF);
104         }, () -> {
105             throwExceptionWithWarn(delegateExecution, "Unable to find the service instance: " + serviceInstanceId);
106         });
107     }
108
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);
113
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");
118
119         return bpmnRequestDetails;
120     }
121
122     private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
123         if (obj == null) {
124             throwExceptionWithWarn(delegateExecution,
125                     "Unable to find the parameter: " + param + " in the execution context");
126         }
127     }
128
129     private void throwExceptionWithWarn(DelegateExecution delegateExecution, String exceptionMsg) {
130         logger.warn(exceptionMsg);
131         exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ServiceLevelConstants.ERROR_CODE, exceptionMsg);
132     }
133 }