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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.concepts;
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;
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
51 @Table(name = "AxTaskParameter")
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"})
58 public class AxTaskParameter extends AxConcept {
59 private static final long serialVersionUID = 7351688156934099977L;
62 @XmlElement(name = "key", required = true)
63 private AxReferenceKey key;
65 @Column(name = "defaultValue")
67 private String defaultValue;
70 * The Default Constructor creates a task parameter with a null reference key and a null default
73 public AxTaskParameter() {
74 this(new AxReferenceKey());
80 * @param copyConcept the concept to copy from
82 public AxTaskParameter(final AxTaskParameter copyConcept) {
87 * The Keyed Constructor creates a task parameter with the given reference key and a null
90 * @param taskParameterKey the task parameter key
92 public AxTaskParameter(final AxReferenceKey taskParameterKey) {
93 this(taskParameterKey, "");
97 * The Default Constructor creates a task parameter with the given reference key and default
100 * @param key the reference key of the task parameter
101 * @param defaultValue the default value of the task parameter
103 public AxTaskParameter(final AxReferenceKey key, final String defaultValue) {
105 Assertions.argumentNotNull(key, "key may not be null");
106 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
109 this.defaultValue = defaultValue.trim();
116 public AxReferenceKey getKey() {
124 public List<AxKey> getKeys() {
125 return key.getKeys();
129 * Sets the reference key of the task parameter.
131 * @param key the reference key of the task parameter
133 public void setKey(final AxReferenceKey key) {
134 Assertions.argumentNotNull(key, "key may not be null");
139 * Gets the default value of the task parameter.
141 * @return the default value of the task parameter
143 public String getTaskParameterValue() {
148 * Sets the default value of the task parameter.
150 * @param defaultValue the default value of the task parameter
152 public void setDefaultValue(final String defaultValue) {
153 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
154 this.defaultValue = defaultValue.trim();
161 public AxValidationResult validate(final AxValidationResult resultIn) {
162 AxValidationResult result = resultIn;
164 if (key.equals(AxReferenceKey.getNullKey())) {
165 result.addValidationMessage(
166 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
169 result = key.validate(result);
171 if (defaultValue.trim().length() == 0) {
172 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
173 "no defaultValue specified, defaultValue is blank"));
183 public void clean() {
185 defaultValue = defaultValue.trim();
192 public String toString() {
193 final StringBuilder builder = new StringBuilder();
194 builder.append(this.getClass().getSimpleName());
195 builder.append(":(");
196 builder.append("key=");
198 builder.append(",defaultValue=");
199 builder.append(defaultValue);
201 return builder.toString();
208 public AxConcept copyTo(final AxConcept targetObject) {
209 Assertions.argumentNotNull(targetObject, "target may not be null");
211 final Object copyObject = targetObject;
212 Assertions.instanceOf(copyObject, AxTaskParameter.class);
214 final AxTaskParameter copy = ((AxTaskParameter) copyObject);
215 copy.setKey(new AxReferenceKey(key));
216 copy.setDefaultValue(defaultValue);
225 public int hashCode() {
226 final int prime = 31;
228 result = prime * result + key.hashCode();
229 result = prime * result + defaultValue.hashCode();
237 public boolean equals(final Object obj) {
244 if (getClass() != obj.getClass()) {
248 final AxTaskParameter other = (AxTaskParameter) obj;
249 if (!key.equals(other.key)) {
252 return defaultValue.equals(other.defaultValue);
259 public int compareTo(final AxConcept otherObj) {
260 if (otherObj == null) {
263 if (this == otherObj) {
266 if (getClass() != otherObj.getClass()) {
267 return this.hashCode() - otherObj.hashCode();
270 final AxTaskParameter other = (AxTaskParameter) otherObj;
271 if (!key.equals(other.key)) {
272 return key.compareTo(other.key);
274 return defaultValue.compareTo(other.defaultValue);