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