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;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
37 import org.apache.velocity.Template;
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 import com.fasterxml.jackson.core.JsonProcessingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
54 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
55 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.introspector.BeanAccess;
57 public class ValidatorService {
59 private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
60 public String execute(String action, String payload, String dataType) throws Exception {
62 String validateResponse = null;
63 log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
64 if(dataType.equals(DesignServiceConstants.DATA_TYPE_XML))
65 validateResponse = validateXML(payload);
66 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_JSON))
67 validateResponse = validateJOSN(payload);
68 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_VELOCITY))
69 validateResponse = validateVelocity(payload);
70 else if(dataType.equals(DesignServiceConstants.DATA_TYPE_YAML))
71 validateResponse = validateYAML(payload);
73 return validateResponse;
77 private String validateYAML(String payload) throws Exception {
78 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
80 InputStream is = new ByteArrayInputStream(payload.getBytes());
82 Reader in = new InputStreamReader(is);
83 Yaml yaml = new Yaml();
84 yaml.setBeanAccess(BeanAccess.FIELD);
86 return DesignServiceConstants.SUCCESS;
89 log.error("Not a Valid YAML Format ");
95 private String validateVelocity(String payload) {
98 VelocityEngine engine = new VelocityEngine();
99 engine.setProperty(Velocity.RESOURCE_LOADER, "string");
100 engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
101 engine.addProperty("string.resource.loader.repository.static", "false");
103 StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
104 repo.putStringResource("TestTemplate", payload);
105 //Template t = ve.getTemplate(payload);
106 Template t = engine.getTemplate("TestTemplate");
108 return DesignServiceConstants.SUCCESS;
110 catch(ResourceNotFoundException e ){
111 log.error("Not a Valid Velocity Template ");
114 catch(ParseErrorException pe){
115 log.error("Not a Valid Velocity Template ");
118 catch(MethodInvocationException mi){
119 log.error("Not a Valid Velocity Template ");
124 private String validateJOSN(String payload) throws Exception {
127 ObjectMapper objectMapper = new ObjectMapper();
128 objectMapper.readTree(payload);
129 return DesignServiceConstants.SUCCESS;
130 } catch(JsonProcessingException e){
131 log.error("Not a Valid JOSN file ");
137 private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
141 DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
142 DocumentBuilder builder = dBF.newDocumentBuilder();
143 InputSource is = new InputSource(payload);
144 builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
145 return DesignServiceConstants.SUCCESS;
147 } catch(ParserConfigurationException e){
148 log.info("Error While parsing Payload : " + e.getMessage());
151 catch(SAXException se){
152 log.info("Error While parsing Payload : " + se.getMessage());
155 catch(IOException io){
156 log.info("Error While parsing Payload : " + io.getMessage());