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;
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;
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.apex.model.contextmodel.concepts.AxContextSchemas;
44 import org.onap.policy.common.utils.validation.Assertions;
47 * A container class for an Apex event model. This class is a container class that allows an Apex model to be
48 * constructed that contains events and context and the key information for those events and context. The model contains
49 * schema definitions and the definitions of events that use those schemas.
51 * <p>Validation runs {@link AxModel} validation on the model. In addition, the {@link AxContextSchemas} and
52 * {@link AxEvents} validation is run on the context schemas and events in the model.
56 @Table(name = "AxEventModel")
58 @XmlRootElement(name = "apexEventModel", namespace = "http://www.onap.org/policy/apex-pdp")
59 @XmlAccessorType(XmlAccessType.FIELD)
60 @XmlType(name = "AxEventModel", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "schemas", "events" })
62 public class AxEventModel extends AxModel {
63 private static final long serialVersionUID = 8800599637708309945L;
66 @OneToOne(cascade = CascadeType.ALL)
67 @JoinColumns({ @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 @JoinColumns({ @JoinColumn(name = "eventsName", referencedColumnName = "name"),
74 @JoinColumn(name = "eventsVersion", referencedColumnName = "version") })
75 @XmlElement(name = "events", required = true)
76 private AxEvents events;
80 * The Default Constructor creates a {@link AxEventModel} object with a null artifact key and creates an empty event
83 public AxEventModel() {
84 this(new AxArtifactKey());
90 * @param copyConcept the concept to copy from
92 public AxEventModel(final AxEventModel copyConcept) {
97 * The Key Constructor creates a {@link AxEventModel} object with the given artifact key and creates an empty event
100 * @param key the event model key
102 public AxEventModel(final AxArtifactKey key) {
103 this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
104 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())),
105 new AxEvents(new AxArtifactKey(key.getName() + "_Events", key.getVersion())));
109 * Constructor that initiates a {@link AxEventModel} with all its fields.
111 * @param key the event model key
112 * @param schemas the schemas for events in the event model
113 * @param keyInformation the key information for context schemas and events in the event model
114 * @param events the events in the event model
116 public AxEventModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxKeyInformation keyInformation,
117 final AxEvents events) {
118 super(key, keyInformation);
119 Assertions.argumentNotNull(events, "events may not be null");
121 this.schemas = schemas;
122 this.events = events;
129 public void register() {
131 ModelService.registerModel(AxContextSchemas.class, getSchemas());
132 ModelService.registerModel(AxEvents.class, getEvents());
139 public List<AxKey> getKeys() {
140 final List<AxKey> keyList = super.getKeys();
142 keyList.addAll(schemas.getKeys());
143 keyList.addAll(events.getKeys());
149 * Gets the context schemas.
151 * @return the context schemas
153 public AxContextSchemas getSchemas() {
158 * Sets the context schemas.
160 * @param schemas the context schemas
162 public void setSchemas(final AxContextSchemas schemas) {
163 Assertions.argumentNotNull(schemas, "schemas may not be null");
164 this.schemas = schemas;
168 * Gets the events from the model.
172 public AxEvents getEvents() {
177 * Sets the events in the model.
179 * @param events the events
181 public void setEvents(final AxEvents events) {
182 Assertions.argumentNotNull(events, "events may not be null");
183 this.events = events;
190 public AxValidationResult validate(final AxValidationResult resultIn) {
191 AxValidationResult result = resultIn;
193 result = super.validate(result);
194 result = schemas.validate(result);
195 return events.validate(result);
202 public void clean() {
212 public String toString() {
213 final StringBuilder builder = new StringBuilder();
214 builder.append(this.getClass().getSimpleName());
215 builder.append(":(");
216 builder.append(super.toString());
217 builder.append(",schemas=");
218 builder.append(schemas);
219 builder.append(",events=");
220 builder.append(events);
222 return builder.toString();
229 public AxConcept copyTo(final AxConcept targetObject) {
230 Assertions.argumentNotNull(targetObject, "target may not be null");
232 final Object copyObject = targetObject;
233 Assertions.instanceOf(copyObject, AxEventModel.class);
235 final AxEventModel copy = ((AxEventModel) copyObject);
236 super.copyTo(targetObject);
237 copy.setSchemas(new AxContextSchemas(schemas));
238 copy.setEvents(new AxEvents(events));
247 public int hashCode() {
248 final int prime = 31;
250 result = prime * result + super.hashCode();
251 result = prime * result + schemas.hashCode();
252 result = prime * result + events.hashCode();
260 public boolean equals(final Object obj) {
262 throw new IllegalArgumentException("comparison object may not be null");
268 if (getClass() != obj.getClass()) {
272 final AxEventModel other = (AxEventModel) obj;
273 if (!super.equals(other)) {
276 if (!schemas.equals(other.schemas)) {
279 return events.equals(other.events);
286 public int compareTo(final AxConcept otherObj) {
287 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
289 if (this == otherObj) {
292 if (getClass() != otherObj.getClass()) {
293 return this.hashCode() - otherObj.hashCode();
296 final AxEventModel other = (AxEventModel) otherObj;
297 if (!super.equals(other)) {
298 return super.compareTo(other);
300 if (!schemas.equals(other.schemas)) {
301 return schemas.compareTo(other.schemas);
303 return events.compareTo(other.events);