Implement SDL CLI 'healthcheck' -command
[ric-plt/sdlgo.git] / internal / sdlgoredis / dbstate.go
1 /*
2    Copyright (c) 2021 AT&T Intellectual Property.
3    Copyright (c) 2018-2021 Nokia.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package sdlgoredis
24
25 import (
26         "fmt"
27 )
28
29 //DbState struct is a holder for DB state information, which is received from
30 //sdlgoredis sentinel 'Master' and 'Slaves' calls output.
31 type DbState struct {
32         MasterDbState   MasterDbState
33         ReplicasDbState *ReplicasDbState
34 }
35
36 //MasterDbState struct is a holder for master Redis state information.
37 type MasterDbState struct {
38         Err    error
39         Fields MasterDbStateFields
40 }
41
42 //ReplicasDbState struct is a holder for Redis slaves state information.
43 type ReplicasDbState struct {
44         Err    error
45         States []*ReplicaDbState
46 }
47
48 //ReplicaDbState struct is a holder for one Redis slave state information.
49 type ReplicaDbState struct {
50         Fields ReplicaDbStateFields
51 }
52
53 //MasterDbStateFields struct is a holder for master Redis state information
54 //fields which are read from sdlgoredis sentinel 'Master' call output.
55 type MasterDbStateFields struct {
56         Role  string
57         Ip    string
58         Port  string
59         Flags string
60 }
61
62 //ReplicaDbStateFields struct is a holder for slave Redis state information
63 //fields which are read from sdlgoredis sentinel 'Slaves' call output.
64 type ReplicaDbStateFields struct {
65         Role             string
66         Ip               string
67         Port             string
68         MasterLinkStatus string
69         Flags            string
70 }
71
72 func (dbst *DbState) IsOnline() error {
73         if err := dbst.MasterDbState.IsOnline(); err != nil {
74                 return err
75         }
76         if dbst.ReplicasDbState != nil {
77                 if err := dbst.ReplicasDbState.IsOnline(); err != nil {
78                         return err
79                 }
80         }
81         return nil
82 }
83
84 func (mdbst *MasterDbState) IsOnline() error {
85         if mdbst.Err != nil {
86                 return mdbst.Err
87         }
88         if mdbst.Fields.Role != "master" {
89                 return fmt.Errorf("No master DB, current role '%s'", mdbst.Fields.Role)
90         }
91         if mdbst.Fields.Flags != "master" {
92                 return fmt.Errorf("Master flags are '%s', expected 'master'", mdbst.Fields.Flags)
93         }
94         return nil
95 }
96
97 func (mdbst *MasterDbState) GetAddress() string {
98         if mdbst.Fields.Ip != "" || mdbst.Fields.Port != "" {
99                 return mdbst.Fields.Ip + ":" + mdbst.Fields.Port
100         } else {
101                 return ""
102         }
103 }
104
105 func (rdbst *ReplicasDbState) IsOnline() error {
106         if rdbst.Err != nil {
107                 return rdbst.Err
108         }
109         for _, state := range rdbst.States {
110                 if err := state.IsOnline(); err != nil {
111                         return err
112                 }
113         }
114         return nil
115 }
116
117 func (rdbst *ReplicaDbState) IsOnline() error {
118         if rdbst.Fields.Role != "slave" {
119                 return fmt.Errorf("Replica role is '%s', expected 'slave'", rdbst.Fields.Role)
120         }
121
122         if rdbst.Fields.MasterLinkStatus != "ok" {
123                 return fmt.Errorf("Replica link to the master is down")
124         }
125
126         if rdbst.Fields.Flags != "slave" {
127                 return fmt.Errorf("Replica flags are '%s', expected 'slave'", rdbst.Fields.Flags)
128         }
129         return nil
130 }
131
132 func (rdbst *ReplicaDbState) GetAddress() string {
133         if rdbst.Fields.Ip != "" || rdbst.Fields.Port != "" {
134                 return rdbst.Fields.Ip + ":" + rdbst.Fields.Port
135         } else {
136                 return ""
137         }
138 }