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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
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;
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;
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.
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.
58 @Table(name = "AxContextModel")
61 @ToString(callSuper = true)
62 @EqualsAndHashCode(callSuper = true)
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" })
69 public class AxContextModel extends AxModel {
70 private static final long serialVersionUID = 8800599637708309945L;
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;
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;
87 * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
90 public AxContextModel() {
91 this(new AxArtifactKey());
95 * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
98 * @param key the key of the context model
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())));
109 * @param copyConcept the concept to copy from
111 public AxContextModel(final AxContextModel copyConcept) {
116 * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
117 * {@link AxContextAlbums} container is created.
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
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())),
130 * Constructor that initiates a {@link AxContextModel} with all its fields.
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
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;
150 public void register() {
152 ModelService.registerModel(AxContextSchemas.class, getSchemas());
153 ModelService.registerModel(AxContextAlbums.class, getAlbums());
160 public List<AxKey> getKeys() {
161 final List<AxKey> keyList = super.getKeys();
163 keyList.addAll(schemas.getKeys());
164 keyList.addAll(albums.getKeys());
170 * Sets the context schemas on the model.
172 * @param schemas the context schemas
174 public void setSchemas(final AxContextSchemas schemas) {
175 Assertions.argumentNotNull(schemas, "schemas may not be null");
176 this.schemas = schemas;
180 * Sets the context albums on the model.
182 * @param albums the context albums
184 public void setAlbums(final AxContextAlbums albums) {
185 Assertions.argumentNotNull(albums, "albums may not be null");
186 this.albums = albums;
193 public AxValidationResult validate(final AxValidationResult resultIn) {
194 AxValidationResult result = resultIn;
196 result = super.validate(result);
197 result = schemas.validate(result);
198 return albums.validate(result);
205 public void clean() {
215 public AxConcept copyTo(final AxConcept target) {
216 Assertions.argumentNotNull(target, "target may not be null");
218 final Object copyObject = target;
219 Assertions.instanceOf(copyObject, AxContextModel.class);
221 final AxContextModel copy = ((AxContextModel) copyObject);
222 super.copyTo(target);
223 copy.setSchemas(new AxContextSchemas(schemas));
224 copy.setAlbums(new AxContextAlbums(albums));
233 public int compareTo(final AxConcept otherObj) {
234 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
236 if (this == otherObj) {
239 if (getClass() != otherObj.getClass()) {
240 return this.hashCode() - otherObj.hashCode();
243 final AxContextModel other = (AxContextModel) otherObj;
244 if (!super.equals(other)) {
245 return super.compareTo(other);
247 if (!schemas.equals(other.schemas)) {
248 return schemas.compareTo(other.schemas);
250 return albums.compareTo(other.albums);