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.core.engine.EngineParameters;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
30 import org.onap.policy.common.parameters.GroupValidationResult;
31 import org.onap.policy.common.parameters.ParameterGroup;
32 import org.onap.policy.common.parameters.ValidationStatus;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
36 * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
38 * <p>The following parameters are defined:
40 * <li>name: 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#NAME_REGEXP}.
42 * <li>version: 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#VERSION_REGEXP}.
44 * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
45 * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
47 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
48 * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
49 * service, and to update the policy model of the engine service.
50 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
51 * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
52 * executors and context management for the engines.
53 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
54 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
55 * APEX, 0 means no periodic event generation, negative values are illegal.
58 * @author Liam Fallon (liam.fallon@ericsson.com)
60 public class EngineServiceParameters implements ParameterGroup {
61 private static final int MAX_PORT = 65535;
64 /** The default name of the Apex engine service. */
65 public static final String DEFAULT_NAME = "ApexEngineService";
67 /** The default version of the Apex engine service. */
68 public static final String DEFAULT_VERSION = "1.0.0";
70 /** The default ID of the Apex engine service. */
71 public static final int DEFAULT_ID = -1;
73 /** The default instance count for the Apex engine service. */
74 public static final int DEFAULT_INSTANCE_COUNT = 1;
76 /** The default EngDep deployment port of the Apex engine service. */
77 public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
79 // Constants for repeated strings
80 private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
82 // Apex engine service parameters
83 private String name = DEFAULT_NAME;
84 private String version = DEFAULT_VERSION;
85 private int id = DEFAULT_ID;
86 private int instanceCount = DEFAULT_INSTANCE_COUNT;
87 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
88 private String policyModelFileName = null;
89 private long periodicEventPeriod = 0;
92 // Apex engine internal parameters
93 private EngineParameters engineParameters = new EngineParameters();
96 * Constructor to create an apex engine service parameters instance and register the instance with the parameter
99 public EngineServiceParameters() {
102 // Set the name for the parameters
103 this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
107 * Gets the key of the Apex engine service.
109 * @return the Apex engine service key
111 public AxArtifactKey getEngineKey() {
112 return new AxArtifactKey(name, version);
116 * Sets the key of the Apex engine service.
118 * @param key the the Apex engine service key
120 public void setEngineKey(final AxArtifactKey key) {
121 this.setName(key.getName());
122 this.setVersion(key.getVersion());
126 * Gets the name of the engine service.
128 * @return the name of the engine service
130 public String getName() {
135 * Sets the name of the engine service.
137 * @param name the name of the engine service
139 public void setName(final String name) {
144 * Gets the version of the engine service.
146 * @return the version of the engine service
148 public String getVersion() {
153 * Sets the version of the engine service.
155 * @param version the version of the engine service
157 public void setVersion(final String version) {
158 this.version = version;
162 * Gets the id of the engine service.
164 * @return the id of the engine service
171 * Sets the id of the engine service.
173 * @param id the id of the engine service
175 public void setId(final int id) {
180 * Gets the instance count of the engine service.
182 * @return the instance count of the engine service
184 public int getInstanceCount() {
185 return instanceCount;
189 * Sets the instance count of the engine service.
191 * @param instanceCount the instance count of the engine service
193 public void setInstanceCount(final int instanceCount) {
194 this.instanceCount = instanceCount;
198 * Gets the deployment port of the engine service.
200 * @return the deployment port of the engine service
202 public int getDeploymentPort() {
203 return deploymentPort;
207 * Sets the deployment port of the engine service.
209 * @param deploymentPort the deployment port of the engine service
211 public void setDeploymentPort(final int deploymentPort) {
212 this.deploymentPort = deploymentPort;
216 * Gets the file name of the policy engine for deployment on the engine service.
218 * @return the file name of the policy engine for deployment on the engine service
220 public String getPolicyModelFileName() {
221 return ResourceUtils.getFilePath4Resource(policyModelFileName);
225 * Sets the file name of the policy engine for deployment on the engine service.
227 * @param policyModelFileName the file name of the policy engine for deployment on the engine service
229 public void setPolicyModelFileName(final String policyModelFileName) {
230 this.policyModelFileName = policyModelFileName;
234 * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
236 * @return the periodic period
238 public long getPeriodicEventPeriod() {
239 return periodicEventPeriod;
243 * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
244 * negative values are illegal.
246 * @param periodicEventPeriod the periodic period
248 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
249 this.periodicEventPeriod = periodicEventPeriod;
253 * Gets the engine parameters for engines in the engine service.
255 * @return the engine parameters for engines in the engine service
257 public EngineParameters getEngineParameters() {
258 return engineParameters;
262 * Sets the engine parameters for engines in the engine service.
264 * @param engineParameters the engine parameters for engines in the engine service
266 public void setEngineParameters(final EngineParameters engineParameters) {
267 this.engineParameters = engineParameters;
273 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
276 public GroupValidationResult validate() {
277 final GroupValidationResult result = new GroupValidationResult(this);
279 if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
280 result.setResult("name", ValidationStatus.INVALID,
281 "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
284 if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
285 result.setResult("version", ValidationStatus.INVALID,
286 "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
290 result.setResult("id", ValidationStatus.INVALID,
291 "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
294 if (instanceCount < 1) {
295 result.setResult("instanceCount", ValidationStatus.INVALID,
296 "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
299 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
300 result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
301 + "] invalid, must be specified as 1024 <= port <= 65535");
304 if (policyModelFileName != null) {
305 validatePolicyModelFileName(result);
308 if (periodicEventPeriod < 0) {
309 result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
310 + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
317 * Validate the policy model file name parameter.
318 * @param result the variable in which to store the result of the validation
320 private void validatePolicyModelFileName(final GroupValidationResult result) {
321 if (policyModelFileName.trim().length() == 0) {
322 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "\""
323 + policyModelFileName + "\" invalid, must be specified as a non-empty string");
327 // The file name can refer to a resource on the local file system or on the class
329 final URL fileUrl = ResourceUtils.getUrl4Resource(policyModelFileName);
330 if (fileUrl == null) {
331 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");
333 final File policyModelFile = new File(fileUrl.getPath());
334 if (!policyModelFile.isFile()) {
335 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");