df6a55291772b07fd87df4d727a3de69bf45950c
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / artifact / PayloadTypeEnum.java
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2019 Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.sdc.be.components.impl.artifact;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import fj.data.Either;
28 import javax.xml.parsers.SAXParser;
29 import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.YamlToObjectConverter;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.SAXNotRecognizedException;
36 import org.xml.sax.SAXNotSupportedException;
37 import org.xml.sax.XMLReader;
38
39 import javax.xml.XMLConstants;
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.parsers.SAXParserFactory;
42 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44
45 public enum PayloadTypeEnum {
46     HEAT_YAML {
47         @Override
48         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
49             YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
50             if (isNotValidYaml(payload, yamlToObjectConverter)) {
51                 return Either.right(ActionStatus.INVALID_YAML);
52             }
53
54             DeploymentArtifactHeatConfiguration heatConfiguration =
55                     yamlToObjectConverter.convert(payload, DeploymentArtifactHeatConfiguration.class);
56             if (heatConfiguration == null || heatConfiguration.getHeat_template_version() == null) {
57                 log.debug("HEAT doesn't contain required \"heat_template_version\" section.");
58                 return Either.right(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT);
59             }
60             return Either.left(true);
61         }
62
63         @Override
64         public boolean isHeatRelated() {
65             return true;
66         }
67
68         private boolean isNotValidYaml(byte[] payload, YamlToObjectConverter yamlToObjectConverter) {
69             return !yamlToObjectConverter.isValidYaml(payload);
70         }
71     },
72     HEAT_ENV {
73         @Override
74         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
75             return isValidYaml(payload);
76         }
77
78         @Override
79         public boolean isHeatRelated() {
80             return true;
81         }
82     },
83     XML {
84         @Override
85         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
86             try {
87                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
88                 saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
89                 saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
90                 XMLReader reader = saxParser.getXMLReader();
91                 setFeatures(reader);
92                 reader.parse(new InputSource(new ByteArrayInputStream(payload)));
93             } catch (ParserConfigurationException | IOException | SAXException exception) {
94                 log.debug("Xml is invalid : {}", exception.getMessage(), exception);
95                 return Either.right(ActionStatus.INVALID_XML);
96             }
97             return Either.left(true);
98         }
99
100         private void setFeatures(XMLReader reader) throws SAXNotSupportedException {
101             try {
102                 reader.setFeature("http://apache.org/xml/features/validation/schema", false);
103                 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
104             } catch (SAXNotRecognizedException exception) {
105                 log.debug("Xml parser couldn't set feature: \"http://apache.org/xml/features/validation/schema\", false",
106                         exception.getMessage(), exception);
107             }
108         }
109     },
110     JSON {
111         @Override
112         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
113             try {
114                 gson.fromJson(new String(payload), Object.class);
115             } catch (Exception e) {
116                 log.debug("Json is invalid : {}", e.getMessage(), e);
117                 return Either.right(ActionStatus.INVALID_JSON);
118             }
119             return Either.left(true);
120         }
121     },
122     YAML {
123         @Override
124         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
125             return isValidYaml(payload);
126         }
127     },
128     NOT_DEFINED {
129         @Override
130         public Either<Boolean, ActionStatus> isValid(byte[] payload) {
131             return Either.left(true);
132         }
133     };
134
135     private static final Logger log = Logger.getLogger(PayloadTypeEnum.class);
136     private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
137
138     public abstract Either<Boolean, ActionStatus> isValid(byte[] payload);
139
140     public boolean isHeatRelated() {
141         return false;
142     }
143
144     private static Either<Boolean, ActionStatus> isValidYaml(byte[] payload) {
145         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
146         if (yamlToObjectConverter.isValidYaml(payload)) {
147             log.debug("Invalid YAML format");
148             return Either.left(true);
149         }
150         return Either.right(ActionStatus.INVALID_YAML);
151     }
152
153 }