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;
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;
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;
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.
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.
57 @Table(name = "AxContextModel")
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" })
64 public class AxContextModel extends AxModel {
65 private static final long serialVersionUID = 8800599637708309945L;
68 @OneToOne(cascade = CascadeType.ALL)
70 @JoinColumn(name = "schemasName", referencedColumnName = "name"),
71 @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
73 @XmlElement(name = "schemas", required = true)
74 private AxContextSchemas schemas;
76 @OneToOne(cascade = CascadeType.ALL)
78 @JoinColumn(name = "albumsName", referencedColumnName = "name"),
79 @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
81 @XmlElement(name = "albums", required = true)
82 private AxContextAlbums albums;
86 * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
89 public AxContextModel() {
90 this(new AxArtifactKey());
94 * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
97 * @param key the key of the context model
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())));
108 * @param copyConcept the concept to copy from
110 public AxContextModel(final AxContextModel copyConcept) {
115 * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
116 * {@link AxContextAlbums} container is created.
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
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())),
129 * Constructor that initiates a {@link AxContextModel} with all its fields.
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
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;
148 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#register()
151 public void register() {
153 ModelService.registerModel(AxContextSchemas.class, getSchemas());
154 ModelService.registerModel(AxContextAlbums.class, getAlbums());
160 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#getKeys()
163 public List<AxKey> getKeys() {
164 final List<AxKey> keyList = super.getKeys();
166 keyList.addAll(schemas.getKeys());
167 keyList.addAll(albums.getKeys());
173 * Gets the context schemas from the model.
175 * @return the context schemas
177 public AxContextSchemas getSchemas() {
182 * Sets the context schemas on the model.
184 * @param schemas the context schemas
186 public void setSchemas(final AxContextSchemas schemas) {
187 Assertions.argumentNotNull(schemas, "schemas may not be null");
188 this.schemas = schemas;
192 * Gets the context albums from the model.
194 * @return the context albums
196 public AxContextAlbums getAlbums() {
201 * Sets the context albums on the model.
203 * @param albums the context albums
205 public void setAlbums(final AxContextAlbums albums) {
206 Assertions.argumentNotNull(albums, "albums may not be null");
207 this.albums = albums;
213 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#validate(org.onap.policy.apex.model.
214 * basicmodel.concepts.AxValidationResult)
217 public AxValidationResult validate(final AxValidationResult resultIn) {
218 AxValidationResult result = resultIn;
220 result = super.validate(result);
221 result = schemas.validate(result);
222 return albums.validate(result);
228 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#clean()
231 public void clean() {
240 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#toString()
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);
253 return builder.toString();
259 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
260 * basicmodel.concepts.AxConcept)
263 public AxConcept copyTo(final AxConcept target) {
264 Assertions.argumentNotNull(target, "target may not be null");
266 final Object copyObject = target;
267 Assertions.instanceOf(copyObject, AxContextModel.class);
269 final AxContextModel copy = ((AxContextModel) copyObject);
270 super.copyTo(target);
271 copy.setSchemas(new AxContextSchemas(schemas));
272 copy.setAlbums(new AxContextAlbums(albums));
280 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#hashCode()
283 public int hashCode() {
284 final int prime = 31;
286 result = prime * result + super.hashCode();
287 result = prime * result + schemas.hashCode();
288 result = prime * result + albums.hashCode();
295 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#equals(java.lang.Object)
298 public boolean equals(final Object obj) {
300 throw new IllegalArgumentException("comparison object may not be null");
306 if (getClass() != obj.getClass()) {
310 final AxContextModel other = (AxContextModel) obj;
311 if (!super.equals(other)) {
314 if (!schemas.equals(other.schemas)) {
317 return albums.equals(other.albums);
323 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#compareTo(org.onap.policy.apex.model.
324 * basicmodel.concepts.AxConcept)
327 public int compareTo(final AxConcept otherObj) {
328 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
330 if (this == otherObj) {
333 if (getClass() != otherObj.getClass()) {
334 return this.hashCode() - otherObj.hashCode();
337 final AxContextModel other = (AxContextModel) otherObj;
338 if (!super.equals(other)) {
339 return super.compareTo(other);
341 if (!schemas.equals(other.schemas)) {
342 return schemas.compareTo(other.schemas);
344 return albums.compareTo(other.albums);