2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
 
  24 package org.onap.appc.design.validator;
 
  26 import com.fasterxml.jackson.core.JsonProcessingException;
 
  27 import com.fasterxml.jackson.databind.ObjectMapper;
 
  28 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
 
  29 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.introspector.BeanAccess;
 
  30 import java.io.ByteArrayInputStream;
 
  31 import java.io.IOException;
 
  32 import java.io.InputStream;
 
  33 import java.io.InputStreamReader;
 
  34 import java.io.Reader;
 
  35 import javax.xml.parsers.DocumentBuilder;
 
  36 import javax.xml.parsers.DocumentBuilderFactory;
 
  37 import javax.xml.parsers.ParserConfigurationException;
 
  38 import javax.xml.XMLConstants;
 
  39 import org.apache.velocity.app.Velocity;
 
  40 import org.apache.velocity.app.VelocityEngine;
 
  41 import org.apache.velocity.exception.MethodInvocationException;
 
  42 import org.apache.velocity.exception.ParseErrorException;
 
  43 import org.apache.velocity.exception.ResourceNotFoundException;
 
  44 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
 
  45 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
 
  46 import org.onap.appc.design.services.util.DesignServiceConstants;
 
  47 import org.slf4j.Logger;
 
  48 import org.slf4j.LoggerFactory;
 
  49 import org.xml.sax.InputSource;
 
  50 import org.xml.sax.SAXException;
 
  52 public class ValidatorService {
 
  54     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
 
  56     public String execute(String action, String payload, String dataType) throws ValidatorException {
 
  58         log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
 
  59         String validateResponse  = null;
 
  63                 case DesignServiceConstants.DATA_TYPE_XML:
 
  64                     validateResponse = validateXML(payload);
 
  66                 case DesignServiceConstants.DATA_TYPE_JSON:
 
  67                     validateResponse = validateJSON(payload);
 
  69                 case DesignServiceConstants.DATA_TYPE_VELOCITY:
 
  70                     validateResponse = validateVelocity(payload);
 
  72                 case DesignServiceConstants.DATA_TYPE_YAML:
 
  73                     validateResponse = validateYAML(payload);
 
  79         catch (ParserConfigurationException | SAXException | IOException e){
 
  80             log.info("An error occurred while executing validator", e);
 
  81             throw new ValidatorException("An error occurred while executing validator", e);
 
  83         return validateResponse;
 
  86     private String validateYAML(String payload) throws IOException {
 
  88             InputStream is = new ByteArrayInputStream(payload.getBytes());
 
  90             Reader in = new InputStreamReader(is);
 
  91             Yaml yaml = new Yaml();
 
  92             yaml.setBeanAccess(BeanAccess.FIELD);
 
  94             return DesignServiceConstants.SUCCESS;
 
  95         } catch (Exception e) {
 
  96             log.error("Not a Valid YAML Format", e);
 
 102     private String validateVelocity(String payload) {
 
 105             VelocityEngine engine = new VelocityEngine();
 
 106             engine.setProperty(Velocity.RESOURCE_LOADER, "string");
 
 107             engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
 
 108             engine.addProperty("string.resource.loader.repository.static", "false");
 
 110             StringResourceRepository repo = (StringResourceRepository) engine
 
 111                 .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
 
 112             repo.putStringResource("TestTemplate", payload);
 
 114             return DesignServiceConstants.SUCCESS;
 
 115         } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
 
 116             log.error("Not a Valid Velocity Template", e);
 
 121     private String validateJSON(String payload) throws IOException {
 
 124             ObjectMapper objectMapper = new ObjectMapper();
 
 125             objectMapper.readTree(payload);
 
 126             return DesignServiceConstants.SUCCESS;
 
 127         } catch (JsonProcessingException e) {
 
 128             log.error("Not a Valid JSON file", e);
 
 134     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
 
 137             DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
 
 138             dBF.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
 
 139             dBF.setFeature("http://xml.org/sax/features/external-general-entities", false);
 
 140             dBF.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
 
 142             DocumentBuilder builder = dBF.newDocumentBuilder();
 
 143             builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
 
 144             return DesignServiceConstants.SUCCESS;
 
 146         } catch (ParserConfigurationException | SAXException | IOException e) {
 
 147             log.info("Error While parsing Payload", e);