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.eventmodel.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.apex.model.contextmodel.concepts.AxContextSchemas;
46 import org.onap.policy.common.utils.validation.Assertions;
49 * A container class for an Apex event model. This class is a container class that allows an Apex model to be
50 * constructed that contains events and context and the key information for those events and context. The model contains
51 * schema definitions and the definitions of events that use those schemas.
53 * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
54 * {@link AxEvents} validation is run on the context schemas and events in the model.
58 @Table(name = "AxEventModel")
60 @XmlRootElement(name = "apexEventModel", namespace = "http://www.onap.org/policy/apex-pdp")
61 @XmlAccessorType(XmlAccessType.FIELD)
62 @XmlType(name = "AxEventModel", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "schemas", "events" })
64 public class AxEventModel extends AxModel {
65 private static final long serialVersionUID = 8800599637708309945L;
68 @OneToOne(cascade = CascadeType.ALL)
69 @JoinColumns({ @JoinColumn(name = "schemasName", referencedColumnName = "name"),
70 @JoinColumn(name = "schemasVersion", referencedColumnName = "version") })
71 @XmlElement(name = "schemas", required = true)
72 private AxContextSchemas schemas;
74 @OneToOne(cascade = CascadeType.ALL)
75 @JoinColumns({ @JoinColumn(name = "eventsName", referencedColumnName = "name"),
76 @JoinColumn(name = "eventsVersion", referencedColumnName = "version") })
77 @XmlElement(name = "events", required = true)
78 private AxEvents events;
82 * The Default Constructor creates a {@link AxEventModel} object with a null artifact key and creates an empty event
85 public AxEventModel() {
86 this(new AxArtifactKey());
92 * @param copyConcept the concept to copy from
94 public AxEventModel(final AxEventModel copyConcept) {
99 * The Key Constructor creates a {@link AxEventModel} object with the given artifact key and creates an empty event
102 * @param key the event model key
104 public AxEventModel(final AxArtifactKey key) {
105 this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
106 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())),
107 new AxEvents(new AxArtifactKey(key.getName() + "_Events", key.getVersion())));
111 * Constructor that initiates a {@link AxEventModel} with all its fields.
113 * @param key the event model key
114 * @param schemas the schemas for events in the event model
115 * @param keyInformation the key information for context schemas and events in the event model
116 * @param events the events in the event model
118 public AxEventModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxKeyInformation keyInformation,
119 final AxEvents events) {
120 super(key, keyInformation);
121 Assertions.argumentNotNull(events, "events may not be null");
123 this.schemas = schemas;
124 this.events = events;
131 public void register() {
133 ModelService.registerModel(AxContextSchemas.class, getSchemas());
134 ModelService.registerModel(AxEvents.class, getEvents());
141 public List<AxKey> getKeys() {
142 final List<AxKey> keyList = super.getKeys();
144 keyList.addAll(schemas.getKeys());
145 keyList.addAll(events.getKeys());
151 * Gets the context schemas.
153 * @return the context schemas
155 public AxContextSchemas getSchemas() {
160 * Sets the context schemas.
162 * @param schemas the context schemas
164 public void setSchemas(final AxContextSchemas schemas) {
165 Assertions.argumentNotNull(schemas, "schemas may not be null");
166 this.schemas = schemas;
170 * Gets the events from the model.
174 public AxEvents getEvents() {
179 * Sets the events in the model.
181 * @param events the events
183 public void setEvents(final AxEvents events) {
184 Assertions.argumentNotNull(events, "events may not be null");
185 this.events = events;
192 public AxValidationResult validate(final AxValidationResult resultIn) {
193 AxValidationResult result = resultIn;
195 result = super.validate(result);
196 result = schemas.validate(result);
197 return events.validate(result);
204 public void clean() {
214 public String toString() {
215 final StringBuilder builder = new StringBuilder();
216 builder.append(this.getClass().getSimpleName());
217 builder.append(":(");
218 builder.append(super.toString());
219 builder.append(",schemas=");
220 builder.append(schemas);
221 builder.append(",events=");
222 builder.append(events);
224 return builder.toString();
231 public AxConcept copyTo(final AxConcept targetObject) {
232 Assertions.argumentNotNull(targetObject, "target may not be null");
234 final Object copyObject = targetObject;
235 Assertions.instanceOf(copyObject, AxEventModel.class);
237 final AxEventModel copy = ((AxEventModel) copyObject);
238 super.copyTo(targetObject);
239 copy.setSchemas(new AxContextSchemas(schemas));
240 copy.setEvents(new AxEvents(events));
249 public int hashCode() {
250 final int prime = 31;
252 result = prime * result + super.hashCode();
253 result = prime * result + schemas.hashCode();
254 result = prime * result + events.hashCode();
262 public boolean equals(final Object obj) {
264 throw new IllegalArgumentException("comparison object may not be null");
270 if (getClass() != obj.getClass()) {
274 final AxEventModel other = (AxEventModel) obj;
275 if (!super.equals(other)) {
278 if (!schemas.equals(other.schemas)) {
281 return events.equals(other.events);
288 public int compareTo(final AxConcept otherObj) {
289 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
291 if (this == otherObj) {
294 if (getClass() != otherObj.getClass()) {
295 return this.hashCode() - otherObj.hashCode();
298 final AxEventModel other = (AxEventModel) otherObj;
299 if (!super.equals(other)) {
300 return super.compareTo(other);
302 if (!schemas.equals(other.schemas)) {
303 return schemas.compareTo(other.schemas);
305 return events.compareTo(other.events);