9796fa6256a5be42a860c33b2214302fbf90c5f7
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2021 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 lombok.EqualsAndHashCode;
37 import lombok.Getter;
38 import lombok.ToString;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
45 import org.onap.policy.apex.model.basicmodel.service.ModelService;
46 import org.onap.policy.common.utils.validation.Assertions;
47
48 /**
49  * A container class for an Apex context model. This class is a container class that allows an Apex model to be
50  * constructed that just contains context and the key information for that context. The model contains schema
51  * definitions and the definitions of context albums that use those schemas. In the case where Apex context is being
52  * used without policy or independent of policy, an Apex context model is sufficient to get Apex context working.
53  *
54  * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
55  * {@link AxContextAlbums} validation is run on the context schemas and albums in the model.
56  */
57 @Entity
58 @Table(name = "AxContextModel")
59
60 @Getter
61 @ToString(callSuper = true)
62 @EqualsAndHashCode(callSuper = true)
63
64 @XmlRootElement(name = "apexContextModel", namespace = "http://www.onap.org/policy/apex-pdp")
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlType(name = "AxContextModel", namespace = "http://www.onap.org/policy/apex-pdp",
67         propOrder = { "schemas", "albums" })
68
69 public class AxContextModel extends AxModel {
70     private static final long serialVersionUID = 8800599637708309945L;
71
72     // @formatter:off
73     @OneToOne(cascade = CascadeType.ALL)
74     @JoinColumn(name = "schemasName", referencedColumnName = "name")
75     @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
76     @XmlElement(name = "schemas", required = true)
77     private AxContextSchemas schemas;
78
79     @OneToOne(cascade = CascadeType.ALL)
80     @JoinColumn(name = "albumsName", referencedColumnName = "name")
81     @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
82     @XmlElement(name = "albums", required = true)
83     private AxContextAlbums albums;
84     // @formatter:on
85
86     /**
87      * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
88      * context model.
89      */
90     public AxContextModel() {
91         this(new AxArtifactKey());
92     }
93
94     /**
95      * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
96      * context model.
97      *
98      * @param key the key of the context model
99      */
100     public AxContextModel(final AxArtifactKey key) {
101         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
102                 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
103                 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
104     }
105
106     /**
107      * Copy constructor.
108      *
109      * @param copyConcept the concept to copy from
110      */
111     public AxContextModel(final AxContextModel copyConcept) {
112         super(copyConcept);
113     }
114
115     /**
116      * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
117      * {@link AxContextAlbums} container is created.
118      *
119      * @param key the key of the context model
120      * @param schemas the context schema definitions
121      * @param keyInformation the key information for those context schemas
122      */
123     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
124             final AxKeyInformation keyInformation) {
125         this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
126                 keyInformation);
127     }
128
129     /**
130      * Constructor that initiates a {@link AxContextModel} with all its fields.
131      *
132      * @param key the key of the context model
133      * @param schemas the context schema definitions
134      * @param albums the context album container containing context albums
135      * @param keyInformation the key information for those context schemas
136      */
137     public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
138             final AxKeyInformation keyInformation) {
139         super(key, keyInformation);
140         Assertions.argumentNotNull(schemas, "schemas may not be null");
141         Assertions.argumentNotNull(albums, "albums may not be null");
142         this.schemas = schemas;
143         this.albums = albums;
144     }
145
146     /**
147      * {@inheritDoc}.
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      * {@inheritDoc}.
158      */
159     @Override
160     public List<AxKey> getKeys() {
161         final List<AxKey> keyList = super.getKeys();
162
163         keyList.addAll(schemas.getKeys());
164         keyList.addAll(albums.getKeys());
165
166         return keyList;
167     }
168
169     /**
170      * Sets the context schemas on the model.
171      *
172      * @param schemas the context schemas
173      */
174     public void setSchemas(final AxContextSchemas schemas) {
175         Assertions.argumentNotNull(schemas, "schemas may not be null");
176         this.schemas = schemas;
177     }
178
179     /**
180      * Sets the context albums on the model.
181      *
182      * @param albums the context albums
183      */
184     public void setAlbums(final AxContextAlbums albums) {
185         Assertions.argumentNotNull(albums, "albums may not be null");
186         this.albums = albums;
187     }
188
189     /**
190      * {@inheritDoc}.
191      */
192     @Override
193     public AxValidationResult validate(final AxValidationResult resultIn) {
194         AxValidationResult result = resultIn;
195
196         result = super.validate(result);
197         result = schemas.validate(result);
198         return albums.validate(result);
199     }
200
201     /**
202      * {@inheritDoc}.
203      */
204     @Override
205     public void clean() {
206         super.clean();
207         schemas.clean();
208         albums.clean();
209     }
210
211     /**
212      * {@inheritDoc}.
213      */
214     @Override
215     public AxConcept copyTo(final AxConcept target) {
216         Assertions.argumentNotNull(target, "target may not be null");
217
218         final Object copyObject = target;
219         Assertions.instanceOf(copyObject, AxContextModel.class);
220
221         final AxContextModel copy = ((AxContextModel) copyObject);
222         super.copyTo(target);
223         copy.setSchemas(new AxContextSchemas(schemas));
224         copy.setAlbums(new AxContextAlbums(albums));
225
226         return copy;
227     }
228
229     /**
230      * {@inheritDoc}.
231      */
232     @Override
233     public int compareTo(final AxConcept otherObj) {
234         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
235
236         if (this == otherObj) {
237             return 0;
238         }
239         if (getClass() != otherObj.getClass()) {
240             return this.hashCode() - otherObj.hashCode();
241         }
242
243         final AxContextModel other = (AxContextModel) otherObj;
244         if (!super.equals(other)) {
245             return super.compareTo(other);
246         }
247         if (!schemas.equals(other.schemas)) {
248             return schemas.compareTo(other.schemas);
249         }
250         return albums.compareTo(other.albums);
251     }
252 }