2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019,2022 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.xml.bind.annotation.XmlAccessType;
26 import javax.xml.bind.annotation.XmlAccessorType;
27 import javax.xml.bind.annotation.XmlElement;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import javax.xml.bind.annotation.XmlType;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36 import org.onap.policy.common.utils.validation.Assertions;
39 * This class is used to specify the configuration parameters that may be passed to a task
40 * {@link AxTask}. Task parameters are read from a configuration file when Apex starts and are
41 * passed to the task by the Apex engine when a task is executed. Each task parameter has a key and
42 * a default value. If the task parameter is not set in a configuration file, the task uses its
46 @XmlAccessorType(XmlAccessType.FIELD)
47 @XmlRootElement(name = "apexTaskParameter", namespace = "http://www.onap.org/policy/apex-pdp")
48 @XmlType(name = "AxTaskParameter", namespace = "http://www.onap.org/policy/apex-pdp",
49 propOrder = {"key", "defaultValue"})
51 public class AxTaskParameter extends AxConcept {
52 private static final long serialVersionUID = 7351688156934099977L;
54 @XmlElement(name = "key", required = true)
55 private AxReferenceKey key;
58 private String defaultValue;
61 * The Default Constructor creates a task parameter with a null reference key and a null default
64 public AxTaskParameter() {
65 this(new AxReferenceKey());
71 * @param copyConcept the concept to copy from
73 public AxTaskParameter(final AxTaskParameter copyConcept) {
78 * The Keyed Constructor creates a task parameter with the given reference key and a null
81 * @param taskParameterKey the task parameter key
83 public AxTaskParameter(final AxReferenceKey taskParameterKey) {
84 this(taskParameterKey, "");
88 * The Default Constructor creates a task parameter with the given reference key and default
91 * @param key the reference key of the task parameter
92 * @param defaultValue the default value of the task parameter
94 public AxTaskParameter(final AxReferenceKey key, final String defaultValue) {
96 Assertions.argumentNotNull(key, "key may not be null");
97 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
100 this.defaultValue = defaultValue.trim();
107 public AxReferenceKey getKey() {
115 public List<AxKey> getKeys() {
116 return key.getKeys();
120 * Sets the reference key of the task parameter.
122 * @param key the reference key of the task parameter
124 public void setKey(final AxReferenceKey key) {
125 Assertions.argumentNotNull(key, "key may not be null");
130 * Gets the default value of the task parameter.
132 * @return the default value of the task parameter
134 public String getTaskParameterValue() {
139 * Sets the default value of the task parameter.
141 * @param defaultValue the default value of the task parameter
143 public void setDefaultValue(final String defaultValue) {
144 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
145 this.defaultValue = defaultValue.trim();
152 public AxValidationResult validate(final AxValidationResult resultIn) {
153 AxValidationResult result = resultIn;
155 if (key.equals(AxReferenceKey.getNullKey())) {
156 result.addValidationMessage(
157 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
160 result = key.validate(result);
162 if (defaultValue.trim().length() == 0) {
163 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
164 "no defaultValue specified, defaultValue is blank"));
174 public void clean() {
176 defaultValue = defaultValue.trim();
183 public String toString() {
184 final StringBuilder builder = new StringBuilder();
185 builder.append(this.getClass().getSimpleName());
186 builder.append(":(");
187 builder.append("key=");
189 builder.append(",defaultValue=");
190 builder.append(defaultValue);
192 return builder.toString();
199 public AxConcept copyTo(final AxConcept targetObject) {
200 Assertions.argumentNotNull(targetObject, "target may not be null");
202 final Object copyObject = targetObject;
203 Assertions.instanceOf(copyObject, AxTaskParameter.class);
205 final AxTaskParameter copy = ((AxTaskParameter) copyObject);
206 copy.setKey(new AxReferenceKey(key));
207 copy.setDefaultValue(defaultValue);
216 public int hashCode() {
217 final int prime = 31;
219 result = prime * result + key.hashCode();
220 result = prime * result + defaultValue.hashCode();
228 public boolean equals(final Object obj) {
235 if (getClass() != obj.getClass()) {
239 final AxTaskParameter other = (AxTaskParameter) obj;
240 if (!key.equals(other.key)) {
243 return defaultValue.equals(other.defaultValue);
250 public int compareTo(final AxConcept otherObj) {
251 if (otherObj == null) {
254 if (this == otherObj) {
257 if (getClass() != otherObj.getClass()) {
258 return this.hashCode() - otherObj.hashCode();
261 final AxTaskParameter other = (AxTaskParameter) otherObj;
262 if (!key.equals(other.key)) {
263 return key.compareTo(other.key);
265 return defaultValue.compareTo(other.defaultValue);