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;
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;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.apex.core.engine.EngineParameters;
37 * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
40 * The following parameters are defined:
42 * <li>name: The name of the Apex engine service, which can be set to any value that matches the regular expression
43 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#NAME_REGEXP}.
44 * <li>version: The name of the Apex engine service, which can be set to any value that matches the regular expression
45 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#VERSION_REGEXP}.
46 * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
47 * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
49 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
50 * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
51 * service, and to update the policy model of the engine service.
52 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
53 * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
54 * executors and context management for the engines.
55 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
56 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
57 * APEX, 0 means no periodic event generation, negative values are illegal.
60 * @author Liam Fallon (liam.fallon@ericsson.com)
62 public class EngineServiceParameters implements ParameterGroup {
63 private static final int MAX_PORT = 65535;
66 /** The default name of the Apex engine service. */
67 public static final String DEFAULT_NAME = "ApexEngineService";
69 /** The default version of the Apex engine service. */
70 public static final String DEFAULT_VERSION = "1.0.0";
72 /** The default ID of the Apex engine service. */
73 public static final int DEFAULT_ID = -1;
75 /** The default instance count for the Apex engine service. */
76 public static final int DEFAULT_INSTANCE_COUNT = 1;
78 /** The default EngDep deployment port of the Apex engine service. */
79 public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
81 // Constants for repeated strings
82 private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
84 // Apex engine service parameters
85 private String name = DEFAULT_NAME;
86 private String version = DEFAULT_VERSION;
87 private int id = DEFAULT_ID;
88 private int instanceCount = DEFAULT_INSTANCE_COUNT;
89 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
90 private String policyModelFileName = null;
91 private long periodicEventPeriod = 0;
94 // Apex engine internal parameters
95 private EngineParameters engineParameters = new EngineParameters();
98 * Constructor to create an apex engine service parameters instance and register the instance with the parameter
101 public EngineServiceParameters() {
104 // Set the name for the parameters
105 this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
109 * Gets the key of the Apex engine service.
111 * @return the Apex engine service key
113 public AxArtifactKey getEngineKey() {
114 return new AxArtifactKey(name, version);
118 * Sets the key of the Apex engine service.
120 * @param key the the Apex engine service key
122 public void setEngineKey(final AxArtifactKey key) {
123 this.setName(key.getName());
124 this.setVersion(key.getVersion());
128 * Gets the name of the engine service.
130 * @return the name of the engine service
132 public String getName() {
137 * Sets the name of the engine service.
139 * @param name the name of the engine service
141 public void setName(final String name) {
146 * Gets the version of the engine service.
148 * @return the version of the engine service
150 public String getVersion() {
155 * Sets the version of the engine service.
157 * @param version the version of the engine service
159 public void setVersion(final String version) {
160 this.version = version;
164 * Gets the id of the engine service.
166 * @return the id of the engine service
173 * Sets the id of the engine service.
175 * @param id the id of the engine service
177 public void setId(final int id) {
182 * Gets the instance count of the engine service.
184 * @return the instance count of the engine service
186 public int getInstanceCount() {
187 return instanceCount;
191 * Sets the instance count of the engine service.
193 * @param instanceCount the instance count of the engine service
195 public void setInstanceCount(final int instanceCount) {
196 this.instanceCount = instanceCount;
200 * Gets the deployment port of the engine service.
202 * @return the deployment port of the engine service
204 public int getDeploymentPort() {
205 return deploymentPort;
209 * Sets the deployment port of the engine service.
211 * @param deploymentPort the deployment port of the engine service
213 public void setDeploymentPort(final int deploymentPort) {
214 this.deploymentPort = deploymentPort;
218 * Gets the file name of the policy engine for deployment on the engine service.
220 * @return the file name of the policy engine for deployment on the engine service
222 public String getPolicyModelFileName() {
223 return ResourceUtils.getFilePath4Resource(policyModelFileName);
227 * Sets the file name of the policy engine for deployment on the engine service.
229 * @param policyModelFileName the file name of the policy engine for deployment on the engine service
231 public void setPolicyModelFileName(final String policyModelFileName) {
232 this.policyModelFileName = policyModelFileName;
236 * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
238 * @return the periodic period
240 public long getPeriodicEventPeriod() {
241 return periodicEventPeriod;
245 * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
246 * negative values are illegal.
248 * @param periodicEventPeriod the periodic period
250 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
251 this.periodicEventPeriod = periodicEventPeriod;
255 * Gets the engine parameters for engines in the engine service.
257 * @return the engine parameters for engines in the engine service
259 public EngineParameters getEngineParameters() {
260 return engineParameters;
264 * Sets the engine parameters for engines in the engine service.
266 * @param engineParameters the engine parameters for engines in the engine service
268 public void setEngineParameters(final EngineParameters engineParameters) {
269 this.engineParameters = engineParameters;
275 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
278 public GroupValidationResult validate() {
279 final GroupValidationResult result = new GroupValidationResult(this);
281 if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
282 result.setResult("name", ValidationStatus.INVALID,
283 "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
286 if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
287 result.setResult("version", ValidationStatus.INVALID,
288 "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
292 result.setResult("id", ValidationStatus.INVALID,
293 "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
296 if (instanceCount < 1) {
297 result.setResult("instanceCount", ValidationStatus.INVALID,
298 "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
301 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
302 result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
303 + "] invalid, must be specified as 1024 <= port <= 65535");
306 if (policyModelFileName != null) {
307 validatePolicyModelFileName(result);
310 if (periodicEventPeriod < 0) {
311 result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
312 + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
319 * Validate the policy model file name parameter
320 * @param result the variable in which to store the result of the validation
322 private void validatePolicyModelFileName(final GroupValidationResult result) {
323 if (policyModelFileName.trim().length() == 0) {
324 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "\""
325 + policyModelFileName + "\" invalid, must be specified as a non-empty string");
329 // The file name can refer to a resource on the local file system or on the class
331 final URL fileURL = ResourceUtils.getUrl4Resource(policyModelFileName);
332 if (fileURL == null) {
333 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");
335 final File policyModelFile = new File(fileURL.getPath());
336 if (!policyModelFile.isFile()) {
337 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");