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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.contextmodel.concepts;
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;
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.
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.
55 @Table(name = "AxContextModel")
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" })
62 public class AxContextModel extends AxModel {
63 private static final long serialVersionUID = 8800599637708309945L;
66 @OneToOne(cascade = CascadeType.ALL)
68 @JoinColumn(name = "schemasName", referencedColumnName = "name"),
69 @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
71 @XmlElement(name = "schemas", required = true)
72 private AxContextSchemas schemas;
74 @OneToOne(cascade = CascadeType.ALL)
76 @JoinColumn(name = "albumsName", referencedColumnName = "name"),
77 @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
79 @XmlElement(name = "albums", required = true)
80 private AxContextAlbums albums;
84 * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
87 public AxContextModel() {
88 this(new AxArtifactKey());
92 * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
95 * @param key the key of the context model
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())));
106 * @param copyConcept the concept to copy from
108 public AxContextModel(final AxContextModel copyConcept) {
113 * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
114 * {@link AxContextAlbums} container is created.
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
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())),
127 * Constructor that initiates a {@link AxContextModel} with all its fields.
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
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;
146 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#register()
149 public void register() {
151 ModelService.registerModel(AxContextSchemas.class, getSchemas());
152 ModelService.registerModel(AxContextAlbums.class, getAlbums());
158 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#getKeys()
161 public List<AxKey> getKeys() {
162 final List<AxKey> keyList = super.getKeys();
164 keyList.addAll(schemas.getKeys());
165 keyList.addAll(albums.getKeys());
171 * Gets the context schemas from the model.
173 * @return the context schemas
175 public AxContextSchemas getSchemas() {
180 * Sets the context schemas on the model.
182 * @param schemas the context schemas
184 public void setSchemas(final AxContextSchemas schemas) {
185 Assertions.argumentNotNull(schemas, "schemas may not be null");
186 this.schemas = schemas;
190 * Gets the context albums from the model.
192 * @return the context albums
194 public AxContextAlbums getAlbums() {
199 * Sets the context albums on the model.
201 * @param albums the context albums
203 public void setAlbums(final AxContextAlbums albums) {
204 Assertions.argumentNotNull(albums, "albums may not be null");
205 this.albums = albums;
211 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#validate(org.onap.policy.apex.model.
212 * basicmodel.concepts.AxValidationResult)
215 public AxValidationResult validate(final AxValidationResult resultIn) {
216 AxValidationResult result = resultIn;
218 result = super.validate(result);
219 result = schemas.validate(result);
220 return albums.validate(result);
226 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#clean()
229 public void clean() {
238 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#toString()
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);
251 return builder.toString();
257 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
258 * basicmodel.concepts.AxConcept)
261 public AxConcept copyTo(final AxConcept target) {
262 Assertions.argumentNotNull(target, "target may not be null");
264 final Object copyObject = target;
265 Assertions.instanceOf(copyObject, AxContextModel.class);
267 final AxContextModel copy = ((AxContextModel) copyObject);
268 super.copyTo(target);
269 copy.setSchemas(new AxContextSchemas(schemas));
270 copy.setAlbums(new AxContextAlbums(albums));
278 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#hashCode()
281 public int hashCode() {
282 final int prime = 31;
284 result = prime * result + super.hashCode();
285 result = prime * result + schemas.hashCode();
286 result = prime * result + albums.hashCode();
293 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#equals(java.lang.Object)
296 public boolean equals(final Object obj) {
298 throw new IllegalArgumentException("comparison object may not be null");
304 if (getClass() != obj.getClass()) {
308 final AxContextModel other = (AxContextModel) obj;
309 if (!super.equals(other)) {
312 if (!schemas.equals(other.schemas)) {
315 return albums.equals(other.albums);
321 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#compareTo(org.onap.policy.apex.model.
322 * basicmodel.concepts.AxConcept)
325 public int compareTo(final AxConcept otherObj) {
326 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
328 if (this == otherObj) {
331 if (getClass() != otherObj.getClass()) {
332 return this.hashCode() - otherObj.hashCode();
335 final AxContextModel other = (AxContextModel) otherObj;
336 if (!super.equals(other)) {
337 return super.compareTo(other);
339 if (!schemas.equals(other.schemas)) {
340 return schemas.compareTo(other.schemas);
342 return albums.compareTo(other.albums);