2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.sdnc.config.generator.convert;
 
  27 import com.att.eelf.configuration.EELFLogger;
 
  28 import com.att.eelf.configuration.EELFManager;
 
  29 import com.fasterxml.jackson.databind.ObjectMapper;
 
  30 import com.fasterxml.jackson.databind.node.ObjectNode;
 
  31 import java.util.ArrayList;
 
  32 import java.util.Arrays;
 
  33 import java.util.List;
 
  35 import java.util.Map.Entry;
 
  37 import org.apache.commons.lang.StringEscapeUtils;
 
  38 import org.apache.commons.lang3.StringUtils;
 
  39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  41 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 
  42 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
 
  43 import org.onap.sdnc.config.generator.tool.EscapeUtils;
 
  44 import org.onap.sdnc.config.generator.tool.JSONTool;
 
  46 public class ConvertNode implements SvcLogicJavaPlugin {
 
  48     private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
 
  49     private static final String DATA_TYPE_STR = " Datatype (";
 
  51     public void convertJson2DGContext(Map<String, String> inParams, SvcLogicContext ctx)
 
  52         throws SvcLogicException {
 
  53         log.trace("Received convertJson2DGContext call with params : " + inParams);
 
  54         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
 
  56             String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
 
  57             String isEscaped = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED);
 
  58             String blockKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS);
 
  59             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
 
  61             if (StringUtils.isNotBlank(jsonData)) {
 
  62                 if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) {
 
  63                     jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
 
  66                 List<String> blockKeys = new ArrayList<>();
 
  67                 if (blockKey != null) {
 
  68                     blockKeys = Arrays.asList(blockKey.split(","));
 
  71                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
 
  72                 log.trace("DG Context Populated:" + dgContext);
 
  74                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
 
  75                     trySetContextAttribute(ctx, entry);
 
  78             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
  79                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
 
  81         } catch (Exception e) {
 
  82             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
  83                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
 
  84             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
 
  86             log.error("Failed in JSON to DGContext Conversion", e);
 
  87             throw new SvcLogicException(e.getMessage());
 
  91     private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
 
  92         if (entry != null && entry.getKey() != null) {
 
  93             ctx.setAttribute(entry.getKey(), entry.getValue());
 
  98     public void escapeData(Map<String, String> inParams, SvcLogicContext ctx)
 
  99         throws SvcLogicException {
 
 100         log.trace("Received escapeData call with params : " + inParams);
 
 101         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
 
 103             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
 
 104             String unEscapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
 
 105             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
 
 107             String escapedData = tryFetchEscapedData(unEscapeData, dataType);
 
 108             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
 
 110             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 111                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
 
 112             log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
 
 113         } catch (Exception e) {
 
 114             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 115                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
 
 116             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
 
 118             log.error("Failed in escapeData Conversion", e);
 
 119             throw new SvcLogicException(e.getMessage());
 
 123     private String tryFetchEscapedData(String unEscapeData, String dataType) throws InvalidParameterException {
 
 124         validateInput(unEscapeData, dataType);
 
 125         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
 
 126             return StringEscapeUtils.escapeJavaScript(unEscapeData);
 
 127         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
 
 128             return StringEscapeUtils.escapeXml(unEscapeData);
 
 129         } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
 
 130             return EscapeUtils.escapeSql(unEscapeData);
 
 132             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
 
 133                 + ") param  value (" + dataType
 
 134                 + ")is not supported  for escapeData conversion.");
 
 138     private void validateInput(String unEscapeData, String dataType) throws InvalidParameterException {
 
 139         if (StringUtils.isBlank(unEscapeData)) {
 
 140             throw new InvalidParameterException("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
 
 141                 + ") param is missing for escapeData conversion." + unEscapeData);
 
 143         if (StringUtils.isBlank(dataType)) {
 
 144             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
 
 145                 + ")param is missing for escapeData conversion.");
 
 149     public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
 
 150         throws SvcLogicException {
 
 151         log.trace("Received unEscapeData call with params : " + inParams);
 
 152         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
 
 154             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
 
 155             String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
 
 156             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
 
 157             String unEscapedData = tryFetchUnescapedData(escapeData, dataType);
 
 159             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
 
 161             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 162                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
 
 163             log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
 
 164         } catch (Exception e) {
 
 165             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 166                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
 
 167             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
 
 169             log.error("Failed in unEscapeData Conversion", e);
 
 170             throw new SvcLogicException(e.getMessage());
 
 174     private String tryFetchUnescapedData(String escapeData, String dataType) throws InvalidParameterException {
 
 175         if (StringUtils.isBlank(escapeData)) {
 
 176             throw new InvalidParameterException("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
 
 177                 + ") param is missing for escapeData conversion.");
 
 180         if (StringUtils.isBlank(dataType)) {
 
 181             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
 
 182                 + ")param is missing for escapeData conversion.");
 
 184         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
 
 185             return StringEscapeUtils.unescapeJavaScript(escapeData);
 
 186         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
 
 187             return StringEscapeUtils.unescapeXml(escapeData);
 
 189             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
 
 190                 + ") param  value (" + dataType
 
 191                 + ")is not supported  for unEscapeData conversion.");
 
 196     public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
 
 197         throws SvcLogicException {
 
 198         log.trace("Received convertContextToJson call with params : " + inParams);
 
 199         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
 
 200         String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
 
 202             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
 
 204             ObjectMapper mapper = new ObjectMapper();
 
 205             ObjectNode objectNode = mapper.createObjectNode();
 
 207             Set<String> keys = ctx.getAttributeKeySet();
 
 208             for (String key : keys) {
 
 209                 if (key.startsWith(contextKey + ".")) {
 
 210                     String objkey = key.replaceFirst(contextKey + ".", "");
 
 211                     objectNode.put(objkey, ctx.getAttribute(key));
 
 215             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
 
 216                 objectNode.toString());
 
 217             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 218                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
 
 219             log.trace("convertContextToJson Successful");
 
 220         } catch (Exception e) {
 
 221             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
 
 222                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
 
 223             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
 
 225             log.error("Failed in convertContextToJson", e);
 
 226             throw new SvcLogicException(e.getMessage());