Enabling Code Formatter
[ccsdk/cds.git] / ms / error-catalog / core / src / main / kotlin / org / onap / ccsdk / cds / error / catalog / core / ErrorLibData.kt
1 /*
2  *  Copyright © 2020 IBM, Bell Canada.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.error.catalog.core
18
19 import com.fasterxml.jackson.annotation.JsonFormat
20 import org.onap.ccsdk.cds.error.catalog.core.ErrorMessageLibConstants.ERROR_CATALOG_DEFAULT_ERROR_CODE
21 import org.slf4j.event.Level
22 import java.time.LocalDateTime
23 import java.time.ZoneId
24 import java.util.Date
25
26 open class ErrorPayload {
27
28     var code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE
29     var status: String = ""
30
31     @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
32     var timestamp: Date = controllerDate()
33     var message: String = ""
34     var debugMessage: String = ""
35     var logLevel: String = Level.ERROR.name
36     val subErrors: ArrayList<ErrorMessage> = ArrayList()
37
38     constructor()
39
40     constructor(
41         code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE,
42         status: String,
43         message: String,
44         logLevel: String = Level.ERROR.name,
45         debugMessage: String = ""
46     ) {
47         this.code = code
48         this.status = status
49         this.message = message
50         this.logLevel = logLevel
51         this.debugMessage = debugMessage
52     }
53
54     constructor(
55         code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE,
56         status: String,
57         message: String,
58         logLevel: String = Level.ERROR.name,
59         debugMessage: String = "",
60         errorMessage: ErrorMessage
61     ) {
62         this.code = code
63         this.status = status
64         this.message = message
65         this.logLevel = logLevel
66         this.debugMessage = debugMessage
67         this.subErrors.add(errorMessage)
68     }
69
70     fun isEqualTo(errorPayload: ErrorPayload): Boolean {
71         return (
72             this.code == errorPayload.code && this.status == errorPayload.status && this.message == errorPayload.message &&
73                 this.logLevel == errorPayload.logLevel && this.debugMessage == errorPayload.debugMessage &&
74                 this.subErrors == errorPayload.subErrors
75             )
76     }
77
78     private fun controllerDate(): Date {
79         val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
80         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
81     }
82 }
83
84 /**
85  *
86  *
87  * @author Steve Siani
88  */
89 data class ErrorMessage(
90     val domainId: String,
91     val message: String,
92     val cause: String
93 )
94
95 data class ErrorCatalog(
96     val errorId: String,
97     val domainId: String,
98     val code: Int,
99     val action: String,
100     val cause: String
101 ) {
102
103     fun getMessage(): String {
104         return "Cause: $cause ${System.lineSeparator()} Action : $action"
105     }
106 }