5b79a3dcb540587b87946ae7bc44c046631dcd81
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.contextmodel.concepts;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.NavigableMap;
29 import java.util.Set;
30 import java.util.TreeMap;
31 import lombok.AccessLevel;
32 import lombok.EqualsAndHashCode;
33 import lombok.Getter;
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;
44
45 /**
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.
50  *
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.
54  */
55 @Getter
56 @ToString
57 @EqualsAndHashCode(callSuper = false)
58 public class AxContextSchemas extends AxConcept implements AxConceptGetter<AxContextSchema> {
59     private static final long serialVersionUID = -3203734282886453582L;
60
61     private AxArtifactKey key;
62
63     @Getter(AccessLevel.NONE)
64     private Map<AxArtifactKey, AxContextSchema> schemas;
65
66     /**
67      * The Default Constructor creates a {@link AxContextSchemas} object with a null artifact key and creates an empty
68      * context schemas map.
69      */
70     public AxContextSchemas() {
71         this(new AxArtifactKey());
72     }
73
74     /**
75      * Copy constructor.
76      *
77      * @param copyConcept the concept to copy from
78      */
79     public AxContextSchemas(final AxContextSchemas copyConcept) {
80         super(copyConcept);
81     }
82
83     /**
84      * The Key Constructor creates a {@link AxContextSchemas} object with the given artifact key and creates an empty
85      * context schemas map.
86      *
87      * @param key the key of the context album container
88      */
89     public AxContextSchemas(final AxArtifactKey key) {
90         this(key, new TreeMap<>());
91     }
92
93     /**
94      * This Constructor creates a {@link AxContextSchemas} object with all its fields defined.
95      *
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
98      */
99     public AxContextSchemas(final AxArtifactKey key, final Map<AxArtifactKey, AxContextSchema> schemas) {
100         super();
101         Assertions.argumentNotNull(key, "key may not be null");
102         Assertions.argumentNotNull(schemas, "schemas may not be null");
103
104         this.key = key;
105         this.schemas = new TreeMap<>();
106         this.schemas.putAll(schemas);
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public List<AxKey> getKeys() {
114         final List<AxKey> keyList = key.getKeys();
115         keyList.addAll(schemas.keySet());
116
117         return keyList;
118     }
119
120     /**
121      * {@inheritDoc}.
122      */
123     @Override
124     public void buildReferences() {
125         schemas.values().stream().forEach(schema -> schema.buildReferences());
126     }
127
128     /**
129      * Sets the key of the context schema container.
130      *
131      * @param key the key of the container
132      */
133     public void setKey(final AxArtifactKey key) {
134         Assertions.argumentNotNull(key, "key may not be null");
135         this.key = key;
136     }
137
138     /**
139      * Gets the map of context schemas in this container.
140      *
141      * @return the map of schemas
142      */
143     public Map<AxArtifactKey, AxContextSchema> getSchemasMap() {
144         return schemas;
145     }
146
147     /**
148      * Sets the map of context schemas in this container.
149      *
150      * @param schemasMap the map of schemas
151      */
152     public void setSchemasMap(final Map<AxArtifactKey, AxContextSchema> schemasMap) {
153         Assertions.argumentNotNull(schemasMap, "schemasMap may not be null");
154
155         this.schemas = new TreeMap<>();
156         this.schemas.putAll(schemasMap);
157     }
158
159     /**
160      * {@inheritDoc}.
161      */
162     @Override
163     public AxValidationResult validate(final AxValidationResult resultIn) {
164         AxValidationResult result = resultIn;
165
166         if (key.equals(AxArtifactKey.getNullKey())) {
167             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
168                 "key is a null key"));
169         }
170
171         result = key.validate(result);
172
173         if (schemas.size() == 0) {
174             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
175                 "contextSchemas may not be empty"));
176         } else {
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"));
185                 } else {
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()));
192                     }
193
194                     result = contextSchemaEntry.getValue().validate(result);
195                 }
196             }
197         }
198
199         return result;
200     }
201
202     /**
203      * {@inheritDoc}.
204      */
205     @Override
206     public void clean() {
207         key.clean();
208         for (final Entry<AxArtifactKey, AxContextSchema> contextSchemaEntry : schemas.entrySet()) {
209             contextSchemaEntry.getKey().clean();
210             contextSchemaEntry.getValue().clean();
211         }
212     }
213
214     /**
215      * {@inheritDoc}.
216      */
217     @Override
218     public AxConcept copyTo(final AxConcept target) {
219         Assertions.argumentNotNull(target, "target may not be null");
220
221         final Object copyObject = target;
222         Assertions.instanceOf(copyObject, AxContextSchemas.class);
223
224         final AxContextSchemas copy = ((AxContextSchemas) copyObject);
225         copy.setKey(new AxArtifactKey(key));
226
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()));
231         }
232         copy.setSchemasMap(newcontextSchemas);
233
234         return copy;
235     }
236
237     /**
238      * {@inheritDoc}.
239      */
240     @Override
241     public int compareTo(final AxConcept otherObj) {
242         if (otherObj == null) {
243             return -1;
244         }
245         if (this == otherObj) {
246             return 0;
247         }
248         if (getClass() != otherObj.getClass()) {
249             return this.hashCode() - otherObj.hashCode();
250         }
251
252         final AxContextSchemas other = (AxContextSchemas) otherObj;
253         if (!key.equals(other.key)) {
254             return key.compareTo(other.key);
255         }
256         if (!schemas.equals(other.schemas)) {
257             return (schemas.hashCode() - other.schemas.hashCode());
258         }
259
260         return 0;
261     }
262
263     /**
264      * {@inheritDoc}.
265      */
266     @Override
267     public AxContextSchema get(final AxArtifactKey conceptKey) {
268         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKey);
269     }
270
271     /**
272      * {@inheritDoc}.
273      */
274     @Override
275     public AxContextSchema get(final String conceptKeyName) {
276         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName);
277     }
278
279     /**
280      * {@inheritDoc}.
281      */
282     @Override
283     public AxContextSchema get(final String conceptKeyName, final String conceptKeyVersion) {
284         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).get(conceptKeyName,
285             conceptKeyVersion);
286     }
287
288     /**
289      * {@inheritDoc}.
290      */
291     @Override
292     public Set<AxContextSchema> getAll(final String conceptKeyName) {
293         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName);
294     }
295
296     /**
297      * {@inheritDoc}.
298      */
299     @Override
300     public Set<AxContextSchema> getAll(final String conceptKeyName, final String conceptKeyVersion) {
301         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextSchema>) schemas).getAll(conceptKeyName,
302             conceptKeyVersion);
303     }
304 }