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 +++++ .../tasks/InputParameterRetrieverTaskTest.java | 128 ----------- .../adapter/vnfm/tasks/TestConstants.java | 28 ++- .../utils/InputParameterRetrieverTaskTest.java | 248 +++++++++++++++++++++ .../vnfm/tasks/utils/InputParameterTest.java | 79 +++++++ .../utils/InputParametersProviderImplTest.java | 179 --------------- .../vnfm/tasks/utils/NullInputParameterTest.java | 62 ++++++ .../utils/SdncInputParametersProviderImplTest.java | 179 +++++++++++++++ .../UserParamInputParametersProviderTest.java | 98 ++++++++ 17 files changed, 1062 insertions(+), 477 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 delete mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTaskTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterRetrieverTaskTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterTest.java delete mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImplTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameterTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProviderImplTest.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProviderTest.java 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; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTaskTest.java deleted file mode 100644 index caae90bfff..0000000000 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/InputParameterRetrieverTaskTest.java +++ /dev/null @@ -1,128 +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; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; -import java.io.Serializable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.onap.so.bpmn.BaseTaskTest; -import org.onap.so.bpmn.common.BuildingBlockExecution; -import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception; -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.entities.ResourceKey; -import org.onap.so.client.exception.BBObjectNotFoundException; - -/** - * @author waqas.ikram@est.tech - */ -public class InputParameterRetrieverTaskTest extends BaseTaskTest { - - private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution(); - - @Mock - private InputParametersProvider inputParametersProvider; - - @Test - public void testGGetInputParameters_inputParameterStoredInExecutionContext() throws BBObjectNotFoundException { - final InputParameterRetrieverTask objUnderTest = - new InputParameterRetrieverTask(inputParametersProvider, extractPojosForBB); - - final InputParameter inputParameter = new InputParameter(Collections.emptyMap(), Collections.emptyList()); - when(inputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))).thenReturn(inputParameter); - when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf()); - objUnderTest.getInputParameters(stubbedxecution); - - final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER); - assertNotNull(actual); - assertTrue(actual instanceof InputParameter); - } - - @Test - public void testGGetInputParameters_ThrowExecption_NullInputParameterStoredInExecutionContext() - throws BBObjectNotFoundException { - final InputParameterRetrieverTask objUnderTest = - new InputParameterRetrieverTask(inputParametersProvider, extractPojosForBB); - - when(inputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))) - .thenThrow(RuntimeException.class); - when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf()); - objUnderTest.getInputParameters(stubbedxecution); - - final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER); - assertNotNull(actual); - assertTrue(actual instanceof NullInputParameter); - } - - - private class StubbedBuildingBlockExecution implements BuildingBlockExecution { - - private final Map execution = new HashMap<>(); - - @Override - public GeneralBuildingBlock getGeneralBuildingBlock() { - return null; - } - - @SuppressWarnings("unchecked") - @Override - public T getVariable(final String key) { - return (T) execution.get(key); - } - - @Override - public T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception { - return null; - } - - @Override - public void setVariable(final String key, final Serializable value) { - execution.put(key, value); - } - - @Override - public Map getLookupMap() { - return Collections.emptyMap(); - } - - @Override - public String getFlowToBeCalled() { - return null; - } - - @Override - public int getCurrentSequence() { - return 0; - } - - } -} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/TestConstants.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/TestConstants.java index 7b63e5f811..686b4f80cc 100644 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/TestConstants.java +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/TestConstants.java @@ -20,9 +20,11 @@ package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks; -import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.DUMMY_URL; +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.HashMap; +import java.util.Map; import java.util.UUID; -import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.VnfmBasicHttpConfigProvider; /** * @author waqas.ikram@est.tech @@ -30,6 +32,7 @@ import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.VnfmBasicHttpConfigPro */ public class TestConstants { + public static final String EXT_VIRTUAL_LINK_ID = "ac1ed33d-8dc1-4800-8ce8-309b99c38eec"; public static final String DUMMY_GENERIC_VND_ID = "5956a99d-9736-11e8-8caf-022ac9304eeb"; public static final String DUMMY_BASIC_AUTH = "Basic 123abc"; public static final String DUMMY_URL = "http://localhost:30406/so/vnfm-adapter/v1/"; @@ -38,6 +41,19 @@ public class TestConstants { public static final String DUMMY_JOB_ID = UUID.randomUUID().toString(); public static final String JOB_STATUS_EXPECTED_URL = DUMMY_URL + "jobs/" + DUMMY_JOB_ID; + public static final String EXT_VIRTUAL_LINK_VALUE = "{\"id\":\"" + EXT_VIRTUAL_LINK_ID + "\"," + + "\"tenant\":{\"cloudOwner\":\"CloudOwner\",\"regionName\":\"RegionOne\"," + + "\"tenantId\":\"80c26954-2536-4bca-9e20-10f8a2c9c2ad\"},\"resourceId\":\"8ef8cd54-75fd-4372-a6dd-2e05ea8fbd9b\"," + + "\"extCps\":[{\"cpdId\":\"f449292f-2f0f-4656-baa3-a18d86bac80f\"," + + "\"cpConfig\":[{\"cpInstanceId\":\"07876709-b66f-465c-99a7-0f4d026197f2\"," + + "\"linkPortId\":null,\"cpProtocolData\":null}]}],\"extLinkPorts\":null}"; + + public static final String ADDITIONAL_PARAMS_VALUE = "{\"image_id\": \"DUMMYVNF\",\"instance_type\": \"m1.small\"," + + "\"ftp_address\": \"ftp://0.0.0.0:2100/\"}"; + + public static final String EXT_VIRTUAL_LINKS_VALUE = "[" + EXT_VIRTUAL_LINK_VALUE + "]"; + + public static VnfmBasicHttpConfigProvider getVnfmBasicHttpConfigProvider() { return getVnfmBasicHttpConfigProvider(DUMMY_URL, DUMMY_BASIC_AUTH); } @@ -49,6 +65,14 @@ public class TestConstants { return vnfmBasicHttpConfigProvider; } + public static Map getUserParamsMap(final String additionalParams, + final String extVirtualLinksValue) { + final Map userParams = new HashMap<>(); + userParams.put(ADDITIONAL_PARAMS, additionalParams); + userParams.put(EXT_VIRTUAL_LINKS, extVirtualLinksValue); + return userParams; + } + private TestConstants() {} } diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterRetrieverTaskTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterRetrieverTaskTest.java new file mode 100644 index 0000000000..9003510be7 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterRetrieverTaskTest.java @@ -0,0 +1,248 @@ +/*- + * ============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.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.ADDITIONAL_PARAMS_VALUE; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.EXT_VIRTUAL_LINKS_VALUE; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.EXT_VIRTUAL_LINK_ID; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.getUserParamsMap; +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.so.bpmn.BaseTaskTest; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception; +import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants; +import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.InputParameterRetrieverTask; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext; +import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters; +import org.onap.so.client.exception.BBObjectNotFoundException; +import org.onap.so.client.sdnc.SDNCClient; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLinkCpConfig; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLinkExtCps; + +/** + * @author waqas.ikram@est.tech + */ +public class InputParameterRetrieverTaskTest extends BaseTaskTest { + + private static final String INSTANCE_TYPE_VALUE_1 = "m1.small"; + + private static final String INSTANCE_TYPE_VALUE_2 = "m1.large"; + + private static final String INSTANCE_TYPE = "instance_type"; + + private static final String RANDOM_EXT_VIRTUAL_LINK_ID = UUID.randomUUID().toString(); + + private static final String CPU_INSTANCE_ID = EXT_VIRTUAL_LINK_ID; + + private static final String FLAVOR_VALUE = "ubuntu"; + + private static final String FLAVOR = "flavor_type"; + + private final StubbedBuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution(); + + @Mock + private InputParametersProvider sdncInputParametersProvider; + + private final InputParametersProvider> userParamsinputParametersProvider = + new UserParamInputParametersProvider(); + + @Mock + private SDNCClient mockedSdncClient; + + @Test + public void testGetInputParameters_inputParameterStoredInExecutionContext() throws BBObjectNotFoundException { + final InputParameterRetrieverTask objUnderTest = new InputParameterRetrieverTask(sdncInputParametersProvider, + userParamsinputParametersProvider, extractPojosForBB); + + + final GeneralBuildingBlock buildingBlock = + getGeneralBuildingBlock(getUserParamsMap(ADDITIONAL_PARAMS_VALUE, null)); + stubbedxecution.setGeneralBuildingBlock(buildingBlock); + + final InputParameter inputParameter = new InputParameter(Collections.emptyMap(), Collections.emptyList()); + when(sdncInputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))).thenReturn(inputParameter); + + when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf()); + objUnderTest.getInputParameters(stubbedxecution); + + final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER); + assertNotNull(actual); + assertTrue(actual instanceof InputParameter); + final InputParameter actualInputParameter = (InputParameter) actual; + final Map actualAdditionalParams = actualInputParameter.getAdditionalParams(); + assertEquals(3, actualAdditionalParams.size()); + + final String actualInstanceType = actualAdditionalParams.get(INSTANCE_TYPE); + assertEquals(INSTANCE_TYPE_VALUE_1, actualInstanceType); + + } + + @Test + public void testGetInputParameters_ThrowExecption_NullInputParameterStoredInExecutionContext() + throws BBObjectNotFoundException { + final InputParameterRetrieverTask objUnderTest = new InputParameterRetrieverTask(sdncInputParametersProvider, + userParamsinputParametersProvider, extractPojosForBB); + + when(sdncInputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))) + .thenThrow(RuntimeException.class); + when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf()); + objUnderTest.getInputParameters(stubbedxecution); + + final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER); + assertNotNull(actual); + assertTrue(actual instanceof NullInputParameter); + } + + @Test + public void testGetInputParameters_SdncAndUserParamInputParameterStoredInExecutionContext() + throws BBObjectNotFoundException { + final InputParameterRetrieverTask objUnderTest = new InputParameterRetrieverTask(sdncInputParametersProvider, + userParamsinputParametersProvider, extractPojosForBB); + + + final GeneralBuildingBlock buildingBlock = + getGeneralBuildingBlock(getUserParamsMap(ADDITIONAL_PARAMS_VALUE, EXT_VIRTUAL_LINKS_VALUE)); + stubbedxecution.setGeneralBuildingBlock(buildingBlock); + + final InputParameter inputParameter = new InputParameter(getAdditionalParams(), getExternalVirtualLink()); + when(sdncInputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))).thenReturn(inputParameter); + + when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf()); + objUnderTest.getInputParameters(stubbedxecution); + + final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER); + assertNotNull(actual); + assertTrue(actual instanceof InputParameter); + final InputParameter actualInputParameter = (InputParameter) actual; + final Map actualAdditionalParams = actualInputParameter.getAdditionalParams(); + assertEquals(4, actualAdditionalParams.size()); + + final String actualInstanceType = actualAdditionalParams.get(INSTANCE_TYPE); + assertEquals(INSTANCE_TYPE_VALUE_1, actualInstanceType); + + assertEquals(FLAVOR_VALUE, actualAdditionalParams.get(FLAVOR)); + final List actualExtVirtualLinks = actualInputParameter.getExtVirtualLinks(); + assertEquals(2, actualExtVirtualLinks.size()); + + final Optional externalVirtualLink0 = actualExtVirtualLinks.stream() + .filter(extVirtualLink -> EXT_VIRTUAL_LINK_ID.equals(extVirtualLink.getId())).findAny(); + assertTrue(externalVirtualLink0.isPresent()); + assertEquals(EXT_VIRTUAL_LINK_ID, externalVirtualLink0.get().getId()); + + final Optional externalVirtualLink1 = actualExtVirtualLinks.stream() + .filter(extVirtualLink -> RANDOM_EXT_VIRTUAL_LINK_ID.equals(extVirtualLink.getId())).findAny(); + assertTrue(externalVirtualLink1.isPresent()); + assertEquals(RANDOM_EXT_VIRTUAL_LINK_ID, externalVirtualLink1.get().getId()); + + + } + + private List getExternalVirtualLink() { + return Arrays.asList( + new ExternalVirtualLink().id(RANDOM_EXT_VIRTUAL_LINK_ID).addExtCpsItem(new ExternalVirtualLinkExtCps() + .addCpConfigItem(new ExternalVirtualLinkCpConfig().cpInstanceId(CPU_INSTANCE_ID)))); + } + + private Map getAdditionalParams() { + final Map additionalParams = new HashMap<>(); + additionalParams.put(FLAVOR, FLAVOR_VALUE); + additionalParams.put(INSTANCE_TYPE, INSTANCE_TYPE_VALUE_2); + return additionalParams; + } + + + private GeneralBuildingBlock getGeneralBuildingBlock(final Map userParams) { + final GeneralBuildingBlock buildingBlock = new GeneralBuildingBlock(); + final RequestContext requestContext = new RequestContext(); + final RequestParameters requestParameters = new RequestParameters(); + requestParameters.setUserParams(Arrays.asList(userParams)); + requestContext.setRequestParameters(requestParameters); + buildingBlock.setRequestContext(requestContext); + return buildingBlock; + + } + + private class StubbedBuildingBlockExecution implements BuildingBlockExecution { + + private final Map execution = new HashMap<>(); + private GeneralBuildingBlock buildingBlock; + + private void setGeneralBuildingBlock(final GeneralBuildingBlock buildingBlock) { + this.buildingBlock = buildingBlock; + } + + @Override + public GeneralBuildingBlock getGeneralBuildingBlock() { + return buildingBlock; + } + + @SuppressWarnings("unchecked") + @Override + public T getVariable(final String key) { + return (T) execution.get(key); + } + + @Override + public T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception { + return null; + } + + @Override + public void setVariable(final String key, final Serializable value) { + execution.put(key, value); + } + + @Override + public Map getLookupMap() { + return Collections.emptyMap(); + } + + @Override + public String getFlowToBeCalled() { + return null; + } + + @Override + public int getCurrentSequence() { + return 0; + } + + } +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterTest.java new file mode 100644 index 0000000000..6dee904fde --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParameterTest.java @@ -0,0 +1,79 @@ +/*- + * ============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.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.Test; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + * + */ +public class InputParameterTest { + @Test + public void test_putAdditionalParams_addsEntryToExistingMap() { + final InputParameter objUnderTest = new InputParameter(); + objUnderTest.setAdditionalParams(getMap("name", "value")); + objUnderTest.putAdditionalParams(getMap("name1", "value1")); + + final Map additionalParams = objUnderTest.getAdditionalParams(); + assertEquals(2, additionalParams.size()); + assertTrue(additionalParams.containsKey("name")); + assertTrue(additionalParams.containsKey("name1")); + + } + + @Test + public void test_addExtVirtualLinks_adddistinctEntriesToExistingList() { + final InputParameter objUnderTest = new InputParameter(); + String firstId = UUID.randomUUID().toString(); + String secondId = UUID.randomUUID().toString(); + objUnderTest.setExtVirtualLinks(getExternalVirtualLinkList(firstId)); + objUnderTest.addExtVirtualLinks(getExternalVirtualLinkList(secondId)); + objUnderTest.addExtVirtualLinks(getExternalVirtualLinkList(secondId)); + objUnderTest.addExtVirtualLinks(getExternalVirtualLinkList(secondId)); + + final List externalVirtualLinks = objUnderTest.getExtVirtualLinks(); + assertEquals(2, externalVirtualLinks.size()); + + } + + private List getExternalVirtualLinkList(final String id) { + final ExternalVirtualLink externalVirtualLink = new ExternalVirtualLink(); + externalVirtualLink.setId(id); + final List list = new ArrayList<>(); + list.add(externalVirtualLink); + return list; + } + + private Map getMap(final String name, final String value) { + final Map map = new HashMap<>(); + map.put(name, value); + return map; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImplTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImplTest.java deleted file mode 100644 index 51e86c1fea..0000000000 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/InputParametersProviderImplTest.java +++ /dev/null @@ -1,179 +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.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; -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 static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.DUMMY_GENERIC_VND_ID; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; -import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf; -import org.onap.so.client.exception.BadResponseException; -import org.onap.so.client.exception.MapperException; -import org.onap.so.client.sdnc.SDNCClient; -import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; - -/** - * @author waqas.ikram@est.tech - */ -@RunWith(MockitoJUnitRunner.class) -public class InputParametersProviderImplTest { - - private static final String BASE_DIR = "src/test/resources/__files/"; - - private static final Path PRE_LOAD_SDNC_RESPONSE = Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponse.json"); - - private static final Path INVALID_PRE_LOAD_SDNC_RESPONSE = - Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidData.json"); - - private static final Path INVALID_ADDITIONAL_AND_EXT_VM_DATA = - Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidAdditionalAndExtVmData.json"); - - - private static final Path INVALID_VNF_PARAMS = - Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidVnfParamsTag.json"); - - - private static final String MODEL_NAME = "MODEL_NAME"; - - private static final String GENERIC_VNF_NAME = "GENERIC_VNF_NAME"; - - private static final String URL = PRELOAD_VNFS_URL + GENERIC_VNF_NAME + FORWARD_SLASH + MODEL_NAME; - - private static final String GENERIC_VNF_TYPE = MODEL_NAME; - - @Mock - private SDNCClient mockedSdncClient; - - @Test - public void testGetInputParameter_ValidResponseFromSdnc_NotEmptyInputParameter() throws Exception { - assertValues(getGenericVnf()); - } - - @Test - public void testGetInputParameter_ValidResponseFromSdncAndVnfType_NotEmptyInputParameter() throws Exception { - assertValues(getGenericVnf(GENERIC_VNF_TYPE)); - } - - @Test - public void testGetInputParameter_ValidResponseFromSdncInvalidData_EmptyInputParameter() throws Exception { - when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_PRE_LOAD_SDNC_RESPONSE)); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); - assertNotNull(actual); - assertTrue(actual.getAdditionalParams().isEmpty()); - assertTrue(actual.getExtVirtualLinks().isEmpty()); - } - - @Test - public void testGetInputParameter_ExceptionThrownFromSdnc_EmptyInputParameter() throws Exception { - when(mockedSdncClient.get(Mockito.eq(URL))).thenThrow(RuntimeException.class); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); - assertNotNull(actual); - assertTrue(actual instanceof NullInputParameter); - assertTrue(actual.getAdditionalParams().isEmpty()); - assertTrue(actual.getExtVirtualLinks().isEmpty()); - } - - @Test - public void testGetInputParameter_InvalidResponseData_EmptyInputParameter() throws Exception { - when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_ADDITIONAL_AND_EXT_VM_DATA)); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); - assertNotNull(actual); - assertTrue(actual.getAdditionalParams().isEmpty()); - assertTrue(actual.getExtVirtualLinks().isEmpty()); - } - - @Test - public void testGetInputParameter_EmptyResponseData_EmptyInputParameter() throws Exception { - when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(""); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); - assertNotNull(actual); - assertTrue(actual instanceof NullInputParameter); - assertTrue(actual.getAdditionalParams().isEmpty()); - assertTrue(actual.getExtVirtualLinks().isEmpty()); - } - - @Test - public void testGetInputParameter_InvalidVnfParamsResponseData_EmptyInputParameter() throws Exception { - when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_VNF_PARAMS)); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); - assertNotNull(actual); - assertTrue(actual.getAdditionalParams().isEmpty()); - assertTrue(actual.getExtVirtualLinks().isEmpty()); - } - - private void assertValues(final GenericVnf genericVnf) throws MapperException, BadResponseException, IOException { - when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(PRE_LOAD_SDNC_RESPONSE)); - final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient); - final InputParameter actual = objUnderTest.getInputParameter(genericVnf); - assertNotNull(actual); - - final Map actualAdditionalParams = actual.getAdditionalParams(); - assertEquals(3, actualAdditionalParams.size()); - - final String actualInstanceType = actualAdditionalParams.get("instance_type"); - assertEquals("m1.small", actualInstanceType); - - final List actualExtVirtualLinks = actual.getExtVirtualLinks(); - assertEquals(1, actualExtVirtualLinks.size()); - - final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0); - assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId()); - } - - private String getReponseAsString(final Path filePath) throws IOException { - return new String(Files.readAllBytes(filePath)); - } - - private GenericVnf getGenericVnf() { - final GenericVnf genericVnf = getGenericVnf(GENERIC_VNF_TYPE); - final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf(); - modelInfoGenericVnf.setModelName(MODEL_NAME); - genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf); - return genericVnf; - } - - private GenericVnf getGenericVnf(final String vnfType) { - final GenericVnf genericVnf = new GenericVnf(); - genericVnf.setVnfId(DUMMY_GENERIC_VND_ID); - genericVnf.setVnfName(GENERIC_VNF_NAME); - genericVnf.setVnfType(vnfType); - return genericVnf; - } -} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameterTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameterTest.java new file mode 100644 index 0000000000..e076df7a9b --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/NullInputParameterTest.java @@ -0,0 +1,62 @@ +/*- + * ============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.junit.Assert.assertTrue; +import java.util.Collections; +import org.junit.Test; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + * + */ +public class NullInputParameterTest { + + @Test(expected = UnsupportedOperationException.class) + public void test_addExtVirtualLinks_throwException() { + NullInputParameter.NULL_INSTANCE.addExtVirtualLinks(Collections.emptyList()); + } + + @Test(expected = UnsupportedOperationException.class) + public void test_setAdditionalParams_throwException() { + NullInputParameter.NULL_INSTANCE.setAdditionalParams(Collections.emptyMap()); + } + + @Test(expected = UnsupportedOperationException.class) + public void test_setExtVirtualLinks_throwException() { + NullInputParameter.NULL_INSTANCE.setExtVirtualLinks(Collections.emptyList()); + } + + @Test(expected = UnsupportedOperationException.class) + public void test_putAdditionalParams_throwException() { + NullInputParameter.NULL_INSTANCE.putAdditionalParams(Collections.emptyMap()); + } + + @Test + public void test_getAdditionalParams_ReturnEmptyCollection() { + assertTrue(NullInputParameter.NULL_INSTANCE.getAdditionalParams().isEmpty()); + } + + @Test + public void test_getExtVirtualLinks_ReturnEmptyCollection() { + assertTrue(NullInputParameter.NULL_INSTANCE.getExtVirtualLinks().isEmpty()); + } + +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProviderImplTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProviderImplTest.java new file mode 100644 index 0000000000..e2e37ac6e4 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/SdncInputParametersProviderImplTest.java @@ -0,0 +1,179 @@ +/*- + * ============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.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +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 static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.DUMMY_GENERIC_VND_ID; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf; +import org.onap.so.client.exception.BadResponseException; +import org.onap.so.client.exception.MapperException; +import org.onap.so.client.sdnc.SDNCClient; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; + +/** + * @author waqas.ikram@est.tech + */ +@RunWith(MockitoJUnitRunner.class) +public class SdncInputParametersProviderImplTest { + + private static final String BASE_DIR = "src/test/resources/__files/"; + + private static final Path PRE_LOAD_SDNC_RESPONSE = Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponse.json"); + + private static final Path INVALID_PRE_LOAD_SDNC_RESPONSE = + Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidData.json"); + + private static final Path INVALID_ADDITIONAL_AND_EXT_VM_DATA = + Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidAdditionalAndExtVmData.json"); + + + private static final Path INVALID_VNF_PARAMS = + Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidVnfParamsTag.json"); + + + private static final String MODEL_NAME = "MODEL_NAME"; + + private static final String GENERIC_VNF_NAME = "GENERIC_VNF_NAME"; + + private static final String URL = PRELOAD_VNFS_URL + GENERIC_VNF_NAME + FORWARD_SLASH + MODEL_NAME; + + private static final String GENERIC_VNF_TYPE = MODEL_NAME; + + @Mock + private SDNCClient mockedSdncClient; + + @Test + public void testGetInputParameter_ValidResponseFromSdnc_NotEmptyInputParameter() throws Exception { + assertValues(getGenericVnf()); + } + + @Test + public void testGetInputParameter_ValidResponseFromSdncAndVnfType_NotEmptyInputParameter() throws Exception { + assertValues(getGenericVnf(GENERIC_VNF_TYPE)); + } + + @Test + public void testGetInputParameter_ValidResponseFromSdncInvalidData_EmptyInputParameter() throws Exception { + when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_PRE_LOAD_SDNC_RESPONSE)); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); + assertNotNull(actual); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + } + + @Test + public void testGetInputParameter_ExceptionThrownFromSdnc_EmptyInputParameter() throws Exception { + when(mockedSdncClient.get(Mockito.eq(URL))).thenThrow(RuntimeException.class); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); + assertNotNull(actual); + assertTrue(actual instanceof NullInputParameter); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + } + + @Test + public void testGetInputParameter_InvalidResponseData_EmptyInputParameter() throws Exception { + when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_ADDITIONAL_AND_EXT_VM_DATA)); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); + assertNotNull(actual); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + } + + @Test + public void testGetInputParameter_EmptyResponseData_EmptyInputParameter() throws Exception { + when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(""); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); + assertNotNull(actual); + assertTrue(actual instanceof NullInputParameter); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + } + + @Test + public void testGetInputParameter_InvalidVnfParamsResponseData_EmptyInputParameter() throws Exception { + when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_VNF_PARAMS)); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf()); + assertNotNull(actual); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + } + + private void assertValues(final GenericVnf genericVnf) throws MapperException, BadResponseException, IOException { + when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(PRE_LOAD_SDNC_RESPONSE)); + final InputParametersProvider objUnderTest = new SdncInputParametersProvider(mockedSdncClient); + final InputParameter actual = objUnderTest.getInputParameter(genericVnf); + assertNotNull(actual); + + final Map actualAdditionalParams = actual.getAdditionalParams(); + assertEquals(3, actualAdditionalParams.size()); + + final String actualInstanceType = actualAdditionalParams.get("instance_type"); + assertEquals("m1.small", actualInstanceType); + + final List actualExtVirtualLinks = actual.getExtVirtualLinks(); + assertEquals(1, actualExtVirtualLinks.size()); + + final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0); + assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId()); + } + + private String getReponseAsString(final Path filePath) throws IOException { + return new String(Files.readAllBytes(filePath)); + } + + private GenericVnf getGenericVnf() { + final GenericVnf genericVnf = getGenericVnf(GENERIC_VNF_TYPE); + final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf(); + modelInfoGenericVnf.setModelName(MODEL_NAME); + genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf); + return genericVnf; + } + + private GenericVnf getGenericVnf(final String vnfType) { + final GenericVnf genericVnf = new GenericVnf(); + genericVnf.setVnfId(DUMMY_GENERIC_VND_ID); + genericVnf.setVnfName(GENERIC_VNF_NAME); + genericVnf.setVnfType(vnfType); + return genericVnf; + } +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProviderTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProviderTest.java new file mode 100644 index 0000000000..e6d4ad056a --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/utils/UserParamInputParametersProviderTest.java @@ -0,0 +1,98 @@ +/*- + * ============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.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.ADDITIONAL_PARAMS_VALUE; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.EXT_VIRTUAL_LINKS_VALUE; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.EXT_VIRTUAL_LINK_VALUE; +import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.getUserParamsMap; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.onap.vnfmadapter.v1.model.ExternalVirtualLink; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + * + */ +public class UserParamInputParametersProviderTest { + + @Test + public void testGetInputParameter_ValidUserParams_NotEmptyInputParameter() throws Exception { + final InputParametersProvider> objUnderTest = new UserParamInputParametersProvider(); + + final InputParameter actual = + objUnderTest.getInputParameter(getUserParamsMap(ADDITIONAL_PARAMS_VALUE, EXT_VIRTUAL_LINKS_VALUE)); + assertNotNull(actual); + + final Map actualAdditionalParams = actual.getAdditionalParams(); + assertEquals(3, actualAdditionalParams.size()); + + final String actualInstanceType = actualAdditionalParams.get("instance_type"); + assertEquals("m1.small", actualInstanceType); + + final List actualExtVirtualLinks = actual.getExtVirtualLinks(); + assertEquals(1, actualExtVirtualLinks.size()); + + final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0); + assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId()); + + } + + @Test + public void testGetInputParameter_EmptyOrNullUserParams_EmptyInputParameter() throws Exception { + final InputParametersProvider> objUnderTest = new UserParamInputParametersProvider(); + + InputParameter actual = objUnderTest.getInputParameter(Collections.emptyMap()); + assertNotNull(actual); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + + actual = objUnderTest.getInputParameter(null); + assertNotNull(actual); + assertTrue(actual instanceof NullInputParameter); + assertTrue(actual.getAdditionalParams().isEmpty()); + assertTrue(actual.getExtVirtualLinks().isEmpty()); + + } + + @Test + public void testGetInputParameter_InValidExtVirtualLinks_NotEmptyInputParameter() throws Exception { + final InputParametersProvider> objUnderTest = new UserParamInputParametersProvider(); + + final InputParameter actual = + objUnderTest.getInputParameter(getUserParamsMap(ADDITIONAL_PARAMS_VALUE, EXT_VIRTUAL_LINK_VALUE)); + assertNotNull(actual); + + final Map actualAdditionalParams = actual.getAdditionalParams(); + assertEquals(3, actualAdditionalParams.size()); + + final String actualInstanceType = actualAdditionalParams.get("instance_type"); + assertEquals("m1.small", actualInstanceType); + + assertTrue(actual.getExtVirtualLinks().isEmpty()); + + } + +} -- cgit 1.2.3-korg