e3b9d0a2a541619ec9d9f649b61b99d4983775c4
[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         PrimaryDbState   PrimaryDbState
33         ReplicasDbState  *ReplicasDbState
34         SentinelsDbState *SentinelsDbState
35 }
36
37 //PrimaryDbState struct is a holder for primary Redis state information.
38 type PrimaryDbState struct {
39         Err    error
40         Fields PrimaryDbStateFields
41 }
42
43 //ReplicasDbState struct is a holder for Redis replicas state information.
44 type ReplicasDbState struct {
45         Err    error
46         States []*ReplicaDbState
47 }
48
49 //ReplicaDbState struct is a holder for one Redis replica 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 //PrimaryDbStateFields struct is a holder for primary Redis state information
66 //fields which are read from sdlgoredis sentinel 'Master' call output.
67 type PrimaryDbStateFields struct {
68         Role  string
69         Ip    string
70         Port  string
71         Flags string
72 }
73
74 //ReplicaDbStateFields struct is a holder for replica 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         PrimaryLinkStatus 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.PrimaryDbState.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 (pdbst *PrimaryDbState) IsOnline() error {
110         if pdbst.Err != nil {
111                 return pdbst.Err
112         }
113         if pdbst.Fields.Role != "master" {
114                 return fmt.Errorf("No primary DB, current role '%s'", pdbst.Fields.Role)
115         }
116         if pdbst.Fields.Flags != "master" {
117                 return fmt.Errorf("Primary flags are '%s', expected 'master'", pdbst.Fields.Flags)
118         }
119         return nil
120 }
121
122 func (pdbst *PrimaryDbState) GetAddress() string {
123         if pdbst.Fields.Ip != "" || pdbst.Fields.Port != "" {
124                 return pdbst.Fields.Ip + ":" + pdbst.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.PrimaryLinkStatus != "ok" {
148                 return fmt.Errorf("Replica link to the primary 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 }