6c8ce15f13758f3bd6f883001aef8ef107154d2b
[policy/apex-pdp.git] / model / event-model / src / main / java / org / onap / policy / apex / model / eventmodel / concepts / AxEventModel.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.eventmodel.concepts;
23
24 import java.util.List;
25
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;
37
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;
47
48 /**
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.
52  *
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.
55  */
56
57 @Entity
58 @Table(name = "AxEventModel")
59
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" })
63
64 public class AxEventModel extends AxModel {
65     private static final long serialVersionUID = 8800599637708309945L;
66
67     // @formatter:off
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;
73
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;
79     // @formatter:on
80
81     /**
82      * The Default Constructor creates a {@link AxEventModel} object with a null artifact key and creates an empty event
83      * model.
84      */
85     public AxEventModel() {
86         this(new AxArtifactKey());
87     }
88
89     /**
90      * Copy constructor.
91      *
92      * @param copyConcept the concept to copy from
93      */
94     public AxEventModel(final AxEventModel copyConcept) {
95         super(copyConcept);
96     }
97
98     /**
99      * The Key Constructor creates a {@link AxEventModel} object with the given artifact key and creates an empty event
100      * model.
101      *
102      * @param key the event model key
103      */
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())));
108     }
109
110     /**
111      * Constructor that initiates a {@link AxEventModel} with all its fields.
112      *
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
117      */
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");
122
123         this.schemas = schemas;
124         this.events = events;
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public void register() {
132         super.register();
133         ModelService.registerModel(AxContextSchemas.class, getSchemas());
134         ModelService.registerModel(AxEvents.class, getEvents());
135     }
136
137     /**
138      * {@inheritDoc}.
139      */
140     @Override
141     public List<AxKey> getKeys() {
142         final List<AxKey> keyList = super.getKeys();
143
144         keyList.addAll(schemas.getKeys());
145         keyList.addAll(events.getKeys());
146
147         return keyList;
148     }
149
150     /**
151      * Gets the context schemas.
152      *
153      * @return the context schemas
154      */
155     public AxContextSchemas getSchemas() {
156         return schemas;
157     }
158
159     /**
160      * Sets the context schemas.
161      *
162      * @param schemas the context schemas
163      */
164     public void setSchemas(final AxContextSchemas schemas) {
165         Assertions.argumentNotNull(schemas, "schemas may not be null");
166         this.schemas = schemas;
167     }
168
169     /**
170      * Gets the events from the model.
171      *
172      * @return the events
173      */
174     public AxEvents getEvents() {
175         return events;
176     }
177
178     /**
179      * Sets the events in the model.
180      *
181      * @param events the events
182      */
183     public void setEvents(final AxEvents events) {
184         Assertions.argumentNotNull(events, "events may not be null");
185         this.events = events;
186     }
187
188     /**
189      * {@inheritDoc}.
190      */
191     @Override
192     public AxValidationResult validate(final AxValidationResult resultIn) {
193         AxValidationResult result = resultIn;
194
195         result = super.validate(result);
196         result = schemas.validate(result);
197         return events.validate(result);
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public void clean() {
205         super.clean();
206         schemas.clean();
207         events.clean();
208     }
209
210     /**
211      * {@inheritDoc}.
212      */
213     @Override
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);
223         builder.append(")");
224         return builder.toString();
225     }
226
227     /**
228      * {@inheritDoc}.
229      */
230     @Override
231     public AxConcept copyTo(final AxConcept targetObject) {
232         Assertions.argumentNotNull(targetObject, "target may not be null");
233
234         final Object copyObject = targetObject;
235         Assertions.instanceOf(copyObject, AxEventModel.class);
236
237         final AxEventModel copy = ((AxEventModel) copyObject);
238         super.copyTo(targetObject);
239         copy.setSchemas(new AxContextSchemas(schemas));
240         copy.setEvents(new AxEvents(events));
241
242         return copy;
243     }
244
245     /**
246      * {@inheritDoc}.
247      */
248     @Override
249     public int hashCode() {
250         final int prime = 31;
251         int result = 1;
252         result = prime * result + super.hashCode();
253         result = prime * result + schemas.hashCode();
254         result = prime * result + events.hashCode();
255         return result;
256     }
257
258     /**
259      * {@inheritDoc}.
260      */
261     @Override
262     public boolean equals(final Object obj) {
263         if (obj == null) {
264             throw new IllegalArgumentException("comparison object may not be null");
265         }
266
267         if (this == obj) {
268             return true;
269         }
270         if (getClass() != obj.getClass()) {
271             return false;
272         }
273
274         final AxEventModel other = (AxEventModel) obj;
275         if (!super.equals(other)) {
276             return false;
277         }
278         if (!schemas.equals(other.schemas)) {
279             return false;
280         }
281         return events.equals(other.events);
282     }
283
284     /**
285      * {@inheritDoc}.
286      */
287     @Override
288     public int compareTo(final AxConcept otherObj) {
289         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
290
291         if (this == otherObj) {
292             return 0;
293         }
294         if (getClass() != otherObj.getClass()) {
295             return this.hashCode() - otherObj.hashCode();
296         }
297
298         final AxEventModel other = (AxEventModel) otherObj;
299         if (!super.equals(other)) {
300             return super.compareTo(other);
301         }
302         if (!schemas.equals(other.schemas)) {
303             return schemas.compareTo(other.schemas);
304         }
305         return events.compareTo(other.events);
306     }
307 }