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