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.service.AbstractParameters;
28 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
29 import org.onap.policy.apex.service.parameters.ApexParameterValidator;
30 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.apex.core.engine.EngineParameters;
35 * This class holds the parameters for an Apex Engine Service with multiple engine threads running
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
41 * regular expression {@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
43 * regular expression {@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
46 * executes in its own thread.
47 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed
48 * using the EngDep protocol. The EngDep protocol allows the engine service to be monitored, to
49 * start and stop engines in the engine service, and to update the policy model of the engine
51 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in
52 * the engine service will use. All engine threads use the same parameters and act as a pool of
53 * engines. Engine parameters specify the executors and context management for the engines.
54 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine
56 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT
57 * will be generated by APEX, 0 means no periodic event generation, negative values are illegal.
60 * @author Liam Fallon (liam.fallon@ericsson.com)
62 public class EngineServiceParameters extends AbstractParameters implements ApexParameterValidator {
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 // Apex engine service parameters
82 private String name = DEFAULT_NAME;
83 private String version = DEFAULT_VERSION;
84 private int id = DEFAULT_ID;
85 private int instanceCount = DEFAULT_INSTANCE_COUNT;
86 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
87 private String policyModelFileName = null;
88 private long periodicEventPeriod = 0;
91 // Apex engine internal parameters
92 private EngineParameters engineParameters = new EngineParameters();
95 * Constructor to create an apex engine service parameters instance and register the instance
96 * with the parameter service.
98 public EngineServiceParameters() {
99 super(EngineServiceParameters.class.getCanonicalName());
100 ParameterService.registerParameters(EngineServiceParameters.class, this);
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 ResourceUtils.getFilePath4Resource(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
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
233 * events are being sent.
235 * @return the periodic period
237 public long getPeriodicEventPeriod() {
238 return periodicEventPeriod;
242 * Set the period in milliseconds at which periodic events are sent, zero means no periodic
243 * events are to be sent, negative values are illegal.
245 * @param periodicEventPeriod the periodic period
247 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
248 this.periodicEventPeriod = periodicEventPeriod;
252 * Gets the engine parameters for engines in the engine service.
254 * @return the engine parameters for engines in the engine service
256 public EngineParameters getEngineParameters() {
257 return engineParameters;
261 * Sets the engine parameters for engines in the engine service.
263 * @param engineParameters the engine parameters for engines in the engine service
265 public void setEngineParameters(final EngineParameters engineParameters) {
266 this.engineParameters = engineParameters;
272 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
275 public String validate() {
276 final StringBuilder errorMessageBuilder = new StringBuilder();
279 new AxArtifactKey(name, version);
280 } catch (final Exception e) {
281 errorMessageBuilder.append(" name [" + name + "] and/or version [" + version + "] invalid\n");
282 errorMessageBuilder.append(" " + e.getMessage() + "\n");
286 errorMessageBuilder.append(
287 " id not specified or specified value [" + id + "] invalid, must be specified as id >= 0\n");
290 if (instanceCount < 1) {
291 errorMessageBuilder.append(
292 " instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1\n");
295 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
296 errorMessageBuilder.append(
297 " deploymentPort [" + deploymentPort + "] invalid, must be specified as 1024 <= port <= 65535\n");
300 if (policyModelFileName != null) {
301 if (policyModelFileName.trim().length() == 0) {
302 errorMessageBuilder.append(" policyModelFileName [" + policyModelFileName
303 + "] invalid, must be specified as a non-empty string\n");
305 // The file name can refer to a resource on the local file system or on the class
307 final URL fileURL = ResourceUtils.getUrl4Resource(policyModelFileName);
308 if (fileURL == null) {
309 errorMessageBuilder.append(
310 " policyModelFileName [" + policyModelFileName + "] not found or is not a plain file\n");
312 final File policyModelFile = new File(fileURL.getPath());
313 if (!policyModelFile.isFile()) {
314 errorMessageBuilder.append(" policyModelFileName [" + policyModelFileName
315 + "] not found or is not a plain file\n");
321 if (periodicEventPeriod < 0) {
322 errorMessageBuilder.append(" periodicEventPeriod [" + periodicEventPeriod
323 + "] invalid, must be specified in milliseconds as >=0\n");
326 return errorMessageBuilder.toString();