0c51c0c1f674c35ca039f2463635e8b1f3ab9ccc
[clamp.git] / src / main / java / org / onap / clamp / loop / log / LoopLog.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.log;
25
26 import com.google.gson.annotations.Expose;
27
28 import java.io.Serializable;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.EnumType;
35 import javax.persistence.Enumerated;
36 import javax.persistence.FetchType;
37 import javax.persistence.GeneratedValue;
38 import javax.persistence.GenerationType;
39 import javax.persistence.Id;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.ManyToOne;
42 import javax.persistence.Table;
43 import org.onap.clamp.loop.Loop;
44
45 /**
46  * This class holds the logs created by the Clamp Backend. The Instant is always
47  * rounded to the nearest second as the nano seconds can't be stored in the
48  * database. The logs can be therefore exposed to the UI or the client doing
49  * some GET Loop on the backend.
50  *
51  */
52 @Entity
53 @Table(name = "loop_logs")
54 public class LoopLog implements Serializable {
55     /**
56      * The serial version ID.
57      */
58     private static final long serialVersionUID = 1988276670074437631L;
59
60     @Expose
61     @Id
62     @GeneratedValue(strategy = GenerationType.AUTO)
63     private Long id;
64
65     @Expose
66     @Column(name = "log_type", nullable = false)
67     @Enumerated(EnumType.STRING)
68     private LogType logType;
69
70     @Expose
71     @Column(name = "message", nullable = false)
72     private String message;
73
74     @ManyToOne(fetch = FetchType.LAZY)
75     @JoinColumn(name = "loop_id", nullable = false)
76     private Loop loop;
77
78     @Expose
79     @Column(name = "log_instant", nullable = false)
80     private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS);
81
82     public Long getId() {
83         return id;
84     }
85
86     public void setId(Long id) {
87         this.id = id;
88     }
89
90     public LogType getLogType() {
91         return logType;
92     }
93
94     public void setLogType(LogType logType) {
95         this.logType = logType;
96     }
97
98     public String getMessage() {
99         return message;
100     }
101
102     public void setMessage(String message) {
103         this.message = message;
104     }
105
106     public Loop getLoop() {
107         return loop;
108     }
109
110     public void setLoop(Loop loop) {
111         this.loop = loop;
112     }
113
114     public Instant getLogInstant() {
115         return logInstant;
116     }
117
118     public void setLogInstant(Instant logInstant) {
119         this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS);
120     }
121
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = 1;
126         result = prime * result + ((id == null) ? 0 : id.hashCode());
127         return result;
128     }
129
130     @Override
131     public boolean equals(Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (obj == null) {
136             return false;
137         }
138         if (getClass() != obj.getClass()) {
139             return false;
140         }
141         LoopLog other = (LoopLog) obj;
142         if (id == null) {
143             if (other.id != null) {
144                 return false;
145             }
146         } else if (!id.equals(other.id)) {
147             return false;
148         }
149         return true;
150     }
151
152 }