eaf5478c4d67c78ecad4f2062fdbe88a8dc1d234
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.design.validator;
25
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;
50
51 public class ValidatorService {
52
53     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
54
55     public String execute(String action, String payload, String dataType) throws ValidatorException {
56
57         log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
58         String validateResponse  = null;
59
60         try{
61             switch (dataType) {
62                 case DesignServiceConstants.DATA_TYPE_XML:
63                     validateResponse = validateXML(payload);
64                     break;
65                 case DesignServiceConstants.DATA_TYPE_JSON:
66                     validateResponse = validateJSON(payload);
67                     break;
68                 case DesignServiceConstants.DATA_TYPE_VELOCITY:
69                     validateResponse = validateVelocity(payload);
70                     break;
71                 case DesignServiceConstants.DATA_TYPE_YAML:
72                     validateResponse = validateYAML(payload);
73                     break;
74                 default:
75                     break;
76             }
77         }
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);
81         }
82         return validateResponse;
83     }
84
85     private String validateYAML(String payload) throws IOException {
86         try {
87             InputStream is = new ByteArrayInputStream(payload.getBytes());
88
89             Reader in = new InputStreamReader(is);
90             Yaml yaml = new Yaml();
91             yaml.setBeanAccess(BeanAccess.FIELD);
92             yaml.load(in);
93             return DesignServiceConstants.SUCCESS;
94         } catch (Exception e) {
95             log.error("Not a Valid YAML Format", e);
96             throw e;
97         }
98
99     }
100
101     private String validateVelocity(String payload) {
102
103         try {
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");
108             engine.init();
109             StringResourceRepository repo = (StringResourceRepository) engine
110                 .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
111             repo.putStringResource("TestTemplate", payload);
112
113             return DesignServiceConstants.SUCCESS;
114         } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
115             log.error("Not a Valid Velocity Template", e);
116             throw e;
117         }
118     }
119
120     private String validateJSON(String payload) throws IOException {
121
122         try {
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);
128             throw e;
129         }
130
131     }
132
133     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
134
135         try {
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;
140
141         } catch (ParserConfigurationException | SAXException | IOException e) {
142             log.info("Error While parsing Payload", e);
143             throw e;
144         }
145     }
146 }
147