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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
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;
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;
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;
41 * Process the Apex Config JSON template file.
43 public class ApexConfigProcessor {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ApexConfigProcessor.class);
47 private final StandardCoder standardCoder;
49 public ApexConfigProcessor(final StandardCoder standardCoder) {
50 this.standardCoder = standardCoder;
54 * Process the Apex Config JSON template file.
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.
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);
65 final Set<String> errorSet = validate(templateAsString);
66 processedTemplate.setContent(templateAsString);
67 processedTemplate.addToErrors(errorSet);
69 return processedTemplate;
72 private Set<String> validate(final String apexConfig) {
73 final Set<String> errorSet = new HashSet<>();
74 final JsonObject apexConfigJson;
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());
83 final JsonObject topologyTemplate;
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);
93 if (topologyTemplate == null) {
94 errorSet.add(MISSING_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey()));
102 * Stores the possible error messages for the Apex Config template validation process.
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");
110 private final String messageFormat;
112 public String getMessage(final String... params) {
113 return String.format(this.messageFormat, params);