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
10 * * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * * SPDX-License-Identifier: Apache-2.0
19 * * ============LICENSE_END=========================================================
23 package org.openecomp.sdc.be.components.impl.artifact;
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;
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;
45 public enum PayloadTypeEnum {
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);
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);
60 return Either.left(true);
64 public boolean isHeatRelated() {
68 private boolean isNotValidYaml(byte[] payload, YamlToObjectConverter yamlToObjectConverter) {
69 return !yamlToObjectConverter.isValidYaml(payload);
74 public Either<Boolean, ActionStatus> isValid(byte[] payload) {
75 return isValidYaml(payload);
79 public boolean isHeatRelated() {
85 public Either<Boolean, ActionStatus> isValid(byte[] payload) {
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();
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);
97 return Either.left(true);
100 private void setFeatures(XMLReader reader) throws SAXNotSupportedException {
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);
112 public Either<Boolean, ActionStatus> isValid(byte[] payload) {
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);
119 return Either.left(true);
124 public Either<Boolean, ActionStatus> isValid(byte[] payload) {
125 return isValidYaml(payload);
130 public Either<Boolean, ActionStatus> isValid(byte[] payload) {
131 return Either.left(true);
135 private static final Logger log = Logger.getLogger(PayloadTypeEnum.class);
136 private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
138 public abstract Either<Boolean, ActionStatus> isValid(byte[] payload);
140 public boolean isHeatRelated() {
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);
150 return Either.right(ActionStatus.INVALID_YAML);