e05b83333f7632c5999ac929ae92ea0085062ac0
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
21
22 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor.ErrorMessage.INVALID_APEX_CONFIG;
23 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor.ErrorMessage.INVALID_ENTRY;
24 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor.ErrorMessage.MISSING_ENTRY;
25 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.ENGINE_SERVICE_PARAMETERS;
26
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.HashSet;
32 import java.util.Set;
33 import lombok.AllArgsConstructor;
34 import org.apache.commons.io.IOUtils;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Process the Apex Config JSON template file.
42  */
43 public class ApexConfigProcessor {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApexConfigProcessor.class);
46
47     private final StandardCoder standardCoder;
48
49     public ApexConfigProcessor(final StandardCoder standardCoder) {
50         this.standardCoder = standardCoder;
51     }
52
53     /**
54      * Process the Apex Config JSON template file.
55      *
56      * @param apexConfigInputStream the input stream for the Apex Config JSON template
57      * @return the result of the processing with the read JSON and its errors.
58      */
59     public ProcessedTemplate process(final InputStream apexConfigInputStream) throws IOException {
60         final ProcessedTemplate processedTemplate = new ProcessedTemplate();
61         final String templateAsString;
62         try (final InputStream inputStream = apexConfigInputStream) {
63             templateAsString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
64         }
65         final Set<String> errorSet = validate(templateAsString);
66         processedTemplate.setContent(templateAsString);
67         processedTemplate.addToErrors(errorSet);
68
69         return processedTemplate;
70     }
71
72     private Set<String> validate(final String apexConfig) {
73         final Set<String> errorSet = new HashSet<>();
74         final JsonObject apexConfigJson;
75         try {
76             apexConfigJson = standardCoder.decode(apexConfig, JsonObject.class);
77         } catch (final CoderException e) {
78             LOGGER.debug(INVALID_APEX_CONFIG.getMessage(), e);
79             errorSet.add(INVALID_APEX_CONFIG.getMessage());
80             return errorSet;
81         }
82
83         final JsonObject topologyTemplate;
84         try {
85             topologyTemplate = apexConfigJson.getAsJsonObject(ENGINE_SERVICE_PARAMETERS.getKey());
86         } catch (final Exception e) {
87             final String errorMsg = INVALID_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey());
88             LOGGER.debug(errorMsg, e);
89             errorSet.add(errorMsg);
90             return errorSet;
91         }
92
93         if (topologyTemplate == null) {
94             errorSet.add(MISSING_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey()));
95             return errorSet;
96         }
97
98         return errorSet;
99     }
100
101     /**
102      * Stores the possible error messages for the Apex Config template validation process.
103      */
104     @AllArgsConstructor
105     public enum ErrorMessage {
106         MISSING_ENTRY("Missing '%s' entry"),
107         INVALID_ENTRY("Invalid entry '%s' provided"),
108         INVALID_APEX_CONFIG("Invalid apex config provided");
109
110         private final String messageFormat;
111
112         public String getMessage(final String... params) {
113             return String.format(this.messageFormat, params);
114         }
115     }
116
117 }