2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.model;
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;
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)
55 @Table(name = "vid_job_audit_status")
56 public class JobAuditStatus extends VidBaseEntity {
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";
62 public JobAuditStatus(){}
64 private JobAuditStatus(UUID jobId, String instanceName, String instanceType, String jobStatus,
65 SourceStatus source, UUID requestId, String additionalInfo, Date date, int ordinal) {
67 this.instanceName = instanceName;
68 this.instanceType = instanceType;
69 this.jobStatus = jobStatus;
71 this.requestId = requestId;
72 setAdditionalInfo(additionalInfo);
73 this.ordinal = ordinal;
77 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
78 this(jobId, null, null, jobStatus, source, null, null, null, 0);
81 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
82 this(jobId, null, null, jobStatus, source, requestId, additionalInfo, null, 0);
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);
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);
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);
98 private Date dateStringToDate(String dateAsString){
99 if (StringUtils.isEmpty(dateAsString)) {
103 DateFormat format = new SimpleDateFormat(defaultFormat);
104 format.setTimeZone(TimeZone.getTimeZone("GMT"));
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());
115 public boolean equals(Object o) {
116 if (this == o) return true;
118 if (o == null || getClass() != o.getClass()) return false;
120 JobAuditStatus that = (JobAuditStatus) o;
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)
133 public int hashCode() {
134 return new HashCodeBuilder(17, 37)
139 .append(additionalInfo)
140 // ordinal is not part of equality (similarly to "created" field)
144 public enum SourceStatus {
149 public enum ResourceTypeFilter {
150 SERVICE("serviceInstanceId"),
151 VNF("vnfInstanceId"),
152 VFMODULE("vfModuleInstanceId"),
153 NETWORK("networkInstanceId"),
154 VNFGROUP("instanceGroupId");
156 private final String filterBy;
158 ResourceTypeFilter(String filterBy) {
159 this.filterBy = filterBy;
162 public String getFilterBy() {
169 private String instanceName;
170 private String instanceType;
171 private String jobStatus;
172 private SourceStatus source;
173 private UUID requestId;
174 private String additionalInfo;
178 @GeneratedValue(strategy = GenerationType.IDENTITY)
180 @Column(name = "ID", columnDefinition = "INT(11)")
181 public Long getId() {
185 @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
186 @Type(type="org.hibernate.type.UUIDCharType")
187 public UUID getJobId() {
191 public void setJobId(UUID jobId) {
196 @Column(name = "JOB_STATUS")
197 public String getJobStatus() {
201 public void setJobStatus(String jobStatus) {
202 this.jobStatus = jobStatus;
206 @Enumerated(EnumType.STRING)
207 @Column(name = "SOURCE")
208 public SourceStatus getSource() {
212 public void setSource(SourceStatus source) {
213 this.source = source;
216 @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
217 @Type(type="org.hibernate.type.UUIDCharType")
218 public UUID getRequestId() {
222 public void setRequestId(UUID requestId) {
223 this.requestId = requestId;
226 @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
227 public String getAdditionalInfo() {
228 return additionalInfo;
231 public void setAdditionalInfo(String additionalInfo) {
232 this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
235 @Column(name = "ORDINAL", columnDefinition = "INT")
236 public int getOrdinal() {
237 // Ordinal allows sorting audit statuses by
238 // insertion order, regardless of "created"
243 public void setOrdinal(int ordinal) {
244 this.ordinal = ordinal;
248 public String getInstanceName() {
252 public void setInstanceName(String instanceName) {
253 this.instanceName = instanceName;
257 public String getInstanceType() {
261 public void setInstanceType(String instanceType) {
262 this.instanceType = instanceType;
266 public Boolean isFinal(){
268 if (getSource() == SourceStatus.VID) {
269 return JobStatus.valueOf(getJobStatus()).isFinal();
272 catch (IllegalArgumentException e){
273 logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
280 public Date getCreatedDate() {