Third part of onap rename
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / validator / ValidatorService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.design.validator;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
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;
36
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import javax.xml.parsers.ParserConfigurationException;
40 import javax.xml.validation.Validator;
41
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;
57
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;
63
64 public class ValidatorService {
65
66     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
67     public String execute(String action, String payload, String dataType) throws Exception {
68         
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);
79         
80         return validateResponse;
81
82     }
83
84     private String validateYAML(String payload) throws Exception {
85         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());        
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         }
95         catch(Exception e){
96             log.error("Not a Valid YAML Format ");
97             throw e;
98         }
99
100     }
101
102     private String validateVelocity(String payload) {
103
104         try{
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");
109             engine.init();                
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");
114             
115             return DesignServiceConstants.SUCCESS;
116         }
117         catch(ResourceNotFoundException e ){
118             log.error("Not a Valid Velocity Template ");
119             throw e;
120         }
121         catch(ParseErrorException pe){
122             log.error("Not a Valid Velocity Template ");
123             throw pe;
124         }
125         catch(MethodInvocationException mi){
126             log.error("Not a Valid Velocity Template ");
127             throw mi;
128         }
129     }
130     
131     private String validateJOSN(String payload) throws Exception {
132
133         try{ 
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 ");
139             throw e;
140         }
141
142     }
143
144     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
145
146         try{
147             
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;
153
154         } catch(ParserConfigurationException e){
155             log.info("Error While parsing Payload : " + e.getMessage());
156             throw e;
157         }
158         catch(SAXException se){
159             log.info("Error While parsing Payload : " + se.getMessage());
160             throw se;
161         }
162         catch(IOException io){
163             log.info("Error While parsing Payload : " + io.getMessage());
164             throw io;
165         }
166     }
167 }
168