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