097803304d192e8050d2f63d0eb9822856699fd2
[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 instanceId, 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.instanceId = instanceId;
182          this.jobStatus = jobStatus;
183          this.additionalInfo = additionalInfo;
184          this.created = dateStringToDate(finishTime);
185     }
186     private String modelType;
187     private String startTime;
188     private String finishTime;
189         
190          @Transient
191     public String getInstanceId() {
192         return instanceId;
193     }
194
195     public void setInstanceId(String instanceId) {
196         this.instanceId = instanceId;
197     }
198
199     private String instanceId;
200
201     @Transient
202     public String getModelType() {
203         return modelType;
204     }
205
206     public void setModelType(String modelType) {
207         this.modelType = modelType;
208     }
209
210     @Transient
211     public String getFinishTime() {
212         return finishTime;
213     }
214
215     public void setFinishTime(String finishTime) {
216         this.finishTime = finishTime;
217     }
218
219     @Transient
220     public String getStartTime() {
221         return startTime;
222     }
223
224     public void setStartTime(String startTime) {
225         this.startTime = startTime;
226     }
227
228
229     private UUID jobId;
230     private String instanceName;
231     private String instanceType;
232     private String jobStatus;
233     private SourceStatus source;
234     private UUID requestId;
235     private String additionalInfo;
236     private int ordinal;
237
238     @Id
239     @GeneratedValue(strategy = GenerationType.IDENTITY)
240     @Override
241     @Column(name = "ID", columnDefinition = "INT(11)")
242     public Long getId() {
243         return this.id;
244     }
245
246     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
247     @Type(type="org.hibernate.type.UUIDCharType")
248     public UUID getJobId() {
249         return jobId;
250     }
251
252     public void setJobId(UUID jobId) {
253         this.jobId = jobId;
254     }
255
256
257     @Column(name = "JOB_STATUS")
258     public String getJobStatus() {
259         return jobStatus;
260     }
261
262     public void setJobStatus(String jobStatus) {
263         this.jobStatus = jobStatus;
264     }
265
266
267     @Enumerated(EnumType.STRING)
268     @Column(name = "SOURCE")
269     public SourceStatus getSource() {
270         return source;
271     }
272
273     public void setSource(SourceStatus source) {
274         this.source = source;
275     }
276
277     @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
278     @Type(type="org.hibernate.type.UUIDCharType")
279     public UUID getRequestId() {
280         return requestId;
281     }
282
283     public void setRequestId(UUID requestId) {
284         this.requestId = requestId;
285     }
286
287     @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
288     public String getAdditionalInfo() {
289         return additionalInfo;
290     }
291
292     public void setAdditionalInfo(String additionalInfo) {
293         this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
294     }
295
296     @Column(name = "ORDINAL", columnDefinition = "INT")
297     public int getOrdinal() {
298         // Ordinal allows sorting audit statuses by
299         // insertion order, regardless of "created"
300         // field
301         return ordinal;
302     }
303
304     public void setOrdinal(int ordinal) {
305         this.ordinal = ordinal;
306     }
307
308     @Transient
309     public String getInstanceName() {
310         return instanceName;
311     }
312
313     public void setInstanceName(String instanceName) {
314         this.instanceName = instanceName;
315     }
316
317     @Transient
318     public String getInstanceType() {
319         return instanceType;
320     }
321
322     public void setInstanceType(String instanceType) {
323         this.instanceType = instanceType;
324     }
325
326     @Transient
327     public Boolean isFinal(){
328         try {
329             if (getSource() == SourceStatus.VID) {
330                 return JobStatus.valueOf(getJobStatus()).isFinal();
331             }
332         }
333         catch (IllegalArgumentException e){
334             logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
335             return false;
336         }
337         return false;
338     }
339
340     @Transient
341     public Date getCreatedDate() {
342         return getCreated();
343     }
344
345 }