2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.concepts;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.NavigableMap;
28 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;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
51 import org.onap.policy.apex.model.utilities.Assertions;
54 * This class is a policy container and holds a map of the policies for an entire Apex model. All Apex models that use
55 * policies must have an {@link AxPolicies} field. The {@link AxPolicies} class implements the helper methods of the
56 * {@link AxConceptGetter} interface to allow {@link AxPolicy} instances to be retrieved by calling methods directly on
57 * this class without referencing the contained map.
59 * <p>Validation checks that the container key is not null. An error is issued if no policies are defined in the
60 * container. Each policy entry is checked to ensure that its key and value are not null and that the key matches the
61 * key in the map value. Each policy entry is then validated individually.
64 @Table(name = "AxPolicies")
66 @XmlAccessorType(XmlAccessType.FIELD)
67 @XmlType(name = "AxPolicies", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
68 { "key", "policyMap" })
70 public class AxPolicies extends AxConcept implements AxConceptGetter<AxPolicy> {
71 private static final long serialVersionUID = 4290442590545820316L;
74 @XmlElement(name = "key", required = true)
75 private AxArtifactKey key;
78 @ManyToMany(cascade = CascadeType.ALL)
80 joinColumns = {@JoinColumn(name = "policyMapName", referencedColumnName = "name"),
81 @JoinColumn(name = "policyMapVersion", referencedColumnName = "version")},
82 inverseJoinColumns = {@JoinColumn(name = "policyName", referencedColumnName = "name"),
83 @JoinColumn(name = "policyVersion", referencedColumnName = "version")})
84 @XmlElement(required = true)
85 private Map<AxArtifactKey, AxPolicy> policyMap;
89 * The Default Constructor creates a {@link AxPolicies} object with a null artifact key and creates an empty event
93 this(new AxArtifactKey());
97 * The Key Constructor creates a {@link AxPolicies} object with the given artifact key and creates an empty event
102 public AxPolicies(final AxArtifactKey key) {
103 this(key, new TreeMap<AxArtifactKey, AxPolicy>());
109 * @param copyConcept the concept to copy from
111 public AxPolicies(final AxPolicies copyConcept) {
116 * This Constructor creates a policy container with all of its fields defined.
118 * @param key the policy container key
119 * @param policyMap the policies to be stored in the policy container
121 public AxPolicies(final AxArtifactKey key, final Map<AxArtifactKey, AxPolicy> policyMap) {
123 Assertions.argumentNotNull(key, "key may not be null");
124 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
127 this.policyMap = new TreeMap<>();
128 this.policyMap.putAll(policyMap);
132 * When a model is unmarshalled from disk or from the database, the policy map is returned as a raw hash map. This
133 * method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so that
134 * it will work with the {@link AxConceptGetter} interface.
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, AxPolicy> navigablePolicyMap = new TreeMap<>();
143 navigablePolicyMap.putAll(policyMap);
144 policyMap = navigablePolicyMap;
150 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
153 public AxArtifactKey getKey() {
160 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
163 public List<AxKey> getKeys() {
164 final List<AxKey> keyList = key.getKeys();
166 for (final AxPolicy policy : policyMap.values()) {
167 keyList.addAll(policy.getKeys());
174 * Sets the key of the policy container.
176 * @param key the policy container key
178 public void setKey(final AxArtifactKey key) {
179 Assertions.argumentNotNull(key, "key may not be null");
184 * Gets the policy map containing all policies in the policy container.
186 * @return the policy map with all the policies in the container
188 public Map<AxArtifactKey, AxPolicy> getPolicyMap() {
193 * Sets the policy map containing all policies in the policy container.
195 * @param policyMap the policy map with all the policies to be put in the container
197 public void setPolicyMap(final Map<AxArtifactKey, AxPolicy> policyMap) {
198 Assertions.argumentNotNull(policyMap, "policyMap may not be null");
199 this.policyMap = new TreeMap<>();
200 this.policyMap.putAll(policyMap);
206 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
207 * basicmodel.concepts.AxValidationResult)
210 public AxValidationResult validate(final AxValidationResult resultIn) {
211 AxValidationResult result = resultIn;
213 if (key.equals(AxArtifactKey.getNullKey())) {
214 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
215 "key is a null key"));
218 result = key.validate(result);
220 if (policyMap.size() == 0) {
221 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
222 "policyMap may not be empty"));
224 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
225 final AxArtifactKey entryKey = policyEntry.getKey();
226 if (entryKey.equals(AxArtifactKey.getNullKey())) {
227 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
228 "key on policy entry " + entryKey + " may not be the null key"));
229 } else if (policyEntry.getValue() == null) {
230 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
231 "value on policy entry " + entryKey + " may not be null"));
233 validate(result, policyEntry, entryKey);
234 result = policyEntry.getValue().validate(result);
242 private void validate(final AxValidationResult result, final Entry<AxArtifactKey, AxPolicy> policyEntry,
243 final AxArtifactKey entryKey) {
244 if (!entryKey.equals(policyEntry.getValue().getKey())) {
245 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
246 "key on policy entry key " + entryKey + " does not equal policy value key "
247 + policyEntry.getValue().getKey()));
254 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
257 public void clean() {
259 for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
260 policyEntry.getKey().clean();
261 policyEntry.getValue().clean();
268 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
271 public String toString() {
272 final StringBuilder builder = new StringBuilder();
273 builder.append(this.getClass().getSimpleName());
274 builder.append(":(");
275 builder.append("key=");
277 builder.append(",policyMap=");
278 builder.append(policyMap);
280 return builder.toString();
286 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
287 * basicmodel.concepts.AxConcept)
290 public AxConcept copyTo(final AxConcept targetObject) {
291 Assertions.argumentNotNull(targetObject, "target may not be null");
293 final Object copyObject = targetObject;
294 Assertions.instanceOf(copyObject, AxPolicies.class);
296 final AxPolicies copy = ((AxPolicies) copyObject);
297 copy.setKey(new AxArtifactKey(key));
299 final Map<AxArtifactKey, AxPolicy> newPolicyMap = new TreeMap<>();
300 for (final Entry<AxArtifactKey, AxPolicy> policyMapEntry : policyMap.entrySet()) {
301 newPolicyMap.put(new AxArtifactKey(policyMapEntry.getKey()), new AxPolicy(policyMapEntry.getValue()));
303 copy.setPolicyMap(newPolicyMap);
311 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
314 public int hashCode() {
315 final int prime = 31;
317 result = prime * result + key.hashCode();
318 result = prime * result + policyMap.hashCode();
325 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
328 public boolean equals(final Object obj) {
336 if (getClass() != obj.getClass()) {
340 final AxPolicies other = (AxPolicies) obj;
341 if (!key.equals(other.key)) {
344 return policyMap.equals(other.policyMap);
350 * @see java.lang.Comparable#compareTo(java.lang.Object)
353 public int compareTo(final AxConcept otherObj) {
354 if (otherObj == null) {
357 if (this == otherObj) {
360 if (getClass() != otherObj.getClass()) {
361 return this.hashCode() - otherObj.hashCode();
364 final AxPolicies other = (AxPolicies) otherObj;
365 if (!key.equals(other.key)) {
366 return key.compareTo(other.key);
368 if (!policyMap.equals(other.policyMap)) {
369 return (policyMap.hashCode() - other.policyMap.hashCode());
378 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.model
379 * .basicmodel.concepts.AxArtifactKey)
382 public AxPolicy get(final AxArtifactKey conceptKey) {
383 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKey);
389 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
392 public AxPolicy get(final String conceptKeyName) {
393 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName);
399 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
402 public AxPolicy get(final String conceptKeyName, final String conceptKeyVersion) {
403 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName,
410 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
413 public Set<AxPolicy> getAll(final String conceptKeyName) {
414 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName);
420 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
423 public Set<AxPolicy> getAll(final String conceptKeyName, final String conceptKeyVersion) {
424 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName,