Refactoring BP Code with ErrorCatalog
[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.slf4j.event.Level
21 import org.onap.ccsdk.cds.error.catalog.core.ErrorMessageLibConstants.ERROR_CATALOG_DEFAULT_ERROR_CODE
22 import java.time.LocalDateTime
23 import java.time.ZoneId
24 import java.util.Date
25 import kotlin.collections.ArrayList
26
27 open class ErrorPayload {
28     var code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE
29     var status: String = ""
30     @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
31     var timestamp: Date = controllerDate()
32     var message: String = ""
33     var debugMessage: String = ""
34     var logLevel: String = Level.ERROR.name
35     val subErrors: ArrayList<ErrorMessage> = ArrayList()
36
37     constructor()
38
39     constructor(
40         code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE,
41         status: String,
42         message: String,
43         logLevel: String = Level.ERROR.name,
44         debugMessage: String = ""
45     ) {
46         this.code = code
47         this.status = status
48         this.message = message
49         this.logLevel = logLevel
50         this.debugMessage = debugMessage
51     }
52
53     constructor(
54         code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE,
55         status: String,
56         message: String,
57         logLevel: String = Level.ERROR.name,
58         debugMessage: String = "",
59         errorMessage: ErrorMessage
60     ) {
61         this.code = code
62         this.status = status
63         this.message = message
64         this.logLevel = logLevel
65         this.debugMessage = debugMessage
66         this.subErrors.add(errorMessage)
67     }
68
69     fun isEqualTo(errorPayload: ErrorPayload): Boolean {
70         return (this.code == errorPayload.code && this.status == errorPayload.status && this.message == errorPayload.message &&
71             this.logLevel == errorPayload.logLevel && this.debugMessage == errorPayload.debugMessage &&
72             this.subErrors == errorPayload.subErrors)
73     }
74
75     private fun controllerDate(): Date {
76         val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
77         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
78     }
79 }
80
81 /**
82  *
83  *
84  * @author Steve Siani
85  */
86 data class ErrorMessage(
87     val domainId: String,
88     val message: String,
89     val cause: String
90 )
91
92 data class ErrorCatalog(
93     val errorId: String,
94     val domainId: String,
95     val code: Int,
96     val action: String,
97     val cause: String
98 ) {
99     fun getMessage(): String {
100         return "Cause: $cause ${System.lineSeparator()} Action : $action"
101     }
102 }