2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2022 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 lombok.AccessLevel;
32 import lombok.EqualsAndHashCode;
34 import lombok.ToString;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
43 import org.onap.policy.common.utils.validation.Assertions;
46 * This class is a context schema container and holds a map of the context schemas for an entire Apex model. All Apex
47 * models that use context schemas must have an {@link AxContextSchemas} field. The {@link AxContextSchemas} class
48 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextSchema} instances to
49 * be retrieved by calling methods directly on this class without referencing the contained map.
51 * <p>Validation checks that the container key is not null. An error is issued if no context schemas are defined in the
52 * container. Each context schema entry is checked to ensure that its key and value are not null and that the key
53 * matches the key in the map value. Each context schema entry is then validated individually.
57 @EqualsAndHashCode(callSuper = false)
58 public class AxContextSchemas extends AxConcept implements AxConceptGetter<AxContextSchema> {
59 private static final long serialVersionUID = -3203734282886453582L;
61 private AxArtifactKey key;
63 @Getter(AccessLevel.NONE)
64 private Map<AxArtifactKey, AxContextSchema> schemas;
67 * The Default Constructor creates a {@link AxContextSchemas} object with a null artifact key and creates an empty
68 * context schemas map.
70 public AxContextSchemas() {
71 this(new AxArtifactKey());
77 * @param copyConcept the concept to copy from
79 public AxContextSchemas(final AxContextSchemas copyConcept) {
84 * The Key Constructor creates a {@link AxContextSchemas} object with the given artifact key and creates an empty
85 * context schemas map.
87 * @param key the key of the context album container
89 public AxContextSchemas(final AxArtifactKey key) {
90 this(key, new TreeMap<>());
94 * This Constructor creates a {@link AxContextSchemas} object with all its fields defined.
96 * @param key the key of the context schema container
97 * @param schemas a map of the schemas to insert in the context schema container
99 public AxContextSchemas(final AxArtifactKey key, final Map<AxArtifactKey, AxContextSchema> schemas) {
101 Assertions.argumentNotNull(key, "key may not be null");
102 Assertions.argumentNotNull(schemas, "schemas may not be null");
105 this.schemas = new TreeMap<>();
106 this.schemas.putAll(schemas);
113 public List<AxKey> getKeys() {
114 final List<AxKey> keyList = key.getKeys();
115 keyList.addAll(schemas.keySet());
124 public void buildReferences() {
125 schemas.values().stream().forEach(schema -> schema.buildReferences());
129 * Sets the key of the context schema container.
131 * @param key the key of the container
133 public void setKey(final AxArtifactKey key) {
134 Assertions.argumentNotNull(key, "key may not be null");
139 * Gets the map of context schemas in this container.
141 * @return the map of schemas
143 public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
148 * Sets the map of context schemas in this container.
150 * @param schemasMap the map of schemas
152 public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
153 Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
155 this.schemas = new TreeMap<>();
156 this.schemas.putAll(schemasMap);
163 public AxValidationResult validate(final AxValidationResult resultIn) {
164 AxValidationResult result = resultIn;
166 if (key.equals(AxArtifactKey.getNullKey())) {
167 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
168 "key is a null key"));
171 result = key.validate(result);
173 if (schemas.size() == 0) {
174 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
175 "contextSchemas may not be empty"));
177 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
178 if (contextSchemaEntry.getKey().equals(AxArtifactKey.getNullKey())) {
179 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
180 "key on schemas entry " + contextSchemaEntry.getKey()
181 + " may not be the null key"));
182 } else if (contextSchemaEntry.getValue() == null) {
183 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
184 "value on schemas entry " + contextSchemaEntry.getKey() + " may not be null"));
186 if (!contextSchemaEntry.getKey().equals(contextSchemaEntry.getValue().getKey())) {
187 result.addValidationMessage(
188 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
189 "key on schemas entry " + contextSchemaEntry.getKey()
190 + " does not equal entry key "
191 + contextSchemaEntry.getValue().getKey()));
194 result = contextSchemaEntry.getValue().validate(result);
206 public void clean() {
208 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
209 contextSchemaEntry.getKey().clean();
210 contextSchemaEntry.getValue().clean();
218 public AxConcept copyTo(final AxConcept target) {
219 Assertions.argumentNotNull(target, "target may not be null");
221 final Object copyObject = target;
222 Assertions.instanceOf(copyObject, AxContextSchemas.class);
224 final AxContextSchemas copy = ((AxContextSchemas) copyObject);
225 copy.setKey(new AxArtifactKey(key));
227 final Map<AxArtifactKey, AxContextSchema> newcontextSchemas = new TreeMap<>();
228 for (final Entry<AxArtifactKey, AxContextSchema> contextSchemasEntry : schemas.entrySet()) {
229 newcontextSchemas.put(new AxArtifactKey(contextSchemasEntry.getKey()),
230 new AxContextSchema(contextSchemasEntry.getValue()));
232 copy.setSchemasMap(newcontextSchemas);
241 public int compareTo(final AxConcept otherObj) {
242 if (otherObj == null) {
245 if (this == otherObj) {
248 if (getClass() != otherObj.getClass()) {
249 return this.hashCode() - otherObj.hashCode();
252 final AxContextSchemas other = (AxContextSchemas) otherObj;
253 if (!key.equals(other.key)) {
254 return key.compareTo(other.key);
256 if (!schemas.equals(other.schemas)) {
257 return (schemas.hashCode() - other.schemas.hashCode());
267 public AxContextSchema get(final AxArtifactKey conceptKey) {
268 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
275 public AxContextSchema get(final String conceptKeyName) {
276 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
283 public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
284 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
292 public Set<AxContextSchema> getAll(final String conceptKeyName) {
293 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
300 public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
301 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,