Fix sdlcli healthcheck DBAAS status in SEP install
[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         Err           error
33         ConfigNodeCnt int
34
35         PrimaryDbState   PrimaryDbState
36         ReplicasDbState  *ReplicasDbState
37         SentinelsDbState *SentinelsDbState
38 }
39
40 //PrimaryDbState struct is a holder for primary Redis state information.
41 type PrimaryDbState struct {
42         Err    error
43         Fields PrimaryDbStateFields
44 }
45
46 //ReplicasDbState struct is a holder for Redis replicas state information.
47 type ReplicasDbState struct {
48         Err    error
49         States []*ReplicaDbState
50 }
51
52 //ReplicaDbState struct is a holder for one Redis replica state information.
53 type ReplicaDbState struct {
54         Fields ReplicaDbStateFields
55 }
56
57 //SentinelsDbState struct is a holder for Redis sentinels state information.
58 type SentinelsDbState struct {
59         Err    error
60         States []*SentinelDbState
61 }
62
63 //SentinelDbState struct is a holder for one Redis sentinel state information.
64 type SentinelDbState struct {
65         Fields SentinelDbStateFields
66 }
67
68 //PrimaryDbStateFields struct is a holder for primary Redis state information
69 //fields which are read from sdlgoredis sentinel 'Master' call output.
70 type PrimaryDbStateFields struct {
71         Role  string
72         Ip    string
73         Port  string
74         Flags string
75 }
76
77 //ReplicaDbStateFields struct is a holder for replica Redis state information
78 //fields which are read from sdlgoredis sentinel 'Slaves' call output.
79 type ReplicaDbStateFields struct {
80         Role              string
81         Ip                string
82         Port              string
83         PrimaryLinkStatus string
84         Flags             string
85 }
86
87 //SentinelDbStateFields struct is a holder for sentinel Redis state information
88 //fields which are read from sdlgoredis sentinel 'Sentinels' call output.
89 type SentinelDbStateFields struct {
90         Ip    string
91         Port  string
92         Flags string
93 }
94
95 func (dbst *DbState) IsOnline() error {
96         if err := dbst.Err; err != nil {
97                 return err
98         }
99
100         if err := dbst.PrimaryDbState.IsOnline(); err != nil {
101                 return err
102         }
103         if dbst.ReplicasDbState != nil {
104                 if err := dbst.ReplicasDbState.IsOnline(); err != nil {
105                         return err
106                 }
107         }
108         if dbst.SentinelsDbState != nil {
109                 if err := dbst.SentinelsDbState.IsOnline(); err != nil {
110                         return err
111                 }
112         }
113         return nil
114 }
115
116 func (pdbst *PrimaryDbState) IsOnline() error {
117         if pdbst.Err != nil {
118                 return pdbst.Err
119         }
120         if pdbst.Fields.Role != "master" {
121                 return fmt.Errorf("No primary DB, current role '%s'", pdbst.Fields.Role)
122         }
123         if pdbst.Fields.Flags != "master" {
124                 return fmt.Errorf("Primary flags are '%s', expected 'master'", pdbst.Fields.Flags)
125         }
126         return nil
127 }
128
129 func (pdbst *PrimaryDbState) GetAddress() string {
130         if pdbst.Fields.Ip != "" || pdbst.Fields.Port != "" {
131                 return pdbst.Fields.Ip + ":" + pdbst.Fields.Port
132         } else {
133                 return ""
134         }
135 }
136
137 func (rdbst *ReplicasDbState) IsOnline() error {
138         if rdbst.Err != nil {
139                 return rdbst.Err
140         }
141         for _, state := range rdbst.States {
142                 if err := state.IsOnline(); err != nil {
143                         return err
144                 }
145         }
146         return nil
147 }
148
149 func (rdbst *ReplicaDbState) IsOnline() error {
150         if rdbst.Fields.Role != "slave" {
151                 return fmt.Errorf("Replica role is '%s', expected 'slave'", rdbst.Fields.Role)
152         }
153
154         if rdbst.Fields.PrimaryLinkStatus != "ok" {
155                 return fmt.Errorf("Replica link to the primary is down")
156         }
157
158         if rdbst.Fields.Flags != "slave" {
159                 return fmt.Errorf("Replica flags are '%s', expected 'slave'", rdbst.Fields.Flags)
160         }
161         return nil
162 }
163
164 func (rdbst *ReplicaDbState) GetAddress() string {
165         if rdbst.Fields.Ip != "" || rdbst.Fields.Port != "" {
166                 return rdbst.Fields.Ip + ":" + rdbst.Fields.Port
167         } else {
168                 return ""
169         }
170 }
171
172 func (sdbst *SentinelsDbState) IsOnline() error {
173         if sdbst.Err != nil {
174                 return sdbst.Err
175         }
176         for _, state := range sdbst.States {
177                 if err := state.IsOnline(); err != nil {
178                         return err
179                 }
180         }
181         return nil
182 }
183
184 func (sdbst *SentinelDbState) IsOnline() error {
185         if sdbst.Fields.Flags != "sentinel" {
186                 return fmt.Errorf("Sentinel flags are '%s', expected 'sentinel'", sdbst.Fields.Flags)
187         }
188         return nil
189 }
190
191 func (sdbst *SentinelDbState) GetAddress() string {
192         if sdbst.Fields.Ip != "" || sdbst.Fields.Port != "" {
193                 return sdbst.Fields.Ip + ":" + sdbst.Fields.Port
194         } else {
195                 return ""
196         }
197 }