afede627e6381069e3421dc88090afeb413abe8c
[policy/apex-pdp.git] /
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>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
55  * APEX, 0 means no periodic event generation, negative values are illegal.
56  * </ol>
57  */
58 // @formatter:on
59 @Getter
60 @Setter
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
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 policyModel = 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 with the parameter
97      * service.
98      */
99     public EngineServiceParameters() {
100         super();
101
102         // Set the name for the parameters
103         this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
104     }
105
106     /**
107      * Gets the key of the Apex engine service.
108      *
109      * @return the Apex engine service key
110      */
111     public AxArtifactKey getEngineKey() {
112         return new AxArtifactKey(name, version);
113     }
114
115     /**
116      * Sets the key of the Apex engine service.
117      *
118      * @param key the the Apex engine service key
119      */
120     public void setEngineKey(final AxArtifactKey key) {
121         this.setName(key.getName());
122         this.setVersion(key.getVersion());
123     }
124
125     /**
126      * {@inheritDoc}.
127      */
128     @Override
129     public GroupValidationResult validate() {
130         final GroupValidationResult result = new GroupValidationResult(this);
131
132         validateStringParameters(result);
133
134         validateNumericParameters(result);
135
136         if (StringUtils.isBlank(policyModel)) {
137             result.setResult("policyModel", ValidationStatus.INVALID, "must be specified");
138         }
139         result.setResult("engineParameters", engineParameters.validate());
140
141         return result;
142     }
143
144     /**
145      * Validate string parameters.
146      *
147      * @param result the result of string parameter validation
148      */
149     private void validateStringParameters(final GroupValidationResult result) {
150         if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
151             result.setResult("name", ValidationStatus.INVALID,
152                             "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
153         }
154
155         if (version == null || !version.matches(AxKey.VERSION_REGEXP)) {
156             result.setResult("version", ValidationStatus.INVALID,
157                             "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
158         }
159     }
160
161     /**
162      * Validate numeric parameters.
163      *
164      * @param result the result of numeric parameter validation
165      */
166     private void validateNumericParameters(final GroupValidationResult result) {
167         if (id < 0) {
168             result.setResult("id", ValidationStatus.INVALID,
169                             "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
170         }
171
172         if (instanceCount < 1) {
173             result.setResult("instanceCount", ValidationStatus.INVALID,
174                             "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1");
175         }
176
177         if (deploymentPort < 1 || deploymentPort > MAX_PORT) {
178             result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort
179                             + "] invalid, must be specified as 1024 <= port <= 65535");
180         }
181
182         if (periodicEventPeriod < 0) {
183             result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
184                             + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
185         }
186     }
187
188 }