17980ed65caaad40dd9f7980bbd414432b776444
[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      * (non-Javadoc)
145      *
146      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#register()
147      */
148     @Override
149     public void register() {
150         super.register();
151         ModelService.registerModel(AxContextSchemas.class, getSchemas());
152         ModelService.registerModel(AxContextAlbums.class, getAlbums());
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#getKeys()
159      */
160     @Override
161     public List<AxKey> getKeys() {
162         final List<AxKey> keyList = super.getKeys();
163
164         keyList.addAll(schemas.getKeys());
165         keyList.addAll(albums.getKeys());
166
167         return keyList;
168     }
169
170     /**
171      * Gets the context schemas from the model.
172      *
173      * @return the context schemas
174      */
175     public AxContextSchemas getSchemas() {
176         return schemas;
177     }
178
179     /**
180      * Sets the context schemas on the model.
181      *
182      * @param schemas the context schemas
183      */
184     public void setSchemas(final AxContextSchemas schemas) {
185         Assertions.argumentNotNull(schemas, "schemas may not be null");
186         this.schemas = schemas;
187     }
188
189     /**
190      * Gets the context albums from the model.
191      *
192      * @return the context albums
193      */
194     public AxContextAlbums getAlbums() {
195         return albums;
196     }
197
198     /**
199      * Sets the context albums on the model.
200      *
201      * @param albums the context albums
202      */
203     public void setAlbums(final AxContextAlbums albums) {
204         Assertions.argumentNotNull(albums, "albums may not be null");
205         this.albums = albums;
206     }
207
208     /*
209      * (non-Javadoc)
210      *
211      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#validate(org.onap.policy.apex.model.
212      * basicmodel.concepts.AxValidationResult)
213      */
214     @Override
215     public AxValidationResult validate(final AxValidationResult resultIn) {
216         AxValidationResult result = resultIn;
217
218         result = super.validate(result);
219         result = schemas.validate(result);
220         return albums.validate(result);
221     }
222
223     /*
224      * (non-Javadoc)
225      *
226      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#clean()
227      */
228     @Override
229     public void clean() {
230         super.clean();
231         schemas.clean();
232         albums.clean();
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#toString()
239      */
240     @Override
241     public String toString() {
242         final StringBuilder builder = new StringBuilder();
243         builder.append(this.getClass().getSimpleName());
244         builder.append(":(");
245         builder.append(super.toString());
246         builder.append(",schemas=");
247         builder.append(schemas);
248         builder.append(",albums=");
249         builder.append(albums);
250         builder.append(")");
251         return builder.toString();
252     }
253
254     /*
255      * (non-Javadoc)
256      *
257      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
258      * basicmodel.concepts.AxConcept)
259      */
260     @Override
261     public AxConcept copyTo(final AxConcept target) {
262         Assertions.argumentNotNull(target, "target may not be null");
263
264         final Object copyObject = target;
265         Assertions.instanceOf(copyObject, AxContextModel.class);
266
267         final AxContextModel copy = ((AxContextModel) copyObject);
268         super.copyTo(target);
269         copy.setSchemas(new AxContextSchemas(schemas));
270         copy.setAlbums(new AxContextAlbums(albums));
271
272         return copy;
273     }
274
275     /*
276      * (non-Javadoc)
277      *
278      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#hashCode()
279      */
280     @Override
281     public int hashCode() {
282         final int prime = 31;
283         int result = 1;
284         result = prime * result + super.hashCode();
285         result = prime * result + schemas.hashCode();
286         result = prime * result + albums.hashCode();
287         return result;
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#equals(java.lang.Object)
294      */
295     @Override
296     public boolean equals(final Object obj) {
297         if (obj == null) {
298             throw new IllegalArgumentException("comparison object may not be null");
299         }
300
301         if (this == obj) {
302             return true;
303         }
304         if (getClass() != obj.getClass()) {
305             return false;
306         }
307
308         final AxContextModel other = (AxContextModel) obj;
309         if (!super.equals(other)) {
310             return false;
311         }
312         if (!schemas.equals(other.schemas)) {
313             return false;
314         }
315         return albums.equals(other.albums);
316     }
317
318     /*
319      * (non-Javadoc)
320      *
321      * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#compareTo(org.onap.policy.apex.model.
322      * basicmodel.concepts.AxConcept)
323      */
324     @Override
325     public int compareTo(final AxConcept otherObj) {
326         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
327
328         if (this == otherObj) {
329             return 0;
330         }
331         if (getClass() != otherObj.getClass()) {
332             return this.hashCode() - otherObj.hashCode();
333         }
334
335         final AxContextModel other = (AxContextModel) otherObj;
336         if (!super.equals(other)) {
337             return super.compareTo(other);
338         }
339         if (!schemas.equals(other.schemas)) {
340             return schemas.compareTo(other.schemas);
341         }
342         return albums.compareTo(other.albums);
343     }
344 }