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 java.io.ByteArrayInputStream;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.io.StringWriter;
35 import java.nio.charset.StandardCharsets;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import javax.xml.parsers.ParserConfigurationException;
40 import javax.xml.validation.Validator;
42 import org.apache.velocity.Template;
43 import org.apache.velocity.VelocityContext;
44 import org.apache.velocity.app.Velocity;
45 import org.apache.velocity.app.VelocityEngine;
46 import org.apache.velocity.exception.MethodInvocationException;
47 import org.apache.velocity.exception.ParseErrorException;
48 import org.apache.velocity.exception.ResourceNotFoundException;
49 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
50 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
51 import org.onap.appc.design.services.util.DesignServiceConstants;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.w3c.dom.Document;
55 import org.xml.sax.InputSource;
56 import org.xml.sax.SAXException;
58 import com.fasterxml.jackson.core.JsonProcessingException;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
61 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
62 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.introspector.BeanAccess;
64 public class ValidatorService {
66 private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
67 public String execute(String action, String payload, String dataType) throws Exception {
69 String validateResponse = null;
70 log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
71 if(dataType.equals(DesignServiceConstants.DATA_TYPE_XML))
72 validateResponse = validateXML(payload);
73 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_JSON))
74 validateResponse = validateJOSN(payload);
75 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_VELOCITY))
76 validateResponse = validateVelocity(payload);
77 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_YAML))
78 validateResponse = validateYAML(payload);
80 return validateResponse;
84 private String validateYAML(String payload) throws Exception {
85 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
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;
96 log.error("Not a Valid YAML Format ");
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.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
111 repo.putStringResource("TestTemplate", payload);
112 //Template t = ve.getTemplate(payload);
113 Template t = engine.getTemplate("TestTemplate");
115 return DesignServiceConstants.SUCCESS;
117 catch(ResourceNotFoundException e ){
118 log.error("Not a Valid Velocity Template ");
121 catch(ParseErrorException pe){
122 log.error("Not a Valid Velocity Template ");
125 catch(MethodInvocationException mi){
126 log.error("Not a Valid Velocity Template ");
131 private String validateJOSN(String payload) throws Exception {
134 ObjectMapper objectMapper = new ObjectMapper();
135 objectMapper.readTree(payload);
136 return DesignServiceConstants.SUCCESS;
137 } catch(JsonProcessingException e){
138 log.error("Not a Valid JOSN file ");
144 private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
148 DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
149 DocumentBuilder builder = dBF.newDocumentBuilder();
150 InputSource is = new InputSource(payload);
151 builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
152 return DesignServiceConstants.SUCCESS;
154 } catch(ParserConfigurationException e){
155 log.info("Error While parsing Payload : " + e.getMessage());
158 catch(SAXException se){
159 log.info("Error While parsing Payload : " + se.getMessage());
162 catch(IOException io){
163 log.info("Error While parsing Payload : " + io.getMessage());