9fbcc81cb79c7fd3e4e8e737884f92cbd78040fa
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.persistence.CascadeType;
27 import javax.persistence.Entity;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Table;
31 import javax.xml.bind.annotation.XmlAccessType;
32 import javax.xml.bind.annotation.XmlAccessorType;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import javax.xml.bind.annotation.XmlType;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
42 import org.onap.policy.apex.model.basicmodel.service.ModelService;
43 import org.onap.policy.common.utils.validation.Assertions;
44
45 /**
46  * A container class for an Apex context model. This class is a container class that allows an Apex model to be
47  * constructed that just contains context and the key information for that context. The model contains schema
48  * definitions and the definitions of context albums that use those schemas. In the case where Apex context is being
49  * used without policy or independent of policy, an Apex context model is sufficient to get Apex context working.
50  *
51  * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
52  * {@link AxContextAlbums} validation is run on the context schemas and albums in the model.
53  */
54 @Entity
55 @Table(name = "AxContextModel")
56
57 @XmlRootElement(name = "apexContextModel", namespace = "http://www.onap.org/policy/apex-pdp")
58 @XmlAccessorType(XmlAccessType.FIELD)
59 @XmlType(name = "AxContextModel", namespace = "http://www.onap.org/policy/apex-pdp",
60         propOrder = { "schemas", "albums" })
61
62 public class AxContextModel extends AxModel {
63     private static final long serialVersionUID = 8800599637708309945L;
64
65     // @formatter:off
66     @OneToOne(cascade = CascadeType.ALL)
67     @JoinColumn(name = "schemasName", referencedColumnName = "name")
68     @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
69     @XmlElement(name = "schemas", required = true)
70     private AxContextSchemas schemas;
71
72     @OneToOne(cascade = CascadeType.ALL)
73     @JoinColumn(name = "albumsName", referencedColumnName = "name")
74     @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
75     @XmlElement(name = "albums", required = true)
76     private AxContextAlbums albums;
77     // @formatter:on
78
79     /**
80      * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
81      * context model.
82      */
83     public AxContextModel() {
84         this(new AxArtifactKey());
85     }
86
87     /**
88      * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
89      * context model.
90      *
91      * @param key the key of the context model
92      */
93     public AxContextModel(final AxArtifactKey key) {
94         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
95                 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
96                 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
97     }
98
99     /**
100      * Copy constructor.
101      *
102      * @param copyConcept the concept to copy from
103      */
104     public AxContextModel(final AxContextModel copyConcept) {
105         super(copyConcept);
106     }
107
108     /**
109      * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
110      * {@link AxContextAlbums} container is created.
111      *
112      * @param key the key of the context model
113      * @param schemas the context schema definitions
114      * @param keyInformation the key information for those context schemas
115      */
116     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
117             final AxKeyInformation keyInformation) {
118         this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
119                 keyInformation);
120     }
121
122     /**
123      * Constructor that initiates a {@link AxContextModel} with all its fields.
124      *
125      * @param key the key of the context model
126      * @param schemas the context schema definitions
127      * @param albums the context album container containing context albums
128      * @param keyInformation the key information for those context schemas
129      */
130     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
131             final AxKeyInformation keyInformation) {
132         super(key, keyInformation);
133         Assertions.argumentNotNull(schemas, "schemas may not be null");
134         Assertions.argumentNotNull(albums, "albums may not be null");
135         this.schemas = schemas;
136         this.albums = albums;
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public void register() {
144         super.register();
145         ModelService.registerModel(AxContextSchemas.class, getSchemas());
146         ModelService.registerModel(AxContextAlbums.class, getAlbums());
147     }
148
149     /**
150      * {@inheritDoc}.
151      */
152     @Override
153     public List<AxKey> getKeys() {
154         final List<AxKey> keyList = super.getKeys();
155
156         keyList.addAll(schemas.getKeys());
157         keyList.addAll(albums.getKeys());
158
159         return keyList;
160     }
161
162     /**
163      * Gets the context schemas from the model.
164      *
165      * @return the context schemas
166      */
167     public AxContextSchemas getSchemas() {
168         return schemas;
169     }
170
171     /**
172      * Sets the context schemas on the model.
173      *
174      * @param schemas the context schemas
175      */
176     public void setSchemas(final AxContextSchemas schemas) {
177         Assertions.argumentNotNull(schemas, "schemas may not be null");
178         this.schemas = schemas;
179     }
180
181     /**
182      * Gets the context albums from the model.
183      *
184      * @return the context albums
185      */
186     public AxContextAlbums getAlbums() {
187         return albums;
188     }
189
190     /**
191      * Sets the context albums on the model.
192      *
193      * @param albums the context albums
194      */
195     public void setAlbums(final AxContextAlbums albums) {
196         Assertions.argumentNotNull(albums, "albums may not be null");
197         this.albums = albums;
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public AxValidationResult validate(final AxValidationResult resultIn) {
205         AxValidationResult result = resultIn;
206
207         result = super.validate(result);
208         result = schemas.validate(result);
209         return albums.validate(result);
210     }
211
212     /**
213      * {@inheritDoc}.
214      */
215     @Override
216     public void clean() {
217         super.clean();
218         schemas.clean();
219         albums.clean();
220     }
221
222     /**
223      * {@inheritDoc}.
224      */
225     @Override
226     public String toString() {
227         final StringBuilder builder = new StringBuilder();
228         builder.append(this.getClass().getSimpleName());
229         builder.append(":(");
230         builder.append(super.toString());
231         builder.append(",schemas=");
232         builder.append(schemas);
233         builder.append(",albums=");
234         builder.append(albums);
235         builder.append(")");
236         return builder.toString();
237     }
238
239     /**
240      * {@inheritDoc}.
241      */
242     @Override
243     public AxConcept copyTo(final AxConcept target) {
244         Assertions.argumentNotNull(target, "target may not be null");
245
246         final Object copyObject = target;
247         Assertions.instanceOf(copyObject, AxContextModel.class);
248
249         final AxContextModel copy = ((AxContextModel) copyObject);
250         super.copyTo(target);
251         copy.setSchemas(new AxContextSchemas(schemas));
252         copy.setAlbums(new AxContextAlbums(albums));
253
254         return copy;
255     }
256
257     /**
258      * {@inheritDoc}.
259      */
260     @Override
261     public int hashCode() {
262         final int prime = 31;
263         int result = 1;
264         result = prime * result + super.hashCode();
265         result = prime * result + schemas.hashCode();
266         result = prime * result + albums.hashCode();
267         return result;
268     }
269
270     /**
271      * {@inheritDoc}.
272      */
273     @Override
274     public boolean equals(final Object obj) {
275         if (obj == null) {
276             throw new IllegalArgumentException("comparison object may not be null");
277         }
278
279         if (this == obj) {
280             return true;
281         }
282         if (getClass() != obj.getClass()) {
283             return false;
284         }
285
286         final AxContextModel other = (AxContextModel) obj;
287         if (!super.equals(other)) {
288             return false;
289         }
290         if (!schemas.equals(other.schemas)) {
291             return false;
292         }
293         return albums.equals(other.albums);
294     }
295
296     /**
297      * {@inheritDoc}.
298      */
299     @Override
300     public int compareTo(final AxConcept otherObj) {
301         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
302
303         if (this == otherObj) {
304             return 0;
305         }
306         if (getClass() != otherObj.getClass()) {
307             return this.hashCode() - otherObj.hashCode();
308         }
309
310         final AxContextModel other = (AxContextModel) otherObj;
311         if (!super.equals(other)) {
312             return super.compareTo(other);
313         }
314         if (!schemas.equals(other.schemas)) {
315             return schemas.compareTo(other.schemas);
316         }
317         return albums.compareTo(other.albums);
318     }
319 }