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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.contextmodel.concepts;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
29 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;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
50 import org.onap.policy.common.utils.validation.Assertions;
53 * This class is a context schema container and holds a map of the context schemas for an entire Apex model. All Apex
54 * models that use context schemas must have an {@link AxContextSchemas} field. The {@link AxContextSchemas} class
55 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextSchema} instances to
56 * be retrieved by calling methods directly on this class without referencing the contained map.
58 * <p>Validation checks that the container key is not null. An error is issued if no context schemas are defined in the
59 * container. Each context schema entry is checked to ensure that its key and value are not null and that the key
60 * matches the key in the map value. Each context schema entry is then validated individually.
63 @Table(name = "AxContextSchemas")
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlType(name = "AxContextSchemas", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
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 unmarshaller 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 unmarshaller, 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;
150 public AxArtifactKey getKey() {
158 public List<AxKey> getKeys() {
159 final List<AxKey> keyList = key.getKeys();
160 keyList.addAll(schemas.keySet());
166 * Sets the key of the context schema container.
168 * @param key the key of the container
170 public void setKey(final AxArtifactKey key) {
171 Assertions.argumentNotNull(key, "key may not be null");
176 * Gets the map of context schemas in this container.
178 * @return the map of schemas
180 public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
185 * Sets the map of context schemas in this container.
187 * @param schemasMap the map of schemas
189 public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
190 Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
192 this.schemas = new TreeMap<>();
193 this.schemas.putAll(schemasMap);
200 public AxValidationResult validate(final AxValidationResult resultIn) {
201 AxValidationResult result = resultIn;
203 if (key.equals(AxArtifactKey.getNullKey())) {
204 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
205 "key is a null key"));
208 result = key.validate(result);
210 if (schemas.size() == 0) {
211 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
212 "contextSchemas may not be empty"));
214 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
215 if (contextSchemaEntry.getKey().equals(AxArtifactKey.getNullKey())) {
216 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
217 "key on schemas entry " + contextSchemaEntry.getKey()
218 + " may not be the null key"));
219 } else if (contextSchemaEntry.getValue() == null) {
220 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
221 "value on schemas entry " + contextSchemaEntry.getKey() + " may not be null"));
223 if (!contextSchemaEntry.getKey().equals(contextSchemaEntry.getValue().getKey())) {
224 result.addValidationMessage(
225 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
226 "key on schemas entry " + contextSchemaEntry.getKey()
227 + " does not equal entry key "
228 + contextSchemaEntry.getValue().getKey()));
231 result = contextSchemaEntry.getValue().validate(result);
243 public void clean() {
245 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
246 contextSchemaEntry.getKey().clean();
247 contextSchemaEntry.getValue().clean();
255 public String toString() {
256 final StringBuilder builder = new StringBuilder();
257 builder.append(this.getClass().getSimpleName());
258 builder.append(":(");
259 builder.append("key=");
261 builder.append(",schemas=");
262 builder.append(schemas);
264 return builder.toString();
271 public AxConcept copyTo(final AxConcept target) {
272 Assertions.argumentNotNull(target, "target may not be null");
274 final Object copyObject = target;
275 Assertions.instanceOf(copyObject, AxContextSchemas.class);
277 final AxContextSchemas copy = ((AxContextSchemas) copyObject);
278 copy.setKey(new AxArtifactKey(key));
280 final Map<AxArtifactKey, AxContextSchema> newcontextSchemas = new TreeMap<>();
281 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemasEntry : schemas.entrySet()) {
282 newcontextSchemas.put(new AxArtifactKey(contextSchemasEntry.getKey()),
283 new AxContextSchema(contextSchemasEntry.getValue()));
285 copy.setSchemasMap(newcontextSchemas);
294 public int hashCode() {
295 final int prime = 31;
297 result = prime * result + key.hashCode();
298 result = prime * result + schemas.hashCode();
306 public boolean equals(final Object obj) {
313 if (getClass() != obj.getClass()) {
317 final AxContextSchemas other = (AxContextSchemas) obj;
318 if (!key.equals(other.key)) {
321 return schemas.equals(other.schemas);
328 public int compareTo(final AxConcept otherObj) {
329 if (otherObj == null) {
332 if (this == otherObj) {
335 if (getClass() != otherObj.getClass()) {
336 return this.hashCode() - otherObj.hashCode();
339 final AxContextSchemas other = (AxContextSchemas) otherObj;
340 if (!key.equals(other.key)) {
341 return key.compareTo(other.key);
343 if (!schemas.equals(other.schemas)) {
344 return (schemas.hashCode() - other.schemas.hashCode());
354 public AxContextSchema get(final AxArtifactKey conceptKey) {
355 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
362 public AxContextSchema get(final String conceptKeyName) {
363 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
370 public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
371 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
379 public Set<AxContextSchema> getAll(final String conceptKeyName) {
380 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
387 public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
388 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,