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