2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
6 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.service.parameters;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Map.Entry;
33 import org.onap.policy.apex.core.engine.EngineParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
35 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
36 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
37 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJsonAdapter;
38 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJsonAdapter;
39 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
40 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJsonAdapter;
41 import org.onap.policy.common.parameters.ParameterException;
42 import org.onap.policy.common.parameters.ParameterService;
43 import org.onap.policy.common.parameters.ValidationResult;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.slf4j.ext.XLogger;
47 import org.slf4j.ext.XLoggerFactory;
50 * This class handles reading, parsing and validating of Apex parameters from JSON files.
52 * @author Liam Fallon (liam.fallon@ericsson.com)
54 public class ApexParameterHandler {
55 private static final String EVENT_OUTPUT_PARAMETERS = "eventOutputParameters";
57 private static final String EVENT_INPUT_PARAMETERS = "eventInputParameters";
59 private static final String ENGINE_SERVICE_PARAMETERS = "engineServiceParameters";
61 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
63 private static final String POLICY_TYPE_IMPL = "policy_type_impl";
64 private String policyModel;
65 private String apexConfig;
68 * Read the parameters from the parameter file.
70 * @param arguments the arguments passed to Apex
71 * @return the parameters read from the configuration file
72 * @throws ParameterException on parameter exceptions
74 public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {
76 ApexParameters parameters = null;
77 String toscaPolicyFilePath = arguments.getToscaPolicyFilePath();
78 // Read the parameters
80 parseConfigAndModel(toscaPolicyFilePath);
81 // Register the adapters for our carrier technologies and event protocols with GSON
83 final var gson = new GsonBuilder()
84 .registerTypeAdapter(EngineParameters.class,
85 new EngineServiceParametersJsonAdapter())
86 .registerTypeAdapter(CarrierTechnologyParameters.class,
87 new CarrierTechnologyParametersJsonAdapter())
88 .registerTypeAdapter(EventProtocolParameters.class,
89 new EventProtocolParametersJsonAdapter())
92 parameters = gson.fromJson(apexConfig, ApexParameters.class);
93 } catch (final Exception e) {
94 final String errorMessage = "error reading parameters from \"" + toscaPolicyFilePath + "\"\n" + "("
95 + e.getClass().getSimpleName() + "):" + e.getMessage();
96 throw new ParameterException(errorMessage, e);
99 // The JSON processing returns null if there is an empty file
100 if (parameters == null) {
101 final String errorMessage = "no parameters found in \"" + toscaPolicyFilePath + "\"";
102 throw new ParameterException(errorMessage);
105 if (null != parameters.getEngineServiceParameters()) {
106 parameters.getEngineServiceParameters().setPolicyModel(policyModel);
109 // Validate the parameters
110 final ValidationResult validationResult = parameters.validate();
111 if (!validationResult.isValid()) {
112 String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
113 returnMessage += validationResult.getResult();
114 throw new ParameterException(returnMessage);
117 if (!validationResult.isClean()) {
118 String returnMessage = "validation messages(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
119 returnMessage += validationResult.getResult();
121 LOGGER.info(returnMessage);
128 * Register all the incoming parameters with the parameter service.
130 * @param parameters The parameters to register
132 public void registerParameters(ApexParameters parameters) {
133 ParameterService.register(parameters);
134 ParameterService.register(parameters.getEngineServiceParameters());
135 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
136 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
137 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
138 .getSchemaParameters());
139 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
140 .getDistributorParameters());
141 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
142 .getLockManagerParameters());
143 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
144 .getPersistorParameters());
147 private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException {
150 final var standardCoder = new StandardCoder();
151 var apexConfigJsonObject = new JsonObject();
153 var toscaServiceTemplate = standardCoder
154 .decode(Files.readString(Paths.get(toscaPolicyFilePath)), ToscaServiceTemplate.class);
155 for (Entry<String, Object> property : toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
156 .entrySet().iterator().next().getValue().getProperties().entrySet()) {
157 JsonElement body = null;
158 if ("javaProperties".equals(property.getKey())) {
159 body = standardCoder.convert(property.getValue(), JsonArray.class);
160 } else if (EVENT_INPUT_PARAMETERS.equals(property.getKey())
161 || ENGINE_SERVICE_PARAMETERS.equals(property.getKey())
162 || EVENT_OUTPUT_PARAMETERS.equals(property.getKey())) {
163 body = standardCoder.convert(property.getValue(), JsonObject.class);
164 if (ENGINE_SERVICE_PARAMETERS.equals(property.getKey())) {
165 JsonElement policyModelObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
166 if (null != policyModelObject) {
167 policyModel = standardCoder.encode(policyModelObject);
169 ((JsonObject) body).remove(POLICY_TYPE_IMPL);
172 apexConfigJsonObject.add(property.getKey(), body);
174 apexConfig = standardCoder.encode(apexConfigJsonObject);
175 } catch (Exception e) {
176 throw new ApexException("Parsing config and model from the tosca policy failed.", e);