95b8ff0cf4bc23801739829f2637851a07ba6727
[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      * {@inheritDoc}.
116      */
117     @Override
118     public AxReferenceKey getKey() {
119         return key;
120     }
121
122     /**
123      * {@inheritDoc}.
124      */
125     @Override
126     public List<AxKey> getKeys() {
127         return key.getKeys();
128     }
129
130     /**
131      * Sets the reference key of the task parameter.
132      *
133      * @param key the reference key of the task parameter
134      */
135     public void setKey(final AxReferenceKey key) {
136         Assertions.argumentNotNull(key, "key may not be null");
137         this.key = key;
138     }
139
140     /**
141      * Gets the default value of the task parameter.
142      *
143      * @return the default value of the task parameter
144      */
145     public String getTaskParameterValue() {
146         return defaultValue;
147     }
148
149     /**
150      * Sets the default value of the task parameter.
151      *
152      * @param defaultValue the default value of the task parameter
153      */
154     public void setDefaultValue(final String defaultValue) {
155         Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
156         this.defaultValue = defaultValue.trim();
157     }
158
159     /**
160      * {@inheritDoc}.
161      */
162     @Override
163     public AxValidationResult validate(final AxValidationResult resultIn) {
164         AxValidationResult result = resultIn;
165
166         if (key.equals(AxReferenceKey.getNullKey())) {
167             result.addValidationMessage(
168                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
169         }
170
171         result = key.validate(result);
172
173         if (defaultValue.trim().length() == 0) {
174             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
175                     "no defaultValue specified, defaultValue is blank"));
176         }
177
178         return result;
179     }
180
181     /**
182      * {@inheritDoc}.
183      */
184     @Override
185     public void clean() {
186         key.clean();
187         defaultValue = defaultValue.trim();
188     }
189
190     /**
191      * {@inheritDoc}.
192      */
193     @Override
194     public String toString() {
195         final StringBuilder builder = new StringBuilder();
196         builder.append(this.getClass().getSimpleName());
197         builder.append(":(");
198         builder.append("key=");
199         builder.append(key);
200         builder.append(",defaultValue=");
201         builder.append(defaultValue);
202         builder.append(")");
203         return builder.toString();
204     }
205
206     /**
207      * {@inheritDoc}.
208      */
209     @Override
210     public AxConcept copyTo(final AxConcept targetObject) {
211         Assertions.argumentNotNull(targetObject, "target may not be null");
212
213         final Object copyObject = targetObject;
214         Assertions.instanceOf(copyObject, AxTaskParameter.class);
215
216         final AxTaskParameter copy = ((AxTaskParameter) copyObject);
217         copy.setKey(new AxReferenceKey(key));
218         copy.setDefaultValue(defaultValue);
219
220         return copy;
221     }
222
223     /**
224      * {@inheritDoc}.
225      */
226     @Override
227     public int hashCode() {
228         final int prime = 31;
229         int result = 1;
230         result = prime * result + key.hashCode();
231         result = prime * result + defaultValue.hashCode();
232         return result;
233     }
234
235     /**
236      * {@inheritDoc}.
237      */
238     @Override
239     public boolean equals(final Object obj) {
240         if (obj == null) {
241             return false;
242         }
243         if (this == obj) {
244             return true;
245         }
246         if (getClass() != obj.getClass()) {
247             return false;
248         }
249
250         final AxTaskParameter other = (AxTaskParameter) obj;
251         if (!key.equals(other.key)) {
252             return false;
253         }
254         return defaultValue.equals(other.defaultValue);
255     }
256
257     /**
258      * {@inheritDoc}.
259      */
260     @Override
261     public int compareTo(final AxConcept otherObj) {
262         if (otherObj == null) {
263             return -1;
264         }
265         if (this == otherObj) {
266             return 0;
267         }
268         if (getClass() != otherObj.getClass()) {
269             return this.hashCode() - otherObj.hashCode();
270         }
271
272         final AxTaskParameter other = (AxTaskParameter) otherObj;
273         if (!key.equals(other.key)) {
274             return key.compareTo(other.key);
275         }
276         return defaultValue.compareTo(other.defaultValue);
277     }
278 }