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.appc.design.validator;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
30 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.introspector.BeanAccess;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
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 DocumentBuilder builder = dBF.newDocumentBuilder();
139 builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
140 return DesignServiceConstants.SUCCESS;
142 } catch (ParserConfigurationException | SAXException | IOException e) {
143 log.info("Error While parsing Payload", e);