2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.contextmodel.concepts;
23 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;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.service.ModelService;
44 import org.onap.policy.apex.model.utilities.Assertions;
47 * A container class for an Apex context model. This class is a container class that allows an Apex model to be
48 * constructed that just contains context and the key information for that context. The model contains schema
49 * definitions and the definitions of context albums that use those schemas. In the case where Apex context is being
50 * used without policy or independent of policy, an Apex context model is sufficient to get Apex context working.
52 * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
53 * {@link AxContextAlbums} validation is run on the context schemas and albums in the model.
56 @Table(name = "AxContextModel")
58 @XmlRootElement(name = "apexContextModel", namespace = "http://www.onap.org/policy/apex-pdp")
59 @XmlAccessorType(XmlAccessType.FIELD)
60 @XmlType(name = "AxContextModel", namespace = "http://www.onap.org/policy/apex-pdp",
61 propOrder = { "schemas", "albums" })
63 public class AxContextModel extends AxModel {
64 private static final long serialVersionUID = 8800599637708309945L;
67 @OneToOne(cascade = CascadeType.ALL)
69 @JoinColumn(name = "schemasName", referencedColumnName = "name"),
70 @JoinColumn(name = "schemasVersion", referencedColumnName = "version")
72 @XmlElement(name = "schemas", required = true)
73 private AxContextSchemas schemas;
75 @OneToOne(cascade = CascadeType.ALL)
77 @JoinColumn(name = "albumsName", referencedColumnName = "name"),
78 @JoinColumn(name = "albumsVersion", referencedColumnName = "version")
80 @XmlElement(name = "albums", required = true)
81 private AxContextAlbums albums;
85 * The Default Constructor creates a {@link AxContextModel} object with a null artifact key and creates an empty
88 public AxContextModel() {
89 this(new AxArtifactKey());
93 * The Key Constructor creates a {@link AxContextModel} object with the given artifact key and creates an empty
96 * @param key the key of the context model
98 public AxContextModel(final AxArtifactKey key) {
99 this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
100 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
101 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())));
107 * @param copyConcept the concept to copy from
109 public AxContextModel(final AxContextModel copyConcept) {
114 * Constructor that initiates a {@link AxContextModel} with schemas and keys for those schemas. An empty
115 * {@link AxContextAlbums} container is created.
117 * @param key the key of the context model
118 * @param schemas the context schema definitions
119 * @param keyInformation the key information for those context schemas
121 public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas,
122 final AxKeyInformation keyInformation) {
123 this(key, schemas, new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
128 * Constructor that initiates a {@link AxContextModel} with all its fields.
130 * @param key the key of the context model
131 * @param schemas the context schema definitions
132 * @param albums the context album container containing context albums
133 * @param keyInformation the key information for those context schemas
135 public AxContextModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxContextAlbums albums,
136 final AxKeyInformation keyInformation) {
137 super(key, keyInformation);
138 Assertions.argumentNotNull(schemas, "schemas may not be null");
139 Assertions.argumentNotNull(albums, "albums may not be null");
140 this.schemas = schemas;
141 this.albums = albums;
147 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#register()
150 public void register() {
152 ModelService.registerModel(AxContextSchemas.class, getSchemas());
153 ModelService.registerModel(AxContextAlbums.class, getAlbums());
159 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#getKeys()
162 public List<AxKey> getKeys() {
163 final List<AxKey> keyList = super.getKeys();
165 keyList.addAll(schemas.getKeys());
166 keyList.addAll(albums.getKeys());
172 * Gets the context schemas from the model.
174 * @return the context schemas
176 public AxContextSchemas getSchemas() {
181 * Sets the context schemas on the model.
183 * @param schemas the context schemas
185 public void setSchemas(final AxContextSchemas schemas) {
186 Assertions.argumentNotNull(schemas, "schemas may not be null");
187 this.schemas = schemas;
191 * Gets the context albums from the model.
193 * @return the context albums
195 public AxContextAlbums getAlbums() {
200 * Sets the context albums on the model.
202 * @param albums the context albums
204 public void setAlbums(final AxContextAlbums albums) {
205 Assertions.argumentNotNull(albums, "albums may not be null");
206 this.albums = albums;
212 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#validate(org.onap.policy.apex.model.
213 * basicmodel.concepts.AxValidationResult)
216 public AxValidationResult validate(final AxValidationResult resultIn) {
217 AxValidationResult result = resultIn;
219 result = super.validate(result);
220 result = schemas.validate(result);
221 return albums.validate(result);
227 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#clean()
230 public void clean() {
239 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#toString()
242 public String toString() {
243 final StringBuilder builder = new StringBuilder();
244 builder.append(this.getClass().getSimpleName());
245 builder.append(":(");
246 builder.append(super.toString());
247 builder.append(",schemas=");
248 builder.append(schemas);
249 builder.append(",albums=");
250 builder.append(albums);
252 return builder.toString();
258 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
259 * basicmodel.concepts.AxConcept)
262 public AxConcept copyTo(final AxConcept target) {
263 Assertions.argumentNotNull(target, "target may not be null");
265 final Object copyObject = target;
266 Assertions.instanceOf(copyObject, AxContextModel.class);
268 final AxContextModel copy = ((AxContextModel) copyObject);
269 super.copyTo(target);
270 copy.setSchemas(new AxContextSchemas(schemas));
271 copy.setAlbums(new AxContextAlbums(albums));
279 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#hashCode()
282 public int hashCode() {
283 final int prime = 31;
285 result = prime * result + super.hashCode();
286 result = prime * result + schemas.hashCode();
287 result = prime * result + albums.hashCode();
294 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#equals(java.lang.Object)
297 public boolean equals(final Object obj) {
299 throw new IllegalArgumentException("comparison object may not be null");
305 if (getClass() != obj.getClass()) {
309 final AxContextModel other = (AxContextModel) obj;
310 if (!super.equals(other)) {
313 if (!schemas.equals(other.schemas)) {
316 return albums.equals(other.albums);
322 * @see org.onap.policy.apex.model.basicmodel.concepts.AxModel#compareTo(org.onap.policy.apex.model.
323 * basicmodel.concepts.AxConcept)
326 public int compareTo(final AxConcept otherObj) {
327 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
329 if (this == otherObj) {
332 if (getClass() != otherObj.getClass()) {
333 return this.hashCode() - otherObj.hashCode();
336 final AxContextModel other = (AxContextModel) otherObj;
337 if (!super.equals(other)) {
338 return super.compareTo(other);
340 if (!schemas.equals(other.schemas)) {
341 return schemas.compareTo(other.schemas);
343 return albums.compareTo(other.albums);