summaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java
blob: 72cbd28004dcd06f43be8ae27c5d842c7bd29745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2022 Nordix Foundation.
 *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.parameters;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.onap.policy.apex.core.engine.EngineParameters;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJsonAdapter;
import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJsonAdapter;
import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJsonAdapter;
import org.onap.policy.common.parameters.ParameterException;
import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.common.parameters.ValidationResult;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * This class handles reading, parsing and validating of Apex parameters from JSON files.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ApexParameterHandler {
    private static final String EVENT_OUTPUT_PARAMETERS = "eventOutputParameters";

    private static final String EVENT_INPUT_PARAMETERS = "eventInputParameters";

    private static final String ENGINE_SERVICE_PARAMETERS = "engineServiceParameters";

    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);

    private static final String POLICY_TYPE_IMPL = "policy_type_impl";
    private static final String APEX_POLICY_MODEL = "apexPolicyModel";
    private static final String METADATA_SET = "metadataSet";

    private String policyModel;
    private String apexConfig;

    /**
     * Read the parameters from the parameter file.
     *
     * @param arguments the arguments passed to Apex
     * @return the parameters read from the configuration file
     * @throws ParameterException on parameter exceptions
     */
    public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {

        ApexParameters parameters = null;
        String toscaPolicyFilePath = arguments.getToscaPolicyFilePath();
        // Read the parameters
        try {
            parseConfigAndModel(toscaPolicyFilePath);
            // Register the adapters for our carrier technologies and event protocols with GSON
            // @formatter:off
            final var gson = new GsonBuilder()
                .registerTypeAdapter(EngineParameters.class,
                    new EngineServiceParametersJsonAdapter())
                .registerTypeAdapter(CarrierTechnologyParameters.class,
                    new CarrierTechnologyParametersJsonAdapter())
                .registerTypeAdapter(EventProtocolParameters.class,
                    new EventProtocolParametersJsonAdapter())
                .create();
            // @formatter:on
            parameters = gson.fromJson(apexConfig, ApexParameters.class);
        } catch (final Exception e) {
            final String errorMessage = "error reading parameters from \"" + toscaPolicyFilePath + "\"\n" + "("
                + e.getClass().getSimpleName() + "):" + e.getMessage();
            throw new ParameterException(errorMessage, e);
        }

        // The JSON processing returns null if there is an empty file
        if (parameters == null) {
            final String errorMessage = "no parameters found in \"" + toscaPolicyFilePath + "\"";
            throw new ParameterException(errorMessage);
        }

        if (null != parameters.getEngineServiceParameters()) {
            parameters.getEngineServiceParameters().setPolicyModel(policyModel);
        }

        // Validate the parameters
        final ValidationResult validationResult = parameters.validate();
        if (!validationResult.isValid()) {
            String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
            returnMessage += validationResult.getResult();
            throw new ParameterException(returnMessage);
        }

        if (!validationResult.isClean()) {
            String returnMessage = "validation messages(s) on parameters from \"" + toscaPolicyFilePath + "\"\n";
            returnMessage += validationResult.getResult();

            LOGGER.info(returnMessage);
        }

        return parameters;
    }

    /**
     * Register all the incoming parameters with the parameter service.
     *
     * @param parameters The parameters to register
     */
    public void registerParameters(ApexParameters parameters) {
        ParameterService.register(parameters);
        ParameterService.register(parameters.getEngineServiceParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
            .getSchemaParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
            .getDistributorParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
            .getLockManagerParameters());
        ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
            .getPersistorParameters());
    }

    private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException {
        policyModel = null;
        apexConfig = null;
        final var standardCoder = new StandardCoder();
        var apexConfigJsonObject = new JsonObject();
        try {
            var toscaServiceTemplate = standardCoder
                .decode(Files.readString(Paths.get(toscaPolicyFilePath)), ToscaServiceTemplate.class);
            for (Entry<String, Object> property : toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
                .entrySet().iterator().next().getValue().getProperties().entrySet()) {
                JsonElement body = null;
                if ("javaProperties".equals(property.getKey())) {
                    body = standardCoder.convert(property.getValue(), JsonArray.class);
                } else if (EVENT_INPUT_PARAMETERS.equals(property.getKey())
                    || ENGINE_SERVICE_PARAMETERS.equals(property.getKey())
                    || EVENT_OUTPUT_PARAMETERS.equals(property.getKey())) {
                    body = standardCoder.convert(property.getValue(), JsonObject.class);
                    if (ENGINE_SERVICE_PARAMETERS.equals(property.getKey())) {
                        policyModel = extractPolicyModel(standardCoder, body);
                    }
                }
                apexConfigJsonObject.add(property.getKey(), body);
            }
            apexConfig = standardCoder.encode(apexConfigJsonObject);

            // populate policyModel from metadata if present
            Optional<Map<String, Object>> metadata =
                Optional.ofNullable(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
                    .entrySet().iterator().next().getValue().getMetadata());
            if (metadata.isPresent() && metadata.get().containsKey(METADATA_SET)) {
                JsonElement body = standardCoder.convert(metadata.get(), JsonObject.class);
                policyModel = extractPolicyModel(standardCoder, body);
            }
        } catch (Exception e) {
            throw new ApexException("Parsing config and model from the tosca policy failed.", e);
        }
    }

    private String extractPolicyModel(StandardCoder standardCoder, JsonElement body) throws CoderException {
        JsonElement policyTypeImplObject = null;
        // Check for "policy_type_impl, if not present check for "metadataSet"
        if (body.getAsJsonObject().has(POLICY_TYPE_IMPL)) {
            policyTypeImplObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
        } else if (body.getAsJsonObject().has(METADATA_SET))  {
            policyTypeImplObject = ((JsonObject) body).get(METADATA_SET);
        }
        if (null == policyTypeImplObject) {
            return null;
        }

        // "policy_type_impl" found
        if (policyTypeImplObject instanceof JsonObject) {

            // Check for "apexPolicyModel", this is used to encapsulate policy models sometimes
            JsonElement policyModelObject = ((JsonObject) policyTypeImplObject).get(APEX_POLICY_MODEL);

            if (policyModelObject != null) {
                // Policy model encased in an "apexPolicyModel" object
                return standardCoder.encode(policyModelObject);
            } else {
                // No encasement
                return standardCoder.encode(policyTypeImplObject);
            }
        } else {
            return policyTypeImplObject.getAsString();
        }
    }
}