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