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