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.
39 * <p>The following parameters are defined:
41 * <li>name: 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#NAME_REGEXP}.
43 * <li>version: The name of the Apex engine service, which can be set to any value that matches the regular expression
44 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#VERSION_REGEXP}.
45 * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
46 * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
48 * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
49 * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
50 * service, and to update the policy model of the engine service.
51 * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
52 * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
53 * executors and context management for the engines.
54 * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
55 * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
56 * APEX, 0 means no periodic event generation, negative values are illegal.
59 * @author Liam Fallon (liam.fallon@ericsson.com)
61 public class EngineServiceParameters implements ParameterGroup {
62 private static final int MAX_PORT = 65535;
65 /** The default name of the Apex engine service. */
66 public static final String DEFAULT_NAME = "ApexEngineService";
68 /** The default version of the Apex engine service. */
69 public static final String DEFAULT_VERSION = "1.0.0";
71 /** The default ID of the Apex engine service. */
72 public static final int DEFAULT_ID = -1;
74 /** The default instance count for the Apex engine service. */
75 public static final int DEFAULT_INSTANCE_COUNT = 1;
77 /** The default EngDep deployment port of the Apex engine service. */
78 public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
80 // Constants for repeated strings
81 private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
83 // Apex engine service parameters
84 private String name = DEFAULT_NAME;
85 private String version = DEFAULT_VERSION;
86 private int id = DEFAULT_ID;
87 private int instanceCount = DEFAULT_INSTANCE_COUNT;
88 private int deploymentPort = DEFAULT_DEPLOYMENT_PORT;
89 private String policyModelFileName = null;
90 private long periodicEventPeriod = 0;
93 // Apex engine internal parameters
94 private EngineParameters engineParameters = new EngineParameters();
97 * Constructor to create an apex engine service parameters instance and register the instance with the parameter
100 public EngineServiceParameters() {
103 // Set the name for the parameters
104 this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
108 * Gets the key of the Apex engine service.
110 * @return the Apex engine service key
112 public AxArtifactKey getEngineKey() {
113 return new AxArtifactKey(name, version);
117 * Sets the key of the Apex engine service.
119 * @param key the the Apex engine service key
121 public void setEngineKey(final AxArtifactKey key) {
122 this.setName(key.getName());
123 this.setVersion(key.getVersion());
127 * Gets the name of the engine service.
129 * @return the name of the engine service
131 public String getName() {
136 * Sets the name of the engine service.
138 * @param name the name of the engine service
140 public void setName(final String name) {
145 * Gets the version of the engine service.
147 * @return the version of the engine service
149 public String getVersion() {
154 * Sets the version of the engine service.
156 * @param version the version of the engine service
158 public void setVersion(final String version) {
159 this.version = version;
163 * Gets the id of the engine service.
165 * @return the id of the engine service
172 * Sets the id of the engine service.
174 * @param id the id of the engine service
176 public void setId(final int id) {
181 * Gets the instance count of the engine service.
183 * @return the instance count of the engine service
185 public int getInstanceCount() {
186 return instanceCount;
190 * Sets the instance count of the engine service.
192 * @param instanceCount the instance count of the engine service
194 public void setInstanceCount(final int instanceCount) {
195 this.instanceCount = instanceCount;
199 * Gets the deployment port of the engine service.
201 * @return the deployment port of the engine service
203 public int getDeploymentPort() {
204 return deploymentPort;
208 * Sets the deployment port of the engine service.
210 * @param deploymentPort the deployment port of the engine service
212 public void setDeploymentPort(final int deploymentPort) {
213 this.deploymentPort = deploymentPort;
217 * Gets the file name of the policy engine for deployment on the engine service.
219 * @return the file name of the policy engine for deployment on the engine service
221 public String getPolicyModelFileName() {
222 return ResourceUtils.getFilePath4Resource(policyModelFileName);
226 * Sets the file name of the policy engine for deployment on the engine service.
228 * @param policyModelFileName the file name of the policy engine for deployment on the engine service
230 public void setPolicyModelFileName(final String policyModelFileName) {
231 this.policyModelFileName = policyModelFileName;
235 * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
237 * @return the periodic period
239 public long getPeriodicEventPeriod() {
240 return periodicEventPeriod;
244 * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
245 * negative values are illegal.
247 * @param periodicEventPeriod the periodic period
249 public void setPeriodicEventPeriod(final long periodicEventPeriod) {
250 this.periodicEventPeriod = periodicEventPeriod;
254 * Gets the engine parameters for engines in the engine service.
256 * @return the engine parameters for engines in the engine service
258 public EngineParameters getEngineParameters() {
259 return engineParameters;
263 * Sets the engine parameters for engines in the engine service.
265 * @param engineParameters the engine parameters for engines in the engine service
267 public void setEngineParameters(final EngineParameters engineParameters) {
268 this.engineParameters = engineParameters;
274 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
277 public GroupValidationResult validate() {
278 final GroupValidationResult result = new GroupValidationResult(this);
280 if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
281 result.setResult("name", ValidationStatus.INVALID,
282 "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
285 if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
286 result.setResult("version", ValidationStatus.INVALID,
287 "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
291 result.setResult("id", ValidationStatus.INVALID,
292 "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
295 if (instanceCount < 1) {
296 result.setResult("instanceCount", ValidationStatus.INVALID,
297 "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
300 if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
301 result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
302 + "] invalid, must be specified as 1024 <= port <= 65535");
305 if (policyModelFileName != null) {
306 validatePolicyModelFileName(result);
309 if (periodicEventPeriod < 0) {
310 result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
311 + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
318 * Validate the policy model file name parameter
319 * @param result the variable in which to store the result of the validation
321 private void validatePolicyModelFileName(final GroupValidationResult result) {
322 if (policyModelFileName.trim().length() == 0) {
323 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "\""
324 + policyModelFileName + "\" invalid, must be specified as a non-empty string");
328 // The file name can refer to a resource on the local file system or on the class
330 final URL fileURL = ResourceUtils.getUrl4Resource(policyModelFileName);
331 if (fileURL == null) {
332 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");
334 final File policyModelFile = new File(fileURL.getPath());
335 if (!policyModelFile.isFile()) {
336 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");