3a53b59cb72da6398d43fdf6a80f5d454e040bd7
[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>The following parameters are defined:
39  * <ol>
40  * <li>name: The name of the Apex engine service, which can be set to any value that matches the
41  * regular expression {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#NAME_REGEXP}.
42  * <li>version: The name of the Apex engine service, which can be set to any value that matches the
43  * regular expression {@link org.onap.policy.apex.model.basicmodel.concepts.AxKey#VERSION_REGEXP}.
44  * <li>id: The ID of the Apex engine service, which can be set to any integer value by a user.
45  * <li>instanceCount: The number of Apex engines to spawn in this engine service. Each engine
46  * executes in its own thread.
47  * <li>deploymentPort: The port that the Apex Engine Service will open so that it can be managed
48  * using the EngDep protocol. The EngDep protocol allows the engine service to be monitored, to
49  * start and stop engines in the engine service, and to update the policy model of the engine
50  * service.
51  * <li>engineParameters: Parameters (a {@link EngineParameters} instance) that all of the engines in
52  * the engine service will use. All engine threads use the same parameters and act as a pool of
53  * engines. Engine parameters specify the executors and context management for the engines.
54  * <li>policyModelFileName: The full path to the policy model file name to deploy on the engine
55  * service.
56  * <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT
57  * will be generated by 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 extends AbstractParameters implements ApexParameterValidator {
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     // Apex engine service parameters
82     private String name                = DEFAULT_NAME;
83     private String version             = DEFAULT_VERSION;
84     private int    id                  = DEFAULT_ID;
85     private int    instanceCount       = DEFAULT_INSTANCE_COUNT;
86     private int    deploymentPort      = DEFAULT_DEPLOYMENT_PORT;
87     private String policyModelFileName = null;
88     private long   periodicEventPeriod = 0;
89     // @formatter:on
90
91     // Apex engine internal parameters
92     private EngineParameters engineParameters = new EngineParameters();
93
94     /**
95      * Constructor to create an apex engine service parameters instance and register the instance
96      * with the parameter service.
97      */
98     public EngineServiceParameters() {
99         super(EngineServiceParameters.class.getCanonicalName());
100         ParameterService.registerParameters(EngineServiceParameters.class, this);
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 ResourceUtils.getFilePath4Resource(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
225      *        service
226      */
227     public void setPolicyModelFileName(final String policyModelFileName) {
228         this.policyModelFileName = policyModelFileName;
229     }
230
231     /**
232      * Get the period in milliseconds at which periodic events are sent, zero means no periodic
233      * events are being sent.
234      * 
235      * @return the periodic period
236      */
237     public long getPeriodicEventPeriod() {
238         return periodicEventPeriod;
239     }
240
241     /**
242      * Set the period in milliseconds at which periodic events are sent, zero means no periodic
243      * events are to be sent, negative values are illegal.
244      * 
245      * @param periodicEventPeriod the periodic period
246      */
247     public void setPeriodicEventPeriod(final long periodicEventPeriod) {
248         this.periodicEventPeriod = periodicEventPeriod;
249     }
250
251     /**
252      * Gets the engine parameters for engines in the engine service.
253      *
254      * @return the engine parameters for engines in the engine service
255      */
256     public EngineParameters getEngineParameters() {
257         return engineParameters;
258     }
259
260     /**
261      * Sets the engine parameters for engines in the engine service.
262      *
263      * @param engineParameters the engine parameters for engines in the engine service
264      */
265     public void setEngineParameters(final EngineParameters engineParameters) {
266         this.engineParameters = engineParameters;
267     }
268
269     /*
270      * (non-Javadoc)
271      *
272      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
273      */
274     @Override
275     public String validate() {
276         final StringBuilder errorMessageBuilder = new StringBuilder();
277
278         try {
279             new AxArtifactKey(name, version);
280         } catch (final Exception e) {
281             errorMessageBuilder.append("  name [" + name + "] and/or version [" + version + "] invalid\n");
282             errorMessageBuilder.append("   " + e.getMessage() + "\n");
283         }
284
285         if (id < 0) {
286             errorMessageBuilder.append(
287                     "  id not specified or specified value [" + id + "] invalid, must be specified as id >= 0\n");
288         }
289
290         if (instanceCount < 1) {
291             errorMessageBuilder.append(
292                     "  instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1\n");
293         }
294
295         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
296             errorMessageBuilder.append(
297                     "  deploymentPort [" + deploymentPort + "] invalid, must be specified as 1024 <= port <= 65535\n");
298         }
299
300         if (policyModelFileName != null) {
301             if (policyModelFileName.trim().length() == 0) {
302                 errorMessageBuilder.append("  policyModelFileName [" + policyModelFileName
303                         + "] invalid, must be specified as a non-empty string\n");
304             } else {
305                 // The file name can refer to a resource on the local file system or on the class
306                 // path
307                 final URL fileURL = ResourceUtils.getUrl4Resource(policyModelFileName);
308                 if (fileURL == null) {
309                     errorMessageBuilder.append(
310                             "  policyModelFileName [" + policyModelFileName + "] not found or is not a plain file\n");
311                 } else {
312                     final File policyModelFile = new File(fileURL.getPath());
313                     if (!policyModelFile.isFile()) {
314                         errorMessageBuilder.append("  policyModelFileName [" + policyModelFileName
315                                 + "] not found or is not a plain file\n");
316                     }
317                 }
318             }
319         }
320
321         if (periodicEventPeriod < 0) {
322             errorMessageBuilder.append("  periodicEventPeriod [" + periodicEventPeriod
323                     + "] invalid, must be specified in milliseconds as >=0\n");
324         }
325
326         return errorMessageBuilder.toString();
327     }
328 }