2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.parameters;
25 import com.google.gson.Gson;
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.GroupValidationResult;
42 import org.onap.policy.common.parameters.ParameterException;
43 import org.onap.policy.common.parameters.ParameterService;
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 ParameterService.clear();
78 ApexParameters parameters = null;
79 String toscaPolicyFilePath = arguments.getToscaPolicyFilePath();
80 // Read the parameters
82 parseConfigAndModel(toscaPolicyFilePath);
83 // Register the adapters for our carrier technologies and event protocols with GSON
85 final Gson gson = new GsonBuilder()
86 .registerTypeAdapter(EngineParameters.class,
87 new EngineServiceParametersJsonAdapter())
88 .registerTypeAdapter(CarrierTechnologyParameters.class,
89 new CarrierTechnologyParametersJsonAdapter())
90 .registerTypeAdapter(EventProtocolParameters.class,
91 new EventProtocolParametersJsonAdapter())
94 parameters = gson.fromJson(apexConfig, ApexParameters.class);
95 } catch (final Exception e) {
96 final String errorMessage = "error reading parameters from \"" + toscaPolicyFilePath + "\"\n" + "("
97 + e.getClass().getSimpleName() + "):" + e.getMessage();
98 throw new ParameterException(errorMessage, e);
101 // The JSON processing returns null if there is an empty file
102 if (parameters == null) {
103 final String errorMessage = "no parameters found in \"" + toscaPolicyFilePath + "\"";
104 throw new ParameterException(errorMessage);
107 if (null != parameters.getEngineServiceParameters()) {
108 parameters.getEngineServiceParameters().setPolicyModel(policyModel);
111 // Validate the parameters
112 final GroupValidationResult validationResult = parameters.validate();
113 if (!validationResult.isValid()) {
114 String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
115 returnMessage += validationResult.getResult();
116 throw new ParameterException(returnMessage);
119 if (!validationResult.isClean()) {
120 String returnMessage = "validation messages(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
121 returnMessage += validationResult.getResult();
123 LOGGER.info(returnMessage);
130 * Register all the incoming parameters with the parameter service.
132 * @param parameters The parameters to register
134 public void registerParameters(ApexParameters parameters) {
135 ParameterService.register(parameters);
136 ParameterService.register(parameters.getEngineServiceParameters());
137 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
138 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
139 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
140 .getSchemaParameters());
141 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
142 .getDistributorParameters());
143 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
144 .getLockManagerParameters());
145 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
146 .getPersistorParameters());
149 private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException {
152 final StandardCoder standardCoder = new StandardCoder();
153 JsonObject apexConfigJsonObject = new JsonObject();
155 ToscaServiceTemplate toscaServiceTemplate = standardCoder
156 .decode(Files.readString(Paths.get(toscaPolicyFilePath)), ToscaServiceTemplate.class);
157 for (Entry<String, Object> property : toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
158 .entrySet().iterator().next().getValue().getProperties().entrySet()) {
159 JsonElement body = null;
160 if ("javaProperties".equals(property.getKey())) {
161 body = standardCoder.convert(property.getValue(), JsonArray.class);
162 } else if (EVENT_INPUT_PARAMETERS.equals(property.getKey())
163 || ENGINE_SERVICE_PARAMETERS.equals(property.getKey())
164 || EVENT_OUTPUT_PARAMETERS.equals(property.getKey())) {
165 body = standardCoder.convert(property.getValue(), JsonObject.class);
166 if (ENGINE_SERVICE_PARAMETERS.equals(property.getKey())) {
167 JsonElement policyModelObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
168 if (null != policyModelObject) {
169 policyModel = standardCoder.encode(policyModelObject);
171 ((JsonObject) body).remove(POLICY_TYPE_IMPL);
174 apexConfigJsonObject.add(property.getKey(), body);
176 apexConfig = standardCoder.encode(apexConfigJsonObject);
177 } catch (Exception e) {
178 throw new ApexException("Parsing config and model from the tosca policy failed.", e);