2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.Map.Entry;
34 import org.onap.policy.apex.core.engine.EngineParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
37 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
38 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJsonAdapter;
39 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJsonAdapter;
40 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
41 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJsonAdapter;
42 import org.onap.policy.common.parameters.ParameterException;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.parameters.ValidationResult;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
51 * This class handles reading, parsing and validating of Apex parameters from JSON files.
53 * @author Liam Fallon (liam.fallon@ericsson.com)
55 public class ApexParameterHandler {
56 private static final String EVENT_OUTPUT_PARAMETERS = "eventOutputParameters";
58 private static final String EVENT_INPUT_PARAMETERS = "eventInputParameters";
60 private static final String ENGINE_SERVICE_PARAMETERS = "engineServiceParameters";
62 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
64 private static final String POLICY_TYPE_IMPL = "policy_type_impl";
65 private String policyModel;
66 private String apexConfig;
69 * Read the parameters from the parameter file.
71 * @param arguments the arguments passed to Apex
72 * @return the parameters read from the configuration file
73 * @throws ParameterException on parameter exceptions
75 public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {
77 ApexParameters parameters = null;
78 String toscaPolicyFilePath = arguments.getToscaPolicyFilePath();
79 // Read the parameters
81 parseConfigAndModel(toscaPolicyFilePath);
82 // Register the adapters for our carrier technologies and event protocols with GSON
84 final Gson gson = new GsonBuilder()
85 .registerTypeAdapter(EngineParameters.class,
86 new EngineServiceParametersJsonAdapter())
87 .registerTypeAdapter(CarrierTechnologyParameters.class,
88 new CarrierTechnologyParametersJsonAdapter())
89 .registerTypeAdapter(EventProtocolParameters.class,
90 new EventProtocolParametersJsonAdapter())
93 parameters = gson.fromJson(apexConfig, ApexParameters.class);
94 } catch (final Exception e) {
95 final String errorMessage = "error reading parameters from \"" + toscaPolicyFilePath + "\"\n" + "("
96 + e.getClass().getSimpleName() + "):" + e.getMessage();
97 throw new ParameterException(errorMessage, e);
100 // The JSON processing returns null if there is an empty file
101 if (parameters == null) {
102 final String errorMessage = "no parameters found in \"" + toscaPolicyFilePath + "\"";
103 throw new ParameterException(errorMessage);
106 if (null != parameters.getEngineServiceParameters()) {
107 parameters.getEngineServiceParameters().setPolicyModel(policyModel);
110 // Validate the parameters
111 final ValidationResult validationResult = parameters.validate();
112 if (!validationResult.isValid()) {
113 String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
114 returnMessage += validationResult.getResult();
115 throw new ParameterException(returnMessage);
118 if (!validationResult.isClean()) {
119 String returnMessage = "validation messages(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
120 returnMessage += validationResult.getResult();
122 LOGGER.info(returnMessage);
129 * Register all the incoming parameters with the parameter service.
131 * @param parameters The parameters to register
133 public void registerParameters(ApexParameters parameters) {
134 ParameterService.register(parameters);
135 ParameterService.register(parameters.getEngineServiceParameters());
136 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
137 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
138 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
139 .getSchemaParameters());
140 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
141 .getDistributorParameters());
142 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
143 .getLockManagerParameters());
144 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
145 .getPersistorParameters());
148 private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException {
151 final StandardCoder standardCoder = new StandardCoder();
152 JsonObject apexConfigJsonObject = new JsonObject();
154 ToscaServiceTemplate toscaServiceTemplate = standardCoder
155 .decode(Files.readString(Paths.get(toscaPolicyFilePath)), ToscaServiceTemplate.class);
156 for (Entry<String, Object> property : toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
157 .entrySet().iterator().next().getValue().getProperties().entrySet()) {
158 JsonElement body = null;
159 if ("javaProperties".equals(property.getKey())) {
160 body = standardCoder.convert(property.getValue(), JsonArray.class);
161 } else if (EVENT_INPUT_PARAMETERS.equals(property.getKey())
162 || ENGINE_SERVICE_PARAMETERS.equals(property.getKey())
163 || EVENT_OUTPUT_PARAMETERS.equals(property.getKey())) {
164 body = standardCoder.convert(property.getValue(), JsonObject.class);
165 if (ENGINE_SERVICE_PARAMETERS.equals(property.getKey())) {
166 JsonElement policyModelObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
167 if (null != policyModelObject) {
168 policyModel = standardCoder.encode(policyModelObject);
170 ((JsonObject) body).remove(POLICY_TYPE_IMPL);
173 apexConfigJsonObject.add(property.getKey(), body);
175 apexConfig = standardCoder.encode(apexConfigJsonObject);
176 } catch (Exception e) {
177 throw new ApexException("Parsing config and model from the tosca policy failed.", e);