2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
29 //DbState struct is a holder for DB state information, which is received from
30 //sdlgoredis sentinel 'Master' and 'Slaves' calls output.
35 PrimaryDbState PrimaryDbState
36 ReplicasDbState *ReplicasDbState
37 SentinelsDbState *SentinelsDbState
40 //PrimaryDbState struct is a holder for primary Redis state information.
41 type PrimaryDbState struct {
43 Fields PrimaryDbStateFields
46 //ReplicasDbState struct is a holder for Redis replicas state information.
47 type ReplicasDbState struct {
49 States []*ReplicaDbState
52 //ReplicaDbState struct is a holder for one Redis replica state information.
53 type ReplicaDbState struct {
54 Fields ReplicaDbStateFields
57 //SentinelsDbState struct is a holder for Redis sentinels state information.
58 type SentinelsDbState struct {
60 States []*SentinelDbState
63 //SentinelDbState struct is a holder for one Redis sentinel state information.
64 type SentinelDbState struct {
65 Fields SentinelDbStateFields
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 {
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 {
83 PrimaryLinkStatus string
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 {
95 func (dbst *DbState) IsOnline() error {
96 if err := dbst.Err; err != nil {
100 if err := dbst.PrimaryDbState.IsOnline(); err != nil {
103 if dbst.ReplicasDbState != nil {
104 if err := dbst.ReplicasDbState.IsOnline(); err != nil {
108 if dbst.SentinelsDbState != nil {
109 if err := dbst.SentinelsDbState.IsOnline(); err != nil {
116 func (pdbst *PrimaryDbState) IsOnline() error {
117 if pdbst.Err != nil {
120 if pdbst.Fields.Role != "master" {
121 return fmt.Errorf("No primary DB, current role '%s'", pdbst.Fields.Role)
123 if pdbst.Fields.Flags != "master" {
124 return fmt.Errorf("Primary flags are '%s', expected 'master'", pdbst.Fields.Flags)
129 func (pdbst *PrimaryDbState) GetAddress() string {
130 if pdbst.Fields.Ip != "" || pdbst.Fields.Port != "" {
131 return pdbst.Fields.Ip + ":" + pdbst.Fields.Port
137 func (rdbst *ReplicasDbState) IsOnline() error {
138 if rdbst.Err != nil {
141 for _, state := range rdbst.States {
142 if err := state.IsOnline(); err != nil {
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)
154 if rdbst.Fields.PrimaryLinkStatus != "ok" {
155 return fmt.Errorf("Replica link to the primary is down")
158 if rdbst.Fields.Flags != "slave" {
159 return fmt.Errorf("Replica flags are '%s', expected 'slave'", rdbst.Fields.Flags)
164 func (rdbst *ReplicaDbState) GetAddress() string {
165 if rdbst.Fields.Ip != "" || rdbst.Fields.Port != "" {
166 return rdbst.Fields.Ip + ":" + rdbst.Fields.Port
172 func (sdbst *SentinelsDbState) IsOnline() error {
173 if sdbst.Err != nil {
176 for _, state := range sdbst.States {
177 if err := state.IsOnline(); err != nil {
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)
191 func (sdbst *SentinelDbState) GetAddress() string {
192 if sdbst.Fields.Ip != "" || sdbst.Fields.Port != "" {
193 return sdbst.Fields.Ip + ":" + sdbst.Fields.Port