9d4566b78eb2be5c692a00b0d515220ac924e063
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / parameters / engineservice / EngineServiceParameters.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.parameters.engineservice;
22
23 import java.io.File;
24
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;
32
33 // @formatter:off
34 /**
35  * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
36  *
37  * <p>The following parameters are defined:
38  * <ol>
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
45  * thread.
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.
55  * </ol>
56  */
57 // @formatter:on
58 public class EngineServiceParameters implements ParameterGroup {
59     private static final int MAX_PORT = 65535;
60
61     // @formatter:off
62     /** The default name of the Apex engine service. */
63     public static final String DEFAULT_NAME = "ApexEngineService";
64
65     /** The default version of the Apex engine service. */
66     public static final String DEFAULT_VERSION = "1.0.0";
67
68     /** The default ID of the Apex engine service. */
69     public static final int DEFAULT_ID = -1;
70
71     /** The default instance count for the Apex engine service. */
72     public static final int DEFAULT_INSTANCE_COUNT  = 1;
73
74     /** The default EngDep deployment port of the Apex engine service. */
75     public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
76
77     // Constants for repeated strings
78     private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
79
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;
88     // @formatter:on
89
90     // Apex engine internal parameters
91     private EngineParameters engineParameters = new EngineParameters();
92
93     /**
94      * Constructor to create an apex engine service parameters instance and register the instance with the parameter
95      * service.
96      */
97     public EngineServiceParameters() {
98         super();
99
100         // Set the name for the parameters
101         this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
102     }
103
104     /**
105      * Gets the key of the Apex engine service.
106      *
107      * @return the Apex engine service key
108      */
109     public AxArtifactKey getEngineKey() {
110         return new AxArtifactKey(name, version);
111     }
112
113     /**
114      * Sets the key of the Apex engine service.
115      * 
116      * @param key the the Apex engine service key
117      */
118     public void setEngineKey(final AxArtifactKey key) {
119         this.setName(key.getName());
120         this.setVersion(key.getVersion());
121     }
122
123     /**
124      * Gets the name of the engine service.
125      *
126      * @return the name of the engine service
127      */
128     public String getName() {
129         return name;
130     }
131
132     /**
133      * Sets the name of the engine service.
134      *
135      * @param name the name of the engine service
136      */
137     public void setName(final String name) {
138         this.name = name;
139     }
140
141     /**
142      * Gets the version of the engine service.
143      *
144      * @return the version of the engine service
145      */
146     public String getVersion() {
147         return version;
148     }
149
150     /**
151      * Sets the version of the engine service.
152      *
153      * @param version the version of the engine service
154      */
155     public void setVersion(final String version) {
156         this.version = version;
157     }
158
159     /**
160      * Gets the id of the engine service.
161      *
162      * @return the id of the engine service
163      */
164     public int getId() {
165         return id;
166     }
167
168     /**
169      * Sets the id of the engine service.
170      *
171      * @param id the id of the engine service
172      */
173     public void setId(final int id) {
174         this.id = id;
175     }
176
177     /**
178      * Gets the instance count of the engine service.
179      *
180      * @return the instance count of the engine service
181      */
182     public int getInstanceCount() {
183         return instanceCount;
184     }
185
186     /**
187      * Sets the instance count of the engine service.
188      *
189      * @param instanceCount the instance count of the engine service
190      */
191     public void setInstanceCount(final int instanceCount) {
192         this.instanceCount = instanceCount;
193     }
194
195     /**
196      * Gets the deployment port of the engine service.
197      *
198      * @return the deployment port of the engine service
199      */
200     public int getDeploymentPort() {
201         return deploymentPort;
202     }
203
204     /**
205      * Sets the deployment port of the engine service.
206      *
207      * @param deploymentPort the deployment port of the engine service
208      */
209     public void setDeploymentPort(final int deploymentPort) {
210         this.deploymentPort = deploymentPort;
211     }
212
213     /**
214      * Gets the file name of the policy engine for deployment on the engine service.
215      *
216      * @return the file name of the policy engine for deployment on the engine service
217      */
218     public String getPolicyModelFileName() {
219         return policyModelFileName;
220     }
221
222     /**
223      * Sets the file name of the policy engine for deployment on the engine service.
224      *
225      * @param policyModelFileName the file name of the policy engine for deployment on the engine service
226      */
227     public void setPolicyModelFileName(final String policyModelFileName) {
228         this.policyModelFileName = policyModelFileName;
229     }
230
231     /**
232      * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
233      * 
234      * @return the periodic period
235      */
236     public long getPeriodicEventPeriod() {
237         return periodicEventPeriod;
238     }
239
240     /**
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.
243      * 
244      * @param periodicEventPeriod the periodic period
245      */
246     public void setPeriodicEventPeriod(final long periodicEventPeriod) {
247         this.periodicEventPeriod = periodicEventPeriod;
248     }
249
250     /**
251      * Gets the engine parameters for engines in the engine service.
252      *
253      * @return the engine parameters for engines in the engine service
254      */
255     public EngineParameters getEngineParameters() {
256         return engineParameters;
257     }
258
259     /**
260      * Sets the engine parameters for engines in the engine service.
261      *
262      * @param engineParameters the engine parameters for engines in the engine service
263      */
264     public void setEngineParameters(final EngineParameters engineParameters) {
265         this.engineParameters = engineParameters;
266     }
267
268     /**
269      * {@inheritDoc}.
270      */
271     @Override
272     public GroupValidationResult validate() {
273         final GroupValidationResult result = new GroupValidationResult(this);
274
275         validateStringParameters(result);
276
277         validateNumericParameters(result);
278
279         if (policyModelFileName != null) {
280             validatePolicyModelFileName(result);
281         }
282         result.setResult("engineParameters", engineParameters.validate());
283
284         return result;
285     }
286
287     /**
288      * Validate string parameters.
289      * 
290      * @param result the result of string parameter validation
291      */
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);
296         }
297
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);
301         }
302     }
303
304     /**
305      * Validate numeric parameters.
306      * 
307      * @param result the result of numeric parameter validation
308      */
309     private void validateNumericParameters(final GroupValidationResult result) {
310         if (id < 0) {
311             result.setResult("id", ValidationStatus.INVALID,
312                             "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
313         }
314
315         if (instanceCount < 1) {
316             result.setResult("instanceCount", ValidationStatus.INVALID,
317                             "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
318         }
319
320         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
321             result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
322                             + "] invalid, must be specified as 1024 <= port <= 65535");
323         }
324
325         if (periodicEventPeriod < 0) {
326             result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
327                             + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
328         }
329     }
330
331     /**
332      * Validate the policy model file name parameter.
333      * 
334      * @param result the variable in which to store the result of the validation
335      */
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");
340             return;
341         }
342
343         String absolutePolicyFileName = null;
344
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;
349         } else {
350             absolutePolicyFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator
351                             + policyModelFileName;
352             policyModelFile = new File(absolutePolicyFileName);
353         }
354
355         // Check that the file exists
356         if (!policyModelFile.exists()) {
357             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found");
358         }
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");
362         } else {
363             // OK, we found the file and it's OK, so reset the file name
364             policyModelFileName = absolutePolicyFileName;
365
366             // Check that the file is readable
367             if (!policyModelFile.canRead()) {
368                 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not readable");
369             }
370         }
371     }
372 }