Fix sonar issues for APPC
[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.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36
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;
50
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;
56
57 public class ValidatorService {
58
59     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
60     public String execute(String action, String payload, String dataType) throws Exception {
61         
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);
72         
73         return validateResponse;
74
75     }
76
77     private String validateYAML(String payload) throws Exception {
78         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());        
79         try{
80             InputStream is = new ByteArrayInputStream(payload.getBytes());
81
82             Reader in = new InputStreamReader(is);
83             Yaml yaml = new Yaml();
84             yaml.setBeanAccess(BeanAccess.FIELD);
85             yaml.load(in);
86             return DesignServiceConstants.SUCCESS;
87         }
88         catch(Exception e){
89             log.error("Not a Valid YAML Format ");
90             throw e;
91         }
92
93     }
94
95     private String validateVelocity(String payload) {
96
97         try{
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");
102             engine.init();                
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");
107             
108             return DesignServiceConstants.SUCCESS;
109         }
110         catch(ResourceNotFoundException e ){
111             log.error("Not a Valid Velocity Template ");
112             throw e;
113         }
114         catch(ParseErrorException pe){
115             log.error("Not a Valid Velocity Template ");
116             throw pe;
117         }
118         catch(MethodInvocationException mi){
119             log.error("Not a Valid Velocity Template ");
120             throw mi;
121         }
122     }
123     
124     private String validateJOSN(String payload) throws Exception {
125
126         try{ 
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 ");
132             throw e;
133         }
134
135     }
136
137     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
138
139         try{
140             
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;
146
147         } catch(ParserConfigurationException e){
148             log.info("Error While parsing Payload : " + e.getMessage());
149             throw e;
150         }
151         catch(SAXException se){
152             log.info("Error While parsing Payload : " + se.getMessage());
153             throw se;
154         }
155         catch(IOException io){
156             log.info("Error While parsing Payload : " + io.getMessage());
157             throw io;
158         }
159     }
160 }
161