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.contextmodel.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 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.
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.
64 @Table(name = "AxContextSchemas")
66 @XmlAccessorType(XmlAccessType.FIELD)
67 @XmlType(name = "AxContextSchemas", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "key", "schemas" })
69 public class AxContextSchemas extends AxConcept implements AxConceptGetter<AxContextSchema> {
70 private static final long serialVersionUID = -3203734282886453582L;
73 @XmlElement(name = "key", required = true)
74 private AxArtifactKey key;
77 @ManyToMany(cascade = CascadeType.ALL)
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;
88 * The Default Constructor creates a {@link AxContextSchemas} object with a null artifact key and creates an empty
89 * context schemas map.
91 public AxContextSchemas() {
92 this(new AxArtifactKey());
98 * @param copyConcept the concept to copy from
100 public AxContextSchemas(final AxContextSchemas copyConcept) {
105 * The Key Constructor creates a {@link AxContextSchemas} object with the given artifact key and creates an empty
106 * context schemas map.
108 * @param key the key of the context album container
110 public AxContextSchemas(final AxArtifactKey key) {
111 this(key, new TreeMap<AxArtifactKey, AxContextSchema>());
115 * This Constructor creates a {@link AxContextSchemas} object with all its fields defined.
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
120 public AxContextSchemas(final AxArtifactKey key, final Map<AxArtifactKey, AxContextSchema> schemas) {
122 Assertions.argumentNotNull(key, "key may not be null");
123 Assertions.argumentNotNull(schemas, "schemas may not be null");
126 this.schemas = new TreeMap<>();
127 this.schemas.putAll(schemas);
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.
135 * @param u the unmarshaler that is unmarshaling the model
136 * @param parent the parent object of this object in the unmarshaler
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
141 final NavigableMap<AxArtifactKey, AxContextSchema> navigableContextSchemas = new TreeMap<>();
142 navigableContextSchemas.putAll(schemas);
143 schemas = navigableContextSchemas;
149 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
152 public AxArtifactKey getKey() {
159 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
162 public List<AxKey> getKeys() {
163 final List<AxKey> keyList = key.getKeys();
164 keyList.addAll(schemas.keySet());
170 * Sets the key of the context schema container.
172 * @param key the key of the container
174 public void setKey(final AxArtifactKey key) {
175 Assertions.argumentNotNull(key, "key may not be null");
180 * Gets the map of context schemas in this container.
182 * @return the map of schemas
184 public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
189 * Sets the map of context schemas in this container.
191 * @param schemasMap the map of schemas
193 public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
194 Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
196 this.schemas = new TreeMap<>();
197 this.schemas.putAll(schemasMap);
203 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
204 * basicmodel.concepts.AxValidationResult)
207 public AxValidationResult validate(final AxValidationResult resultIn) {
208 AxValidationResult result = resultIn;
210 if (key.equals(AxArtifactKey.getNullKey())) {
211 result.addValidationMessage(
212 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
215 result = key.validate(result);
217 if (schemas.size() == 0) {
218 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219 "contextSchemas may not be empty"));
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"));
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()));
235 result = contextSchemaEntry.getValue().validate(result);
246 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
249 public void clean() {
251 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
252 contextSchemaEntry.getKey().clean();
253 contextSchemaEntry.getValue().clean();
260 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
263 public String toString() {
264 final StringBuilder builder = new StringBuilder();
265 builder.append(this.getClass().getSimpleName());
266 builder.append(":(");
267 builder.append("key=");
269 builder.append(",schemas=");
270 builder.append(schemas);
272 return builder.toString();
278 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
279 * basicmodel.concepts.AxConcept)
282 public AxConcept copyTo(final AxConcept target) {
283 Assertions.argumentNotNull(target, "target may not be null");
285 final Object copyObject = target;
286 Assertions.instanceOf(copyObject, AxContextSchemas.class);
288 final AxContextSchemas copy = ((AxContextSchemas) copyObject);
289 copy.setKey(new AxArtifactKey(key));
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()));
296 copy.setSchemasMap(newcontextSchemas);
304 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
307 public int hashCode() {
308 final int prime = 31;
310 result = prime * result + key.hashCode();
311 result = prime * result + schemas.hashCode();
318 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
321 public boolean equals(final Object obj) {
328 if (getClass() != obj.getClass()) {
332 final AxContextSchemas other = (AxContextSchemas) obj;
333 if (!key.equals(other.key)) {
336 return schemas.equals(other.schemas);
342 * @see java.lang.Comparable#compareTo(java.lang.Object)
345 public int compareTo(final AxConcept otherObj) {
346 if (otherObj == null) {
349 if (this == otherObj) {
352 if (getClass() != otherObj.getClass()) {
353 return this.hashCode() - otherObj.hashCode();
356 final AxContextSchemas other = (AxContextSchemas) otherObj;
357 if (!key.equals(other.key)) {
358 return key.compareTo(other.key);
360 if (!schemas.equals(other.schemas)) {
361 return (schemas.hashCode() - other.schemas.hashCode());
370 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.core.
371 * basicmodel.concepts.AxArtifactKey)
374 public AxContextSchema get(final AxArtifactKey conceptKey) {
375 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
381 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
384 public AxContextSchema get(final String conceptKeyName) {
385 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
391 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
394 public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
395 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
402 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
405 public Set<AxContextSchema> getAll(final String conceptKeyName) {
406 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
412 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
415 public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
416 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,