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;
25 import org.onap.policy.apex.core.engine.EngineParameters;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
28 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.parameters.ParameterGroup;
31 import org.onap.policy.common.parameters.ValidationStatus;
35 * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
37 * <p>The following parameters are defined:
39 * <li>name: The name of the Apex engine service, which can be set to any value that matches the regular expression
40 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#NAME_REGEXP}.
41 * <li>version: The name of the Apex engine service, which can be set to any value that matches the regular expression
42 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#VERSION_REGEXP}.
43 * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
44 * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
46 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
47 * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
48 * service, and to update the policy model of the engine service.
49 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
50 * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
51 * executors and context management for the engines.
52 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
53 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
54 * APEX, 0 means no periodic event generation, negative values are illegal.
58 public class EngineServiceParameters implements ParameterGroup {
59 private static final int MAX_PORT = 65535;
62 /** The default name of the Apex engine service. */
63 public static final String DEFAULT_NAME = "ApexEngineService";
65 /** The default version of the Apex engine service. */
66 public static final String DEFAULT_VERSION = "1.0.0";
68 /** The default ID of the Apex engine service. */
69 public static final int DEFAULT_ID = -1;
71 /** The default instance count for the Apex engine service. */
72 public static final int DEFAULT_INSTANCE_COUNT = 1;
74 /** The default EngDep deployment port of the Apex engine service. */
75 public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
77 // Constants for repeated strings
78 private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
80 // Apex engine service parameters
81 private String name = DEFAULT_NAME;
82 private String version = DEFAULT_VERSION;
83 private int id = DEFAULT_ID;
84 private int instanceCount = DEFAULT_INSTANCE_COUNT;
85 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
86 private String policyModelFileName = null;
87 private long periodicEventPeriod = 0;
90 // Apex engine internal parameters
91 private EngineParameters engineParameters = new EngineParameters();
94 * Constructor to create an apex engine service parameters instance and register the instance with the parameter
97 public EngineServiceParameters() {
100 // Set the name for the parameters
101 this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
105 * Gets the key of the Apex engine service.
107 * @return the Apex engine service key
109 public AxArtifactKey getEngineKey() {
110 return new AxArtifactKey(name, version);
114 * Sets the key of the Apex engine service.
116 * @param key the the Apex engine service key
118 public void setEngineKey(final AxArtifactKey key) {
119 this.setName(key.getName());
120 this.setVersion(key.getVersion());
124 * Gets the name of the engine service.
126 * @return the name of the engine service
128 public String getName() {
133 * Sets the name of the engine service.
135 * @param name the name of the engine service
137 public void setName(final String name) {
142 * Gets the version of the engine service.
144 * @return the version of the engine service
146 public String getVersion() {
151 * Sets the version of the engine service.
153 * @param version the version of the engine service
155 public void setVersion(final String version) {
156 this.version = version;
160 * Gets the id of the engine service.
162 * @return the id of the engine service
169 * Sets the id of the engine service.
171 * @param id the id of the engine service
173 public void setId(final int id) {
178 * Gets the instance count of the engine service.
180 * @return the instance count of the engine service
182 public int getInstanceCount() {
183 return instanceCount;
187 * Sets the instance count of the engine service.
189 * @param instanceCount the instance count of the engine service
191 public void setInstanceCount(final int instanceCount) {
192 this.instanceCount = instanceCount;
196 * Gets the deployment port of the engine service.
198 * @return the deployment port of the engine service
200 public int getDeploymentPort() {
201 return deploymentPort;
205 * Sets the deployment port of the engine service.
207 * @param deploymentPort the deployment port of the engine service
209 public void setDeploymentPort(final int deploymentPort) {
210 this.deploymentPort = deploymentPort;
214 * Gets the file name of the policy engine for deployment on the engine service.
216 * @return the file name of the policy engine for deployment on the engine service
218 public String getPolicyModelFileName() {
219 return policyModelFileName;
223 * Sets the file name of the policy engine for deployment on the engine service.
225 * @param policyModelFileName the file name of the policy engine for deployment on the engine service
227 public void setPolicyModelFileName(final String policyModelFileName) {
228 this.policyModelFileName = policyModelFileName;
232 * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
234 * @return the periodic period
236 public long getPeriodicEventPeriod() {
237 return periodicEventPeriod;
241 * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
242 * negative values are illegal.
244 * @param periodicEventPeriod the periodic period
246 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
247 this.periodicEventPeriod = periodicEventPeriod;
251 * Gets the engine parameters for engines in the engine service.
253 * @return the engine parameters for engines in the engine service
255 public EngineParameters getEngineParameters() {
256 return engineParameters;
260 * Sets the engine parameters for engines in the engine service.
262 * @param engineParameters the engine parameters for engines in the engine service
264 public void setEngineParameters(final EngineParameters engineParameters) {
265 this.engineParameters = engineParameters;
272 public GroupValidationResult validate() {
273 final GroupValidationResult result = new GroupValidationResult(this);
275 validateStringParameters(result);
277 validateNumericParameters(result);
279 if (policyModelFileName != null) {
280 validatePolicyModelFileName(result);
282 result.setResult("engineParameters", engineParameters.validate());
288 * Validate string parameters.
290 * @param result the result of string parameter validation
292 private void validateStringParameters(final GroupValidationResult result) {
293 if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
294 result.setResult("name", ValidationStatus.INVALID,
295 "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
298 if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
299 result.setResult("version", ValidationStatus.INVALID,
300 "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
305 * Validate numeric parameters.
307 * @param result the result of numeric parameter validation
309 private void validateNumericParameters(final GroupValidationResult result) {
311 result.setResult("id", ValidationStatus.INVALID,
312 "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
315 if (instanceCount < 1) {
316 result.setResult("instanceCount", ValidationStatus.INVALID,
317 "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
320 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
321 result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
322 + "] invalid, must be specified as 1024 <= port <= 65535");
325 if (periodicEventPeriod < 0) {
326 result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
327 + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
332 * Validate the policy model file name parameter.
334 * @param result the variable in which to store the result of the validation
336 private void validatePolicyModelFileName(final GroupValidationResult result) {
337 if (policyModelFileName.trim().length() == 0) {
338 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID,
339 "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string");
343 String absolutePolicyFileName = null;
345 // Resolve the file name if it is a relative file name
346 File policyModelFile = new File(policyModelFileName);
347 if (policyModelFile.isAbsolute()) {
348 absolutePolicyFileName = policyModelFileName;
350 absolutePolicyFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator
351 + policyModelFileName;
352 policyModelFile = new File(absolutePolicyFileName);
355 // Check that the file exists
356 if (!policyModelFile.exists()) {
357 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found");
359 // Check that the file is a regular file
360 else if (!policyModelFile.isFile()) {
361 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not a plain file");
363 // OK, we found the file and it's OK, so reset the file name
364 policyModelFileName = absolutePolicyFileName;
366 // Check that the file is readable
367 if (!policyModelFile.canRead()) {
368 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not readable");