Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / ResourceInfo.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 static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import java.io.IOException;
27 import java.util.Objects;
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.Id;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36 import org.hibernate.annotations.DynamicUpdate;
37 import org.hibernate.annotations.SelectBeforeUpdate;
38 import org.hibernate.annotations.Type;
39 import org.onap.vid.exceptions.GenericUncheckedException;
40 import org.onap.vid.job.Job;
41 import org.onap.vid.job.JobException;
42 import org.onap.vid.mso.rest.AsyncRequestStatus;
43
44 @DynamicUpdate
45 @SelectBeforeUpdate
46 @Entity
47 @Table(name = "vid_resource_info")
48 public class ResourceInfo extends VidBaseEntity {
49
50     private String trackById;
51     private UUID rootJobId;
52     private String instanceId;
53     private Job.JobStatus jobStatus;
54     private AsyncRequestStatus errorMessage;
55
56     public ResourceInfo(){}
57
58     public ResourceInfo(String trackById, UUID rootJobId, String instanceId, Job.JobStatus jobStatus, AsyncRequestStatus errorMessage) {
59         this.trackById = trackById;
60         this.rootJobId = rootJobId;
61         this.instanceId = instanceId;
62         this.jobStatus = jobStatus;
63         this.errorMessage = errorMessage;
64     }
65
66     @Id
67     @Column(name = "TRACK_BY_ID", columnDefinition = "CHAR(36)")
68     public String getTrackById() {
69         return trackById;
70     }
71
72     @Column(name = "ROOT_JOB_ID", columnDefinition = "CHAR(36)")
73     @Type(type="org.hibernate.type.UUIDCharType")
74     public UUID getRootJobId() {
75         return rootJobId;
76     }
77
78     @Column(name="JOB_STATUS")
79     @Enumerated(EnumType.STRING)
80     public Job.JobStatus getJobStatus() {
81         return jobStatus;
82     }
83
84     @Column(name="INSTANCE_ID")
85     public String getInstanceId() {
86         return instanceId;
87     }
88
89     public void setTrackById(String trackById) {
90         this.trackById = trackById;
91     }
92
93     public void setRootJobId(UUID rootJobId) {
94         this.rootJobId = rootJobId;
95     }
96
97     public void setInstanceId(String instanceId) {
98         this.instanceId = instanceId;
99     }
100     public void setJobStatus(Job.JobStatus jobStatus) {
101         this.jobStatus = jobStatus;
102     }
103
104     @Column(name = "ERROR_MESSAGE", columnDefinition = "VARCHAR(30000)")
105     public String getErrorMessageRaw() {
106         try {
107             return JACKSON_OBJECT_MAPPER.writeValueAsString(errorMessage);
108         } catch (JsonProcessingException e) {
109             throw new GenericUncheckedException(e);
110         }
111     }
112
113     public void setErrorMessageRaw(String failedMessage) {
114         try {
115             this.errorMessage = JACKSON_OBJECT_MAPPER.readValue(failedMessage, AsyncRequestStatus.class);
116         } catch (IOException e) {
117             throw new JobException("Error parsing mso failed message", rootJobId, e);
118         }
119     }
120
121     @Transient
122     public AsyncRequestStatus getErrorMessage() {
123         return this.errorMessage;
124     }
125
126     public void setErrorMessage(AsyncRequestStatus errorMessage) {
127         this.errorMessage = errorMessage;
128     }
129
130     @Override
131     public boolean equals(Object o) {
132         if (this == o) return true;
133         if (o == null || getClass() != o.getClass()) return false;
134         ResourceInfo that = (ResourceInfo) o;
135         return Objects.equals(trackById, that.trackById) &&
136                 Objects.equals(instanceId, that.instanceId) &&
137                 jobStatus == that.jobStatus;
138     }
139
140     @Override
141     public int hashCode() {
142         return Objects.hash(trackById, instanceId, jobStatus);
143     }
144 }