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