df94a87a9bf29f79765576c7454ffe4b4c0ccd8d
[policy/apex-pdp.git] /
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.contextmodel.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 context schema container and holds a map of the context schemas for an entire Apex model. All Apex
55  * models that use context schemas must have an {@link AxContextSchemas} field. The {@link AxContextSchemas} class
56  * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextSchema} instances to
57  * be retrieved by calling methods directly on this class without referencing the contained map.
58  * <p>
59  * Validation checks that the container key is not null. An error is issued if no context schemas are defined in the
60  * container. Each context schema entry is checked to ensure that its key and value are not null and that the key
61  * matches the key in the map value. Each context schema entry is then validated individually.
62  */
63 @Entity
64 @Table(name = "AxContextSchemas")
65
66 @XmlAccessorType(XmlAccessType.FIELD)
67 @XmlType(name = "AxContextSchemas", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "key", "schemas" })
68
69 public class AxContextSchemas extends AxConcept implements AxConceptGetter<AxContextSchema> {
70     private static final long serialVersionUID = -3203734282886453582L;
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 = "contextSchemasName", referencedColumnName = "name"),
80                     @JoinColumn(name = "contextSchemasVersion", referencedColumnName = "version")},
81             inverseJoinColumns = {@JoinColumn(name = "contextSchemaName", referencedColumnName = "name"),
82                     @JoinColumn(name = "contextSchemaVersion", referencedColumnName = "version")})
83     @XmlElement(name = "schemas", required = true)
84     private Map<AxArtifactKey, AxContextSchema> schemas;
85     // @formatter:on
86
87     /**
88      * The Default Constructor creates a {@link AxContextSchemas} object with a null artifact key and creates an empty
89      * context schemas map.
90      */
91     public AxContextSchemas() {
92         this(new AxArtifactKey());
93     }
94
95     /**
96      * Copy constructor
97      *
98      * @param copyConcept the concept to copy from
99      */
100     public AxContextSchemas(final AxContextSchemas copyConcept) {
101         super(copyConcept);
102     }
103
104     /**
105      * The Key Constructor creates a {@link AxContextSchemas} object with the given artifact key and creates an empty
106      * context schemas map.
107      *
108      * @param key the key of the context album container
109      */
110     public AxContextSchemas(final AxArtifactKey key) {
111         this(key, new TreeMap<AxArtifactKey, AxContextSchema>());
112     }
113
114     /**
115      * This Constructor creates a {@link AxContextSchemas} object with all its fields defined.
116      *
117      * @param key the key of the context schema container
118      * @param schemas a map of the schemas to insert in the context schema container
119      */
120     public AxContextSchemas(final AxArtifactKey key, final Map<AxArtifactKey, AxContextSchema> schemas) {
121         super();
122         Assertions.argumentNotNull(key, "key may not be null");
123         Assertions.argumentNotNull(schemas, "schemas may not be null");
124
125         this.key = key;
126         this.schemas = new TreeMap<>();
127         this.schemas.putAll(schemas);
128     }
129
130     /**
131      * When a model is unmarshalled from disk or from the database, the context schema map is returned as a raw hash
132      * map. This method is called by JAXB after unmarshaling and is used to convert the hash map to a
133      * {@link NavigableMap} so that it will work with the {@link AxConceptGetter} interface.
134      *
135      * @param u 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 u, 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, AxContextSchema> navigableContextSchemas = new TreeMap<>();
142         navigableContextSchemas.putAll(schemas);
143         schemas = navigableContextSchemas;
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
150      */
151     @Override
152     public AxArtifactKey getKey() {
153         return key;
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
160      */
161     @Override
162     public List<AxKey> getKeys() {
163         final List<AxKey> keyList = key.getKeys();
164         keyList.addAll(schemas.keySet());
165
166         return keyList;
167     }
168
169     /**
170      * Sets the key of the context schema container.
171      *
172      * @param key the key of the container
173      */
174     public void setKey(final AxArtifactKey key) {
175         Assertions.argumentNotNull(key, "key may not be null");
176         this.key = key;
177     }
178
179     /**
180      * Gets the map of context schemas in this container.
181      *
182      * @return the map of schemas
183      */
184     public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
185         return schemas;
186     }
187
188     /**
189      * Sets the map of context schemas in this container.
190      *
191      * @param schemasMap the map of schemas
192      */
193     public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
194         Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
195
196         this.schemas = new TreeMap<>();
197         this.schemas.putAll(schemasMap);
198     }
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
204      * basicmodel.concepts.AxValidationResult)
205      */
206     @Override
207     public AxValidationResult validate(final AxValidationResult resultIn) {
208         AxValidationResult result = resultIn;
209
210         if (key.equals(AxArtifactKey.getNullKey())) {
211             result.addValidationMessage(
212                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
213         }
214
215         result = key.validate(result);
216
217         if (schemas.size() == 0) {
218             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219                     "contextSchemas may not be empty"));
220         } else {
221             for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
222                 if (contextSchemaEntry.getKey().equals(AxArtifactKey.getNullKey())) {
223                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
224                             "key on schemas entry " + contextSchemaEntry.getKey() + " may not be the null key"));
225                 } else if (contextSchemaEntry.getValue() == null) {
226                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
227                             "value on schemas entry " + contextSchemaEntry.getKey() + " may not be null"));
228                 } else {
229                     if (!contextSchemaEntry.getKey().equals(contextSchemaEntry.getValue().getKey())) {
230                         result.addValidationMessage(new AxValidationMessage(key, this.getClass(),
231                                 ValidationResult.INVALID, "key on schemas entry " + contextSchemaEntry.getKey()
232                                         + " does not equal entry key " + contextSchemaEntry.getValue().getKey()));
233                     }
234
235                     result = contextSchemaEntry.getValue().validate(result);
236                 }
237             }
238         }
239
240         return result;
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
247      */
248     @Override
249     public void clean() {
250         key.clean();
251         for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
252             contextSchemaEntry.getKey().clean();
253             contextSchemaEntry.getValue().clean();
254         }
255     }
256
257     /*
258      * (non-Javadoc)
259      *
260      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
261      */
262     @Override
263     public String toString() {
264         final StringBuilder builder = new StringBuilder();
265         builder.append(this.getClass().getSimpleName());
266         builder.append(":(");
267         builder.append("key=");
268         builder.append(key);
269         builder.append(",schemas=");
270         builder.append(schemas);
271         builder.append(")");
272         return builder.toString();
273     }
274
275     /*
276      * (non-Javadoc)
277      *
278      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
279      * basicmodel.concepts.AxConcept)
280      */
281     @Override
282     public AxConcept copyTo(final AxConcept target) {
283         Assertions.argumentNotNull(target, "target may not be null");
284
285         final Object copyObject = target;
286         Assertions.instanceOf(copyObject, AxContextSchemas.class);
287
288         final AxContextSchemas copy = ((AxContextSchemas) copyObject);
289         copy.setKey(new AxArtifactKey(key));
290
291         final Map<AxArtifactKey, AxContextSchema> newcontextSchemas = new TreeMap<>();
292         for (final Entry<AxArtifactKey, AxContextSchema> contextSchemasEntry : schemas.entrySet()) {
293             newcontextSchemas.put(new AxArtifactKey(contextSchemasEntry.getKey()),
294                     new AxContextSchema(contextSchemasEntry.getValue()));
295         }
296         copy.setSchemasMap(newcontextSchemas);
297
298         return copy;
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
305      */
306     @Override
307     public int hashCode() {
308         final int prime = 31;
309         int result = 1;
310         result = prime * result + key.hashCode();
311         result = prime * result + schemas.hashCode();
312         return result;
313     }
314
315     /*
316      * (non-Javadoc)
317      *
318      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
319      */
320     @Override
321     public boolean equals(final Object obj) {
322         if (obj == null) {
323             return false;
324         }
325         if (this == obj) {
326             return true;
327         }
328         if (getClass() != obj.getClass()) {
329             return false;
330         }
331
332         final AxContextSchemas other = (AxContextSchemas) obj;
333         if (!key.equals(other.key)) {
334             return false;
335         }
336         return schemas.equals(other.schemas);
337     }
338
339     /*
340      * (non-Javadoc)
341      *
342      * @see java.lang.Comparable#compareTo(java.lang.Object)
343      */
344     @Override
345     public int compareTo(final AxConcept otherObj) {
346         if (otherObj == null) {
347             return -1;
348         }
349         if (this == otherObj) {
350             return 0;
351         }
352         if (getClass() != otherObj.getClass()) {
353             return this.hashCode() - otherObj.hashCode();
354         }
355
356         final AxContextSchemas other = (AxContextSchemas) otherObj;
357         if (!key.equals(other.key)) {
358             return key.compareTo(other.key);
359         }
360         if (!schemas.equals(other.schemas)) {
361             return (schemas.hashCode() - other.schemas.hashCode());
362         }
363
364         return 0;
365     }
366
367     /*
368      * (non-Javadoc)
369      *
370      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.core.
371      * basicmodel.concepts.AxArtifactKey)
372      */
373     @Override
374     public AxContextSchema get(final AxArtifactKey conceptKey) {
375         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
376     }
377
378     /*
379      * (non-Javadoc)
380      *
381      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
382      */
383     @Override
384     public AxContextSchema get(final String conceptKeyName) {
385         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
392      */
393     @Override
394     public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
395         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
396                 conceptKeyVersion);
397     }
398
399     /*
400      * (non-Javadoc)
401      *
402      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
403      */
404     @Override
405     public Set<AxContextSchema> getAll(final String conceptKeyName) {
406         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
407     }
408
409     /*
410      * (non-Javadoc)
411      *
412      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
413      */
414     @Override
415     public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
416         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,
417                 conceptKeyVersion);
418     }
419 }