da04aad8338902f33feac9dad83c58f95c28eddf
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.contextmodel.concepts;
23
24 import java.util.List;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.JoinColumn;
28 import javax.persistence.JoinColumns;
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     @JoinColumns({
68         @JoinColumn(name = "schemasName", referencedColumnName = "name"),
69         @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
70         })
71     @XmlElement(name = "schemas", required = true)
72     private AxContextSchemas schemas;
73
74     @OneToOne(cascade = CascadeType.ALL)
75     @JoinColumns({
76         @JoinColumn(name = "albumsName", referencedColumnName = "name"),
77         @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
78         })
79     @XmlElement(name = "albums", required = true)
80     private AxContextAlbums albums;
81     // @formatter:on
82
83     /**
84      * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
85      * context model.
86      */
87     public AxContextModel() {
88         this(new AxArtifactKey());
89     }
90
91     /**
92      * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
93      * context model.
94      *
95      * @param key the key of the context model
96      */
97     public AxContextModel(final AxArtifactKey key) {
98         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
99                 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
100                 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
101     }
102
103     /**
104      * Copy constructor.
105      *
106      * @param copyConcept the concept to copy from
107      */
108     public AxContextModel(final AxContextModel copyConcept) {
109         super(copyConcept);
110     }
111
112     /**
113      * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
114      * {@link AxContextAlbums} container is created.
115      *
116      * @param key the key of the context model
117      * @param schemas the context schema definitions
118      * @param keyInformation the key information for those context schemas
119      */
120     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
121             final AxKeyInformation keyInformation) {
122         this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
123                 keyInformation);
124     }
125
126     /**
127      * Constructor that initiates a {@link AxContextModel} with all its fields.
128      *
129      * @param key the key of the context model
130      * @param schemas the context schema definitions
131      * @param albums the context album container containing context albums
132      * @param keyInformation the key information for those context schemas
133      */
134     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
135             final AxKeyInformation keyInformation) {
136         super(key, keyInformation);
137         Assertions.argumentNotNull(schemas, "schemas may not be null");
138         Assertions.argumentNotNull(albums, "albums may not be null");
139         this.schemas = schemas;
140         this.albums = albums;
141     }
142
143     /**
144      * {@inheritDoc}.
145      */
146     @Override
147     public void register() {
148         super.register();
149         ModelService.registerModel(AxContextSchemas.class, getSchemas());
150         ModelService.registerModel(AxContextAlbums.class, getAlbums());
151     }
152
153     /**
154      * {@inheritDoc}.
155      */
156     @Override
157     public List<AxKey> getKeys() {
158         final List<AxKey> keyList = super.getKeys();
159
160         keyList.addAll(schemas.getKeys());
161         keyList.addAll(albums.getKeys());
162
163         return keyList;
164     }
165
166     /**
167      * Gets the context schemas from the model.
168      *
169      * @return the context schemas
170      */
171     public AxContextSchemas getSchemas() {
172         return schemas;
173     }
174
175     /**
176      * Sets the context schemas on the model.
177      *
178      * @param schemas the context schemas
179      */
180     public void setSchemas(final AxContextSchemas schemas) {
181         Assertions.argumentNotNull(schemas, "schemas may not be null");
182         this.schemas = schemas;
183     }
184
185     /**
186      * Gets the context albums from the model.
187      *
188      * @return the context albums
189      */
190     public AxContextAlbums getAlbums() {
191         return albums;
192     }
193
194     /**
195      * Sets the context albums on the model.
196      *
197      * @param albums the context albums
198      */
199     public void setAlbums(final AxContextAlbums albums) {
200         Assertions.argumentNotNull(albums, "albums may not be null");
201         this.albums = albums;
202     }
203
204     /**
205      * {@inheritDoc}.
206      */
207     @Override
208     public AxValidationResult validate(final AxValidationResult resultIn) {
209         AxValidationResult result = resultIn;
210
211         result = super.validate(result);
212         result = schemas.validate(result);
213         return albums.validate(result);
214     }
215
216     /**
217      * {@inheritDoc}.
218      */
219     @Override
220     public void clean() {
221         super.clean();
222         schemas.clean();
223         albums.clean();
224     }
225
226     /**
227      * {@inheritDoc}.
228      */
229     @Override
230     public String toString() {
231         final StringBuilder builder = new StringBuilder();
232         builder.append(this.getClass().getSimpleName());
233         builder.append(":(");
234         builder.append(super.toString());
235         builder.append(",schemas=");
236         builder.append(schemas);
237         builder.append(",albums=");
238         builder.append(albums);
239         builder.append(")");
240         return builder.toString();
241     }
242
243     /**
244      * {@inheritDoc}.
245      */
246     @Override
247     public AxConcept copyTo(final AxConcept target) {
248         Assertions.argumentNotNull(target, "target may not be null");
249
250         final Object copyObject = target;
251         Assertions.instanceOf(copyObject, AxContextModel.class);
252
253         final AxContextModel copy = ((AxContextModel) copyObject);
254         super.copyTo(target);
255         copy.setSchemas(new AxContextSchemas(schemas));
256         copy.setAlbums(new AxContextAlbums(albums));
257
258         return copy;
259     }
260
261     /**
262      * {@inheritDoc}.
263      */
264     @Override
265     public int hashCode() {
266         final int prime = 31;
267         int result = 1;
268         result = prime * result + super.hashCode();
269         result = prime * result + schemas.hashCode();
270         result = prime * result + albums.hashCode();
271         return result;
272     }
273
274     /**
275      * {@inheritDoc}.
276      */
277     @Override
278     public boolean equals(final Object obj) {
279         if (obj == null) {
280             throw new IllegalArgumentException("comparison object may not be null");
281         }
282
283         if (this == obj) {
284             return true;
285         }
286         if (getClass() != obj.getClass()) {
287             return false;
288         }
289
290         final AxContextModel other = (AxContextModel) obj;
291         if (!super.equals(other)) {
292             return false;
293         }
294         if (!schemas.equals(other.schemas)) {
295             return false;
296         }
297         return albums.equals(other.albums);
298     }
299
300     /**
301      * {@inheritDoc}.
302      */
303     @Override
304     public int compareTo(final AxConcept otherObj) {
305         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
306
307         if (this == otherObj) {
308             return 0;
309         }
310         if (getClass() != otherObj.getClass()) {
311             return this.hashCode() - otherObj.hashCode();
312         }
313
314         final AxContextModel other = (AxContextModel) otherObj;
315         if (!super.equals(other)) {
316             return super.compareTo(other);
317         }
318         if (!schemas.equals(other.schemas)) {
319             return schemas.compareTo(other.schemas);
320         }
321         return albums.compareTo(other.albums);
322     }
323 }