aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java
blob: f88733f60de889c06ba2e81e34c76684381216ba (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 *  Modifications Copyright (C) 2020 Bell Canada. 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.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileReader;
import org.onap.policy.apex.core.engine.EngineParameters;
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.GroupValidationResult;
import org.onap.policy.common.parameters.ParameterException;
import org.onap.policy.common.parameters.ParameterService;
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 XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);

    /**
     * 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 {

        ParameterService.clear();

        ApexParameters parameters = null;

        // Read the parameters
        try {
            // Register the adapters for our carrier technologies and event protocols with GSON
            // @formatter:off
            final Gson gson = new GsonBuilder()
                            .registerTypeAdapter(EngineParameters.class,
                                            new EngineServiceParametersJsonAdapter())
                            .registerTypeAdapter(CarrierTechnologyParameters.class,
                                            new CarrierTechnologyParametersJsonAdapter())
                            .registerTypeAdapter(EventProtocolParameters.class,
                                            new EventProtocolParametersJsonAdapter())
                            .create();
            // @formatter:on
            parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class);
        } catch (final Exception e) {
            final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
                            + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
            LOGGER.error(errorMessage, e);
            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 \"" + arguments.getConfigurationFilePath() + "\"";
            LOGGER.error(errorMessage);
            throw new ParameterException(errorMessage);
        }

        // Check if we should override the model file parameter
        final String modelFilePath = arguments.getModelFilePath();
        if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) {
            parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath);
        }

        // Validate the parameters
        final GroupValidationResult validationResult = parameters.validate();
        if (!validationResult.isValid()) {
            String returnMessage = "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath()
                            + "\"\n";
            returnMessage += validationResult.getResult();

            LOGGER.error(returnMessage);
            throw new ParameterException(returnMessage);
        }

        if (!validationResult.isClean()) {
            String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath()
                            + "\"\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());
    }
}