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 policy container and holds a map of the policies for an entire Apex model. All Apex models that use
56 * policies must have an {@link AxPolicies} field. The {@link AxPolicies} class implements the helper methods of the
57 * {@link AxConceptGetter} interface to allow {@link AxPolicy} instances to be retrieved by calling methods directly on
58 * this class without referencing the contained map.
60 * <p>Validation checks that the container key is not null. An error is issued if no policies are defined in the
61 * container. Each policy entry is checked to ensure that its key and value are not null and that the key matches the
62 * key in the map value. Each policy entry is then validated individually.
65 @Table(name = "AxPolicies")
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "AxPolicies", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
69 { "key", "policyMap" })
71 public class AxPolicies extends AxConcept implements AxConceptGetter<AxPolicy> {
72 private static final long serialVersionUID = 4290442590545820316L;
75 @XmlElement(name = "key", required = true)
76 private AxArtifactKey key;
79 @ManyToMany(cascade = CascadeType.ALL)
81 joinColumns = {@JoinColumn(name = "policyMapName", referencedColumnName = "name"),
82 @JoinColumn(name = "policyMapVersion", referencedColumnName = "version")},
83 inverseJoinColumns = {@JoinColumn(name = "policyName", referencedColumnName = "name"),
84 @JoinColumn(name = "policyVersion", referencedColumnName = "version")})
85 @XmlElement(required = true)
86 private Map<AxArtifactKey, AxPolicy> policyMap;
90 * The Default Constructor creates a {@link AxPolicies} object with a null artifact key and creates an empty event
94 this(new AxArtifactKey());
98 * The Key Constructor creates a {@link AxPolicies} object with the given artifact key and creates an empty event
103 public AxPolicies(final AxArtifactKey key) {
104 this(key, new TreeMap<AxArtifactKey, AxPolicy>());
110 * @param copyConcept the concept to copy from
112 public AxPolicies(final AxPolicies copyConcept) {
117 * This Constructor creates a policy container with all of its fields defined.
119 * @param key the policy container key
120 * @param policyMap the policies to be stored in the policy container
122 public AxPolicies(final AxArtifactKey key, final Map<AxArtifactKey, AxPolicy> policyMap) {
124 Assertions.argumentNotNull(key, "key may not be null");
125 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
128 this.policyMap = new TreeMap<>();
129 this.policyMap.putAll(policyMap);
133 * When a model is unmarshalled from disk or from the database, the policy map is returned as a raw hash map. This
134 * method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so that
135 * it will work with the {@link AxConceptGetter} interface.
137 * @param unmarshaler the unmarshaler that is unmarshaling the model
138 * @param parent the parent object of this object in the unmarshaler
140 public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) {
141 // The map must be navigable to allow name and version searching, unmarshaling returns a
143 final NavigableMap<AxArtifactKey, AxPolicy> navigablePolicyMap = new TreeMap<>();
144 navigablePolicyMap.putAll(policyMap);
145 policyMap = navigablePolicyMap;
152 public AxArtifactKey getKey() {
160 public List<AxKey> getKeys() {
161 final List<AxKey> keyList = key.getKeys();
163 for (final AxPolicy policy : policyMap.values()) {
164 keyList.addAll(policy.getKeys());
171 * Sets the key of the policy container.
173 * @param key the policy container key
175 public void setKey(final AxArtifactKey key) {
176 Assertions.argumentNotNull(key, "key may not be null");
181 * Gets the policy map containing all policies in the policy container.
183 * @return the policy map with all the policies in the container
185 public Map<AxArtifactKey, AxPolicy> getPolicyMap() {
190 * Sets the policy map containing all policies in the policy container.
192 * @param policyMap the policy map with all the policies to be put in the container
194 public void setPolicyMap(final Map<AxArtifactKey, AxPolicy> policyMap) {
195 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
196 this.policyMap = new TreeMap<>();
197 this.policyMap.putAll(policyMap);
204 public AxValidationResult validate(final AxValidationResult resultIn) {
205 AxValidationResult result = resultIn;
207 if (key.equals(AxArtifactKey.getNullKey())) {
208 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "key is a null key"));
212 result = key.validate(result);
214 if (policyMap.size() == 0) {
215 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
216 "policyMap may not be empty"));
218 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
219 final AxArtifactKey entryKey = policyEntry.getKey();
220 if (entryKey.equals(AxArtifactKey.getNullKey())) {
221 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
222 "key on policy entry " + entryKey + " may not be the null key"));
223 } else if (policyEntry.getValue() == null) {
224 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
225 "value on policy entry " + entryKey + " may not be null"));
227 validate(result, policyEntry, entryKey);
228 result = policyEntry.getValue().validate(result);
236 private void validate(final AxValidationResult result, final Entry<AxArtifactKey, AxPolicy> policyEntry,
237 final AxArtifactKey entryKey) {
238 if (!entryKey.equals(policyEntry.getValue().getKey())) {
239 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
240 "key on policy entry key " + entryKey + " does not equal policy value key "
241 + policyEntry.getValue().getKey()));
249 public void clean() {
251 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
252 policyEntry.getKey().clean();
253 policyEntry.getValue().clean();
261 public String toString() {
262 final StringBuilder builder = new StringBuilder();
263 builder.append(this.getClass().getSimpleName());
264 builder.append(":(");
265 builder.append("key=");
267 builder.append(",policyMap=");
268 builder.append(policyMap);
270 return builder.toString();
277 public AxConcept copyTo(final AxConcept targetObject) {
278 Assertions.argumentNotNull(targetObject, "target may not be null");
280 final Object copyObject = targetObject;
281 Assertions.instanceOf(copyObject, AxPolicies.class);
283 final AxPolicies copy = ((AxPolicies) copyObject);
284 copy.setKey(new AxArtifactKey(key));
286 final Map<AxArtifactKey, AxPolicy> newPolicyMap = new TreeMap<>();
287 for (final Entry<AxArtifactKey, AxPolicy> policyMapEntry : policyMap.entrySet()) {
288 newPolicyMap.put(new AxArtifactKey(policyMapEntry.getKey()), new AxPolicy(policyMapEntry.getValue()));
290 copy.setPolicyMap(newPolicyMap);
299 public int hashCode() {
300 final int prime = 31;
302 result = prime * result + key.hashCode();
303 result = prime * result + policyMap.hashCode();
311 public boolean equals(final Object obj) {
319 if (getClass() != obj.getClass()) {
323 final AxPolicies other = (AxPolicies) obj;
324 if (!key.equals(other.key)) {
327 return policyMap.equals(other.policyMap);
334 public int compareTo(final AxConcept otherObj) {
335 if (otherObj == null) {
338 if (this == otherObj) {
341 if (getClass() != otherObj.getClass()) {
342 return this.hashCode() - otherObj.hashCode();
345 final AxPolicies other = (AxPolicies) otherObj;
346 if (!key.equals(other.key)) {
347 return key.compareTo(other.key);
349 if (!policyMap.equals(other.policyMap)) {
350 return (policyMap.hashCode() - other.policyMap.hashCode());
360 public AxPolicy get(final AxArtifactKey conceptKey) {
361 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKey);
368 public AxPolicy get(final String conceptKeyName) {
369 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName);
376 public AxPolicy get(final String conceptKeyName, final String conceptKeyVersion) {
377 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName,
385 public Set<AxPolicy> getAll(final String conceptKeyName) {
386 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName);
393 public Set<AxPolicy> getAll(final String conceptKeyName, final String conceptKeyVersion) {
394 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName,