Adding preload capability using userParams
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / InputParameterRetrieverTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
29 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParametersProvider;
30 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
34 import org.onap.so.client.exception.BBObjectNotFoundException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * This class retrieve input parameters
42  * 
43  * @author waqas.ikram@est.tech
44  */
45 @Component
46 public class InputParameterRetrieverTask {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(InputParameterRetrieverTask.class);
49
50     private final InputParametersProvider<GenericVnf> sdncInputParametersProvider;
51
52     private final ExtractPojosForBB extractPojosForBB;
53
54     private final InputParametersProvider<Map<String, Object>> userParamInputParametersProvider;
55
56     @Autowired
57     public InputParameterRetrieverTask(final InputParametersProvider<GenericVnf> inputParametersProvider,
58             final InputParametersProvider<Map<String, Object>> userParamInputParametersProvider,
59             final ExtractPojosForBB extractPojosForBB) {
60         this.sdncInputParametersProvider = inputParametersProvider;
61         this.userParamInputParametersProvider = userParamInputParametersProvider;
62         this.extractPojosForBB = extractPojosForBB;
63     }
64
65     public void getInputParameters(final BuildingBlockExecution execution) {
66         try {
67             LOGGER.debug("Executing getInputParameters  ...");
68
69             final InputParameter inputParameter = new InputParameter();
70
71             final InputParameter userParamsInputParameter = getUserParamsInputParameter(execution);
72             final InputParameter sdncInputParameter = getSdncInputParameter(execution);
73
74             inputParameter.putAdditionalParams(sdncInputParameter.getAdditionalParams());
75             inputParameter.addExtVirtualLinks(sdncInputParameter.getExtVirtualLinks());
76
77             inputParameter.putAdditionalParams(userParamsInputParameter.getAdditionalParams());
78             inputParameter.addExtVirtualLinks(userParamsInputParameter.getExtVirtualLinks());
79
80             LOGGER.debug("inputParameter: {}", inputParameter);
81             execution.setVariable(INPUT_PARAMETER, inputParameter);
82
83             LOGGER.debug("Finished executing getInputParameters ...");
84         } catch (final Exception exception) {
85             LOGGER.error("Unable to get input parameters", exception);
86             execution.setVariable(INPUT_PARAMETER, NullInputParameter.NULL_INSTANCE);
87         }
88     }
89
90     private InputParameter getSdncInputParameter(final BuildingBlockExecution execution)
91             throws BBObjectNotFoundException {
92         final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
93         return sdncInputParametersProvider.getInputParameter(vnf);
94     }
95
96     private InputParameter getUserParamsInputParameter(final BuildingBlockExecution execution) {
97         final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
98
99         if (generalBuildingBlock != null && generalBuildingBlock.getRequestContext() != null
100                 && generalBuildingBlock.getRequestContext().getRequestParameters() != null) {
101
102             final List<Map<String, Object>> userParams =
103                     generalBuildingBlock.getRequestContext().getRequestParameters().getUserParams();
104             if (userParams != null) {
105                 final Map<String, Object> params = new HashMap<>();
106
107                 userParams.stream().forEach(obj -> {
108                     params.putAll(obj);
109                 });
110                 LOGGER.info("User params found : {}", params);
111                 if (userParams != null && !userParams.isEmpty()) {
112                     return userParamInputParametersProvider.getInputParameter(params);
113                 }
114             }
115
116         }
117         LOGGER.warn("No input parameters found in userparams ...");
118         return NullInputParameter.NULL_INSTANCE;
119     }
120
121 }