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 org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
31 import org.onap.policy.common.utils.validation.Assertions;
34 * This class is used to specify the configuration parameters that may be passed to a task
35 * {@link AxTask}. Task parameters are read from a configuration file when Apex starts and are
36 * passed to the task by the Apex engine when a task is executed. Each task parameter has a key and
37 * a default value. If the task parameter is not set in a configuration file, the task uses its
40 public class AxTaskParameter extends AxConcept {
41 private static final long serialVersionUID = 7351688156934099977L;
43 private AxReferenceKey key;
44 private String defaultValue;
47 * The Default Constructor creates a task parameter with a null reference key and a null default
50 public AxTaskParameter() {
51 this(new AxReferenceKey());
57 * @param copyConcept the concept to copy from
59 public AxTaskParameter(final AxTaskParameter copyConcept) {
64 * The Keyed Constructor creates a task parameter with the given reference key and a null
67 * @param taskParameterKey the task parameter key
69 public AxTaskParameter(final AxReferenceKey taskParameterKey) {
70 this(taskParameterKey, "");
74 * The Default Constructor creates a task parameter with the given reference key and default
77 * @param key the reference key of the task parameter
78 * @param defaultValue the default value of the task parameter
80 public AxTaskParameter(final AxReferenceKey key, final String defaultValue) {
82 Assertions.argumentNotNull(key, "key may not be null");
83 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
86 this.defaultValue = defaultValue.trim();
93 public AxReferenceKey getKey() {
101 public List<AxKey> getKeys() {
102 return key.getKeys();
106 * Sets the reference key of the task parameter.
108 * @param key the reference key of the task parameter
110 public void setKey(final AxReferenceKey key) {
111 Assertions.argumentNotNull(key, "key may not be null");
116 * Gets the default value of the task parameter.
118 * @return the default value of the task parameter
120 public String getTaskParameterValue() {
125 * Sets the default value of the task parameter.
127 * @param defaultValue the default value of the task parameter
129 public void setDefaultValue(final String defaultValue) {
130 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
131 this.defaultValue = defaultValue.trim();
138 public AxValidationResult validate(final AxValidationResult resultIn) {
139 AxValidationResult result = resultIn;
141 if (key.equals(AxReferenceKey.getNullKey())) {
142 result.addValidationMessage(
143 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
146 result = key.validate(result);
148 if (defaultValue.trim().length() == 0) {
149 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
150 "no defaultValue specified, defaultValue is blank"));
160 public void clean() {
162 defaultValue = defaultValue.trim();
169 public String toString() {
170 final StringBuilder builder = new StringBuilder();
171 builder.append(this.getClass().getSimpleName());
172 builder.append(":(");
173 builder.append("key=");
175 builder.append(",defaultValue=");
176 builder.append(defaultValue);
178 return builder.toString();
185 public AxConcept copyTo(final AxConcept targetObject) {
186 Assertions.argumentNotNull(targetObject, "target may not be null");
188 final Object copyObject = targetObject;
189 Assertions.instanceOf(copyObject, AxTaskParameter.class);
191 final AxTaskParameter copy = ((AxTaskParameter) copyObject);
192 copy.setKey(new AxReferenceKey(key));
193 copy.setDefaultValue(defaultValue);
202 public int hashCode() {
203 final int prime = 31;
205 result = prime * result + key.hashCode();
206 result = prime * result + defaultValue.hashCode();
214 public boolean equals(final Object obj) {
221 if (getClass() != obj.getClass()) {
225 final AxTaskParameter other = (AxTaskParameter) obj;
226 if (!key.equals(other.key)) {
229 return defaultValue.equals(other.defaultValue);
236 public int compareTo(final AxConcept otherObj) {
237 if (otherObj == null) {
240 if (this == otherObj) {
243 if (getClass() != otherObj.getClass()) {
244 return this.hashCode() - otherObj.hashCode();
247 final AxTaskParameter other = (AxTaskParameter) otherObj;
248 if (!key.equals(other.key)) {
249 return key.compareTo(other.key);
251 return defaultValue.compareTo(other.defaultValue);