2a40dac8de9760c175cc7a5c584797a583640c08
[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         "github.com/go-redis/redis"
25         "os"
26         "reflect"
27 )
28
29 type SdlInstance struct {
30         nameSpace string
31         nsPrefix  string
32         client    *redis.Client
33 }
34
35 func Create(nameSpace string) *SdlInstance {
36         hostname := os.Getenv("DBAAS_SERVICE_HOST")
37         if hostname == "" {
38                 hostname = "localhost"
39         }
40         port := os.Getenv("DBAAS_SERVICE_PORT")
41         if port == "" {
42                 port = "6379"
43         }
44         redisAddress := hostname + ":" + port
45         client := redis.NewClient(&redis.Options{
46                 Addr:     redisAddress,
47                 Password: "", // no password set
48                 DB:       0,  // use default DB
49         })
50
51         s := SdlInstance{
52                 nameSpace: nameSpace,
53                 nsPrefix:  "{" + nameSpace + "},",
54                 client:    client,
55         }
56
57         return &s
58 }
59
60 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
61         var retVal []interface{}
62         for i, v := range pairs {
63                 if i%2 == 0 {
64                         reflectType := reflect.TypeOf(v)
65                         switch reflectType.Kind() {
66                         case reflect.Slice:
67                                 x := reflect.ValueOf(v)
68                                 for i2 := 0; i2 < x.Len(); i2++ {
69                                         if i2%2 == 0 {
70                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
71                                         } else {
72                                                 retVal = append(retVal, x.Index(i2).Interface())
73                                         }
74                                 }
75                         case reflect.Array:
76                                 x := reflect.ValueOf(v)
77                                 for i2 := 0; i2 < x.Len(); i2++ {
78                                         if i2%2 == 0 {
79                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
80                                         } else {
81                                                 retVal = append(retVal, x.Index(i2).Interface())
82                                         }
83                                 }
84                         default:
85                                 retVal = append(retVal, s.nsPrefix+v.(string))
86                         }
87                 } else {
88                         retVal = append(retVal, v)
89                 }
90         }
91         return retVal
92 }
93
94 func (s *SdlInstance) Set(pairs ...interface{}) error {
95         keyAndData := s.setNamespaceToKeys(pairs...)
96         err := s.client.MSet(keyAndData...).Err()
97         return err
98 }
99
100 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
101         var keysWithNs []string
102         for _, v := range keys {
103                 keysWithNs = append(keysWithNs, s.nsPrefix+v)
104         }
105         val, err := s.client.MGet(keysWithNs...).Result()
106         m := make(map[string]interface{})
107         if err != nil {
108                 return m, err
109         }
110         for i, v := range val {
111                 m[keys[i]] = v
112         }
113         return m, err
114 }
115
116 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
117         panic("SetIf not implemented\n")
118 }
119
120 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
121         panic("SetIfiNotExists not implemented\n")
122 }
123
124 func (s *SdlInstance) Remove(keys ...string) {
125         panic("Remove not implemented\n")
126 }
127
128 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
129         panic("RemoveIf not implemented\n")
130 }
131
132 func (s *SdlInstance) GetAll() []string {
133         panic("GetAll not implemented\n")
134 }
135
136 func (s *SdlInstance) RemoveAll() {
137         panic("RemoveAll not implemented\n")
138 }
139