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