2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.parameters.engineservice;
24 import org.onap.policy.apex.core.engine.EngineParameters;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
27 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ParameterGroup;
30 import org.onap.policy.common.parameters.ValidationStatus;
34 * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
36 * <p>The following parameters are defined:
38 * <li>name: The name of the Apex engine service, which can be set to any value that matches the regular expression
39 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#NAME_REGEXP}.
40 * <li>version: The name of the Apex engine service, which can be set to any value that matches the regular expression
41 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#VERSION_REGEXP}.
42 * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
43 * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
45 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
46 * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
47 * service, and to update the policy model of the engine service.
48 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
49 * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
50 * executors and context management for the engines.
51 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
52 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
53 * APEX, 0 means no periodic event generation, negative values are illegal.
57 public class EngineServiceParameters implements ParameterGroup {
58 private static final int MAX_PORT = 65535;
61 /** The default name of the Apex engine service. */
62 public static final String DEFAULT_NAME = "ApexEngineService";
64 /** The default version of the Apex engine service. */
65 public static final String DEFAULT_VERSION = "1.0.0";
67 /** The default ID of the Apex engine service. */
68 public static final int DEFAULT_ID = -1;
70 /** The default instance count for the Apex engine service. */
71 public static final int DEFAULT_INSTANCE_COUNT = 1;
73 /** The default EngDep deployment port of the Apex engine service. */
74 public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
76 // Constants for repeated strings
77 private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
79 // Apex engine service parameters
80 private String name = DEFAULT_NAME;
81 private String version = DEFAULT_VERSION;
82 private int id = DEFAULT_ID;
83 private int instanceCount = DEFAULT_INSTANCE_COUNT;
84 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
85 private String policyModelFileName = null;
86 private long periodicEventPeriod = 0;
89 // Apex engine internal parameters
90 private EngineParameters engineParameters = new EngineParameters();
93 * Constructor to create an apex engine service parameters instance and register the instance with the parameter
96 public EngineServiceParameters() {
99 // Set the name for the parameters
100 this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
104 * Gets the key of the Apex engine service.
106 * @return the Apex engine service key
108 public AxArtifactKey getEngineKey() {
109 return new AxArtifactKey(name, version);
113 * Sets the key of the Apex engine service.
115 * @param key the the Apex engine service key
117 public void setEngineKey(final AxArtifactKey key) {
118 this.setName(key.getName());
119 this.setVersion(key.getVersion());
123 * Gets the name of the engine service.
125 * @return the name of the engine service
127 public String getName() {
132 * Sets the name of the engine service.
134 * @param name the name of the engine service
136 public void setName(final String name) {
141 * Gets the version of the engine service.
143 * @return the version of the engine service
145 public String getVersion() {
150 * Sets the version of the engine service.
152 * @param version the version of the engine service
154 public void setVersion(final String version) {
155 this.version = version;
159 * Gets the id of the engine service.
161 * @return the id of the engine service
168 * Sets the id of the engine service.
170 * @param id the id of the engine service
172 public void setId(final int id) {
177 * Gets the instance count of the engine service.
179 * @return the instance count of the engine service
181 public int getInstanceCount() {
182 return instanceCount;
186 * Sets the instance count of the engine service.
188 * @param instanceCount the instance count of the engine service
190 public void setInstanceCount(final int instanceCount) {
191 this.instanceCount = instanceCount;
195 * Gets the deployment port of the engine service.
197 * @return the deployment port of the engine service
199 public int getDeploymentPort() {
200 return deploymentPort;
204 * Sets the deployment port of the engine service.
206 * @param deploymentPort the deployment port of the engine service
208 public void setDeploymentPort(final int deploymentPort) {
209 this.deploymentPort = deploymentPort;
213 * Gets the file name of the policy engine for deployment on the engine service.
215 * @return the file name of the policy engine for deployment on the engine service
217 public String getPolicyModelFileName() {
218 return policyModelFileName;
222 * Sets the file name of the policy engine for deployment on the engine service.
224 * @param policyModelFileName the file name of the policy engine for deployment on the engine service
226 public void setPolicyModelFileName(final String policyModelFileName) {
227 this.policyModelFileName = policyModelFileName;
231 * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
233 * @return the periodic period
235 public long getPeriodicEventPeriod() {
236 return periodicEventPeriod;
240 * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
241 * negative values are illegal.
243 * @param periodicEventPeriod the periodic period
245 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
246 this.periodicEventPeriod = periodicEventPeriod;
250 * Gets the engine parameters for engines in the engine service.
252 * @return the engine parameters for engines in the engine service
254 public EngineParameters getEngineParameters() {
255 return engineParameters;
259 * Sets the engine parameters for engines in the engine service.
261 * @param engineParameters the engine parameters for engines in the engine service
263 public void setEngineParameters(final EngineParameters engineParameters) {
264 this.engineParameters = engineParameters;
271 public GroupValidationResult validate() {
272 final GroupValidationResult result = new GroupValidationResult(this);
274 validateStringParameters(result);
276 validateNumericParameters(result);
278 if (policyModelFileName != null) {
279 validatePolicyModelFileName(result);
281 result.setResult("engineParameters", engineParameters.validate());
287 * Validate string parameters.
289 * @param result the result of string parameter validation
291 private void validateStringParameters(final GroupValidationResult result) {
292 if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
293 result.setResult("name", ValidationStatus.INVALID,
294 "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
297 if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
298 result.setResult("version", ValidationStatus.INVALID,
299 "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
304 * Validate numeric parameters.
306 * @param result the result of numeric parameter validation
308 private void validateNumericParameters(final GroupValidationResult result) {
310 result.setResult("id", ValidationStatus.INVALID,
311 "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
314 if (instanceCount < 1) {
315 result.setResult("instanceCount", ValidationStatus.INVALID,
316 "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
319 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
320 result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
321 + "] invalid, must be specified as 1024 <= port <= 65535");
324 if (periodicEventPeriod < 0) {
325 result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
326 + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
331 * Validate the policy model file name parameter.
333 * @param result the variable in which to store the result of the validation
335 private void validatePolicyModelFileName(final GroupValidationResult result) {
336 if (policyModelFileName.trim().length() == 0) {
337 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID,
338 "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string");
342 String absolutePolicyFileName = null;
344 // Resolve the file name if it is a relative file name
345 File policyModelFile = new File(policyModelFileName);
346 if (policyModelFile.isAbsolute()) {
347 absolutePolicyFileName = policyModelFileName;
349 absolutePolicyFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator
350 + policyModelFileName;
351 policyModelFile = new File(absolutePolicyFileName);
354 // Check that the file exists
355 if (!policyModelFile.exists()) {
356 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found");
357 } else if (!policyModelFile.isFile()) {
358 // Check that the file is a regular file
359 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not a plain file");
361 // OK, we found the file and it's OK, so reset the file name
362 policyModelFileName = absolutePolicyFileName;
364 // Check that the file is readable
365 if (!policyModelFile.canRead()) {
366 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not readable");