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;
30 import javax.persistence.CascadeType;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinTable;
35 import javax.persistence.ManyToMany;
36 import javax.persistence.Table;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.bind.annotation.XmlAccessType;
39 import javax.xml.bind.annotation.XmlAccessorType;
40 import javax.xml.bind.annotation.XmlElement;
41 import javax.xml.bind.annotation.XmlType;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
50 import org.onap.policy.common.utils.validation.Assertions;
53 * This class is a policy container and holds a map of the policies for an entire Apex model. All Apex models that use
54 * policies must have an {@link AxPolicies} field. The {@link AxPolicies} class implements the helper methods of the
55 * {@link AxConceptGetter} interface to allow {@link AxPolicy} instances to be retrieved by calling methods directly on
56 * this class without referencing the contained map.
58 * <p>Validation checks that the container key is not null. An error is issued if no policies are defined in the
59 * container. Each policy entry is checked to ensure that its key and value are not null and that the key matches the
60 * key in the map value. Each policy entry is then validated individually.
63 @Table(name = "AxPolicies")
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlType(name = "AxPolicies", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
67 { "key", "policyMap" })
69 public class AxPolicies extends AxConcept implements AxConceptGetter<AxPolicy> {
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 = "policyMapName", referencedColumnName = "name"),
80 @JoinColumn(name = "policyMapVersion", referencedColumnName = "version")},
81 inverseJoinColumns = {@JoinColumn(name = "policyName", referencedColumnName = "name"),
82 @JoinColumn(name = "policyVersion", referencedColumnName = "version")})
83 @XmlElement(required = true)
84 private Map<AxArtifactKey, AxPolicy> policyMap;
88 * The Default Constructor creates a {@link AxPolicies} object with a null artifact key and creates an empty event
92 this(new AxArtifactKey());
96 * The Key Constructor creates a {@link AxPolicies} object with the given artifact key and creates an empty event
101 public AxPolicies(final AxArtifactKey key) {
102 this(key, new TreeMap<AxArtifactKey, AxPolicy>());
108 * @param copyConcept the concept to copy from
110 public AxPolicies(final AxPolicies copyConcept) {
115 * This Constructor creates a policy container with all of its fields defined.
117 * @param key the policy container key
118 * @param policyMap the policies to be stored in the policy container
120 public AxPolicies(final AxArtifactKey key, final Map<AxArtifactKey, AxPolicy> policyMap) {
122 Assertions.argumentNotNull(key, "key may not be null");
123 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
126 this.policyMap = new TreeMap<>();
127 this.policyMap.putAll(policyMap);
131 * When a model is unmarshalled from disk or from the database, the policy map is returned as a raw hash map. This
132 * method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so that
133 * it will work with the {@link AxConceptGetter} interface.
135 * @param unmarshaler the unmarshaler that is unmarshaling the model
136 * @param parent the parent object of this object in the unmarshaler
138 public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) {
139 // The map must be navigable to allow name and version searching, unmarshaling returns a
141 final NavigableMap<AxArtifactKey, AxPolicy> navigablePolicyMap = new TreeMap<>();
142 navigablePolicyMap.putAll(policyMap);
143 policyMap = navigablePolicyMap;
150 public AxArtifactKey getKey() {
158 public List<AxKey> getKeys() {
159 final List<AxKey> keyList = key.getKeys();
161 for (final AxPolicy policy : policyMap.values()) {
162 keyList.addAll(policy.getKeys());
169 * Sets the key of the policy container.
171 * @param key the policy container key
173 public void setKey(final AxArtifactKey key) {
174 Assertions.argumentNotNull(key, "key may not be null");
179 * Gets the policy map containing all policies in the policy container.
181 * @return the policy map with all the policies in the container
183 public Map<AxArtifactKey, AxPolicy> getPolicyMap() {
188 * Sets the policy map containing all policies in the policy container.
190 * @param policyMap the policy map with all the policies to be put in the container
192 public void setPolicyMap(final Map<AxArtifactKey, AxPolicy> policyMap) {
193 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
194 this.policyMap = new TreeMap<>();
195 this.policyMap.putAll(policyMap);
202 public AxValidationResult validate(final AxValidationResult resultIn) {
203 AxValidationResult result = resultIn;
205 if (key.equals(AxArtifactKey.getNullKey())) {
206 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207 "key is a null key"));
210 result = key.validate(result);
212 if (policyMap.size() == 0) {
213 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214 "policyMap may not be empty"));
216 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
217 final AxArtifactKey entryKey = policyEntry.getKey();
218 if (entryKey.equals(AxArtifactKey.getNullKey())) {
219 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "key on policy entry " + entryKey + " may not be the null key"));
221 } else if (policyEntry.getValue() == null) {
222 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
223 "value on policy entry " + entryKey + " may not be null"));
225 validate(result, policyEntry, entryKey);
226 result = policyEntry.getValue().validate(result);
234 private void validate(final AxValidationResult result, final Entry<AxArtifactKey, AxPolicy> policyEntry,
235 final AxArtifactKey entryKey) {
236 if (!entryKey.equals(policyEntry.getValue().getKey())) {
237 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238 "key on policy entry key " + entryKey + " does not equal policy value key "
239 + policyEntry.getValue().getKey()));
247 public void clean() {
249 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
250 policyEntry.getKey().clean();
251 policyEntry.getValue().clean();
259 public String toString() {
260 final StringBuilder builder = new StringBuilder();
261 builder.append(this.getClass().getSimpleName());
262 builder.append(":(");
263 builder.append("key=");
265 builder.append(",policyMap=");
266 builder.append(policyMap);
268 return builder.toString();
275 public AxConcept copyTo(final AxConcept targetObject) {
276 Assertions.argumentNotNull(targetObject, "target may not be null");
278 final Object copyObject = targetObject;
279 Assertions.instanceOf(copyObject, AxPolicies.class);
281 final AxPolicies copy = ((AxPolicies) copyObject);
282 copy.setKey(new AxArtifactKey(key));
284 final Map<AxArtifactKey, AxPolicy> newPolicyMap = new TreeMap<>();
285 for (final Entry<AxArtifactKey, AxPolicy> policyMapEntry : policyMap.entrySet()) {
286 newPolicyMap.put(new AxArtifactKey(policyMapEntry.getKey()), new AxPolicy(policyMapEntry.getValue()));
288 copy.setPolicyMap(newPolicyMap);
297 public int hashCode() {
298 final int prime = 31;
300 result = prime * result + key.hashCode();
301 result = prime * result + policyMap.hashCode();
309 public boolean equals(final Object obj) {
317 if (getClass() != obj.getClass()) {
321 final AxPolicies other = (AxPolicies) obj;
322 if (!key.equals(other.key)) {
325 return policyMap.equals(other.policyMap);
332 public int compareTo(final AxConcept otherObj) {
333 if (otherObj == null) {
336 if (this == otherObj) {
339 if (getClass() != otherObj.getClass()) {
340 return this.hashCode() - otherObj.hashCode();
343 final AxPolicies other = (AxPolicies) otherObj;
344 if (!key.equals(other.key)) {
345 return key.compareTo(other.key);
347 if (!policyMap.equals(other.policyMap)) {
348 return (policyMap.hashCode() - other.policyMap.hashCode());
358 public AxPolicy get(final AxArtifactKey conceptKey) {
359 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKey);
366 public AxPolicy get(final String conceptKeyName) {
367 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName);
374 public AxPolicy get(final String conceptKeyName, final String conceptKeyVersion) {
375 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName,
383 public Set<AxPolicy> getAll(final String conceptKeyName) {
384 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName);
391 public Set<AxPolicy> getAll(final String conceptKeyName, final String conceptKeyVersion) {
392 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName,