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;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Table;
30 import javax.xml.bind.annotation.XmlAccessType;
31 import javax.xml.bind.annotation.XmlAccessorType;
32 import javax.xml.bind.annotation.XmlElement;
33 import javax.xml.bind.annotation.XmlRootElement;
34 import javax.xml.bind.annotation.XmlType;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
42 import org.onap.policy.common.utils.validation.Assertions;
45 * This class is used to specify the configuration parameters that may be passed to a task
46 * {@link AxTask}. Task parameters are read from a configuration file when Apex starts and are
47 * passed to the task by the Apex engine when a task is executed. Each task parameter has a key and
48 * a default value. If the task parameter is not set in a configuration file, the task uses its
53 @Table(name = "AxTaskParameter")
55 @XmlAccessorType(XmlAccessType.FIELD)
56 @XmlRootElement(name = "apexTaskParameter", namespace = "http://www.onap.org/policy/apex-pdp")
57 @XmlType(name = "AxTaskParameter", namespace = "http://www.onap.org/policy/apex-pdp",
58 propOrder = {"key", "defaultValue"})
60 public class AxTaskParameter extends AxConcept {
61 private static final long serialVersionUID = 7351688156934099977L;
64 @XmlElement(name = "key", required = true)
65 private AxReferenceKey key;
67 @Column(name = "defaultValue")
69 private String defaultValue;
72 * The Default Constructor creates a task parameter with a null reference key and a null default
75 public AxTaskParameter() {
76 this(new AxReferenceKey());
82 * @param copyConcept the concept to copy from
84 public AxTaskParameter(final AxTaskParameter copyConcept) {
89 * The Keyed Constructor creates a task parameter with the given reference key and a null
92 * @param taskParameterKey the task parameter key
94 public AxTaskParameter(final AxReferenceKey taskParameterKey) {
95 this(taskParameterKey, "");
99 * The Default Constructor creates a task parameter with the given reference key and default
102 * @param key the reference key of the task parameter
103 * @param defaultValue the default value of the task parameter
105 public AxTaskParameter(final AxReferenceKey key, final String defaultValue) {
107 Assertions.argumentNotNull(key, "key may not be null");
108 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
111 this.defaultValue = defaultValue.trim();
118 public AxReferenceKey getKey() {
126 public List<AxKey> getKeys() {
127 return key.getKeys();
131 * Sets the reference key of the task parameter.
133 * @param key the reference key of the task parameter
135 public void setKey(final AxReferenceKey key) {
136 Assertions.argumentNotNull(key, "key may not be null");
141 * Gets the default value of the task parameter.
143 * @return the default value of the task parameter
145 public String getTaskParameterValue() {
150 * Sets the default value of the task parameter.
152 * @param defaultValue the default value of the task parameter
154 public void setDefaultValue(final String defaultValue) {
155 Assertions.argumentNotNull(defaultValue, "defaultValue may not be null");
156 this.defaultValue = defaultValue.trim();
163 public AxValidationResult validate(final AxValidationResult resultIn) {
164 AxValidationResult result = resultIn;
166 if (key.equals(AxReferenceKey.getNullKey())) {
167 result.addValidationMessage(
168 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
171 result = key.validate(result);
173 if (defaultValue.trim().length() == 0) {
174 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
175 "no defaultValue specified, defaultValue is blank"));
185 public void clean() {
187 defaultValue = defaultValue.trim();
194 public String toString() {
195 final StringBuilder builder = new StringBuilder();
196 builder.append(this.getClass().getSimpleName());
197 builder.append(":(");
198 builder.append("key=");
200 builder.append(",defaultValue=");
201 builder.append(defaultValue);
203 return builder.toString();
210 public AxConcept copyTo(final AxConcept targetObject) {
211 Assertions.argumentNotNull(targetObject, "target may not be null");
213 final Object copyObject = targetObject;
214 Assertions.instanceOf(copyObject, AxTaskParameter.class);
216 final AxTaskParameter copy = ((AxTaskParameter) copyObject);
217 copy.setKey(new AxReferenceKey(key));
218 copy.setDefaultValue(defaultValue);
227 public int hashCode() {
228 final int prime = 31;
230 result = prime * result + key.hashCode();
231 result = prime * result + defaultValue.hashCode();
239 public boolean equals(final Object obj) {
246 if (getClass() != obj.getClass()) {
250 final AxTaskParameter other = (AxTaskParameter) obj;
251 if (!key.equals(other.key)) {
254 return defaultValue.equals(other.defaultValue);
261 public int compareTo(final AxConcept otherObj) {
262 if (otherObj == null) {
265 if (this == otherObj) {
268 if (getClass() != otherObj.getClass()) {
269 return this.hashCode() - otherObj.hashCode();
272 final AxTaskParameter other = (AxTaskParameter) otherObj;
273 if (!key.equals(other.key)) {
274 return key.compareTo(other.key);
276 return defaultValue.compareTo(other.defaultValue);