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 org.apache.velocity.app.Velocity;
 
  39 import org.apache.velocity.app.VelocityEngine;
 
  40 import org.apache.velocity.exception.MethodInvocationException;
 
  41 import org.apache.velocity.exception.ParseErrorException;
 
  42 import org.apache.velocity.exception.ResourceNotFoundException;
 
  43 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
 
  44 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
 
  45 import org.onap.appc.design.services.util.DesignServiceConstants;
 
  46 import org.slf4j.Logger;
 
  47 import org.slf4j.LoggerFactory;
 
  48 import org.xml.sax.InputSource;
 
  49 import org.xml.sax.SAXException;
 
  51 public class ValidatorService {
 
  53     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
 
  55     public String execute(String action, String payload, String dataType) throws ValidatorException {
 
  57         log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
 
  58         String validateResponse  = null;
 
  62                 case DesignServiceConstants.DATA_TYPE_XML:
 
  63                     validateResponse = validateXML(payload);
 
  65                 case DesignServiceConstants.DATA_TYPE_JSON:
 
  66                     validateResponse = validateJSON(payload);
 
  68                 case DesignServiceConstants.DATA_TYPE_VELOCITY:
 
  69                     validateResponse = validateVelocity(payload);
 
  71                 case DesignServiceConstants.DATA_TYPE_YAML:
 
  72                     validateResponse = validateYAML(payload);
 
  78         catch (ParserConfigurationException | SAXException | IOException e){
 
  79             log.info("An error occurred while executing validator", e);
 
  80             throw new ValidatorException("An error occurred while executing validator", e);
 
  82         return validateResponse;
 
  85     private String validateYAML(String payload) throws IOException {
 
  87             InputStream is = new ByteArrayInputStream(payload.getBytes());
 
  89             Reader in = new InputStreamReader(is);
 
  90             Yaml yaml = new Yaml();
 
  91             yaml.setBeanAccess(BeanAccess.FIELD);
 
  93             return DesignServiceConstants.SUCCESS;
 
  94         } catch (Exception e) {
 
  95             log.error("Not a Valid YAML Format", e);
 
 101     private String validateVelocity(String payload) {
 
 104             VelocityEngine engine = new VelocityEngine();
 
 105             engine.setProperty(Velocity.RESOURCE_LOADER, "string");
 
 106             engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
 
 107             engine.addProperty("string.resource.loader.repository.static", "false");
 
 109             StringResourceRepository repo = (StringResourceRepository) engine
 
 110                 .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
 
 111             repo.putStringResource("TestTemplate", payload);
 
 113             return DesignServiceConstants.SUCCESS;
 
 114         } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
 
 115             log.error("Not a Valid Velocity Template", e);
 
 120     private String validateJSON(String payload) throws IOException {
 
 123             ObjectMapper objectMapper = new ObjectMapper();
 
 124             objectMapper.readTree(payload);
 
 125             return DesignServiceConstants.SUCCESS;
 
 126         } catch (JsonProcessingException e) {
 
 127             log.error("Not a Valid JSON file", e);
 
 133     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
 
 136             DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
 
 137             DocumentBuilder builder = dBF.newDocumentBuilder();
 
 138             builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
 
 139             return DesignServiceConstants.SUCCESS;
 
 141         } catch (ParserConfigurationException | SAXException | IOException e) {
 
 142             log.info("Error While parsing Payload", e);