a468ec499d04a319f0024bb6f026b21382e1df0e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019,2022 Nordix Foundation.
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.model.policymodel.concepts;
23
24 import java.util.List;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 /**
34  * This class is used to specify the configuration parameters that may be passed to a task
35  * {@link AxTask}. Task parameters are read from a configuration file when Apex starts and are
36  * passed to the task by the Apex engine when a task is executed. Each task parameter has a key and
37  * a default value. If the task parameter is not set in a configuration file, the task uses its
38  * default value.
39  */
40 public class AxTaskParameter extends AxConcept {
41     private static final long serialVersionUID = 7351688156934099977L;
42
43     private AxReferenceKey key;
44     private String defaultValue;
45
46     /**
47      * The Default Constructor creates a task parameter with a null reference key and a null default
48      * value.
49      */
50     public AxTaskParameter() {
51         this(new AxReferenceKey());
52     }
53
54     /**
55      * Copy constructor.
56      *
57      * @param copyConcept the concept to copy from
58      */
59     public AxTaskParameter(final AxTaskParameter copyConcept) {
60         super(copyConcept);
61     }
62
63     /**
64      * The Keyed Constructor creates a task parameter with the given reference key and a null
65      * default value.
66      *
67      * @param taskParameterKey the task parameter key
68      */
69     public AxTaskParameter(final AxReferenceKey taskParameterKey) {
70         this(taskParameterKey, "");
71     }
72
73     /**
74      * The Default Constructor creates a task parameter with the given reference key and default
75      * value.
76      *
77      * @param key the reference key of the task parameter
78      * @param defaultValue the default value of the task parameter
79      */
80     public AxTaskParameter(final AxReferenceKey key, final String defaultValue) {
81         super();
82         Assertions.argumentNotNull(key, "key may not be null");
83         Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
84
85         this.key = key;
86         this.defaultValue = defaultValue.trim();
87     }
88
89     /**
90      * {@inheritDoc}.
91      */
92     @Override
93     public AxReferenceKey getKey() {
94         return key;
95     }
96
97     /**
98      * {@inheritDoc}.
99      */
100     @Override
101     public List<AxKey> getKeys() {
102         return key.getKeys();
103     }
104
105     /**
106      * Sets the reference key of the task parameter.
107      *
108      * @param key the reference key of the task parameter
109      */
110     public void setKey(final AxReferenceKey key) {
111         Assertions.argumentNotNull(key, "key may not be null");
112         this.key = key;
113     }
114
115     /**
116      * Gets the default value of the task parameter.
117      *
118      * @return the default value of the task parameter
119      */
120     public String getTaskParameterValue() {
121         return defaultValue;
122     }
123
124     /**
125      * Sets the default value of the task parameter.
126      *
127      * @param defaultValue the default value of the task parameter
128      */
129     public void setDefaultValue(final String defaultValue) {
130         Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
131         this.defaultValue = defaultValue.trim();
132     }
133
134     /**
135      * {@inheritDoc}.
136      */
137     @Override
138     public AxValidationResult validate(final AxValidationResult resultIn) {
139         AxValidationResult result = resultIn;
140
141         if (key.equals(AxReferenceKey.getNullKey())) {
142             result.addValidationMessage(
143                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
144         }
145
146         result = key.validate(result);
147
148         if (defaultValue.trim().length() == 0) {
149             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
150                     "no defaultValue specified, defaultValue is blank"));
151         }
152
153         return result;
154     }
155
156     /**
157      * {@inheritDoc}.
158      */
159     @Override
160     public void clean() {
161         key.clean();
162         defaultValue = defaultValue.trim();
163     }
164
165     /**
166      * {@inheritDoc}.
167      */
168     @Override
169     public String toString() {
170         final StringBuilder builder = new StringBuilder();
171         builder.append(this.getClass().getSimpleName());
172         builder.append(":(");
173         builder.append("key=");
174         builder.append(key);
175         builder.append(",defaultValue=");
176         builder.append(defaultValue);
177         builder.append(")");
178         return builder.toString();
179     }
180
181     /**
182      * {@inheritDoc}.
183      */
184     @Override
185     public AxConcept copyTo(final AxConcept targetObject) {
186         Assertions.argumentNotNull(targetObject, "target may not be null");
187
188         final Object copyObject = targetObject;
189         Assertions.instanceOf(copyObject, AxTaskParameter.class);
190
191         final AxTaskParameter copy = ((AxTaskParameter) copyObject);
192         copy.setKey(new AxReferenceKey(key));
193         copy.setDefaultValue(defaultValue);
194
195         return copy;
196     }
197
198     /**
199      * {@inheritDoc}.
200      */
201     @Override
202     public int hashCode() {
203         final int prime = 31;
204         int result = 1;
205         result = prime * result + key.hashCode();
206         result = prime * result + defaultValue.hashCode();
207         return result;
208     }
209
210     /**
211      * {@inheritDoc}.
212      */
213     @Override
214     public boolean equals(final Object obj) {
215         if (obj == null) {
216             return false;
217         }
218         if (this == obj) {
219             return true;
220         }
221         if (getClass() != obj.getClass()) {
222             return false;
223         }
224
225         final AxTaskParameter other = (AxTaskParameter) obj;
226         if (!key.equals(other.key)) {
227             return false;
228         }
229         return defaultValue.equals(other.defaultValue);
230     }
231
232     /**
233      * {@inheritDoc}.
234      */
235     @Override
236     public int compareTo(final AxConcept otherObj) {
237         if (otherObj == null) {
238             return -1;
239         }
240         if (this == otherObj) {
241             return 0;
242         }
243         if (getClass() != otherObj.getClass()) {
244             return this.hashCode() - otherObj.hashCode();
245         }
246
247         final AxTaskParameter other = (AxTaskParameter) otherObj;
248         if (!key.equals(other.key)) {
249             return key.compareTo(other.key);
250         }
251         return defaultValue.compareTo(other.defaultValue);
252     }
253 }