b6be7716f62a8090f8b2758a9ed2daf838b56118
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Ericsson. All rights reserved.
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 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
21
22 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
28 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParametersProvider;
29 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
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
38 /**
39  * This class performs CNF Instantiation
40  * 
41  * @author sagar.shetty@est.tech
42  */
43 @Component
44 public class CnfInstantiateTask {
45     private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
46     private final InputParametersProvider<GenericVnf> sdncInputParametersProvider;
47     private final ExtractPojosForBB extractPojosForBB;
48     private final InputParametersProvider<Map<String, Object>> userParamInputParametersProvider;
49
50     @Autowired
51     public CnfInstantiateTask(final InputParametersProvider<GenericVnf> inputParametersProvider,
52             final InputParametersProvider<Map<String, Object>> userParamInputParametersProvider,
53             final ExtractPojosForBB extractPojosForBB) {
54         this.sdncInputParametersProvider = inputParametersProvider;
55         this.userParamInputParametersProvider = userParamInputParametersProvider;
56         this.extractPojosForBB = extractPojosForBB;
57     }
58
59     public void handleCnfInstatiate(final BuildingBlockExecution execution) {
60         try {
61             LOGGER.debug("Executing handleCnfInstatiate  ...");
62             final InputParameter userParamsInputParameter = getUserParamsInputParameter(execution);
63             LOGGER.debug("Finished executing handleCnfInstatiate ...");
64         } catch (final Exception exception) {
65             LOGGER.error("Unable to get input parameters", exception);
66             execution.setVariable(INPUT_PARAMETER, NullInputParameter.NULL_INSTANCE);
67         }
68     }
69
70     private InputParameter getUserParamsInputParameter(final BuildingBlockExecution execution) {
71         final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
72         if (generalBuildingBlock != null && generalBuildingBlock.getRequestContext() != null
73                 && generalBuildingBlock.getRequestContext().getRequestParameters() != null) {
74             final List<Map<String, Object>> userParams =
75                     generalBuildingBlock.getRequestContext().getRequestParameters().getUserParams();
76             if (userParams != null) {
77                 final Map<String, Object> params = new HashMap<>();
78                 userParams.stream().forEach(obj -> {
79                     params.putAll(obj);
80                 });
81                 LOGGER.info("User params found : {}", params);
82                 if (userParams != null && !userParams.isEmpty()) {
83                     return userParamInputParametersProvider.getInputParameter(params);
84                 }
85             }
86         }
87         LOGGER.warn("No input parameters found in userparams ...");
88         return NullInputParameter.NULL_INSTANCE;
89     }
90 }