Implant vid-app-common org.onap.vid.job (main and test)
[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.TimeZone;
28 import java.util.UUID;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.EnumType;
32 import javax.persistence.Enumerated;
33 import javax.persistence.GeneratedValue;
34 import javax.persistence.GenerationType;
35 import javax.persistence.Id;
36 import javax.persistence.Table;
37 import javax.persistence.Transient;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.commons.lang3.builder.EqualsBuilder;
40 import org.apache.commons.lang3.builder.HashCodeBuilder;
41 import org.hibernate.annotations.DynamicUpdate;
42 import org.hibernate.annotations.SelectBeforeUpdate;
43 import org.hibernate.annotations.Type;
44 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
45 import org.onap.vid.job.Job.JobStatus;
46
47 /*
48  The following 2 annotations let hibernate to update only fields that actually have been changed.
49  DynamicUpdate tell hibernate to update only dirty fields.
50  SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
51  */
52 @DynamicUpdate()
53 @SelectBeforeUpdate()
54 @Entity
55 @Table(name = "vid_job_audit_status")
56 public class JobAuditStatus extends VidBaseEntity {
57
58     public static final int MAX_ADDITIONAL_INFO_LENGTH = 2000;
59     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobAuditStatus.class);
60     private static final String defaultFormat = "E, dd MMM yyyy HH:mm:ss z";
61
62     public JobAuditStatus(){}
63
64     private JobAuditStatus(UUID jobId, String instanceName, String instanceType, String jobStatus,
65                            SourceStatus source, UUID requestId, String additionalInfo, Date date, int ordinal) {
66         this.jobId = jobId;
67         this.instanceName = instanceName;
68         this.instanceType = instanceType;
69         this.jobStatus = jobStatus;
70         this.source = source;
71         this.requestId = requestId;
72         setAdditionalInfo(additionalInfo);
73         this.ordinal = ordinal;
74         this.created = date;
75     }
76
77     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
78         this(jobId, null, null, jobStatus, source, null, null, null, 0);
79     }
80
81     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
82         this(jobId, null, null, jobStatus, source, requestId, additionalInfo, null, 0);
83     }
84
85     public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo, String date, String instanceType) {
86         this(null, instanceName, instanceType, jobStatus, null, requestId, additionalInfo, null, 0);
87         this.created = dateStringToDate(date);
88     }
89
90     public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, Date date, int ordinal) {
91         return new JobAuditStatus(jobId, null, null, jobStatus, source, null, null, date, ordinal);
92     }
93
94     public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date) {
95         return new JobAuditStatus(jobId, null, null, jobStatus, source, requestId, additionalInfo, date, 0);
96     }
97
98     private Date dateStringToDate(String dateAsString){
99         if (StringUtils.isEmpty(dateAsString)) {
100             return null;
101         }
102
103         DateFormat format = new SimpleDateFormat(defaultFormat);
104         format.setTimeZone(TimeZone.getTimeZone("GMT"));
105         Date date = null ;
106         try {
107             date = format.parse(dateAsString);
108         } catch (ParseException e) {
109             logger.error("There was an error to parse the string "+ dateAsString +" to date ", e.getMessage());
110         }
111         return date;
112     }
113
114     @Override
115     public boolean equals(Object o) {
116         if (this == o) return true;
117
118         if (o == null || getClass() != o.getClass()) return false;
119
120         JobAuditStatus that = (JobAuditStatus) o;
121
122         return new EqualsBuilder()
123                 .append(jobId, that.jobId)
124                 .append(jobStatus, that.jobStatus)
125                 .append(source, that.source)
126                 .append(requestId, that.requestId)
127                 .append(additionalInfo, that.additionalInfo)
128                 // ordinal is not part of equality (similarly to "created" field)
129                 .isEquals();
130     }
131
132     @Override
133     public int hashCode() {
134         return new HashCodeBuilder(17, 37)
135                 .append(jobId)
136                 .append(jobStatus)
137                 .append(source)
138                 .append(requestId)
139                 .append(additionalInfo)
140                 // ordinal is not part of equality (similarly to "created" field)
141                 .toHashCode();
142     }
143
144     public enum SourceStatus {
145         MSO,
146         VID
147     }
148
149     public enum ResourceTypeFilter {
150         SERVICE("serviceInstanceId"),
151         VNF("vnfInstanceId"),
152         VFMODULE("vfModuleInstanceId"),
153         NETWORK("networkInstanceId"),
154         VNFGROUP("instanceGroupId");
155
156         private final String filterBy;
157
158         ResourceTypeFilter(String filterBy) {
159             this.filterBy = filterBy;
160         }
161
162         public String getFilterBy() {
163             return filterBy;
164         }
165     }
166
167
168     private UUID jobId;
169     private String instanceName;
170     private String instanceType;
171     private String jobStatus;
172     private SourceStatus source;
173     private UUID requestId;
174     private String additionalInfo;
175     private int ordinal;
176
177     @Id
178     @GeneratedValue(strategy = GenerationType.IDENTITY)
179     @Override
180     @Column(name = "ID", columnDefinition = "INT(11)")
181     public Long getId() {
182         return this.id;
183     }
184
185     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
186     @Type(type="org.hibernate.type.UUIDCharType")
187     public UUID getJobId() {
188         return jobId;
189     }
190
191     public void setJobId(UUID jobId) {
192         this.jobId = jobId;
193     }
194
195
196     @Column(name = "JOB_STATUS")
197     public String getJobStatus() {
198         return jobStatus;
199     }
200
201     public void setJobStatus(String jobStatus) {
202         this.jobStatus = jobStatus;
203     }
204
205
206     @Enumerated(EnumType.STRING)
207     @Column(name = "SOURCE")
208     public SourceStatus getSource() {
209         return source;
210     }
211
212     public void setSource(SourceStatus source) {
213         this.source = source;
214     }
215
216     @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
217     @Type(type="org.hibernate.type.UUIDCharType")
218     public UUID getRequestId() {
219         return requestId;
220     }
221
222     public void setRequestId(UUID requestId) {
223         this.requestId = requestId;
224     }
225
226     @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
227     public String getAdditionalInfo() {
228         return additionalInfo;
229     }
230
231     public void setAdditionalInfo(String additionalInfo) {
232         this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
233     }
234
235     @Column(name = "ORDINAL", columnDefinition = "INT")
236     public int getOrdinal() {
237         // Ordinal allows sorting audit statuses by
238         // insertion order, regardless of "created"
239         // field
240         return ordinal;
241     }
242
243     public void setOrdinal(int ordinal) {
244         this.ordinal = ordinal;
245     }
246
247     @Transient
248     public String getInstanceName() {
249         return instanceName;
250     }
251
252     public void setInstanceName(String instanceName) {
253         this.instanceName = instanceName;
254     }
255
256     @Transient
257     public String getInstanceType() {
258         return instanceType;
259     }
260
261     public void setInstanceType(String instanceType) {
262         this.instanceType = instanceType;
263     }
264
265     @Transient
266     public Boolean isFinal(){
267         try {
268             if (getSource() == SourceStatus.VID) {
269                 return JobStatus.valueOf(getJobStatus()).isFinal();
270             }
271         }
272         catch (IllegalArgumentException e){
273             logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
274             return false;
275         }
276         return false;
277     }
278
279     @Transient
280     public Date getCreatedDate() {
281         return getCreated();
282     }
283
284 }