Update openssl package of DBAAS docker image
[ric-plt/dbaas.git] / testapplication / go / sdl / sdl.go
1 //   Copyright (c) 2019 AT&T Intellectual Property.
2 //   Copyright (c) 2019 Nokia.
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "License");
5 //   you may not use this file except in compliance with the License.
6 //   You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //   Unless required by applicable law or agreed to in writing, software
11 //   distributed under the License is distributed on an "AS IS" BASIS,
12 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //   See the License for the specific language governing permissions and
14 //   limitations under the License.
15
16 //
17 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //   platform project (RICP).
19 //
20
21 package sdl
22
23 import (
24         "fmt"
25         "github.com/go-redis/redis"
26         "os"
27         "reflect"
28 )
29
30 type SdlInstance struct {
31         nameSpace string
32         nsPrefix  string
33         client    *redis.Client
34 }
35
36 func Create(nameSpace string) *SdlInstance {
37         hostname := os.Getenv("DBAAS_SERVICE_HOST")
38         if hostname == "" {
39                 hostname = "localhost"
40         }
41         port := os.Getenv("DBAAS_SERVICE_PORT")
42         if port == "" {
43                 port = "6379"
44         }
45         redisAddress := hostname + ":" + port
46         client := redis.NewClient(&redis.Options{
47                 Addr:     redisAddress,
48                 Password: "", // no password set
49                 DB:       0,  // use default DB
50         })
51
52         s := SdlInstance{
53                 nameSpace: nameSpace,
54                 nsPrefix:  "{" + nameSpace + "},",
55                 client:    client,
56         }
57         s.CheckRedisModuleExtensionCommands()
58
59         return &s
60 }
61
62 func (s *SdlInstance) CheckRedisModuleExtensionCommands() {
63         var moduleError bool
64         commands, err := s.client.Command().Result()
65         if err == nil {
66                 redisModuleCommands := []string{
67                         "setie", "delie", "setiepub", "deliepub",
68                         "setnxpub", "msetmpub", "delmpub",
69                 }
70                 for _, v := range redisModuleCommands {
71                         _, ok := commands[v]
72                         if !ok {
73                                 fmt.Println("ERROR: Missing command:", v)
74                                 moduleError = true
75                         }
76                 }
77         } else {
78                 fmt.Println("ERROR:", err)
79         }
80         if moduleError {
81                 fmt.Println("Please make sure that redis extension modules have been installed.")
82                 fmt.Println("To install: redis-cli module load /usr/local/libexec/redismodule/libredismodule.so")
83         }
84 }
85
86 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
87         var retVal []interface{}
88         for i, v := range pairs {
89                 if i%2 == 0 {
90                         reflectType := reflect.TypeOf(v)
91                         switch reflectType.Kind() {
92                         case reflect.Slice:
93                                 x := reflect.ValueOf(v)
94                                 for i2 := 0; i2 < x.Len(); i2++ {
95                                         if i2%2 == 0 {
96                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
97                                         } else {
98                                                 retVal = append(retVal, x.Index(i2).Interface())
99                                         }
100                                 }
101                         case reflect.Array:
102                                 x := reflect.ValueOf(v)
103                                 for i2 := 0; i2 < x.Len(); i2++ {
104                                         if i2%2 == 0 {
105                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
106                                         } else {
107                                                 retVal = append(retVal, x.Index(i2).Interface())
108                                         }
109                                 }
110                         default:
111                                 retVal = append(retVal, s.nsPrefix+v.(string))
112                         }
113                 } else {
114                         retVal = append(retVal, v)
115                 }
116         }
117         return retVal
118 }
119
120 func checkResultAndError(result interface{}, err error) (bool, error) {
121         if err != nil {
122                 if err == redis.Nil {
123                         return false, nil
124                 }
125                 return false, err
126         }
127         if result == "OK" {
128                 return true, nil
129         }
130         return false, nil
131 }
132
133 func (s *SdlInstance) Set(pairs ...interface{}) error {
134         keyAndData := s.setNamespaceToKeys(pairs...)
135         err := s.client.MSet(keyAndData...).Err()
136         return err
137 }
138
139 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
140         var keysWithNs []string
141         for _, v := range keys {
142                 keysWithNs = append(keysWithNs, s.nsPrefix+v)
143         }
144         val, err := s.client.MGet(keysWithNs...).Result()
145         m := make(map[string]interface{})
146         if err != nil {
147                 return m, err
148         }
149         for i, v := range val {
150                 m[keys[i]] = v
151         }
152         return m, err
153 }
154
155 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) (bool, error) {
156         return checkResultAndError(s.client.Do("SETIE", key, newData, oldData).Result())
157 }
158
159 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
160         panic("SetIfiNotExists not implemented\n")
161 }
162
163 func (s *SdlInstance) Remove(keys ...string) {
164         panic("Remove not implemented\n")
165 }
166
167 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
168         panic("RemoveIf not implemented\n")
169 }
170
171 func (s *SdlInstance) GetAll() []string {
172         panic("GetAll not implemented\n")
173 }
174
175 func (s *SdlInstance) RemoveAll() {
176         panic("RemoveAll not implemented\n")
177 }