Changes for checkstyle 8.32
[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 import org.onap.policy.apex.core.engine.EngineParameters;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
27 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ParameterGroup;
30 import org.onap.policy.common.parameters.ValidationStatus;
31
32 // @formatter:off
33 /**
34  * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
35  *
36  * <p>The following parameters are defined:
37  * <ol>
38  * <li>name: The name of the Apex engine service, which can be set to any value that matches the regular expression
39  * {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#NAME_REGEXP}.
40  * <li>version: 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#VERSION_REGEXP}.
42  * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
43  * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine executes in its own
44  * thread.
45  * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed using the EngDep
46  * protocol. The EngDep protocol allows the engine service to be monitored, to start and stop engines in the engine
47  * service, and to update the policy model of the engine service.
48  * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in the engine service
49  * will use. All engine threads use the same parameters and act as a pool of engines. Engine parameters specify the
50  * executors and context management for the engines.
51  * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine service.
52  * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
53  * APEX, 0 means no periodic event generation, negative values are illegal.
54  * </ol>
55  */
56 // @formatter:on
57 public class EngineServiceParameters implements ParameterGroup {
58     private static final int MAX_PORT = 65535;
59
60     // @formatter:off
61     /** The default name of the Apex engine service. */
62     public static final String DEFAULT_NAME = "ApexEngineService";
63
64     /** The default version of the Apex engine service. */
65     public static final String DEFAULT_VERSION = "1.0.0";
66
67     /** The default ID of the Apex engine service. */
68     public static final int DEFAULT_ID = -1;
69
70     /** The default instance count for the Apex engine service. */
71     public static final int DEFAULT_INSTANCE_COUNT  = 1;
72
73     /** The default EngDep deployment port of the Apex engine service. */
74     public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
75
76     // Constants for repeated strings
77     private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
78
79     // Apex engine service parameters
80     private String name                = DEFAULT_NAME;
81     private String version             = DEFAULT_VERSION;
82     private int    id                  = DEFAULT_ID;
83     private int    instanceCount       = DEFAULT_INSTANCE_COUNT;
84     private int    deploymentPort      = DEFAULT_DEPLOYMENT_PORT;
85     private String policyModelFileName = null;
86     private long   periodicEventPeriod = 0;
87     // @formatter:on
88
89     // Apex engine internal parameters
90     private EngineParameters engineParameters = new EngineParameters();
91
92     /**
93      * Constructor to create an apex engine service parameters instance and register the instance with the parameter
94      * service.
95      */
96     public EngineServiceParameters() {
97         super();
98
99         // Set the name for the parameters
100         this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
101     }
102
103     /**
104      * Gets the key of the Apex engine service.
105      *
106      * @return the Apex engine service key
107      */
108     public AxArtifactKey getEngineKey() {
109         return new AxArtifactKey(name, version);
110     }
111
112     /**
113      * Sets the key of the Apex engine service.
114      * 
115      * @param key the the Apex engine service key
116      */
117     public void setEngineKey(final AxArtifactKey key) {
118         this.setName(key.getName());
119         this.setVersion(key.getVersion());
120     }
121
122     /**
123      * Gets the name of the engine service.
124      *
125      * @return the name of the engine service
126      */
127     public String getName() {
128         return name;
129     }
130
131     /**
132      * Sets the name of the engine service.
133      *
134      * @param name the name of the engine service
135      */
136     public void setName(final String name) {
137         this.name = name;
138     }
139
140     /**
141      * Gets the version of the engine service.
142      *
143      * @return the version of the engine service
144      */
145     public String getVersion() {
146         return version;
147     }
148
149     /**
150      * Sets the version of the engine service.
151      *
152      * @param version the version of the engine service
153      */
154     public void setVersion(final String version) {
155         this.version = version;
156     }
157
158     /**
159      * Gets the id of the engine service.
160      *
161      * @return the id of the engine service
162      */
163     public int getId() {
164         return id;
165     }
166
167     /**
168      * Sets the id of the engine service.
169      *
170      * @param id the id of the engine service
171      */
172     public void setId(final int id) {
173         this.id = id;
174     }
175
176     /**
177      * Gets the instance count of the engine service.
178      *
179      * @return the instance count of the engine service
180      */
181     public int getInstanceCount() {
182         return instanceCount;
183     }
184
185     /**
186      * Sets the instance count of the engine service.
187      *
188      * @param instanceCount the instance count of the engine service
189      */
190     public void setInstanceCount(final int instanceCount) {
191         this.instanceCount = instanceCount;
192     }
193
194     /**
195      * Gets the deployment port of the engine service.
196      *
197      * @return the deployment port of the engine service
198      */
199     public int getDeploymentPort() {
200         return deploymentPort;
201     }
202
203     /**
204      * Sets the deployment port of the engine service.
205      *
206      * @param deploymentPort the deployment port of the engine service
207      */
208     public void setDeploymentPort(final int deploymentPort) {
209         this.deploymentPort = deploymentPort;
210     }
211
212     /**
213      * Gets the file name of the policy engine for deployment on the engine service.
214      *
215      * @return the file name of the policy engine for deployment on the engine service
216      */
217     public String getPolicyModelFileName() {
218         return policyModelFileName;
219     }
220
221     /**
222      * Sets the file name of the policy engine for deployment on the engine service.
223      *
224      * @param policyModelFileName the file name of the policy engine for deployment on the engine service
225      */
226     public void setPolicyModelFileName(final String policyModelFileName) {
227         this.policyModelFileName = policyModelFileName;
228     }
229
230     /**
231      * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
232      * 
233      * @return the periodic period
234      */
235     public long getPeriodicEventPeriod() {
236         return periodicEventPeriod;
237     }
238
239     /**
240      * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent,
241      * negative values are illegal.
242      * 
243      * @param periodicEventPeriod the periodic period
244      */
245     public void setPeriodicEventPeriod(final long periodicEventPeriod) {
246         this.periodicEventPeriod = periodicEventPeriod;
247     }
248
249     /**
250      * Gets the engine parameters for engines in the engine service.
251      *
252      * @return the engine parameters for engines in the engine service
253      */
254     public EngineParameters getEngineParameters() {
255         return engineParameters;
256     }
257
258     /**
259      * Sets the engine parameters for engines in the engine service.
260      *
261      * @param engineParameters the engine parameters for engines in the engine service
262      */
263     public void setEngineParameters(final EngineParameters engineParameters) {
264         this.engineParameters = engineParameters;
265     }
266
267     /**
268      * {@inheritDoc}.
269      */
270     @Override
271     public GroupValidationResult validate() {
272         final GroupValidationResult result = new GroupValidationResult(this);
273
274         validateStringParameters(result);
275
276         validateNumericParameters(result);
277
278         if (policyModelFileName != null) {
279             validatePolicyModelFileName(result);
280         }
281         result.setResult("engineParameters", engineParameters.validate());
282
283         return result;
284     }
285
286     /**
287      * Validate string parameters.
288      * 
289      * @param result the result of string parameter validation
290      */
291     private void validateStringParameters(final GroupValidationResult result) {
292         if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
293             result.setResult("name", ValidationStatus.INVALID,
294                             "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
295         }
296
297         if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
298             result.setResult("version", ValidationStatus.INVALID,
299                             "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
300         }
301     }
302
303     /**
304      * Validate numeric parameters.
305      * 
306      * @param result the result of numeric parameter validation
307      */
308     private void validateNumericParameters(final GroupValidationResult result) {
309         if (id < 0) {
310             result.setResult("id", ValidationStatus.INVALID,
311                             "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
312         }
313
314         if (instanceCount < 1) {
315             result.setResult("instanceCount", ValidationStatus.INVALID,
316                             "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
317         }
318
319         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
320             result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
321                             + "] invalid, must be specified as 1024 <= port <= 65535");
322         }
323
324         if (periodicEventPeriod < 0) {
325             result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
326                             + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
327         }
328     }
329
330     /**
331      * Validate the policy model file name parameter.
332      * 
333      * @param result the variable in which to store the result of the validation
334      */
335     private void validatePolicyModelFileName(final GroupValidationResult result) {
336         if (policyModelFileName.trim().length() == 0) {
337             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID,
338                             "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string");
339             return;
340         }
341
342         String absolutePolicyFileName = null;
343
344         // Resolve the file name if it is a relative file name
345         File policyModelFile = new File(policyModelFileName);
346         if (policyModelFile.isAbsolute()) {
347             absolutePolicyFileName = policyModelFileName;
348         } else {
349             absolutePolicyFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator
350                             + policyModelFileName;
351             policyModelFile = new File(absolutePolicyFileName);
352         }
353
354         // Check that the file exists
355         if (!policyModelFile.exists()) {
356             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found");
357         } else if (!policyModelFile.isFile()) {
358             // Check that the file is a regular file
359             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not a plain file");
360         } else {
361             // OK, we found the file and it's OK, so reset the file name
362             policyModelFileName = absolutePolicyFileName;
363
364             // Check that the file is readable
365             if (!policyModelFile.canRead()) {
366                 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not readable");
367             }
368         }
369     }
370 }