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 java.util.Map.Entry;
27 import java.util.NavigableMap;
29 import java.util.TreeMap;
31 import javax.persistence.CascadeType;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.JoinTable;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
38 import javax.xml.bind.Unmarshaller;
39 import javax.xml.bind.annotation.XmlAccessType;
40 import javax.xml.bind.annotation.XmlAccessorType;
41 import javax.xml.bind.annotation.XmlElement;
42 import javax.xml.bind.annotation.XmlType;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
52 import org.onap.policy.common.utils.validation.Assertions;
55 * This class is a task container and holds a map of the tasks for an entire Apex model. All Apex
56 * models that use tasks must have an {@link AxTasks} field. The {@link AxTasks} class implements
57 * the helper methods of the {@link AxConceptGetter} interface to allow {@link AxTask} instances to
58 * be retrieved by calling methods directly on this class without referencing the contained map.
60 * <p>Validation checks that the container key is not null. An error is issued if no tasks are defined
61 * in the container. Each task entry is checked to ensure that its key and value are not null and
62 * that the key matches the key in the map value. Each task entry is then validated individually.
65 @Table(name = "AxTasks")
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "AxTasks", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "taskMap"})
69 public class AxTasks extends AxConcept implements AxConceptGetter<AxTask> {
70 private static final long serialVersionUID = 4290442590545820316L;
73 @XmlElement(name = "key", required = true)
74 private AxArtifactKey key;
77 @ManyToMany(cascade = CascadeType.ALL)
79 joinColumns = {@JoinColumn(name = "taskMapName", referencedColumnName = "name"),
80 @JoinColumn(name = "taskMapVersion", referencedColumnName = "version")},
81 inverseJoinColumns = {@JoinColumn(name = "taskName", referencedColumnName = "name"),
82 @JoinColumn(name = "taskVersion", referencedColumnName = "version")})
83 @XmlElement(required = true)
84 private Map<AxArtifactKey, AxTask> taskMap;
88 * The Default Constructor creates a {@link AxTasks} object with a null artifact key and creates
92 this(new AxArtifactKey());
98 * @param copyConcept the concept to copy from
100 public AxTasks(final AxTasks copyConcept) {
105 * The Keyed Constructor creates a {@link AxTasks} object with the given artifact key and
106 * creates an empty event map.
110 public AxTasks(final AxArtifactKey key) {
111 this(key, new TreeMap<AxArtifactKey, AxTask>());
115 * This Constructor creates a task container with all of its fields defined.
117 * @param key the task container key
118 * @param taskMap the tasks to be stored in the task container
120 public AxTasks(final AxArtifactKey key, final Map<AxArtifactKey, AxTask> taskMap) {
122 Assertions.argumentNotNull(key, "key may not be null");
123 Assertions.argumentNotNull(taskMap, "taskMap may not be null");
126 this.taskMap = new TreeMap<>();
127 this.taskMap.putAll(taskMap);
131 * When a model is unmarshalled from disk or from the database, the task map is returned as a
132 * raw hash map. This method is called by JAXB after unmarshaling and is used to convert the
133 * hash map to a {@link NavigableMap} so that it will work with the {@link AxConceptGetter}
136 * @param unmarshaler the unmarshaler that is unmarshaling the model
137 * @param parent the parent object of this object in the unmarshaler
139 public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) {
140 // The map must be navigable to allow name and version searching, unmarshaling returns a
142 final NavigableMap<AxArtifactKey, AxTask> navigableTaskMap = new TreeMap<>();
143 navigableTaskMap.putAll(taskMap);
144 taskMap = navigableTaskMap;
151 public AxArtifactKey getKey() {
159 public List<AxKey> getKeys() {
160 final List<AxKey> keyList = key.getKeys();
162 for (final AxTask task : taskMap.values()) {
163 keyList.addAll(task.getKeys());
170 * Sets the task container key.
172 * @param key the task container key
174 public void setKey(final AxArtifactKey key) {
175 Assertions.argumentNotNull(key, "key may not be null");
180 * Gets the tasks stored in the task container.
182 * @return the tasks stored in the task container
184 public Map<AxArtifactKey, AxTask> getTaskMap() {
189 * Sets the tasks to be stored in the task container.
191 * @param taskMap the tasks to be stored in the task container
193 public void setTaskMap(final Map<AxArtifactKey, AxTask> taskMap) {
194 Assertions.argumentNotNull(taskMap, "taskMap may not be null");
195 this.taskMap = new TreeMap<>();
196 this.taskMap.putAll(taskMap);
203 public AxValidationResult validate(final AxValidationResult resultIn) {
204 AxValidationResult result = resultIn;
206 if (key.equals(AxArtifactKey.getNullKey())) {
207 result.addValidationMessage(
208 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
211 result = key.validate(result);
213 if (taskMap.size() == 0) {
214 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
215 "taskMap may not be empty"));
217 for (final Entry<AxArtifactKey, AxTask> taskEntry : taskMap.entrySet()) {
218 if (taskEntry.getKey().equals(AxArtifactKey.getNullKey())) {
219 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "key on task entry " + taskEntry.getKey() + " may not be the null key"));
221 } else if (taskEntry.getValue() == null) {
222 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
223 "value on task entry " + taskEntry.getKey() + " may not be null"));
225 if (!taskEntry.getKey().equals(taskEntry.getValue().getKey())) {
226 result.addValidationMessage(new AxValidationMessage(key, this.getClass(),
227 ValidationResult.INVALID, "key on task entry key " + taskEntry.getKey()
228 + " does not equal task value key " + taskEntry.getValue().getKey()));
231 result = taskEntry.getValue().validate(result);
243 public void clean() {
245 for (final Entry<AxArtifactKey, AxTask> taskEntry : taskMap.entrySet()) {
246 taskEntry.getKey().clean();
247 taskEntry.getValue().clean();
255 public String toString() {
256 final StringBuilder builder = new StringBuilder();
257 builder.append(this.getClass().getSimpleName());
258 builder.append(":(");
259 builder.append("key=");
261 builder.append(",taskMap=");
262 builder.append(taskMap);
264 return builder.toString();
271 public AxConcept copyTo(final AxConcept targetObject) {
272 Assertions.argumentNotNull(targetObject, "target may not be null");
274 final Object copyObject = targetObject;
275 Assertions.instanceOf(copyObject, AxTasks.class);
277 final AxTasks copy = ((AxTasks) copyObject);
278 copy.setKey(new AxArtifactKey(key));
280 final Map<AxArtifactKey, AxTask> newTaskMap = new TreeMap<>();
281 for (final Entry<AxArtifactKey, AxTask> taskMapEntry : taskMap.entrySet()) {
282 newTaskMap.put(new AxArtifactKey(taskMapEntry.getKey()), new AxTask(taskMapEntry.getValue()));
284 copy.setTaskMap(newTaskMap);
293 public int hashCode() {
294 final int prime = 31;
296 result = prime * result + key.hashCode();
297 result = prime * result + taskMap.hashCode();
305 public boolean equals(final Object obj) {
313 if (getClass() != obj.getClass()) {
317 final AxTasks other = (AxTasks) obj;
318 if (!key.equals(other.key)) {
321 return taskMap.equals(other.taskMap);
328 public int compareTo(final AxConcept otherObj) {
329 if (otherObj == null) {
332 if (this == otherObj) {
335 if (getClass() != otherObj.getClass()) {
336 return this.hashCode() - otherObj.hashCode();
339 final AxTasks other = (AxTasks) otherObj;
340 if (!key.equals(other.key)) {
341 return key.compareTo(other.key);
343 if (!taskMap.equals(other.taskMap)) {
344 return (taskMap.hashCode() - other.taskMap.hashCode());
354 public AxTask get(final AxArtifactKey conceptKey) {
355 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxTask>) taskMap).get(conceptKey);
362 public AxTask get(final String conceptKeyName) {
363 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxTask>) taskMap).get(conceptKeyName);
370 public AxTask get(final String conceptKeyName, final String conceptKeyVersion) {
371 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxTask>) taskMap).get(conceptKeyName,
379 public Set<AxTask> getAll(final String conceptKeyName) {
380 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxTask>) taskMap).getAll(conceptKeyName);
387 public Set<AxTask> getAll(final String conceptKeyName, final String conceptKeyVersion) {
388 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxTask>) taskMap).getAll(conceptKeyName,