3f25b801232e9c92dfdd135b7c549faf9c0acd5b
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / JobAuditStatus.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.model;
22
23 import java.text.DateFormat;
24 import java.text.ParseException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.Locale;
28 import java.util.TimeZone;
29 import java.util.UUID;
30 import javax.persistence.Column;
31 import javax.persistence.Entity;
32 import javax.persistence.EnumType;
33 import javax.persistence.Enumerated;
34 import javax.persistence.GeneratedValue;
35 import javax.persistence.GenerationType;
36 import javax.persistence.Id;
37 import javax.persistence.Table;
38 import javax.persistence.Transient;
39 import org.apache.commons.lang3.StringUtils;
40 import org.apache.commons.lang3.builder.EqualsBuilder;
41 import org.apache.commons.lang3.builder.HashCodeBuilder;
42 import org.hibernate.annotations.DynamicUpdate;
43 import org.hibernate.annotations.SelectBeforeUpdate;
44 import org.hibernate.annotations.Type;
45 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
46 import org.onap.vid.job.Job.JobStatus;
47
48 /*
49  The following 2 annotations let hibernate to update only fields that actually have been changed.
50  DynamicUpdate tell hibernate to update only dirty fields.
51  SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
52  */
53 @DynamicUpdate()
54 @SelectBeforeUpdate()
55 @Entity
56 @Table(name = "vid_job_audit_status")
57 public class JobAuditStatus extends VidBaseEntity {
58
59     public static final int MAX_ADDITIONAL_INFO_LENGTH = 2000;
60     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobAuditStatus.class);
61     private static final String defaultFormat = "E, dd MMM yyyy HH:mm:ss z";
62
63     public JobAuditStatus(){}
64
65     private JobAuditStatus(UUID jobId, String instanceName, String instanceType, String jobStatus,
66                            SourceStatus source, UUID requestId, String additionalInfo, Date date, int ordinal) {
67         this.jobId = jobId;
68         this.instanceName = instanceName;
69         this.instanceType = instanceType;
70         this.jobStatus = jobStatus;
71         this.source = source;
72         this.requestId = requestId;
73         setAdditionalInfo(additionalInfo);
74         this.ordinal = ordinal;
75         this.created = date;
76     }
77
78     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
79         this(jobId, null, null, jobStatus, source, null, null, null, 0);
80     }
81
82     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
83         this(jobId, null, null, jobStatus, source, requestId, additionalInfo, null, 0);
84     }
85
86     public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo, String date, String instanceType) {
87         this(null, instanceName, instanceType, jobStatus, null, requestId, additionalInfo, null, 0);
88         this.created = dateStringToDate(date);
89     }
90
91     public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, Date date, int ordinal) {
92         return new JobAuditStatus(jobId, null, null, jobStatus, source, null, null, date, ordinal);
93     }
94
95     public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date) {
96         return new JobAuditStatus(jobId, null, null, jobStatus, source, requestId, additionalInfo, date, 0);
97     }
98
99     private Date dateStringToDate(String dateAsString){
100         if (StringUtils.isEmpty(dateAsString)) {
101             return null;
102         }
103
104         DateFormat format = new SimpleDateFormat(defaultFormat, Locale.US);
105         format.setTimeZone(TimeZone.getTimeZone("GMT"));
106         Date date = null ;
107         try {
108             date = format.parse(dateAsString);
109         } catch (ParseException e) {
110             logger.error("There was an error to parse the string "+ dateAsString +" to date ", e.getMessage());
111         }
112         return date;
113     }
114
115     @Override
116     public boolean equals(Object o) {
117         if (this == o) return true;
118
119         if (o == null || getClass() != o.getClass()) return false;
120
121         JobAuditStatus that = (JobAuditStatus) o;
122
123         return new EqualsBuilder()
124                 .append(jobId, that.jobId)
125                 .append(jobStatus, that.jobStatus)
126                 .append(source, that.source)
127                 .append(requestId, that.requestId)
128                 .append(additionalInfo, that.additionalInfo)
129                 .append(modelType, that.modelType)
130                 // ordinal is not part of equality (similarly to "created" field)
131                 .isEquals();
132     }
133
134     @Override
135     public int hashCode() {
136         return new HashCodeBuilder(17, 37)
137                 .append(jobId)
138                 .append(jobStatus)
139                 .append(source)
140                 .append(requestId)
141                 .append(additionalInfo)
142                 
143                 // ordinal is not part of equality (similarly to "created" field)
144                 .toHashCode();
145     }
146
147     public enum SourceStatus {
148         MSO,
149         VID
150     }
151
152     public enum ResourceTypeFilter {
153         SERVICE("serviceInstanceId"),
154         VNF("vnfInstanceId"),
155         VFMODULE("vfModuleInstanceId"),
156         NETWORK("networkInstanceId"),
157         VNFGROUP("instanceGroupId");
158
159         private final String filterBy;
160
161         ResourceTypeFilter(String filterBy) {
162             this.filterBy = filterBy;
163         }
164
165         public String getFilterBy() {
166             return filterBy;
167         }
168     }
169
170     public JobAuditStatus(UUID requestId, String instanceName,
171                 String modelType, String instanceType, String startTime,
172                 String finishTime, String jobStatus, String additionalInfo) {
173          this.requestId = requestId;
174          this.instanceName = instanceName;
175          this.modelType = modelType;
176          this.instanceType = instanceType;
177
178          this.startTime = startTime;
179          this.finishTime = finishTime;
180
181          this.jobStatus = jobStatus;
182          this.additionalInfo = additionalInfo;
183          this.created = dateStringToDate(finishTime);
184     }
185     private String modelType;
186     private String startTime;
187     private String finishTime;
188
189     @Transient
190     public String getModelType() {
191         return modelType;
192     }
193
194     public void setModelType(String modelType) {
195         this.modelType = modelType;
196     }
197
198     @Transient
199     public String getFinishTime() {
200         return finishTime;
201     }
202
203     public void setFinishTime(String finishTime) {
204         this.finishTime = finishTime;
205     }
206
207     @Transient
208     public String getStartTime() {
209         return startTime;
210     }
211
212     public void setStartTime(String startTime) {
213         this.startTime = startTime;
214     }
215
216
217     private UUID jobId;
218     private String instanceName;
219     private String instanceType;
220     private String jobStatus;
221     private SourceStatus source;
222     private UUID requestId;
223     private String additionalInfo;
224     private int ordinal;
225
226     @Id
227     @GeneratedValue(strategy = GenerationType.IDENTITY)
228     @Override
229     @Column(name = "ID", columnDefinition = "INT(11)")
230     public Long getId() {
231         return this.id;
232     }
233
234     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
235     @Type(type="org.hibernate.type.UUIDCharType")
236     public UUID getJobId() {
237         return jobId;
238     }
239
240     public void setJobId(UUID jobId) {
241         this.jobId = jobId;
242     }
243
244
245     @Column(name = "JOB_STATUS")
246     public String getJobStatus() {
247         return jobStatus;
248     }
249
250     public void setJobStatus(String jobStatus) {
251         this.jobStatus = jobStatus;
252     }
253
254
255     @Enumerated(EnumType.STRING)
256     @Column(name = "SOURCE")
257     public SourceStatus getSource() {
258         return source;
259     }
260
261     public void setSource(SourceStatus source) {
262         this.source = source;
263     }
264
265     @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
266     @Type(type="org.hibernate.type.UUIDCharType")
267     public UUID getRequestId() {
268         return requestId;
269     }
270
271     public void setRequestId(UUID requestId) {
272         this.requestId = requestId;
273     }
274
275     @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
276     public String getAdditionalInfo() {
277         return additionalInfo;
278     }
279
280     public void setAdditionalInfo(String additionalInfo) {
281         this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
282     }
283
284     @Column(name = "ORDINAL", columnDefinition = "INT")
285     public int getOrdinal() {
286         // Ordinal allows sorting audit statuses by
287         // insertion order, regardless of "created"
288         // field
289         return ordinal;
290     }
291
292     public void setOrdinal(int ordinal) {
293         this.ordinal = ordinal;
294     }
295
296     @Transient
297     public String getInstanceName() {
298         return instanceName;
299     }
300
301     public void setInstanceName(String instanceName) {
302         this.instanceName = instanceName;
303     }
304
305     @Transient
306     public String getInstanceType() {
307         return instanceType;
308     }
309
310     public void setInstanceType(String instanceType) {
311         this.instanceType = instanceType;
312     }
313
314     @Transient
315     public Boolean isFinal(){
316         try {
317             if (getSource() == SourceStatus.VID) {
318                 return JobStatus.valueOf(getJobStatus()).isFinal();
319             }
320         }
321         catch (IllegalArgumentException e){
322             logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
323             return false;
324         }
325         return false;
326     }
327
328     @Transient
329     public Date getCreatedDate() {
330         return getCreated();
331     }
332
333 }