1effe8079bf149c20b742b0f271ac0575f575b26
[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 lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.ToString;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.service.ModelService;
36 import org.onap.policy.common.utils.validation.Assertions;
37
38 /**
39  * A container class for an Apex context model. This class is a container class that allows an Apex model to be
40  * constructed that just contains context and the key information for that context. The model contains schema
41  * definitions and the definitions of context albums that use those schemas. In the case where Apex context is being
42  * used without policy or independent of policy, an Apex context model is sufficient to get Apex context working.
43  *
44  * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
45  * {@link AxContextAlbums} validation is run on the context schemas and albums in the model.
46  */
47 @Getter
48 @ToString(callSuper = true)
49 @EqualsAndHashCode(callSuper = true)
50 public class AxContextModel extends AxModel {
51     private static final long serialVersionUID = 8800599637708309945L;
52
53     private AxContextSchemas schemas;
54     private AxContextAlbums albums;
55
56     /**
57      * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
58      * context model.
59      */
60     public AxContextModel() {
61         this(new AxArtifactKey());
62     }
63
64     /**
65      * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
66      * context model.
67      *
68      * @param key the key of the context model
69      */
70     public AxContextModel(final AxArtifactKey key) {
71         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
72                 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
73                 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
74     }
75
76     /**
77      * Copy constructor.
78      *
79      * @param copyConcept the concept to copy from
80      */
81     public AxContextModel(final AxContextModel copyConcept) {
82         super(copyConcept);
83     }
84
85     /**
86      * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
87      * {@link AxContextAlbums} container is created.
88      *
89      * @param key the key of the context model
90      * @param schemas the context schema definitions
91      * @param keyInformation the key information for those context schemas
92      */
93     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
94             final AxKeyInformation keyInformation) {
95         this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
96                 keyInformation);
97     }
98
99     /**
100      * Constructor that initiates a {@link AxContextModel} with all its fields.
101      *
102      * @param key the key of the context model
103      * @param schemas the context schema definitions
104      * @param albums the context album container containing context albums
105      * @param keyInformation the key information for those context schemas
106      */
107     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
108             final AxKeyInformation keyInformation) {
109         super(key, keyInformation);
110         Assertions.argumentNotNull(schemas, "schemas may not be null");
111         Assertions.argumentNotNull(albums, "albums may not be null");
112         this.schemas = schemas;
113         this.albums = albums;
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
120     public void register() {
121         super.register();
122         ModelService.registerModel(AxContextSchemas.class, getSchemas());
123         ModelService.registerModel(AxContextAlbums.class, getAlbums());
124     }
125
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public List<AxKey> getKeys() {
131         final List<AxKey> keyList = super.getKeys();
132
133         keyList.addAll(schemas.getKeys());
134         keyList.addAll(albums.getKeys());
135
136         return keyList;
137     }
138
139     /**
140      * Sets the context schemas on the model.
141      *
142      * @param schemas the context schemas
143      */
144     public void setSchemas(final AxContextSchemas schemas) {
145         Assertions.argumentNotNull(schemas, "schemas may not be null");
146         this.schemas = schemas;
147     }
148
149     /**
150      * Sets the context albums on the model.
151      *
152      * @param albums the context albums
153      */
154     public void setAlbums(final AxContextAlbums albums) {
155         Assertions.argumentNotNull(albums, "albums may not be null");
156         this.albums = albums;
157     }
158
159     /**
160      * {@inheritDoc}.
161      */
162     @Override
163     public AxValidationResult validate(final AxValidationResult resultIn) {
164         AxValidationResult result = resultIn;
165
166         result = super.validate(result);
167         result = schemas.validate(result);
168         return albums.validate(result);
169     }
170
171     /**
172      * {@inheritDoc}.
173      */
174     @Override
175     public void clean() {
176         super.clean();
177         schemas.clean();
178         albums.clean();
179     }
180
181     /**
182      * {@inheritDoc}.
183      */
184     @Override
185     public AxConcept copyTo(final AxConcept target) {
186         Assertions.argumentNotNull(target, "target may not be null");
187
188         final Object copyObject = target;
189         Assertions.instanceOf(copyObject, AxContextModel.class);
190
191         final AxContextModel copy = ((AxContextModel) copyObject);
192         super.copyTo(target);
193         copy.setSchemas(new AxContextSchemas(schemas));
194         copy.setAlbums(new AxContextAlbums(albums));
195
196         return copy;
197     }
198
199     /**
200      * {@inheritDoc}.
201      */
202     @Override
203     public int compareTo(final AxConcept otherObj) {
204         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
205
206         if (this == otherObj) {
207             return 0;
208         }
209         if (getClass() != otherObj.getClass()) {
210             return this.hashCode() - otherObj.hashCode();
211         }
212
213         final AxContextModel other = (AxContextModel) otherObj;
214         if (!super.equals(other)) {
215             return super.compareTo(other);
216         }
217         if (!schemas.equals(other.schemas)) {
218             return schemas.compareTo(other.schemas);
219         }
220         return albums.compareTo(other.albums);
221     }
222 }