2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.NavigableMap;
30 import java.util.TreeMap;
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 import lombok.AccessLevel;
44 import lombok.EqualsAndHashCode;
46 import lombok.ToString;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
52 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
53 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
54 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
55 import org.onap.policy.common.utils.validation.Assertions;
58 * This class is a context schema container and holds a map of the context schemas for an entire Apex model. All Apex
59 * models that use context schemas must have an {@link AxContextSchemas} field. The {@link AxContextSchemas} class
60 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextSchema} instances to
61 * be retrieved by calling methods directly on this class without referencing the contained map.
63 * <p>Validation checks that the container key is not null. An error is issued if no context schemas are defined in the
64 * container. Each context schema entry is checked to ensure that its key and value are not null and that the key
65 * matches the key in the map value. Each context schema entry is then validated individually.
68 @Table(name = "AxContextSchemas")
72 @EqualsAndHashCode(callSuper = false)
74 @XmlAccessorType(XmlAccessType.FIELD)
75 @XmlType(name = "AxContextSchemas", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
78 public class AxContextSchemas extends AxConcept implements AxConceptGetter<AxContextSchema> {
79 private static final long serialVersionUID = -3203734282886453582L;
82 @XmlElement(name = "key", required = true)
83 private AxArtifactKey key;
86 @ManyToMany(cascade = CascadeType.ALL)
88 joinColumns = {@JoinColumn(name = "contextSchemasName", referencedColumnName = "name"),
89 @JoinColumn(name = "contextSchemasVersion", referencedColumnName = "version")},
90 inverseJoinColumns = {@JoinColumn(name = "contextSchemaName", referencedColumnName = "name"),
91 @JoinColumn(name = "contextSchemaVersion", referencedColumnName = "version")})
92 @XmlElement(name = "schemas", required = true)
93 @Getter(AccessLevel.NONE)
94 private Map<AxArtifactKey, AxContextSchema> schemas;
98 * The Default Constructor creates a {@link AxContextSchemas} object with a null artifact key and creates an empty
99 * context schemas map.
101 public AxContextSchemas() {
102 this(new AxArtifactKey());
108 * @param copyConcept the concept to copy from
110 public AxContextSchemas(final AxContextSchemas copyConcept) {
115 * The Key Constructor creates a {@link AxContextSchemas} object with the given artifact key and creates an empty
116 * context schemas map.
118 * @param key the key of the context album container
120 public AxContextSchemas(final AxArtifactKey key) {
121 this(key, new TreeMap<>());
125 * This Constructor creates a {@link AxContextSchemas} object with all its fields defined.
127 * @param key the key of the context schema container
128 * @param schemas a map of the schemas to insert in the context schema container
130 public AxContextSchemas(final AxArtifactKey key, final Map<AxArtifactKey, AxContextSchema> schemas) {
132 Assertions.argumentNotNull(key, "key may not be null");
133 Assertions.argumentNotNull(schemas, "schemas may not be null");
136 this.schemas = new TreeMap<>();
137 this.schemas.putAll(schemas);
141 * When a model is unmarshalled from disk or from the database, the context schema map is returned as a raw hash
142 * map. This method is called by JAXB after unmarshaling and is used to convert the hash map to a
143 * {@link NavigableMap} so that it will work with the {@link AxConceptGetter} interface.
145 * @param unmarshaller the unmarshaler that is unmarshaling the model
146 * @param parent the parent object of this object in the unmarshaler
148 public void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
149 // The map must be navigable to allow name and version searching, unmarshaling returns a
151 final NavigableMap<AxArtifactKey, AxContextSchema> navigableContextSchemas = new TreeMap<>();
152 navigableContextSchemas.putAll(schemas);
153 schemas = navigableContextSchemas;
160 public List<AxKey> getKeys() {
161 final List<AxKey> keyList = key.getKeys();
162 keyList.addAll(schemas.keySet());
168 * Sets the key of the context schema container.
170 * @param key the key of the container
172 public void setKey(final AxArtifactKey key) {
173 Assertions.argumentNotNull(key, "key may not be null");
178 * Gets the map of context schemas in this container.
180 * @return the map of schemas
182 public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
187 * Sets the map of context schemas in this container.
189 * @param schemasMap the map of schemas
191 public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
192 Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
194 this.schemas = new TreeMap<>();
195 this.schemas.putAll(schemasMap);
202 public AxValidationResult validate(final AxValidationResult resultIn) {
203 AxValidationResult result = resultIn;
205 if (key.equals(AxArtifactKey.getNullKey())) {
206 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207 "key is a null key"));
210 result = key.validate(result);
212 if (schemas.size() == 0) {
213 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214 "contextSchemas may not be empty"));
216 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
217 if (contextSchemaEntry.getKey().equals(AxArtifactKey.getNullKey())) {
218 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219 "key on schemas entry " + contextSchemaEntry.getKey()
220 + " may not be the null key"));
221 } else if (contextSchemaEntry.getValue() == null) {
222 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
223 "value on schemas entry " + contextSchemaEntry.getKey() + " may not be null"));
225 if (!contextSchemaEntry.getKey().equals(contextSchemaEntry.getValue().getKey())) {
226 result.addValidationMessage(
227 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
228 "key on schemas entry " + contextSchemaEntry.getKey()
229 + " does not equal entry key "
230 + contextSchemaEntry.getValue().getKey()));
233 result = contextSchemaEntry.getValue().validate(result);
245 public void clean() {
247 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
248 contextSchemaEntry.getKey().clean();
249 contextSchemaEntry.getValue().clean();
257 public AxConcept copyTo(final AxConcept target) {
258 Assertions.argumentNotNull(target, "target may not be null");
260 final Object copyObject = target;
261 Assertions.instanceOf(copyObject, AxContextSchemas.class);
263 final AxContextSchemas copy = ((AxContextSchemas) copyObject);
264 copy.setKey(new AxArtifactKey(key));
266 final Map<AxArtifactKey, AxContextSchema> newcontextSchemas = new TreeMap<>();
267 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemasEntry : schemas.entrySet()) {
268 newcontextSchemas.put(new AxArtifactKey(contextSchemasEntry.getKey()),
269 new AxContextSchema(contextSchemasEntry.getValue()));
271 copy.setSchemasMap(newcontextSchemas);
280 public int compareTo(final AxConcept otherObj) {
281 if (otherObj == null) {
284 if (this == otherObj) {
287 if (getClass() != otherObj.getClass()) {
288 return this.hashCode() - otherObj.hashCode();
291 final AxContextSchemas other = (AxContextSchemas) otherObj;
292 if (!key.equals(other.key)) {
293 return key.compareTo(other.key);
295 if (!schemas.equals(other.schemas)) {
296 return (schemas.hashCode() - other.schemas.hashCode());
306 public AxContextSchema get(final AxArtifactKey conceptKey) {
307 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
314 public AxContextSchema get(final String conceptKeyName) {
315 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
322 public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
323 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
331 public Set<AxContextSchema> getAll(final String conceptKeyName) {
332 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
339 public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
340 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,