7cd75a300d01bccfd2b336dbd91888fd3e9d9c14
[policy/apex-pdp.git] /
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 java.net.URL;
25
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;
34
35 // @formatter:off
36 /**
37  * This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
38  *
39  * <p>The following parameters are defined:
40  * <ol>
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
47  * thread.
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.
57  * </ol>
58  */
59 // @formatter:on
60 public class EngineServiceParameters implements ParameterGroup {
61     private static final int MAX_PORT = 65535;
62
63     // @formatter:off
64     /** The default name of the Apex engine service. */
65     public static final String DEFAULT_NAME = "ApexEngineService";
66
67     /** The default version of the Apex engine service. */
68     public static final String DEFAULT_VERSION = "1.0.0";
69
70     /** The default ID of the Apex engine service. */
71     public static final int DEFAULT_ID = -1;
72
73     /** The default instance count for the Apex engine service. */
74     public static final int DEFAULT_INSTANCE_COUNT  = 1;
75
76     /** The default EngDep deployment port of the Apex engine service. */
77     public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
78
79     // Constants for repeated strings
80     private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName";
81
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;
90     // @formatter:on
91
92     // Apex engine internal parameters
93     private EngineParameters engineParameters = new EngineParameters();
94
95     /**
96      * Constructor to create an apex engine service parameters instance and register the instance with the parameter
97      * service.
98      */
99     public EngineServiceParameters() {
100         super();
101
102         // Set the name for the parameters
103         this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
104     }
105
106     /**
107      * Gets the key of the Apex engine service.
108      *
109      * @return the Apex engine service key
110      */
111     public AxArtifactKey getEngineKey() {
112         return new AxArtifactKey(name, version);
113     }
114
115     /**
116      * Sets the key of the Apex engine service.
117      * 
118      * @param key the the Apex engine service key
119      */
120     public void setEngineKey(final AxArtifactKey key) {
121         this.setName(key.getName());
122         this.setVersion(key.getVersion());
123     }
124
125     /**
126      * Gets the name of the engine service.
127      *
128      * @return the name of the engine service
129      */
130     public String getName() {
131         return name;
132     }
133
134     /**
135      * Sets the name of the engine service.
136      *
137      * @param name the name of the engine service
138      */
139     public void setName(final String name) {
140         this.name = name;
141     }
142
143     /**
144      * Gets the version of the engine service.
145      *
146      * @return the version of the engine service
147      */
148     public String getVersion() {
149         return version;
150     }
151
152     /**
153      * Sets the version of the engine service.
154      *
155      * @param version the version of the engine service
156      */
157     public void setVersion(final String version) {
158         this.version = version;
159     }
160
161     /**
162      * Gets the id of the engine service.
163      *
164      * @return the id of the engine service
165      */
166     public int getId() {
167         return id;
168     }
169
170     /**
171      * Sets the id of the engine service.
172      *
173      * @param id the id of the engine service
174      */
175     public void setId(final int id) {
176         this.id = id;
177     }
178
179     /**
180      * Gets the instance count of the engine service.
181      *
182      * @return the instance count of the engine service
183      */
184     public int getInstanceCount() {
185         return instanceCount;
186     }
187
188     /**
189      * Sets the instance count of the engine service.
190      *
191      * @param instanceCount the instance count of the engine service
192      */
193     public void setInstanceCount(final int instanceCount) {
194         this.instanceCount = instanceCount;
195     }
196
197     /**
198      * Gets the deployment port of the engine service.
199      *
200      * @return the deployment port of the engine service
201      */
202     public int getDeploymentPort() {
203         return deploymentPort;
204     }
205
206     /**
207      * Sets the deployment port of the engine service.
208      *
209      * @param deploymentPort the deployment port of the engine service
210      */
211     public void setDeploymentPort(final int deploymentPort) {
212         this.deploymentPort = deploymentPort;
213     }
214
215     /**
216      * Gets the file name of the policy engine for deployment on the engine service.
217      *
218      * @return the file name of the policy engine for deployment on the engine service
219      */
220     public String getPolicyModelFileName() {
221         return ResourceUtils.getFilePath4Resource(policyModelFileName);
222     }
223
224     /**
225      * Sets the file name of the policy engine for deployment on the engine service.
226      *
227      * @param policyModelFileName the file name of the policy engine for deployment on the engine service
228      */
229     public void setPolicyModelFileName(final String policyModelFileName) {
230         this.policyModelFileName = policyModelFileName;
231     }
232
233     /**
234      * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent.
235      * 
236      * @return the periodic period
237      */
238     public long getPeriodicEventPeriod() {
239         return periodicEventPeriod;
240     }
241
242     /**
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.
245      * 
246      * @param periodicEventPeriod the periodic period
247      */
248     public void setPeriodicEventPeriod(final long periodicEventPeriod) {
249         this.periodicEventPeriod = periodicEventPeriod;
250     }
251
252     /**
253      * Gets the engine parameters for engines in the engine service.
254      *
255      * @return the engine parameters for engines in the engine service
256      */
257     public EngineParameters getEngineParameters() {
258         return engineParameters;
259     }
260
261     /**
262      * Sets the engine parameters for engines in the engine service.
263      *
264      * @param engineParameters the engine parameters for engines in the engine service
265      */
266     public void setEngineParameters(final EngineParameters engineParameters) {
267         this.engineParameters = engineParameters;
268     }
269
270     /*
271      * (non-Javadoc)
272      *
273      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
274      */
275     @Override
276     public GroupValidationResult validate() {
277         final GroupValidationResult result = new GroupValidationResult(this);
278
279         validateStringParameters(result);
280
281         validateNumericParameters(result);
282
283         if (policyModelFileName != null) {
284             validatePolicyModelFileName(result);
285         }
286         result.setResult("engineParameters", engineParameters.validate());
287
288         return result;
289     }
290
291     /**
292      * Validate string parameters.
293      * 
294      * @param result the result of string parameter validation
295      */
296     private void validateStringParameters(final GroupValidationResult result) {
297         if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
298             result.setResult("name", ValidationStatus.INVALID,
299                             "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
300         }
301
302         if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
303             result.setResult("version", ValidationStatus.INVALID,
304                             "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
305         }
306     }
307
308     /**
309      * Validate numeric parameters.
310      * 
311      * @param result the result of numeric parameter validation
312      */
313     private void validateNumericParameters(final GroupValidationResult result) {
314         if (id < 0) {
315             result.setResult("id", ValidationStatus.INVALID,
316                             "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
317         }
318
319         if (instanceCount < 1) {
320             result.setResult("instanceCount", ValidationStatus.INVALID,
321                             "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
322         }
323
324         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
325             result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
326                             + "] invalid, must be specified as 1024 <= port <= 65535");
327         }
328
329         if (periodicEventPeriod < 0) {
330             result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
331                             + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
332         }
333     }
334
335     /**
336      * Validate the policy model file name parameter.
337      * 
338      * @param result the variable in which to store the result of the validation
339      */
340     private void validatePolicyModelFileName(final GroupValidationResult result) {
341         if (policyModelFileName.trim().length() == 0) {
342             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID,
343                             "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string");
344             return;
345         }
346
347         // The file name can refer to a resource on the local file system or on the class
348         // path
349         final URL fileUrl = ResourceUtils.getUrl4Resource(policyModelFileName);
350         if (fileUrl == null) {
351             result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");
352         } else {
353             final File policyModelFile = new File(fileUrl.getPath());
354             if (!policyModelFile.isFile()) {
355                 result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found or is not a plain file");
356             }
357         }
358     }
359 }