2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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 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)
67 @JoinColumn(name = "schemasName", referencedColumnName = "name")
68 @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
69 @XmlElement(name = "schemas", required = true)
70 private AxContextSchemas schemas;
72 @OneToOne(cascade = CascadeType.ALL)
73 @JoinColumn(name = "albumsName", referencedColumnName = "name")
74 @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
75 @XmlElement(name = "albums", required = true)
76 private AxContextAlbums albums;
80 * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
83 public AxContextModel() {
84 this(new AxArtifactKey());
88 * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
91 * @param key the key of the context model
93 public AxContextModel(final AxArtifactKey key) {
94 this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
95 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
96 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
102 * @param copyConcept the concept to copy from
104 public AxContextModel(final AxContextModel copyConcept) {
109 * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
110 * {@link AxContextAlbums} container is created.
112 * @param key the key of the context model
113 * @param schemas the context schema definitions
114 * @param keyInformation the key information for those context schemas
116 public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
117 final AxKeyInformation keyInformation) {
118 this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
123 * Constructor that initiates a {@link AxContextModel} with all its fields.
125 * @param key the key of the context model
126 * @param schemas the context schema definitions
127 * @param albums the context album container containing context albums
128 * @param keyInformation the key information for those context schemas
130 public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
131 final AxKeyInformation keyInformation) {
132 super(key, keyInformation);
133 Assertions.argumentNotNull(schemas, "schemas may not be null");
134 Assertions.argumentNotNull(albums, "albums may not be null");
135 this.schemas = schemas;
136 this.albums = albums;
143 public void register() {
145 ModelService.registerModel(AxContextSchemas.class, getSchemas());
146 ModelService.registerModel(AxContextAlbums.class, getAlbums());
153 public List<AxKey> getKeys() {
154 final List<AxKey> keyList = super.getKeys();
156 keyList.addAll(schemas.getKeys());
157 keyList.addAll(albums.getKeys());
163 * Gets the context schemas from the model.
165 * @return the context schemas
167 public AxContextSchemas getSchemas() {
172 * Sets the context schemas on the model.
174 * @param schemas the context schemas
176 public void setSchemas(final AxContextSchemas schemas) {
177 Assertions.argumentNotNull(schemas, "schemas may not be null");
178 this.schemas = schemas;
182 * Gets the context albums from the model.
184 * @return the context albums
186 public AxContextAlbums getAlbums() {
191 * Sets the context albums on the model.
193 * @param albums the context albums
195 public void setAlbums(final AxContextAlbums albums) {
196 Assertions.argumentNotNull(albums, "albums may not be null");
197 this.albums = albums;
204 public AxValidationResult validate(final AxValidationResult resultIn) {
205 AxValidationResult result = resultIn;
207 result = super.validate(result);
208 result = schemas.validate(result);
209 return albums.validate(result);
216 public void clean() {
226 public String toString() {
227 final StringBuilder builder = new StringBuilder();
228 builder.append(this.getClass().getSimpleName());
229 builder.append(":(");
230 builder.append(super.toString());
231 builder.append(",schemas=");
232 builder.append(schemas);
233 builder.append(",albums=");
234 builder.append(albums);
236 return builder.toString();
243 public AxConcept copyTo(final AxConcept target) {
244 Assertions.argumentNotNull(target, "target may not be null");
246 final Object copyObject = target;
247 Assertions.instanceOf(copyObject, AxContextModel.class);
249 final AxContextModel copy = ((AxContextModel) copyObject);
250 super.copyTo(target);
251 copy.setSchemas(new AxContextSchemas(schemas));
252 copy.setAlbums(new AxContextAlbums(albums));
261 public int hashCode() {
262 final int prime = 31;
264 result = prime * result + super.hashCode();
265 result = prime * result + schemas.hashCode();
266 result = prime * result + albums.hashCode();
274 public boolean equals(final Object obj) {
276 throw new IllegalArgumentException("comparison object may not be null");
282 if (getClass() != obj.getClass()) {
286 final AxContextModel other = (AxContextModel) obj;
287 if (!super.equals(other)) {
290 if (!schemas.equals(other.schemas)) {
293 return albums.equals(other.albums);
300 public int compareTo(final AxConcept otherObj) {
301 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
303 if (this == otherObj) {
306 if (getClass() != otherObj.getClass()) {
307 return this.hashCode() - otherObj.hashCode();
310 final AxContextModel other = (AxContextModel) otherObj;
311 if (!super.equals(other)) {
312 return super.compareTo(other);
314 if (!schemas.equals(other.schemas)) {
315 return schemas.compareTo(other.schemas);
317 return albums.compareTo(other.albums);