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