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