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