aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/model/JobAuditStatus.java
blob: 3f25b801232e9c92dfdd135b7c549faf9c0acd5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.hibernate.annotations.Type;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.job.Job.JobStatus;

/*
 The following 2 annotations let hibernate to update only fields that actually have been changed.
 DynamicUpdate tell hibernate to update only dirty fields.
 SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
 */
@DynamicUpdate()
@SelectBeforeUpdate()
@Entity
@Table(name = "vid_job_audit_status")
public class JobAuditStatus extends VidBaseEntity {

    public static final int MAX_ADDITIONAL_INFO_LENGTH = 2000;
    static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobAuditStatus.class);
    private static final String defaultFormat = "E, dd MMM yyyy HH:mm:ss z";

    public JobAuditStatus(){}

    private JobAuditStatus(UUID jobId, String instanceName, String instanceType, String jobStatus,
                           SourceStatus source, UUID requestId, String additionalInfo, Date date, int ordinal) {
        this.jobId = jobId;
        this.instanceName = instanceName;
        this.instanceType = instanceType;
        this.jobStatus = jobStatus;
        this.source = source;
        this.requestId = requestId;
        setAdditionalInfo(additionalInfo);
        this.ordinal = ordinal;
        this.created = date;
    }

    public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
        this(jobId, null, null, jobStatus, source, null, null, null, 0);
    }

    public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
        this(jobId, null, null, jobStatus, source, requestId, additionalInfo, null, 0);
    }

    public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo, String date, String instanceType) {
        this(null, instanceName, instanceType, jobStatus, null, requestId, additionalInfo, null, 0);
        this.created = dateStringToDate(date);
    }

    public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, Date date, int ordinal) {
        return new JobAuditStatus(jobId, null, null, jobStatus, source, null, null, date, ordinal);
    }

    public static JobAuditStatus createForTest(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date) {
        return new JobAuditStatus(jobId, null, null, jobStatus, source, requestId, additionalInfo, date, 0);
    }

    private Date dateStringToDate(String dateAsString){
        if (StringUtils.isEmpty(dateAsString)) {
            return null;
        }

        DateFormat format = new SimpleDateFormat(defaultFormat, Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = null ;
        try {
            date = format.parse(dateAsString);
        } catch (ParseException e) {
            logger.error("There was an error to parse the string "+ dateAsString +" to date ", e.getMessage());
        }
        return date;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        JobAuditStatus that = (JobAuditStatus) o;

        return new EqualsBuilder()
                .append(jobId, that.jobId)
                .append(jobStatus, that.jobStatus)
                .append(source, that.source)
                .append(requestId, that.requestId)
                .append(additionalInfo, that.additionalInfo)
                .append(modelType, that.modelType)
                // ordinal is not part of equality (similarly to "created" field)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(jobId)
                .append(jobStatus)
                .append(source)
                .append(requestId)
                .append(additionalInfo)
                
                // ordinal is not part of equality (similarly to "created" field)
                .toHashCode();
    }

    public enum SourceStatus {
        MSO,
        VID
    }

    public enum ResourceTypeFilter {
        SERVICE("serviceInstanceId"),
        VNF("vnfInstanceId"),
        VFMODULE("vfModuleInstanceId"),
        NETWORK("networkInstanceId"),
        VNFGROUP("instanceGroupId");

        private final String filterBy;

        ResourceTypeFilter(String filterBy) {
            this.filterBy = filterBy;
        }

        public String getFilterBy() {
            return filterBy;
        }
    }

    public JobAuditStatus(UUID requestId, String instanceName,
                String modelType, String instanceType, String startTime,
                String finishTime, String jobStatus, String additionalInfo) {
         this.requestId = requestId;
         this.instanceName = instanceName;
         this.modelType = modelType;
         this.instanceType = instanceType;

         this.startTime = startTime;
         this.finishTime = finishTime;

         this.jobStatus = jobStatus;
         this.additionalInfo = additionalInfo;
         this.created = dateStringToDate(finishTime);
    }
    private String modelType;
    private String startTime;
    private String finishTime;

    @Transient
    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    @Transient
    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    @Transient
    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }


    private UUID jobId;
    private String instanceName;
    private String instanceType;
    private String jobStatus;
    private SourceStatus source;
    private UUID requestId;
    private String additionalInfo;
    private int ordinal;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Override
    @Column(name = "ID", columnDefinition = "INT(11)")
    public Long getId() {
        return this.id;
    }

    @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
    @Type(type="org.hibernate.type.UUIDCharType")
    public UUID getJobId() {
        return jobId;
    }

    public void setJobId(UUID jobId) {
        this.jobId = jobId;
    }


    @Column(name = "JOB_STATUS")
    public String getJobStatus() {
        return jobStatus;
    }

    public void setJobStatus(String jobStatus) {
        this.jobStatus = jobStatus;
    }


    @Enumerated(EnumType.STRING)
    @Column(name = "SOURCE")
    public SourceStatus getSource() {
        return source;
    }

    public void setSource(SourceStatus source) {
        this.source = source;
    }

    @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
    @Type(type="org.hibernate.type.UUIDCharType")
    public UUID getRequestId() {
        return requestId;
    }

    public void setRequestId(UUID requestId) {
        this.requestId = requestId;
    }

    @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
    public String getAdditionalInfo() {
        return additionalInfo;
    }

    public void setAdditionalInfo(String additionalInfo) {
        this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
    }

    @Column(name = "ORDINAL", columnDefinition = "INT")
    public int getOrdinal() {
        // Ordinal allows sorting audit statuses by
        // insertion order, regardless of "created"
        // field
        return ordinal;
    }

    public void setOrdinal(int ordinal) {
        this.ordinal = ordinal;
    }

    @Transient
    public String getInstanceName() {
        return instanceName;
    }

    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

    @Transient
    public String getInstanceType() {
        return instanceType;
    }

    public void setInstanceType(String instanceType) {
        this.instanceType = instanceType;
    }

    @Transient
    public Boolean isFinal(){
        try {
            if (getSource() == SourceStatus.VID) {
                return JobStatus.valueOf(getJobStatus()).isFinal();
            }
        }
        catch (IllegalArgumentException e){
            logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
            return false;
        }
        return false;
    }

    @Transient
    public Date getCreatedDate() {
        return getCreated();
    }

}