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