From 280a0f6c73ab1c1aed9ee87fbd053d01d3838d70 Mon Sep 17 00:00:00 2001 From: "Singal, Kapil (ks220y)" Date: Wed, 12 Sep 2018 16:26:03 -0400 Subject: Controller Blueprints Nitrogen to Oxygen Migration Changing package structure from org.onap.ccsdk.config to org.onap.ccsdk.features Change-Id: I7d52b498f11ed892d547220cc4354298b6182c77 Issue-ID: CCSDK-560 Signed-off-by: Singal, Kapil (ks220y) --- .../plugin/generator-provider/pom.xml | 8 +- .../config/generator/ConfigGeneratorConstant.java | 49 ----- .../config/generator/data/ConfigGeneratorInfo.java | 102 ---------- .../generator/service/ConfigGeneratorNode.java | 140 ------------- .../generator/service/ConfigGeneratorService.java | 33 --- .../service/ConfigGeneratorServiceImpl.java | 143 ------------- .../generator/tool/CustomJsonNodeFactory.java | 30 --- .../config/generator/tool/CustomTextNode.java | 42 ---- .../ccsdk/config/generator/tool/EscapeUtils.java | 82 -------- .../generator/ConfigGeneratorConstant.java | 49 +++++ .../generator/data/ConfigGeneratorInfo.java | 102 ++++++++++ .../generator/service/ConfigGeneratorNode.java | 140 +++++++++++++ .../generator/service/ConfigGeneratorService.java | 33 +++ .../service/ConfigGeneratorServiceImpl.java | 143 +++++++++++++ .../generator/tool/CustomJsonNodeFactory.java | 30 +++ .../features/generator/tool/CustomTextNode.java | 42 ++++ .../ccsdk/features/generator/tool/EscapeUtils.java | 82 ++++++++ .../org/opendaylight/blueprint/impl-blueprint.xml | 12 +- .../generator/service/ConfigGeneratorNodeTest.java | 222 -------------------- .../config/generator/tool/EscapeUtilsTest.java | 51 ----- .../generator/service/ConfigGeneratorNodeTest.java | 223 +++++++++++++++++++++ .../features/generator/tool/EscapeUtilsTest.java | 52 +++++ .../service_templates/generate_configuration.json | 6 +- 23 files changed, 909 insertions(+), 907 deletions(-) delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/ConfigGeneratorConstant.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/data/ConfigGeneratorInfo.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNode.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorService.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorServiceImpl.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomJsonNodeFactory.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomTextNode.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/EscapeUtils.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/ConfigGeneratorConstant.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/data/ConfigGeneratorInfo.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNode.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorService.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorServiceImpl.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomJsonNodeFactory.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomTextNode.java create mode 100644 blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/EscapeUtils.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNodeTest.java delete mode 100644 blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/tool/EscapeUtilsTest.java create mode 100644 blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNodeTest.java create mode 100644 blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/tool/EscapeUtilsTest.java (limited to 'blueprints-processor/plugin/generator-provider') diff --git a/blueprints-processor/plugin/generator-provider/pom.xml b/blueprints-processor/plugin/generator-provider/pom.xml index 006e378a2..e8a2ab026 100644 --- a/blueprints-processor/plugin/generator-provider/pom.xml +++ b/blueprints-processor/plugin/generator-provider/pom.xml @@ -108,10 +108,10 @@ ${project.artifactId} ${project.groupId}.${project.artifactId} - org.onap.ccsdk.config.generator, - org.onap.ccsdk.config.generator.data, - org.onap.ccsdk.config.generator.service, - org.onap.ccsdk.config.generator.tool + org.onap.ccsdk.features.generator, + org.onap.ccsdk.features.generator.data, + org.onap.ccsdk.features.generator.service, + org.onap.ccsdk.features.generator.tool diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/ConfigGeneratorConstant.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/ConfigGeneratorConstant.java deleted file mode 100644 index b859b69cd..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/ConfigGeneratorConstant.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator; - -public class ConfigGeneratorConstant { - - private ConfigGeneratorConstant() { - - } - - public static final String STRING_ENCODING = "utf-8"; - public static final String Y = "Y"; - public static final String N = "N"; - public static final String DATA_TYPE_TEXT = "TEXT"; - public static final String DATA_TYPE_JSON = "JSON"; - public static final String DATA_TYPE_XML = "XML"; - public static final String DATA_TYPE_SQL = "SQL"; - - public static final String INPUT_PARAM_REQUEST_ID = "request-id"; - public static final String INPUT_PARAM_RESOURCE_ID = "resource-id"; - public static final String INPUT_PARAM_RESOURCE_TYPE = "resource-type"; - public static final String INPUT_PARAM_ACTION_NAME = "action-name"; - public static final String INPUT_PARAM_TEMPLATE_NAME = "template-name"; - public static final String INPUT_PARAM_TEMPLATE_CONTENT = "template-content"; - public static final String INPUT_PARAM_TEMPLATE_DATA = "template-data"; - - public static final String OUTPUT_PARAM_GENERATED_CONFIG = "generated-config"; - public static final String OUTPUT_PARAM_MASK_INFO = "mask-info"; - public static final String OUTPUT_PARAM_STATUS = "status"; - public static final String OUTPUT_PARAM_ERROR_MESSAGE = "error-message"; - public static final String OUTPUT_STATUS_SUCCESS = "success"; - public static final String OUTPUT_STATUS_FAILURE = "failure"; - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/data/ConfigGeneratorInfo.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/data/ConfigGeneratorInfo.java deleted file mode 100644 index ec461549a..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/data/ConfigGeneratorInfo.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.data; - -public class ConfigGeneratorInfo { - - private String requestId; - private String resourceId; - private String resourceType; - private String templateName; - private String recipeName; - private String resourceData; - private String templateContent; - private String mashedData; - - public String getRequestId() { - return requestId; - } - - public void setRequestId(String requestId) { - this.requestId = requestId; - } - - public String getResourceId() { - return resourceId; - } - - public void setResourceId(String resourceId) { - this.resourceId = resourceId; - } - - public String getResourceType() { - return resourceType; - } - - public void setResourceType(String resourceType) { - this.resourceType = resourceType; - } - - public String getTemplateName() { - return templateName; - } - - public void setTemplateName(String templateName) { - this.templateName = templateName; - } - - public String getRecipeName() { - return recipeName; - } - - public void setRecipeName(String recipeName) { - this.recipeName = recipeName; - } - - public String getResourceData() { - return resourceData; - } - - public void setResourceData(String resourceData) { - this.resourceData = resourceData; - } - - public String getTemplateContent() { - return templateContent; - } - - public void setTemplateContent(String templateContent) { - this.templateContent = templateContent; - } - - public String getMashedData() { - return mashedData; - } - - public void setMashedData(String mashedData) { - this.mashedData = mashedData; - } - - @Override - public String toString() { - return "ConfigGeneratorInfo [requestId=" + requestId + ", resourceId=" + resourceId + ", resourceType=" - + resourceType + ", templateName=" + templateName + ", recipeName=" + recipeName + ", resourceData=" - + resourceData + ", templateContent=" + templateContent + ", mashedData=" + mashedData + "]"; - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNode.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNode.java deleted file mode 100644 index 136ba7f25..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNode.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.service; - -import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; -import org.onap.ccsdk.config.generator.ConfigGeneratorConstant; -import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo; -import org.onap.ccsdk.config.model.ConfigModelConstant; -import org.onap.ccsdk.config.model.service.ComponentNode; -import org.onap.ccsdk.config.model.service.ConfigModelService; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -public class ConfigGeneratorNode implements ComponentNode { - - private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNode.class); - - private ConfigModelService configModelService; - private ConfigResourceService configResourceService; - private ConfigGeneratorService configGeneratorService; - - public ConfigGeneratorNode(ConfigResourceService configResourceService, ConfigModelService configModelService) { - this.configResourceService = configResourceService; - this.configModelService = configModelService; - this.configGeneratorService = new ConfigGeneratorServiceImpl(this.configResourceService); - } - - @Override - public Boolean preCondition(Map inParams, SvcLogicContext ctx, Map componentContext) - throws SvcLogicException { - logger.trace("Received generateConfiguration preCondition call with params : ({})", inParams); - return Boolean.TRUE; - } - - @Override - public void preProcess(Map inParams, SvcLogicContext ctx, Map componentContext) - throws SvcLogicException { - logger.trace("Received generateConfiguration preProcess call with params : ({})", inParams); - } - - @Override - public void process(Map inParams, SvcLogicContext ctx) throws SvcLogicException { - logger.trace("Received generateConfiguration process call with params : ({})", inParams); - } - - @SuppressWarnings("squid:S3776") - @Override - public void process(Map inParams, SvcLogicContext ctx, Map componentContext) - throws SvcLogicException { - logger.trace("Received generateConfiguration process with params : ({})", inParams); - String prifix = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR); - try { - prifix = StringUtils.isNotBlank(prifix) ? (prifix + ".") : ""; - - String templateContent = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT); - String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA); - String requestId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_REQUEST_ID); - String resourceId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_ID); - String resourceType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_TYPE); - String recipeName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ACTION_NAME); - String templateName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_NAME); - - ConfigGeneratorInfo configGeneratorInfo = null; - if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) { - configGeneratorInfo = this.configGeneratorService.generateConfiguration(templateContent, templateData); - } else { - if (StringUtils.isBlank(requestId)) { - throw new SvcLogicException("Config Generator Request Id is missing."); - } - if (StringUtils.isBlank(resourceId)) { - throw new SvcLogicException("Config Generator Resource Id is missing."); - } - if (StringUtils.isBlank(resourceType)) { - throw new SvcLogicException("Config Generator Resource Type is missing."); - } - if (StringUtils.isBlank(recipeName)) { - throw new SvcLogicException("Config Generator Action Name is missing."); - } - if (StringUtils.isBlank(templateName)) { - throw new SvcLogicException("Config Generator Template Name Id is missing."); - } - - templateContent = configModelService.getNodeTemplateContent(ctx, templateName); - - if (StringUtils.isBlank(templateContent)) { - throw new SvcLogicException( - "Failed to get the Template Content for the Temaple Name :" + templateName); - } - - configGeneratorInfo = new ConfigGeneratorInfo(); - configGeneratorInfo.setRequestId(requestId); - configGeneratorInfo.setResourceId(resourceId); - configGeneratorInfo.setResourceType(resourceType); - configGeneratorInfo.setRecipeName(recipeName); - configGeneratorInfo.setTemplateName(templateName); - configGeneratorInfo.setTemplateContent(templateContent); - - this.configGeneratorService.generateConfiguration(configGeneratorInfo); - } - if (configGeneratorInfo != null) { - ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG, - configGeneratorInfo.getMashedData()); - } - ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, - ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS); - } catch (Exception e) { - ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, - ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE); - ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage()); - logger.error("Failed in generateConfiguration ({})", e); - throw new SvcLogicException(e.getMessage()); - } - } - - @Override - public void postProcess(Map inParams, SvcLogicContext ctx, Map componentContext) - throws SvcLogicException { - logger.info("Received generateConfiguration postProcess with params : ({})", inParams); - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorService.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorService.java deleted file mode 100644 index 710d47d25..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorService.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.service; - -import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - -public interface ConfigGeneratorService { - - public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData) - throws SvcLogicException; - - public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull) - throws SvcLogicException; - - public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException; - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorServiceImpl.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorServiceImpl.java deleted file mode 100644 index 472f5b291..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorServiceImpl.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.service; - -import java.io.StringWriter; -import java.util.Iterator; -import java.util.List; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.Velocity; -import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource; -import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; -import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo; -import org.onap.ccsdk.config.generator.tool.CustomJsonNodeFactory; -import org.onap.ccsdk.config.model.utils.TransformationUtils; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class ConfigGeneratorServiceImpl implements ConfigGeneratorService { - - private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorServiceImpl.class); - private static final String CLASS_NAME = "ConfigGeneratorServiceImpl"; - - private ConfigResourceService configResourceService; - - public ConfigGeneratorServiceImpl(ConfigResourceService configResourceService) { - logger.info("{} Constuctor Initated...", CLASS_NAME); - this.configResourceService = configResourceService; - } - - @Override - public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException { - - if (configGeneratorInfo != null && StringUtils.isNotBlank(configGeneratorInfo.getResourceId()) - && StringUtils.isNotBlank(configGeneratorInfo.getResourceType()) - && StringUtils.isNotBlank(configGeneratorInfo.getRecipeName()) - && StringUtils.isNotBlank(configGeneratorInfo.getTemplateName()) - && StringUtils.isNotBlank(configGeneratorInfo.getTemplateContent())) { - - ConfigResource configResourceQuery = new ConfigResource(); - configResourceQuery.setResourceId(configGeneratorInfo.getResourceId()); - configResourceQuery.setResourceType(configGeneratorInfo.getResourceType()); - configResourceQuery.setTemplateName(configGeneratorInfo.getTemplateName()); - - List configResourceList = configResourceService.getConfigResource(configResourceQuery); - - if (CollectionUtils.isEmpty(configResourceList)) { - throw new SvcLogicException("No Config Resource found"); - } else if (configResourceList.size() > 1) { - throw new SvcLogicException("More than one Config Resource found for specified parameter for" - + " resourceId " + configGeneratorInfo.getResourceId() + ", resourceType " - + configGeneratorInfo.getResourceType() + ", recipeName " + configGeneratorInfo.getRecipeName() - + ", templateName " + configGeneratorInfo.getTemplateName()); - } - - ConfigResource configResource = configResourceList.get(0); - - if (configResource != null && StringUtils.isNotBlank(configResource.getResourceData())) { - configGeneratorInfo.setResourceData(configResource.getResourceData()); - logger.debug("Retrieve ConfigResource Data : ({})", configResource.getResourceData()); - ConfigGeneratorInfo generatorInfo = generateConfiguration(configGeneratorInfo.getTemplateContent(), - configResource.getResourceData()); - if (generatorInfo != null) { - configGeneratorInfo.setMashedData(generatorInfo.getMashedData()); - } - } else { - throw new SvcLogicException( - "Failed to get the Resource Data for the Resource Id :" + configGeneratorInfo.getResourceId() - + " of template :" + configGeneratorInfo.getTemplateName()); - } - } - return configGeneratorInfo; - } - - @Override - public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData) - throws SvcLogicException { - return generateConfiguration(templateContent, templateData, true); - } - - @Override - public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull) - throws SvcLogicException { - ConfigGeneratorInfo configGeneratorInfo = null; - try { - if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) { - configGeneratorInfo = new ConfigGeneratorInfo(); - - Velocity.init(); - - ObjectMapper mapper = new ObjectMapper(); - CustomJsonNodeFactory f = new CustomJsonNodeFactory(); - mapper.setNodeFactory(f); - - JsonNode jsonObj = mapper.readValue(templateData, JsonNode.class); - if (ignoreNull) { - TransformationUtils.removeJsonNullNode(jsonObj); - } - - VelocityContext context = new VelocityContext(); - context.put("StringUtils", org.apache.commons.lang3.StringUtils.class); - context.put("BooleanUtils", org.apache.commons.lang3.BooleanUtils.class); - - Iterator ii = jsonObj.fieldNames(); - while (ii.hasNext()) { - String key = ii.next(); - JsonNode node = jsonObj.get(key); - logger.info("Adding key ({}) with value ({})", key, node); - context.put(key, node); - } - - StringWriter writer = new StringWriter(); - Velocity.evaluate(context, writer, "TemplateData", templateContent); - writer.flush(); - configGeneratorInfo.setMashedData(writer.toString()); - } - } catch (Exception e) { - logger.error("Failed to generate Configuration ({})", e.getMessage()); - throw new SvcLogicException(e.getMessage(), e); - } - return configGeneratorInfo; - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomJsonNodeFactory.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomJsonNodeFactory.java deleted file mode 100644 index 3e69ef355..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomJsonNodeFactory.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.tool; - -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.TextNode; - -public class CustomJsonNodeFactory extends JsonNodeFactory { - - @Override - public TextNode textNode(String text) { - return new CustomTextNode(text); - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomTextNode.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomTextNode.java deleted file mode 100644 index cf42c552b..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/CustomTextNode.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.tool; - -import com.fasterxml.jackson.core.io.CharTypes; -import com.fasterxml.jackson.databind.node.TextNode; - -public class CustomTextNode extends TextNode { - - public CustomTextNode(String v) { - super(v); - } - - @Override - public String toString() { - int len = textValue().length(); - len = len + 2 + (len >> 4); - StringBuilder sb = new StringBuilder(len); - appendQuoted(sb, textValue()); - return sb.toString(); - } - - protected static void appendQuoted(StringBuilder sb, String content) { - CharTypes.appendQuoted(sb, content); - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/EscapeUtils.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/EscapeUtils.java deleted file mode 100644 index 3009f87f9..000000000 --- a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/tool/EscapeUtils.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.tool; - -import org.apache.commons.lang3.StringUtils; - -@SuppressWarnings("squid:S1118") -public class EscapeUtils { - - public static String escapeSql(String str) { - if (str == null) { - return null; - } - String[] searchList = new String[] {"'", "\\"}; - String[] replacementList = new String[] {"''", "\\\\"}; - return StringUtils.replaceEach(str, searchList, replacementList); - } - - // For Generic Purpose - public static String escapeSequence(String s) { - if (s == null) { - return null; - } - - int length = s.length(); - int newLength = length; - for (int i = 0; i < length; i++) { - char c = s.charAt(i); - switch (c) { - case '\\': - case '\"': - case '\'': - case '\0': - newLength += 1; - break; - default: - // do nothing - } - } - if (length == newLength) { - // nothing to escape in the string - return s; - } - StringBuilder sb = new StringBuilder(newLength); - for (int i = 0; i < length; i++) { - char c = s.charAt(i); - switch (c) { - case '\\': - sb.append("\\\\"); - break; - case '\"': - sb.append("\\\""); - break; - case '\'': - sb.append("\\\'"); - break; - case '\0': - sb.append("\\0"); - break; - default: - sb.append(c); - } - } - return sb.toString(); - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/ConfigGeneratorConstant.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/ConfigGeneratorConstant.java new file mode 100644 index 000000000..06c423b5e --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/ConfigGeneratorConstant.java @@ -0,0 +1,49 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator; + +public class ConfigGeneratorConstant { + + private ConfigGeneratorConstant() { + + } + + public static final String STRING_ENCODING = "utf-8"; + public static final String Y = "Y"; + public static final String N = "N"; + public static final String DATA_TYPE_TEXT = "TEXT"; + public static final String DATA_TYPE_JSON = "JSON"; + public static final String DATA_TYPE_XML = "XML"; + public static final String DATA_TYPE_SQL = "SQL"; + + public static final String INPUT_PARAM_REQUEST_ID = "request-id"; + public static final String INPUT_PARAM_RESOURCE_ID = "resource-id"; + public static final String INPUT_PARAM_RESOURCE_TYPE = "resource-type"; + public static final String INPUT_PARAM_ACTION_NAME = "action-name"; + public static final String INPUT_PARAM_TEMPLATE_NAME = "template-name"; + public static final String INPUT_PARAM_TEMPLATE_CONTENT = "template-content"; + public static final String INPUT_PARAM_TEMPLATE_DATA = "template-data"; + + public static final String OUTPUT_PARAM_GENERATED_CONFIG = "generated-config"; + public static final String OUTPUT_PARAM_MASK_INFO = "mask-info"; + public static final String OUTPUT_PARAM_STATUS = "status"; + public static final String OUTPUT_PARAM_ERROR_MESSAGE = "error-message"; + public static final String OUTPUT_STATUS_SUCCESS = "success"; + public static final String OUTPUT_STATUS_FAILURE = "failure"; + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/data/ConfigGeneratorInfo.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/data/ConfigGeneratorInfo.java new file mode 100644 index 000000000..0b7ea0435 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/data/ConfigGeneratorInfo.java @@ -0,0 +1,102 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.data; + +public class ConfigGeneratorInfo { + + private String requestId; + private String resourceId; + private String resourceType; + private String templateName; + private String recipeName; + private String resourceData; + private String templateContent; + private String mashedData; + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public String getResourceId() { + return resourceId; + } + + public void setResourceId(String resourceId) { + this.resourceId = resourceId; + } + + public String getResourceType() { + return resourceType; + } + + public void setResourceType(String resourceType) { + this.resourceType = resourceType; + } + + public String getTemplateName() { + return templateName; + } + + public void setTemplateName(String templateName) { + this.templateName = templateName; + } + + public String getRecipeName() { + return recipeName; + } + + public void setRecipeName(String recipeName) { + this.recipeName = recipeName; + } + + public String getResourceData() { + return resourceData; + } + + public void setResourceData(String resourceData) { + this.resourceData = resourceData; + } + + public String getTemplateContent() { + return templateContent; + } + + public void setTemplateContent(String templateContent) { + this.templateContent = templateContent; + } + + public String getMashedData() { + return mashedData; + } + + public void setMashedData(String mashedData) { + this.mashedData = mashedData; + } + + @Override + public String toString() { + return "ConfigGeneratorInfo [requestId=" + requestId + ", resourceId=" + resourceId + ", resourceType=" + + resourceType + ", templateName=" + templateName + ", recipeName=" + recipeName + ", resourceData=" + + resourceData + ", templateContent=" + templateContent + ", mashedData=" + mashedData + "]"; + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNode.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNode.java new file mode 100644 index 000000000..421692b89 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNode.java @@ -0,0 +1,140 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.service; + +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.features.generator.ConfigGeneratorConstant; +import org.onap.ccsdk.features.generator.data.ConfigGeneratorInfo; +import org.onap.ccsdk.features.model.ConfigModelConstant; +import org.onap.ccsdk.features.model.service.ComponentNode; +import org.onap.ccsdk.features.model.service.ConfigModelService; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ConfigGeneratorNode implements ComponentNode { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNode.class); + + private ConfigModelService configModelService; + private ConfigResourceService configResourceService; + private ConfigGeneratorService configGeneratorService; + + public ConfigGeneratorNode(ConfigResourceService configResourceService, ConfigModelService configModelService) { + this.configResourceService = configResourceService; + this.configModelService = configModelService; + this.configGeneratorService = new ConfigGeneratorServiceImpl(this.configResourceService); + } + + @Override + public Boolean preCondition(Map inParams, SvcLogicContext ctx, Map componentContext) + throws SvcLogicException { + logger.trace("Received generateConfiguration preCondition call with params : ({})", inParams); + return Boolean.TRUE; + } + + @Override + public void preProcess(Map inParams, SvcLogicContext ctx, Map componentContext) + throws SvcLogicException { + logger.trace("Received generateConfiguration preProcess call with params : ({})", inParams); + } + + @Override + public void process(Map inParams, SvcLogicContext ctx) throws SvcLogicException { + logger.trace("Received generateConfiguration process call with params : ({})", inParams); + } + + @SuppressWarnings("squid:S3776") + @Override + public void process(Map inParams, SvcLogicContext ctx, Map componentContext) + throws SvcLogicException { + logger.trace("Received generateConfiguration process with params : ({})", inParams); + String prifix = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR); + try { + prifix = StringUtils.isNotBlank(prifix) ? (prifix + ".") : ""; + + String templateContent = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT); + String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA); + String requestId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_REQUEST_ID); + String resourceId = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_ID); + String resourceType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESOURCE_TYPE); + String recipeName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ACTION_NAME); + String templateName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_NAME); + + ConfigGeneratorInfo configGeneratorInfo = null; + if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) { + configGeneratorInfo = this.configGeneratorService.generateConfiguration(templateContent, templateData); + } else { + if (StringUtils.isBlank(requestId)) { + throw new SvcLogicException("Config Generator Request Id is missing."); + } + if (StringUtils.isBlank(resourceId)) { + throw new SvcLogicException("Config Generator Resource Id is missing."); + } + if (StringUtils.isBlank(resourceType)) { + throw new SvcLogicException("Config Generator Resource Type is missing."); + } + if (StringUtils.isBlank(recipeName)) { + throw new SvcLogicException("Config Generator Action Name is missing."); + } + if (StringUtils.isBlank(templateName)) { + throw new SvcLogicException("Config Generator Template Name Id is missing."); + } + + templateContent = configModelService.getNodeTemplateContent(ctx, templateName); + + if (StringUtils.isBlank(templateContent)) { + throw new SvcLogicException( + "Failed to get the Template Content for the Temaple Name :" + templateName); + } + + configGeneratorInfo = new ConfigGeneratorInfo(); + configGeneratorInfo.setRequestId(requestId); + configGeneratorInfo.setResourceId(resourceId); + configGeneratorInfo.setResourceType(resourceType); + configGeneratorInfo.setRecipeName(recipeName); + configGeneratorInfo.setTemplateName(templateName); + configGeneratorInfo.setTemplateContent(templateContent); + + this.configGeneratorService.generateConfiguration(configGeneratorInfo); + } + if (configGeneratorInfo != null) { + ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG, + configGeneratorInfo.getMashedData()); + } + ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, + ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS); + } catch (Exception e) { + ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, + ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE); + ctx.setAttribute(prifix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage()); + logger.error("Failed in generateConfiguration ({})", e); + throw new SvcLogicException(e.getMessage()); + } + } + + @Override + public void postProcess(Map inParams, SvcLogicContext ctx, Map componentContext) + throws SvcLogicException { + logger.info("Received generateConfiguration postProcess with params : ({})", inParams); + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorService.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorService.java new file mode 100644 index 000000000..b8a943083 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorService.java @@ -0,0 +1,33 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.service; + +import org.onap.ccsdk.features.generator.data.ConfigGeneratorInfo; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +public interface ConfigGeneratorService { + + public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData) + throws SvcLogicException; + + public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull) + throws SvcLogicException; + + public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException; + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorServiceImpl.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorServiceImpl.java new file mode 100644 index 000000000..6292b8def --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorServiceImpl.java @@ -0,0 +1,143 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.service; + +import java.io.StringWriter; +import java.util.Iterator; +import java.util.List; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.onap.ccsdk.features.data.adaptor.domain.ConfigResource; +import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.features.generator.data.ConfigGeneratorInfo; +import org.onap.ccsdk.features.generator.tool.CustomJsonNodeFactory; +import org.onap.ccsdk.features.model.utils.TransformationUtils; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ConfigGeneratorServiceImpl implements ConfigGeneratorService { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorServiceImpl.class); + private static final String CLASS_NAME = "ConfigGeneratorServiceImpl"; + + private ConfigResourceService configResourceService; + + public ConfigGeneratorServiceImpl(ConfigResourceService configResourceService) { + logger.info("{} Constuctor Initated...", CLASS_NAME); + this.configResourceService = configResourceService; + } + + @Override + public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException { + + if (configGeneratorInfo != null && StringUtils.isNotBlank(configGeneratorInfo.getResourceId()) + && StringUtils.isNotBlank(configGeneratorInfo.getResourceType()) + && StringUtils.isNotBlank(configGeneratorInfo.getRecipeName()) + && StringUtils.isNotBlank(configGeneratorInfo.getTemplateName()) + && StringUtils.isNotBlank(configGeneratorInfo.getTemplateContent())) { + + ConfigResource configResourceQuery = new ConfigResource(); + configResourceQuery.setResourceId(configGeneratorInfo.getResourceId()); + configResourceQuery.setResourceType(configGeneratorInfo.getResourceType()); + configResourceQuery.setTemplateName(configGeneratorInfo.getTemplateName()); + + List configResourceList = configResourceService.getConfigResource(configResourceQuery); + + if (CollectionUtils.isEmpty(configResourceList)) { + throw new SvcLogicException("No Config Resource found"); + } else if (configResourceList.size() > 1) { + throw new SvcLogicException("More than one Config Resource found for specified parameter for" + + " resourceId " + configGeneratorInfo.getResourceId() + ", resourceType " + + configGeneratorInfo.getResourceType() + ", recipeName " + configGeneratorInfo.getRecipeName() + + ", templateName " + configGeneratorInfo.getTemplateName()); + } + + ConfigResource configResource = configResourceList.get(0); + + if (configResource != null && StringUtils.isNotBlank(configResource.getResourceData())) { + configGeneratorInfo.setResourceData(configResource.getResourceData()); + logger.debug("Retrieve ConfigResource Data : ({})", configResource.getResourceData()); + ConfigGeneratorInfo generatorInfo = generateConfiguration(configGeneratorInfo.getTemplateContent(), + configResource.getResourceData()); + if (generatorInfo != null) { + configGeneratorInfo.setMashedData(generatorInfo.getMashedData()); + } + } else { + throw new SvcLogicException( + "Failed to get the Resource Data for the Resource Id :" + configGeneratorInfo.getResourceId() + + " of template :" + configGeneratorInfo.getTemplateName()); + } + } + return configGeneratorInfo; + } + + @Override + public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData) + throws SvcLogicException { + return generateConfiguration(templateContent, templateData, true); + } + + @Override + public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull) + throws SvcLogicException { + ConfigGeneratorInfo configGeneratorInfo = null; + try { + if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) { + configGeneratorInfo = new ConfigGeneratorInfo(); + + Velocity.init(); + + ObjectMapper mapper = new ObjectMapper(); + CustomJsonNodeFactory f = new CustomJsonNodeFactory(); + mapper.setNodeFactory(f); + + JsonNode jsonObj = mapper.readValue(templateData, JsonNode.class); + if (ignoreNull) { + TransformationUtils.removeJsonNullNode(jsonObj); + } + + VelocityContext context = new VelocityContext(); + context.put("StringUtils", org.apache.commons.lang3.StringUtils.class); + context.put("BooleanUtils", org.apache.commons.lang3.BooleanUtils.class); + + Iterator ii = jsonObj.fieldNames(); + while (ii.hasNext()) { + String key = ii.next(); + JsonNode node = jsonObj.get(key); + logger.info("Adding key ({}) with value ({})", key, node); + context.put(key, node); + } + + StringWriter writer = new StringWriter(); + Velocity.evaluate(context, writer, "TemplateData", templateContent); + writer.flush(); + configGeneratorInfo.setMashedData(writer.toString()); + } + } catch (Exception e) { + logger.error("Failed to generate Configuration ({})", e.getMessage()); + throw new SvcLogicException(e.getMessage(), e); + } + return configGeneratorInfo; + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomJsonNodeFactory.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomJsonNodeFactory.java new file mode 100644 index 000000000..f20cef2a6 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomJsonNodeFactory.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.tool; + +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.TextNode; + +public class CustomJsonNodeFactory extends JsonNodeFactory { + + @Override + public TextNode textNode(String text) { + return new CustomTextNode(text); + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomTextNode.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomTextNode.java new file mode 100644 index 000000000..1a62dd6a9 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/CustomTextNode.java @@ -0,0 +1,42 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.tool; + +import com.fasterxml.jackson.core.io.CharTypes; +import com.fasterxml.jackson.databind.node.TextNode; + +public class CustomTextNode extends TextNode { + + public CustomTextNode(String v) { + super(v); + } + + @Override + public String toString() { + int len = textValue().length(); + len = len + 2 + (len >> 4); + StringBuilder sb = new StringBuilder(len); + appendQuoted(sb, textValue()); + return sb.toString(); + } + + protected static void appendQuoted(StringBuilder sb, String content) { + CharTypes.appendQuoted(sb, content); + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/EscapeUtils.java b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/EscapeUtils.java new file mode 100644 index 000000000..d3246e045 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/features/generator/tool/EscapeUtils.java @@ -0,0 +1,82 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.tool; + +import org.apache.commons.lang3.StringUtils; + +@SuppressWarnings("squid:S1118") +public class EscapeUtils { + + public static String escapeSql(String str) { + if (str == null) { + return null; + } + String[] searchList = new String[] {"'", "\\"}; + String[] replacementList = new String[] {"''", "\\\\"}; + return StringUtils.replaceEach(str, searchList, replacementList); + } + + // For Generic Purpose + public static String escapeSequence(String s) { + if (s == null) { + return null; + } + + int length = s.length(); + int newLength = length; + for (int i = 0; i < length; i++) { + char c = s.charAt(i); + switch (c) { + case '\\': + case '\"': + case '\'': + case '\0': + newLength += 1; + break; + default: + // do nothing + } + } + if (length == newLength) { + // nothing to escape in the string + return s; + } + StringBuilder sb = new StringBuilder(newLength); + for (int i = 0; i < length; i++) { + char c = s.charAt(i); + switch (c) { + case '\\': + sb.append("\\\\"); + break; + case '\"': + sb.append("\\\""); + break; + case '\'': + sb.append("\\\'"); + break; + case '\0': + sb.append("\\0"); + break; + default: + sb.append(c); + } + } + return sb.toString(); + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/blueprints-processor/plugin/generator-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml index 7bf65fa4e..78cb28189 100644 --- a/blueprints-processor/plugin/generator-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml +++ b/blueprints-processor/plugin/generator-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml @@ -21,29 +21,29 @@ odl:use-default-for-reference-types="true"> + interface="org.onap.ccsdk.features.model.service.ConfigModelService" /> + interface="org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService" /> + class="org.onap.ccsdk.features.generator.service.ConfigGeneratorServiceImpl"> + interface="org.onap.ccsdk.features.generator.service.ConfigGeneratorService" /> + class="org.onap.ccsdk.features.generator.service.ConfigGeneratorNode"> + interface="org.onap.ccsdk.features.generator.service.ConfigGeneratorNode" /> diff --git a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNodeTest.java b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNodeTest.java deleted file mode 100644 index 941387347..000000000 --- a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorNodeTest.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.service; - -import static org.mockito.Matchers.any; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.commons.io.IOUtils; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource; -import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog; -import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; -import org.onap.ccsdk.config.generator.ConfigGeneratorConstant; -import org.onap.ccsdk.config.model.ConfigModelConstant; -import org.onap.ccsdk.config.model.service.ConfigModelService; -import org.onap.ccsdk.config.model.service.ConfigModelServiceImpl; -import org.onap.ccsdk.config.model.utils.TransformationUtils; -import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -@RunWith(MockitoJUnitRunner.class) -public class ConfigGeneratorNodeTest { - - private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNodeTest.class); - - @Mock - private ConfigResourceService configResourceService; - - @Mock - private ConfigRestAdaptorService configRestAdaptorService; - - private ConfigModelService configModelService; - - @Before - public void setUp() throws Exception { - - configModelService = new ConfigModelServiceImpl(configRestAdaptorService); - - MockitoAnnotations.initMocks(this); - - try { - Mockito.doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocationOnMock) throws Throwable { - Object[] args = invocationOnMock.getArguments(); - if (args != null) { - logger.trace("Transaction info " + Arrays.asList(args)); - } - return null; - } - }).when(configResourceService).save(any(TransactionLog.class)); - - Mockito.doAnswer(new Answer>() { - @Override - public List answer(InvocationOnMock invocationOnMock) throws Throwable { - List configResources = new ArrayList<>(); - Object[] args = invocationOnMock.getArguments(); - if (args != null) { - logger.trace("Transaction info " + Arrays.asList(args)); - String resourceData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/configdata.json"), Charset.defaultCharset()); - ConfigResource configResource = (ConfigResource) args[0]; - configResource.setRecipeName("Sample-recipe"); - configResource.setResourceData(resourceData); - configResources.add(configResource); - } - return configResources; - } - }).when(configResourceService).getConfigResource(any(ConfigResource.class)); - - } catch (SvcLogicException e) { - e.printStackTrace(); - } - } - - @Test - public void testInputTemplateContentNData() throws Exception { - - ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); - - Map inParams = new HashMap(); - inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test"); - - String jsonData = IOUtils.toString( - ConfigGeneratorNodeTest.class.getClassLoader().getResourceAsStream("service_templates/configdata.json"), - Charset.defaultCharset()); - inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData); - - String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); - inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData); - - SvcLogicContext ctx = new SvcLogicContext(); - Map componentContext = new HashMap<>(); - configGeneratorNode.process(inParams, ctx, componentContext); - Assert.assertEquals("Failed to generate Configuration Status as Failure", - ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, - ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); - Assert.assertNotNull("Failed to generate Configuration", - ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - - logger.trace("Generated Configuration:\n " - + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - logger.trace("Generated Configuration:\n " - + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO)); - - } - - @Test - public void testInputTemplateWithNullData() throws Exception { - - ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); - - Map inParams = new HashMap(); - inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test"); - - String jsonData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/configdata_with_null.json"), Charset.defaultCharset()); - inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData); - - String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); - inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData); - - SvcLogicContext ctx = new SvcLogicContext(); - Map componentContext = new HashMap<>(); - configGeneratorNode.process(inParams, ctx, componentContext); - Assert.assertEquals("Failed to generate Configuration Status as Failure", - ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, - ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); - Assert.assertNotNull("Failed to generate Configuration", - ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - - logger.trace("Generated Configuration:\n " - + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - logger.trace("Generated Configuration:\n " - + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO)); - - } - - @Test - public void testDBTemplateContentNData() throws Exception { - - String fileContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/generate_configuration.json"), Charset.defaultCharset()); - - String baseConfigTemplateContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() - .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); - - Map context = new HashMap<>(); - context = configModelService.convertServiceTemplate2Properties(fileContent, context); - - context.put("node_templates.base-config-template.content", baseConfigTemplateContent); - - Assert.assertNotNull("Failed to Prepare Context : ", context); - - context.put("request-id", "12345"); - context.put("vnf-id", "vnf12345"); - context.put("action-name", "config-generator-action"); - - Map inparams = new HashMap(); - inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "generate-configuration"); - - SvcLogicContext inputContext = new SvcLogicContext(); - context.forEach((name, value) -> { - inputContext.setAttribute(name, value); - }); - - TransformationUtils.printMap(context); - configModelService.assignInParamsFromModel(inputContext, inparams); - ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); - - Map componentContext = new HashMap<>(); - configGeneratorNode.process(inparams, inputContext, componentContext); - - Assert.assertEquals("Failed to generate Configuration Status as Failure", - ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, - inputContext.getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); - Assert.assertNotNull("Failed to generate Configuration", inputContext - .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - - logger.trace("Generated Configuration:\n " + inputContext - .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); - } - - @Test - public void testTemplateContentNDataForMask() throws Exception { - - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/tool/EscapeUtilsTest.java b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/tool/EscapeUtilsTest.java deleted file mode 100644 index 0bd2384bf..000000000 --- a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/config/generator/tool/EscapeUtilsTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. - * - * 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. - */ - -package org.onap.ccsdk.config.generator.tool; - -import org.junit.Assert; -import org.junit.Test; - -public class EscapeUtilsTest { - - @Test - public void testEscapeSql() { - Assert.assertEquals("", EscapeUtils.escapeSql("")); - Assert.assertEquals("text", EscapeUtils.escapeSql("text")); - - Assert.assertEquals("''", EscapeUtils.escapeSql("'")); - Assert.assertEquals("\\\\", EscapeUtils.escapeSql("\\")); - - Assert.assertEquals("text''text", EscapeUtils.escapeSql("text'text")); - Assert.assertEquals("text\\\\text", EscapeUtils.escapeSql("text\\text")); - } - - @Test - public void testEscapeSequence() { - Assert.assertEquals("", EscapeUtils.escapeSequence("")); - Assert.assertEquals("text", EscapeUtils.escapeSequence("text")); - - Assert.assertEquals("\\\'", EscapeUtils.escapeSequence("'")); - Assert.assertEquals("\\\"", EscapeUtils.escapeSequence("\"")); - Assert.assertEquals("\\\\", EscapeUtils.escapeSequence("\\")); - - Assert.assertEquals("text\\\'text", EscapeUtils.escapeSequence("text'text")); - Assert.assertEquals("text\\\"text", EscapeUtils.escapeSequence("text\"text")); - Assert.assertEquals("text\\\\text", EscapeUtils.escapeSequence("text\\text")); - } - -} diff --git a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNodeTest.java b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNodeTest.java new file mode 100644 index 000000000..58a22ac14 --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/service/ConfigGeneratorNodeTest.java @@ -0,0 +1,223 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.service; + +import static org.mockito.Matchers.any; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.onap.ccsdk.features.data.adaptor.domain.ConfigResource; +import org.onap.ccsdk.features.data.adaptor.domain.TransactionLog; +import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.features.generator.ConfigGeneratorConstant; +import org.onap.ccsdk.features.generator.service.ConfigGeneratorNode; +import org.onap.ccsdk.features.model.ConfigModelConstant; +import org.onap.ccsdk.features.model.service.ConfigModelService; +import org.onap.ccsdk.features.model.service.ConfigModelServiceImpl; +import org.onap.ccsdk.features.model.utils.TransformationUtils; +import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +@RunWith(MockitoJUnitRunner.class) +public class ConfigGeneratorNodeTest { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNodeTest.class); + + @Mock + private ConfigResourceService configResourceService; + + @Mock + private ConfigRestAdaptorService configRestAdaptorService; + + private ConfigModelService configModelService; + + @Before + public void setUp() throws Exception { + + configModelService = new ConfigModelServiceImpl(configRestAdaptorService); + + MockitoAnnotations.initMocks(this); + + try { + Mockito.doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocationOnMock) throws Throwable { + Object[] args = invocationOnMock.getArguments(); + if (args != null) { + logger.trace("Transaction info " + Arrays.asList(args)); + } + return null; + } + }).when(configResourceService).save(any(TransactionLog.class)); + + Mockito.doAnswer(new Answer>() { + @Override + public List answer(InvocationOnMock invocationOnMock) throws Throwable { + List configResources = new ArrayList<>(); + Object[] args = invocationOnMock.getArguments(); + if (args != null) { + logger.trace("Transaction info " + Arrays.asList(args)); + String resourceData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/configdata.json"), Charset.defaultCharset()); + ConfigResource configResource = (ConfigResource) args[0]; + configResource.setRecipeName("Sample-recipe"); + configResource.setResourceData(resourceData); + configResources.add(configResource); + } + return configResources; + } + }).when(configResourceService).getConfigResource(any(ConfigResource.class)); + + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + @Test + public void testInputTemplateContentNData() throws Exception { + + ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); + + Map inParams = new HashMap(); + inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test"); + + String jsonData = IOUtils.toString( + ConfigGeneratorNodeTest.class.getClassLoader().getResourceAsStream("service_templates/configdata.json"), + Charset.defaultCharset()); + inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData); + + String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); + inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData); + + SvcLogicContext ctx = new SvcLogicContext(); + Map componentContext = new HashMap<>(); + configGeneratorNode.process(inParams, ctx, componentContext); + Assert.assertEquals("Failed to generate Configuration Status as Failure", + ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); + Assert.assertNotNull("Failed to generate Configuration", + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + + logger.trace("Generated Configuration:\n " + + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + logger.trace("Generated Configuration:\n " + + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO)); + + } + + @Test + public void testInputTemplateWithNullData() throws Exception { + + ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); + + Map inParams = new HashMap(); + inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test"); + + String jsonData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/configdata_with_null.json"), Charset.defaultCharset()); + inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData); + + String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); + inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData); + + SvcLogicContext ctx = new SvcLogicContext(); + Map componentContext = new HashMap<>(); + configGeneratorNode.process(inParams, ctx, componentContext); + Assert.assertEquals("Failed to generate Configuration Status as Failure", + ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); + Assert.assertNotNull("Failed to generate Configuration", + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + + logger.trace("Generated Configuration:\n " + + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + logger.trace("Generated Configuration:\n " + + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO)); + + } + + @Test + public void testDBTemplateContentNData() throws Exception { + + String fileContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/generate_configuration.json"), Charset.defaultCharset()); + + String baseConfigTemplateContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader() + .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset()); + + Map context = new HashMap<>(); + context = configModelService.convertServiceTemplate2Properties(fileContent, context); + + context.put("node_templates.base-config-template.content", baseConfigTemplateContent); + + Assert.assertNotNull("Failed to Prepare Context : ", context); + + context.put("request-id", "12345"); + context.put("vnf-id", "vnf12345"); + context.put("action-name", "config-generator-action"); + + Map inparams = new HashMap(); + inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "generate-configuration"); + + SvcLogicContext inputContext = new SvcLogicContext(); + context.forEach((name, value) -> { + inputContext.setAttribute(name, value); + }); + + TransformationUtils.printMap(context); + configModelService.assignInParamsFromModel(inputContext, inparams); + ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService); + + Map componentContext = new HashMap<>(); + configGeneratorNode.process(inparams, inputContext, componentContext); + + Assert.assertEquals("Failed to generate Configuration Status as Failure", + ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, + inputContext.getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS)); + Assert.assertNotNull("Failed to generate Configuration", inputContext + .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + + logger.trace("Generated Configuration:\n " + inputContext + .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG)); + } + + @Test + public void testTemplateContentNDataForMask() throws Exception { + + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/tool/EscapeUtilsTest.java b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/tool/EscapeUtilsTest.java new file mode 100644 index 000000000..58918685d --- /dev/null +++ b/blueprints-processor/plugin/generator-provider/src/test/java/org/onap/ccsdk/features/generator/tool/EscapeUtilsTest.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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. + */ + +package org.onap.ccsdk.features.generator.tool; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.ccsdk.features.generator.tool.EscapeUtils; + +public class EscapeUtilsTest { + + @Test + public void testEscapeSql() { + Assert.assertEquals("", EscapeUtils.escapeSql("")); + Assert.assertEquals("text", EscapeUtils.escapeSql("text")); + + Assert.assertEquals("''", EscapeUtils.escapeSql("'")); + Assert.assertEquals("\\\\", EscapeUtils.escapeSql("\\")); + + Assert.assertEquals("text''text", EscapeUtils.escapeSql("text'text")); + Assert.assertEquals("text\\\\text", EscapeUtils.escapeSql("text\\text")); + } + + @Test + public void testEscapeSequence() { + Assert.assertEquals("", EscapeUtils.escapeSequence("")); + Assert.assertEquals("text", EscapeUtils.escapeSequence("text")); + + Assert.assertEquals("\\\'", EscapeUtils.escapeSequence("'")); + Assert.assertEquals("\\\"", EscapeUtils.escapeSequence("\"")); + Assert.assertEquals("\\\\", EscapeUtils.escapeSequence("\\")); + + Assert.assertEquals("text\\\'text", EscapeUtils.escapeSequence("text'text")); + Assert.assertEquals("text\\\"text", EscapeUtils.escapeSequence("text\"text")); + Assert.assertEquals("text\\\\text", EscapeUtils.escapeSequence("text\\text")); + } + +} diff --git a/blueprints-processor/plugin/generator-provider/src/test/resources/service_templates/generate_configuration.json b/blueprints-processor/plugin/generator-provider/src/test/resources/service_templates/generate_configuration.json index 0d569ef85..326d2e8ec 100644 --- a/blueprints-processor/plugin/generator-provider/src/test/resources/service_templates/generate_configuration.json +++ b/blueprints-processor/plugin/generator-provider/src/test/resources/service_templates/generate_configuration.json @@ -67,7 +67,7 @@ "generate-configuration": { "type": "component-config-generator", "interfaces": { - "org-onap-ccsdk-config-generator-service-ConfigGeneratorNode": { + "org-onap-ccsdk-features-generator-service-ConfigGeneratorNode": { "operations": { "process": { "inputs": { @@ -164,7 +164,7 @@ } }, "interfaces": { - "org-onap-ccsdk-config-generator-service-ConfigGeneratorNode": { + "org-onap-ccsdk-features-generator-service-ConfigGeneratorNode": { "operations": { "process": { "inputs": { @@ -211,7 +211,7 @@ "type": "string" }, "mask-info": { - "description": "If template contains mask encription keys, then this mask-info field will be generated, This JSON Content alligns to the bean org.onap.ccsdk.config.model.data.custom.MaskInfo ", + "description": "If template contains mask encription keys, then this mask-info field will be generated, This JSON Content alligns to the bean org.onap.ccsdk.features.model.data.custom.MaskInfo ", "required": false, "type": "string" }, -- cgit 1.2.3-korg