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