APEX standalone support for ToscaPolicy format
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / parameters / engineservice / EngineServiceParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.parameters.engineservice;
23
24 import lombok.Getter;
25 import lombok.Setter;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.apex.core.engine.EngineParameters;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
30 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
31 import org.onap.policy.common.parameters.GroupValidationResult;
32 import org.onap.policy.common.parameters.ParameterGroup;
33 import org.onap.policy.common.parameters.ValidationStatus;
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 @Getter
61 @Setter
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
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 policyModel = 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      * {@inheritDoc}.
128      */
129     @Override
130     public GroupValidationResult validate() {
131         final GroupValidationResult result = new GroupValidationResult(this);
132
133         validateStringParameters(result);
134
135         validateNumericParameters(result);
136
137         if (StringUtils.isBlank(policyModel)) {
138             result.setResult("policyModel", ValidationStatus.INVALID, "must be specified");
139         }
140         result.setResult("engineParameters", engineParameters.validate());
141
142         return result;
143     }
144
145     /**
146      * Validate string parameters.
147      *
148      * @param result the result of string parameter validation
149      */
150     private void validateStringParameters(final GroupValidationResult result) {
151         if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
152             result.setResult("name", ValidationStatus.INVALID,
153                             "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
154         }
155
156         if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
157             result.setResult("version", ValidationStatus.INVALID,
158                             "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
159         }
160     }
161
162     /**
163      * Validate numeric parameters.
164      * 
165      * @param result the result of numeric parameter validation
166      */
167     private void validateNumericParameters(final GroupValidationResult result) {
168         if (id < 0) {
169             result.setResult("id", ValidationStatus.INVALID,
170                             "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
171         }
172
173         if (instanceCount < 1) {
174             result.setResult("instanceCount", ValidationStatus.INVALID,
175                             "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
176         }
177
178         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
179             result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
180                             + "] invalid, must be specified as 1024 <= port <= 65535");
181         }
182
183         if (periodicEventPeriod < 0) {
184             result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
185                             + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
186         }
187     }
188
189 }