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