Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / concepts / AxPolicies.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.concepts;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
28 import java.util.Set;
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;
51
52 /**
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.
57  *
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.
61  */
62 @Entity
63 @Table(name = "AxPolicies")
64
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlType(name = "AxPolicies", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
67     { "key", "policyMap" })
68
69 public class AxPolicies extends AxConcept implements AxConceptGetter<AxPolicy> {
70     private static final long serialVersionUID = 4290442590545820316L;
71
72     @EmbeddedId
73     @XmlElement(name = "key", required = true)
74     private AxArtifactKey key;
75
76     // @formatter:off
77     @ManyToMany(cascade = CascadeType.ALL)
78     @JoinTable(
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;
85     // @formatter:on
86
87     /**
88      * The Default Constructor creates a {@link AxPolicies} object with a null artifact key and creates an empty event
89      * map.
90      */
91     public AxPolicies() {
92         this(new AxArtifactKey());
93     }
94
95     /**
96      * The Key Constructor creates a {@link AxPolicies} object with the given artifact key and creates an empty event
97      * map.
98      *
99      * @param key the key
100      */
101     public AxPolicies(final AxArtifactKey key) {
102         this(key, new TreeMap<AxArtifactKey, AxPolicy>());
103     }
104
105     /**
106      * Copy constructor.
107      *
108      * @param copyConcept the concept to copy from
109      */
110     public AxPolicies(final AxPolicies copyConcept) {
111         super(copyConcept);
112     }
113
114     /**
115      * This Constructor creates a policy container with all of its fields defined.
116      *
117      * @param key the policy container key
118      * @param policyMap the policies to be stored in the policy container
119      */
120     public AxPolicies(final AxArtifactKey key, final Map<AxArtifactKey, AxPolicy> policyMap) {
121         super();
122         Assertions.argumentNotNull(key, "key may not be null");
123         Assertions.argumentNotNull(policyMap, "policyMap may not be null");
124
125         this.key = key;
126         this.policyMap = new TreeMap<>();
127         this.policyMap.putAll(policyMap);
128     }
129
130     /**
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.
134      *
135      * @param unmarshaler the unmarshaler that is unmarshaling the model
136      * @param parent the parent object of this object in the unmarshaler
137      */
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
140         // hash map
141         final NavigableMap<AxArtifactKey, AxPolicy> navigablePolicyMap = new TreeMap<>();
142         navigablePolicyMap.putAll(policyMap);
143         policyMap = navigablePolicyMap;
144     }
145
146     /**
147      * {@inheritDoc}.
148      */
149     @Override
150     public AxArtifactKey getKey() {
151         return key;
152     }
153
154     /**
155      * {@inheritDoc}.
156      */
157     @Override
158     public List<AxKey> getKeys() {
159         final List<AxKey> keyList = key.getKeys();
160
161         for (final AxPolicy policy : policyMap.values()) {
162             keyList.addAll(policy.getKeys());
163         }
164
165         return keyList;
166     }
167
168     /**
169      * Sets the key of the policy container.
170      *
171      * @param key the policy container key
172      */
173     public void setKey(final AxArtifactKey key) {
174         Assertions.argumentNotNull(key, "key may not be null");
175         this.key = key;
176     }
177
178     /**
179      * Gets the policy map containing all policies in the policy container.
180      *
181      * @return the policy map with all the policies in the container
182      */
183     public Map<AxArtifactKey, AxPolicy> getPolicyMap() {
184         return policyMap;
185     }
186
187     /**
188      * Sets the policy map containing all policies in the policy container.
189      *
190      * @param policyMap the policy map with all the policies to be put in the container
191      */
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);
196     }
197
198     /**
199      * {@inheritDoc}.
200      */
201     @Override
202     public AxValidationResult validate(final AxValidationResult resultIn) {
203         AxValidationResult result = resultIn;
204
205         if (key.equals(AxArtifactKey.getNullKey())) {
206             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207                             "key is a null key"));
208         }
209
210         result = key.validate(result);
211
212         if (policyMap.size() == 0) {
213             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214                             "policyMap may not be empty"));
215         } else {
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"));
224                 } else {
225                     validate(result, policyEntry, entryKey);
226                     result = policyEntry.getValue().validate(result);
227                 }
228             }
229         }
230
231         return result;
232     }
233
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()));
240         }
241     }
242
243     /**
244      * {@inheritDoc}.
245      */
246     @Override
247     public void clean() {
248         key.clean();
249         for (final Entry<AxArtifactKey, AxPolicy> policyEntry : policyMap.entrySet()) {
250             policyEntry.getKey().clean();
251             policyEntry.getValue().clean();
252         }
253     }
254
255     /**
256      * {@inheritDoc}.
257      */
258     @Override
259     public String toString() {
260         final StringBuilder builder = new StringBuilder();
261         builder.append(this.getClass().getSimpleName());
262         builder.append(":(");
263         builder.append("key=");
264         builder.append(key);
265         builder.append(",policyMap=");
266         builder.append(policyMap);
267         builder.append(")");
268         return builder.toString();
269     }
270
271     /**
272      * {@inheritDoc}.
273      */
274     @Override
275     public AxConcept copyTo(final AxConcept targetObject) {
276         Assertions.argumentNotNull(targetObject, "target may not be null");
277
278         final Object copyObject = targetObject;
279         Assertions.instanceOf(copyObject, AxPolicies.class);
280
281         final AxPolicies copy = ((AxPolicies) copyObject);
282         copy.setKey(new AxArtifactKey(key));
283
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()));
287         }
288         copy.setPolicyMap(newPolicyMap);
289
290         return copy;
291     }
292
293     /**
294      * {@inheritDoc}.
295      */
296     @Override
297     public int hashCode() {
298         final int prime = 31;
299         int result = 1;
300         result = prime * result + key.hashCode();
301         result = prime * result + policyMap.hashCode();
302         return result;
303     }
304
305     /**
306      * {@inheritDoc}.
307      */
308     @Override
309     public boolean equals(final Object obj) {
310         if (obj == null) {
311             return false;
312         }
313         if (this == obj) {
314             return true;
315         }
316
317         if (getClass() != obj.getClass()) {
318             return false;
319         }
320
321         final AxPolicies other = (AxPolicies) obj;
322         if (!key.equals(other.key)) {
323             return false;
324         }
325         return policyMap.equals(other.policyMap);
326     }
327
328     /**
329      * {@inheritDoc}.
330      */
331     @Override
332     public int compareTo(final AxConcept otherObj) {
333         if (otherObj == null) {
334             return -1;
335         }
336         if (this == otherObj) {
337             return 0;
338         }
339         if (getClass() != otherObj.getClass()) {
340             return this.hashCode() - otherObj.hashCode();
341         }
342
343         final AxPolicies other = (AxPolicies) otherObj;
344         if (!key.equals(other.key)) {
345             return key.compareTo(other.key);
346         }
347         if (!policyMap.equals(other.policyMap)) {
348             return (policyMap.hashCode() - other.policyMap.hashCode());
349         }
350
351         return 0;
352     }
353
354     /**
355      * {@inheritDoc}.
356      */
357     @Override
358     public AxPolicy get(final AxArtifactKey conceptKey) {
359         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKey);
360     }
361
362     /**
363      * {@inheritDoc}.
364      */
365     @Override
366     public AxPolicy get(final String conceptKeyName) {
367         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName);
368     }
369
370     /**
371      * {@inheritDoc}.
372      */
373     @Override
374     public AxPolicy get(final String conceptKeyName, final String conceptKeyVersion) {
375         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).get(conceptKeyName,
376                         conceptKeyVersion);
377     }
378
379     /**
380      * {@inheritDoc}.
381      */
382     @Override
383     public Set<AxPolicy> getAll(final String conceptKeyName) {
384         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName);
385     }
386
387     /**
388      * {@inheritDoc}.
389      */
390     @Override
391     public Set<AxPolicy> getAll(final String conceptKeyName, final String conceptKeyVersion) {
392         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxPolicy>) policyMap).getAll(conceptKeyName,
393                         conceptKeyVersion);
394     }
395 }