From 3b83a490bace15afe62b171242f2315f8805269f Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Wed, 16 Oct 2019 13:59:46 +0100 Subject: Adding preload capability using userParams Change-Id: Id107f2c9c2da643ecb57daaef26dfa2bfd89f8aa Issue-ID: SO-1635 Signed-off-by: waqas.ikram --- .../adapter/vnfm/tasks/Constants.java | 3 + .../vnfm/tasks/InputParameterRetrieverTask.java | 60 +++++++- .../utils/AbstractInputParametersProvider.java | 66 +++++++++ .../adapter/vnfm/tasks/utils/InputParameter.java | 20 ++- .../vnfm/tasks/utils/InputParametersProvider.java | 6 +- .../tasks/utils/InputParametersProviderImpl.java | 157 --------------------- .../vnfm/tasks/utils/NullInputParameter.java | 25 ++++ .../tasks/utils/SdncInputParametersProvider.java | 143 +++++++++++++++++++ .../utils/UserParamInputParametersProvider.java | 58 ++++++++ 9 files changed, 370 insertions(+), 168 deletions(-) create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/AbstractInputParametersProvider.java delete mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImpl.java create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProvider.java create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProvider.java (limited to 'bpmn/so-bpmn-tasks/src/main') diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/Constants.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/Constants.java index c112d200e3..9f85feac07 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/Constants.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/Constants.java @@ -60,5 +60,8 @@ public class Constants { public static final String OPERATION_STATUS_PARAM_NAME = "operationStatus"; + public static final String EXT_VIRTUAL_LINKS = "extVirtualLinks"; + public static final String ADDITIONAL_PARAMS = "additionalParams"; + private Constants() {} } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTask.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTask.java index 661fdb258b..877d065085 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTask.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTask.java @@ -21,12 +21,17 @@ package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks; import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER; import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.onap.so.bpmn.common.BuildingBlockExecution; import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter; import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParametersProvider; import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter; import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.exception.BBObjectNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -42,14 +47,18 @@ public class InputParameterRetrieverTask { private static final Logger LOGGER = LoggerFactory.getLogger(InputParameterRetrieverTask.class); - private final InputParametersProvider inputParametersProvider; + private final InputParametersProvider sdncInputParametersProvider; private final ExtractPojosForBB extractPojosForBB; + private final InputParametersProvider> userParamInputParametersProvider; + @Autowired - public InputParameterRetrieverTask(final InputParametersProvider inputParametersProvider, + public InputParameterRetrieverTask(final InputParametersProvider inputParametersProvider, + final InputParametersProvider> userParamInputParametersProvider, final ExtractPojosForBB extractPojosForBB) { - this.inputParametersProvider = inputParametersProvider; + this.sdncInputParametersProvider = inputParametersProvider; + this.userParamInputParametersProvider = userParamInputParametersProvider; this.extractPojosForBB = extractPojosForBB; } @@ -57,17 +66,56 @@ public class InputParameterRetrieverTask { try { LOGGER.debug("Executing getInputParameters ..."); - final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID); - final InputParameter inputParameter = inputParametersProvider.getInputParameter(vnf); + final InputParameter inputParameter = new InputParameter(); + + final InputParameter userParamsInputParameter = getUserParamsInputParameter(execution); + final InputParameter sdncInputParameter = getSdncInputParameter(execution); + + inputParameter.putAdditionalParams(sdncInputParameter.getAdditionalParams()); + inputParameter.addExtVirtualLinks(sdncInputParameter.getExtVirtualLinks()); + + inputParameter.putAdditionalParams(userParamsInputParameter.getAdditionalParams()); + inputParameter.addExtVirtualLinks(userParamsInputParameter.getExtVirtualLinks()); LOGGER.debug("inputParameter: {}", inputParameter); execution.setVariable(INPUT_PARAMETER, inputParameter); LOGGER.debug("Finished executing getInputParameters ..."); } catch (final Exception exception) { - LOGGER.error("Unable to invoke create and instantiation request", exception); + LOGGER.error("Unable to get input parameters", exception); execution.setVariable(INPUT_PARAMETER, NullInputParameter.NULL_INSTANCE); } } + private InputParameter getSdncInputParameter(final BuildingBlockExecution execution) + throws BBObjectNotFoundException { + final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID); + return sdncInputParametersProvider.getInputParameter(vnf); + } + + private InputParameter getUserParamsInputParameter(final BuildingBlockExecution execution) { + final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock(); + + if (generalBuildingBlock != null && generalBuildingBlock.getRequestContext() != null + && generalBuildingBlock.getRequestContext().getRequestParameters() != null) { + + final List> userParams = + generalBuildingBlock.getRequestContext().getRequestParameters().getUserParams(); + if (userParams != null) { + final Map params = new HashMap<>(); + + userParams.stream().forEach(obj -> { + params.putAll(obj); + }); + LOGGER.info("User params found : {}", params); + if (userParams != null && !userParams.isEmpty()) { + return userParamInputParametersProvider.getInputParameter(params); + } + } + + } + LOGGER.warn("No input parameters found in userparams ..."); + return NullInputParameter.NULL_INSTANCE; + } + } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/AbstractInputParametersProvider.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/AbstractInputParametersProvider.java new file mode 100644 index 0000000000..5cd84a2e11 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/AbstractInputParametersProvider.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; + +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.ADDITIONAL_PARAMS; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.EXT_VIRTUAL_LINKS; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + * + */ +public abstract class AbstractInputParametersProvider implements InputParametersProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInputParametersProvider.class); + + protected Map parseAdditionalParameters(final String additionalParamsString) { + try { + final ObjectMapper mapper = new ObjectMapper(); + final TypeReference> typeRef = new TypeReference>() {}; + return mapper.readValue(additionalParamsString, typeRef); + } catch (final Exception exception) { + LOGGER.error("Unable to parse {} ", ADDITIONAL_PARAMS, exception); + } + return Collections.emptyMap(); + + } + + protected List parseExternalVirtualLinks(final String extVirtualLinksString) { + try { + final ObjectMapper mapper = new ObjectMapper(); + final TypeReference> extVirtualLinksStringTypeRef = + new TypeReference>() {}; + return mapper.readValue(extVirtualLinksString, extVirtualLinksStringTypeRef); + } catch (final Exception exception) { + LOGGER.error("Unable to parse {} ", EXT_VIRTUAL_LINKS, exception); + } + return Collections.emptyList(); + } + + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameter.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameter.java index d01d494c58..fde69c7e91 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameter.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameter.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; /** @@ -39,6 +41,8 @@ public class InputParameter implements Serializable { private List extVirtualLinks = new ArrayList<>(); + public InputParameter() {} + public InputParameter(final Map additionalParams, final List extVirtualLinks) { this.additionalParams = additionalParams; this.extVirtualLinks = extVirtualLinks; @@ -65,6 +69,12 @@ public class InputParameter implements Serializable { this.additionalParams = additionalParams; } + public void putAdditionalParams(final Map additionalParams) { + if (additionalParams != null) { + this.additionalParams.putAll(additionalParams); + } + } + /** * @param extVirtualLinks the extVirtualLinks to set */ @@ -72,9 +82,17 @@ public class InputParameter implements Serializable { this.extVirtualLinks = extVirtualLinks; } + public void addExtVirtualLinks(final List extVirtualLinks) { + if (extVirtualLinks != null) { + this.extVirtualLinks = Stream.concat(this.extVirtualLinks.stream(), extVirtualLinks.stream()).distinct() + .collect(Collectors.toList()); + } + } + @Override public String toString() { - return "InputParameter [additionalParams=" + additionalParams + ", extVirtualLinks=" + extVirtualLinks + "]"; + return this.getClass().getSimpleName() + " [additionalParams=" + additionalParams + ", extVirtualLinks=" + + extVirtualLinks + "]"; } } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProvider.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProvider.java index 55203fb7cc..2d11143fff 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProvider.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProvider.java @@ -19,13 +19,11 @@ */ package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; -import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; - /** * @author waqas.ikram@est.tech */ -public interface InputParametersProvider { +public interface InputParametersProvider { - InputParameter getInputParameter(final GenericVnf genericVnf); + InputParameter getInputParameter(final T object); } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImpl.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImpl.java deleted file mode 100644 index bf4f16b355..0000000000 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImpl.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ -package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; - -import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.FORWARD_SLASH; -import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.PRELOAD_VNFS_URL; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; -import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf; -import org.onap.so.client.sdnc.SDNCClient; -import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.jayway.jsonpath.JsonPath; -import net.minidev.json.JSONArray; - -/** - * This class retrieve pre-load data from SDNC using
- * GET /config/VNF-API:preload-vnfs/vnf-preload-list/{vnf-name}/{vnf-type} - * - * @author waqas.ikram@est.tech - */ -@Service -public class InputParametersProviderImpl implements InputParametersProvider { - - private static final Logger LOGGER = LoggerFactory.getLogger(InputParametersProviderImpl.class); - - private static final String EXT_VIRTUAL_LINKS = "extVirtualLinks"; - private static final String ADDITIONAL_PARAMS = "additionalParams"; - private static final String VNF_PARAMETERS_PATH = "$..vnf-parameters"; - - private final SDNCClient sdncClient; - - @Autowired - public InputParametersProviderImpl(final SDNCClient sdncClient) { - this.sdncClient = sdncClient; - } - - @Override - public InputParameter getInputParameter(final GenericVnf genericVnf) { - final String vnfName = genericVnf.getVnfName(); - final String vnfType = getVnfType(genericVnf); - final String url = getPreloadVnfsUrl(vnfName, vnfType); - - try { - LOGGER.debug("Will query sdnc for input parameters using url: {}", url); - final String jsonResponse = sdncClient.get(url); - - final JSONArray vnfParametersArray = JsonPath.read(jsonResponse, VNF_PARAMETERS_PATH); - if (vnfParametersArray != null) { - for (int index = 0; index < vnfParametersArray.size(); index++) { - final Object vnfParametersObject = vnfParametersArray.get(index); - if (vnfParametersObject instanceof JSONArray) { - final JSONArray vnfParameters = (JSONArray) vnfParametersObject; - final Map vnfParametersMap = getVnfParameterMap(vnfParameters); - return new InputParameter(getAdditionalParameters(vnfParametersMap), - getExtVirtualLinks(vnfParametersMap)); - } - } - } - } catch (final Exception exception) { - LOGGER.error("Unable to retrieve/parse input parameters using URL: {} ", url, exception); - } - LOGGER.warn("No input parameters found ..."); - return NullInputParameter.NULL_INSTANCE; - - } - - private List getExtVirtualLinks(final Map vnfParametersMap) - throws JsonParseException, IOException { - try { - final String extVirtualLinksString = vnfParametersMap.get(EXT_VIRTUAL_LINKS); - - if (extVirtualLinksString != null && !extVirtualLinksString.isEmpty()) { - final ObjectMapper mapper = new ObjectMapper(); - final TypeReference> extVirtualLinksStringTypeRef = - new TypeReference>() {}; - - return mapper.readValue(extVirtualLinksString, extVirtualLinksStringTypeRef); - } - } catch (final Exception exception) { - LOGGER.error("Unable to parse {} ", EXT_VIRTUAL_LINKS, exception); - } - return Collections.emptyList(); - } - - private Map getAdditionalParameters(final Map vnfParametersMap) - throws JsonParseException, IOException { - try { - final String additionalParamsString = vnfParametersMap.get(ADDITIONAL_PARAMS); - if (additionalParamsString != null && !additionalParamsString.isEmpty()) { - final ObjectMapper mapper = new ObjectMapper(); - final TypeReference> typeRef = new TypeReference>() {}; - return mapper.readValue(additionalParamsString, typeRef); - } - } catch (final Exception exception) { - LOGGER.error("Unable to parse {} ", ADDITIONAL_PARAMS, exception); - } - return Collections.emptyMap(); - } - - private Map getVnfParameterMap(final JSONArray array) { - try { - if (array != null) { - final ObjectMapper mapper = new ObjectMapper(); - final VnfParameter[] readValue = mapper.readValue(array.toJSONString(), VnfParameter[].class); - LOGGER.debug("Vnf parameters: {}", Arrays.asList(readValue)); - return Arrays.asList(readValue).stream() - .filter(vnfParam -> vnfParam.getName() != null && vnfParam.getValue() != null) - .collect(Collectors.toMap(VnfParameter::getName, VnfParameter::getValue)); - } - } catch (final IOException exception) { - LOGGER.error("Unable to parse vnf parameters : {}", array, exception); - } - return Collections.emptyMap(); - } - - private String getPreloadVnfsUrl(final String vnfName, final String vnfType) { - return PRELOAD_VNFS_URL + vnfName + FORWARD_SLASH + vnfType; - } - - private String getVnfType(final GenericVnf genericVnf) { - final ModelInfoGenericVnf modelInfoGenericVnf = genericVnf.getModelInfoGenericVnf(); - if (modelInfoGenericVnf != null && modelInfoGenericVnf.getModelName() != null) { - return modelInfoGenericVnf.getModelName(); - } - return genericVnf.getVnfType(); - } - -} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameter.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameter.java index fb877ac55d..2eed224eb7 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameter.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameter.java @@ -20,12 +20,18 @@ package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; /** * @author waqas.ikram@est.tech */ public class NullInputParameter extends InputParameter { + private static final String ERROR = + "method should not be called for null object " + NullInputParameter.class.getSimpleName(); + private static final long serialVersionUID = -7261286746726871696L; public static final NullInputParameter NULL_INSTANCE = new NullInputParameter(); @@ -34,4 +40,23 @@ public class NullInputParameter extends InputParameter { super(Collections.emptyMap(), Collections.emptyList()); } + @Override + public void setAdditionalParams(final Map additionalParams) { + throw new UnsupportedOperationException("setAdditionalParams() " + ERROR); + } + + @Override + public void setExtVirtualLinks(final List extVirtualLinks) { + throw new UnsupportedOperationException("setExtVirtualLinks() " + ERROR); + } + + @Override + public void addExtVirtualLinks(final List extVirtualLinks) { + throw new UnsupportedOperationException("addExtVirtualLinks() " + ERROR); + } + + @Override + public void putAdditionalParams(final Map additionalParams) { + throw new UnsupportedOperationException("putAdditionalParams() " + ERROR); + } } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProvider.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProvider.java new file mode 100644 index 0000000000..7d81f4d5c2 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProvider.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; + +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.ADDITIONAL_PARAMS; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.EXT_VIRTUAL_LINKS; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.FORWARD_SLASH; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.PRELOAD_VNFS_URL; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf; +import org.onap.so.client.sdnc.SDNCClient; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.jsonpath.JsonPath; +import net.minidev.json.JSONArray; + +/** + * This class retrieve pre-load data from SDNC using
+ * GET /config/VNF-API:preload-vnfs/vnf-preload-list/{vnf-name}/{vnf-type} + * + * @author waqas.ikram@est.tech + */ +@Service +public class SdncInputParametersProvider extends AbstractInputParametersProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(SdncInputParametersProvider.class); + + private static final String VNF_PARAMETERS_PATH = "$..vnf-parameters"; + + private final SDNCClient sdncClient; + + @Autowired + public SdncInputParametersProvider(final SDNCClient sdncClient) { + this.sdncClient = sdncClient; + } + + @Override + public InputParameter getInputParameter(final GenericVnf genericVnf) { + final String vnfName = genericVnf.getVnfName(); + final String vnfType = getVnfType(genericVnf); + final String url = getPreloadVnfsUrl(vnfName, vnfType); + + try { + LOGGER.debug("Will query sdnc for input parameters using url: {}", url); + final String jsonResponse = sdncClient.get(url); + + final JSONArray vnfParametersArray = JsonPath.read(jsonResponse, VNF_PARAMETERS_PATH); + if (vnfParametersArray != null) { + for (int index = 0; index < vnfParametersArray.size(); index++) { + final Object vnfParametersObject = vnfParametersArray.get(index); + if (vnfParametersObject instanceof JSONArray) { + final JSONArray vnfParameters = (JSONArray) vnfParametersObject; + final Map vnfParametersMap = getVnfParameterMap(vnfParameters); + final Map additionalParameters = getAdditionalParameters(vnfParametersMap); + final List extVirtualLinks = getExtVirtualLinks(vnfParametersMap); + final InputParameter inputParameter = new InputParameter(additionalParameters, extVirtualLinks); + LOGGER.info("InputParameter found in sdnc response : {}", inputParameter); + return inputParameter; + } + } + } + } catch (final Exception exception) { + LOGGER.error("Unable to retrieve/parse input parameters using URL: {} ", url, exception); + } + LOGGER.warn("No input parameters found ..."); + return NullInputParameter.NULL_INSTANCE; + + } + + private List getExtVirtualLinks(final Map vnfParametersMap) { + final String extVirtualLinksString = vnfParametersMap.get(EXT_VIRTUAL_LINKS); + + if (extVirtualLinksString != null && !extVirtualLinksString.isEmpty()) { + return parseExternalVirtualLinks(extVirtualLinksString); + } + return Collections.emptyList(); + } + + private Map getAdditionalParameters(final Map vnfParametersMap) { + final String additionalParamsString = vnfParametersMap.get(ADDITIONAL_PARAMS); + if (additionalParamsString != null && !additionalParamsString.isEmpty()) { + return parseAdditionalParameters(additionalParamsString); + } + return Collections.emptyMap(); + } + + + private Map getVnfParameterMap(final JSONArray array) { + try { + if (array != null) { + final ObjectMapper mapper = new ObjectMapper(); + final VnfParameter[] readValue = mapper.readValue(array.toJSONString(), VnfParameter[].class); + LOGGER.debug("Vnf parameters: {}", Arrays.asList(readValue)); + return Arrays.asList(readValue).stream() + .filter(vnfParam -> vnfParam.getName() != null && vnfParam.getValue() != null) + .collect(Collectors.toMap(VnfParameter::getName, VnfParameter::getValue)); + } + } catch (final IOException exception) { + LOGGER.error("Unable to parse vnf parameters : {}", array, exception); + } + return Collections.emptyMap(); + } + + private String getPreloadVnfsUrl(final String vnfName, final String vnfType) { + return PRELOAD_VNFS_URL + vnfName + FORWARD_SLASH + vnfType; + } + + private String getVnfType(final GenericVnf genericVnf) { + final ModelInfoGenericVnf modelInfoGenericVnf = genericVnf.getModelInfoGenericVnf(); + if (modelInfoGenericVnf != null && modelInfoGenericVnf.getModelName() != null) { + return modelInfoGenericVnf.getModelName(); + } + return genericVnf.getVnfType(); + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProvider.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProvider.java new file mode 100644 index 0000000000..ac939d04ec --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProvider.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils; + +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.ADDITIONAL_PARAMS; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.EXT_VIRTUAL_LINKS; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + * + */ +@Service +public class UserParamInputParametersProvider extends AbstractInputParametersProvider> { + private static final Logger LOGGER = LoggerFactory.getLogger(UserParamInputParametersProvider.class); + + @Override + public InputParameter getInputParameter(final Map userParams) { + if (userParams != null) { + final InputParameter inputParameter = new InputParameter(); + final Object additionalParams = userParams.get(ADDITIONAL_PARAMS); + + if (additionalParams != null && additionalParams instanceof String) { + inputParameter.setAdditionalParams(parseAdditionalParameters(additionalParams.toString())); + } + + final Object extVirtualLinks = userParams.get(EXT_VIRTUAL_LINKS); + if (extVirtualLinks != null && extVirtualLinks instanceof String) { + inputParameter.setExtVirtualLinks(parseExternalVirtualLinks(extVirtualLinks.toString())); + } + LOGGER.info("InputParameter found in userParams : {}", inputParameter); + return inputParameter; + } + LOGGER.warn("No input parameters found ..."); + return NullInputParameter.NULL_INSTANCE; + } + +} -- cgit 1.2.3-korg