1db7aeb2da49a7e2d82fcd35c71bc6829fb97bc2
[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.service.AbstractParameters;
28 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
29 import org.onap.policy.apex.service.parameters.ApexParameterValidator;
30 import org.onap.policy.common.utils.resources.ResourceUtils;
31
32 import org.onap.policy.apex.core.engine.EngineParameters;
33
34 /**
35  * This class holds the parameters for an Apex Engine Service with multiple engine threads running
36  * multiple engines.
37  *
38  * <p>
39  * 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
42  * regular expression {@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
44  * regular expression {@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
47  * executes in its own thread.
48  * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed
49  * using the EngDep protocol. The EngDep protocol allows the engine service to be monitored, to
50  * start and stop engines in the engine service, and to update the policy model of the engine
51  * service.
52  * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in
53  * the engine service will use. All engine threads use the same parameters and act as a pool of
54  * engines. Engine parameters specify the executors and context management for the engines.
55  * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine
56  * service.
57  * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT
58  * will be generated by APEX, 0 means no periodic event generation, negative values are illegal.
59  * </ol>
60  *
61  * @author Liam Fallon (liam.fallon@ericsson.com)
62  */
63 public class EngineServiceParameters extends AbstractParameters implements ApexParameterValidator {
64     private static final int MAX_PORT = 65535;
65
66     // @formatter:off
67     /** The default name of the Apex engine service. */
68     public static final String DEFAULT_NAME = "ApexEngineService";
69
70     /** The default version of the Apex engine service. */
71     public static final String DEFAULT_VERSION = "1.0.0";
72
73     /** The default ID of the Apex engine service. */
74     public static final int DEFAULT_ID = -1;
75
76     /** The default instance count for the Apex engine service. */
77     public static final int DEFAULT_INSTANCE_COUNT  = 1;
78
79     /** The default EngDep deployment port of the Apex engine service. */
80     public static final int DEFAULT_DEPLOYMENT_PORT = 34421;
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
97      * with the parameter service.
98      */
99     public EngineServiceParameters() {
100         super(EngineServiceParameters.class.getCanonicalName());
101         ParameterService.registerParameters(EngineServiceParameters.class, this);
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 ResourceUtils.getFilePath4Resource(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
226      *        service
227      */
228     public void setPolicyModelFileName(final String policyModelFileName) {
229         this.policyModelFileName = policyModelFileName;
230     }
231
232     /**
233      * Get the period in milliseconds at which periodic events are sent, zero means no periodic
234      * 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
244      * events are to be sent, 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 String validate() {
277         final StringBuilder errorMessageBuilder = new StringBuilder();
278
279         try {
280             new AxArtifactKey(name, version);
281         } catch (final Exception e) {
282             errorMessageBuilder.append("  name [" + name + "] and/or version [" + version + "] invalid\n");
283             errorMessageBuilder.append("   " + e.getMessage() + "\n");
284         }
285
286         if (id < 0) {
287             errorMessageBuilder.append(
288                     "  id not specified or specified value [" + id + "] invalid, must be specified as id >= 0\n");
289         }
290
291         if (instanceCount < 1) {
292             errorMessageBuilder.append(
293                     "  instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1\n");
294         }
295
296         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
297             errorMessageBuilder.append(
298                     "  deploymentPort [" + deploymentPort + "] invalid, must be specified as 1024 <= port <= 65535\n");
299         }
300
301         if (policyModelFileName != null) {
302             if (policyModelFileName.trim().length() == 0) {
303                 errorMessageBuilder.append("  policyModelFileName [" + policyModelFileName
304                         + "] invalid, must be specified as a non-empty string\n");
305             } else {
306                 // The file name can refer to a resource on the local file system or on the class
307                 // path
308                 final URL fileURL = ResourceUtils.getUrl4Resource(policyModelFileName);
309                 if (fileURL == null) {
310                     errorMessageBuilder.append(
311                             "  policyModelFileName [" + policyModelFileName + "] not found or is not a plain file\n");
312                 } else {
313                     final File policyModelFile = new File(fileURL.getPath());
314                     if (!policyModelFile.isFile()) {
315                         errorMessageBuilder.append("  policyModelFileName [" + policyModelFileName
316                                 + "] not found or is not a plain file\n");
317                     }
318                 }
319             }
320         }
321
322         if (periodicEventPeriod < 0) {
323             errorMessageBuilder.append("  periodicEventPeriod [" + periodicEventPeriod
324                     + "] invalid, must be specified in milliseconds as >=0\n");
325         }
326
327         return errorMessageBuilder.toString();
328     }
329 }