Add template and tosca model entities and repositories
[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
44 import org.onap.clamp.loop.Loop;
45
46 /**
47  * This class holds the logs created by the Clamp Backend. The Instant is always
48  * rounded to the nearest second as the nano seconds can't be stored in the
49  * database. The logs can be therefore exposed to the UI or the client doing
50  * some GET Loop on the backend.
51  *
52  */
53 @Entity
54 @Table(name = "loop_logs")
55 public class LoopLog implements Serializable, Comparable<LoopLog> {
56     /**
57      * The serial version ID.
58      */
59     private static final long serialVersionUID = 1988276670074437631L;
60
61     @Expose
62     @Id
63     @GeneratedValue(strategy = GenerationType.AUTO)
64     private Long id;
65
66     @Expose
67     @Column(name = "log_type", nullable = false)
68     @Enumerated(EnumType.STRING)
69     private LogType logType;
70
71     @Expose
72     @Column(name = "log_component", nullable = false)
73     private String logComponent;
74
75     @Expose
76     @Column(name = "message", columnDefinition = "MEDIUMTEXT", nullable = false)
77     private String message;
78
79     @ManyToOne(fetch = FetchType.LAZY)
80     @JoinColumn(name = "loop_id", nullable = false)
81     private Loop loop;
82
83     @Expose
84     @Column(name = "log_instant", nullable = false)
85     private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS);
86
87     public LoopLog() {
88     }
89
90     /**
91      * Constructor For LoopLog taking message and logtype, logComponent and loop
92      * reference.
93      * 
94      * @param message      The message as string
95      * @param logType      Type like INFO, WARN, DEBUG
96      * @param logComponent A String with DCAE, POLICY, CLAMP ,etc...
97      * @param loop         The loop object that this log is about
98      */
99     public LoopLog(String message, LogType logType, String logComponent, Loop loop) {
100         this.message = message;
101         this.logType = logType;
102         this.loop = loop;
103         this.logComponent = logComponent;
104     }
105
106     public Long getId() {
107         return id;
108     }
109
110     public void setId(Long id) {
111         this.id = id;
112     }
113
114     public LogType getLogType() {
115         return logType;
116     }
117
118     public void setLogType(LogType logType) {
119         this.logType = logType;
120     }
121
122     public String getMessage() {
123         return message;
124     }
125
126     public void setMessage(String message) {
127         this.message = message;
128     }
129
130     public Loop getLoop() {
131         return loop;
132     }
133
134     public void setLoop(Loop loop) {
135         this.loop = loop;
136     }
137
138     public Instant getLogInstant() {
139         return logInstant;
140     }
141
142     public void setLogInstant(Instant logInstant) {
143         this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS);
144     }
145
146     public String getLogComponent() {
147         return logComponent;
148     }
149
150     public void setLogComponent(String logComponent) {
151         this.logComponent = logComponent;
152     }
153
154     @Override
155     public int hashCode() {
156         final int prime = 31;
157         int result = 1;
158         result = prime * result + ((id == null) ? 0 : id.hashCode());
159         return result;
160     }
161
162     @Override
163     public boolean equals(Object obj) {
164         if (this == obj) {
165             return true;
166         }
167         if (obj == null) {
168             return false;
169         }
170         if (getClass() != obj.getClass()) {
171             return false;
172         }
173         LoopLog other = (LoopLog) obj;
174         if (id == null) {
175             if (other.id != null) {
176                 return false;
177             }
178         } else if (!id.equals(other.id)) {
179             return false;
180         }
181         return true;
182     }
183
184     @Override
185     public int compareTo(LoopLog arg0) {
186         // Reverse it, so that by default we have the latest
187         if (getId() == null) {
188             return 1;
189         }
190         if (arg0.getId() == null) {
191             return -1;
192         }
193         return arg0.getId().compareTo(this.getId());
194     }
195
196 }