Merge "RIC:1060: Change in PTL"
[ric-plt/sdlgo.git] / sdl.go
1 /*
2    Copyright (c) 2019 AT&T Intellectual Property.
3    Copyright (c) 2018-2019 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 sdlgo
24
25 import (
26         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
27         "time"
28 )
29
30 //SdlInstance provides an API to read, write and modify
31 //key-value pairs in a given namespace.
32 //Deprecated: Will be removed in a future release, please use instead SyncStorage
33 //type defined in syncstorage.go.
34 type SdlInstance struct {
35         nameSpace string
36         nsPrefix  string
37         storage   *SyncStorage
38 }
39
40 //Database struct is a holder for the internal database instance. Applications
41 //can use this exported data type to locally store a reference to database
42 //instance returned from NewDabase() function.
43 type Database struct {
44         instances []iDatabase
45 }
46
47 //NewDatabase creates a connection to database that will be used
48 //as a backend for the key-value storage. The returned value
49 //can be reused between multiple SDL instances in which case each instance
50 //is using the same connection.
51 //Deprecated: Will be removed in a future release, because there is no need to
52 //create a database before NewSyncStorage function is called, database will
53 //be created automatically by NewSyncStorage function.
54 func NewDatabase() *Database {
55         db := &Database{}
56         for _, v := range sdlgoredis.Create() {
57                 db.instances = append(db.instances, v)
58         }
59         return db
60 }
61
62 //NewSdlInstance creates a new sdl instance using the given namespace.
63 //The database used as a backend is given as a parameter
64 //Deprecated: Will be removed in a future release, please use NewSyncStorage
65 //function instead.
66 func NewSdlInstance(NameSpace string, db *Database) *SdlInstance {
67         return &SdlInstance{
68                 nameSpace: NameSpace,
69                 nsPrefix:  "{" + NameSpace + "},",
70                 storage:   newSyncStorage(db),
71         }
72 }
73
74 //SubscribeChannel lets you to subscribe for a events on a given channels.
75 //SDL notifications are events that are published on a specific channels.
76 //Both the channel and events are defined by the entity that is publishing
77 //the events.
78 //
79 //When subscribing for a channel, a callback function is given as a parameter.
80 //Whenever a notification is received from a channel, this callback is called
81 //with channel and notifications as parameter (several notifications could be
82 //packed to a single callback function call). A call to SubscribeChannel function
83 //returns immediatelly, callbacks will be called asyncronously.
84 //
85 //It is possible to subscribe to different channels using different callbacks. In
86 //this case simply use SubscribeChannel function separately for each channel.
87 //
88 //When receiving events in callback routine, it is a good practive to return from
89 //callback as quickly as possible. E.g. reading in callback context should be avoided
90 //and using of Go signals is recommended. Also it should be noted that in case of several
91 //events received from different channels, callbacks are called in series one by one.
92 //
93 //Deprecated: Will be removed in a future release, please use the SubscribeChannel
94 //receiver function of the SyncStorage type.
95 func (s *SdlInstance) SubscribeChannel(cb func(string, ...string), channels ...string) error {
96         return s.storage.SubscribeChannel(s.nameSpace, cb, channels...)
97 }
98
99 //UnsubscribeChannel removes subscription from one or several channels.
100 //Deprecated: Will be removed in a future release, please use the UnsubscribeChannel
101 //receiver function of the SyncStorage type.
102 func (s *SdlInstance) UnsubscribeChannel(channels ...string) error {
103         return s.storage.UnsubscribeChannel(s.nameSpace, channels...)
104 }
105
106 //Close connection to backend database.
107 //Deprecated: Will be removed in a future release, please use the Close receiver
108 //function of the SyncStorage type.
109 func (s *SdlInstance) Close() error {
110         return s.storage.Close()
111 }
112
113 //SetAndPublish function writes data to shared data layer storage and sends an event to
114 //a channel. Writing is done atomically, i.e. all succeeds or fails.
115 //Data to be written is given as key-value pairs. Several key-value
116 //pairs can be written with one call.
117 //The key is expected to be string whereas value can be anything, string,
118 //number, slice array or map
119 //
120 //If data was set successfully, an event is sent to a channel.
121 //Channels and events are given as pairs is channelsAndEvents parameter.
122 //It is possible to send several events to several channels by giving several
123 //channel-event pairs.
124 //  E.g. []{"channel1", "event1", "channel2", "event2", "channel1", "event3"}
125 //will send event1 and event3 to channel1 and event2 to channel2.
126 //
127 //Deprecated: Will be removed in a future release, please use the SetAndPublish
128 //receiver function of the SyncStorage type.
129 func (s *SdlInstance) SetAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
130         return s.storage.SetAndPublish(s.nameSpace, channelsAndEvents, pairs...)
131 }
132
133 //Set function writes data to shared data layer storage. Writing is done
134 //atomically, i.e. all succeeds or fails.
135 //Data to be written is given as key-value pairs. Several key-value
136 //pairs can be written with one call.
137 //The key is expected to be string whereas value can be anything, string,
138 //number, slice array or map
139 //Deprecated: Will be removed in a future release, please use the Set receiver
140 //function of the SyncStorage type.
141 func (s *SdlInstance) Set(pairs ...interface{}) error {
142         return s.storage.Set(s.nameSpace, pairs...)
143 }
144
145 //Get function atomically reads one or more keys from SDL. The returned map has the
146 //requested keys as index and data as value. If the requested key is not found
147 //from SDL, it's value is nil.
148 //Deprecated: Will be removed in a future release, please use the Get receiver
149 //function of the SyncStorage type.
150 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
151         return s.storage.Get(s.nameSpace, keys)
152 }
153
154 //SetIfAndPublish atomically replaces existing data with newData in SDL if data matches the oldData.
155 //If replace was done successfully, true will be returned. Also, if publishing was successfull, an event
156 //is published to a given channel.
157 //Deprecated: Will be removed in a future release, please use the SetIfAndPublish
158 //receiver function of the SyncStorage type.
159 func (s *SdlInstance) SetIfAndPublish(channelsAndEvents []string, key string, oldData, newData interface{}) (bool, error) {
160         return s.storage.SetIfAndPublish(s.nameSpace, channelsAndEvents, key, oldData, newData)
161 }
162
163 //SetIf atomically replaces existing data with newData in SDL if data matches the oldData.
164 //If replace was done successfully, true will be returned.
165 //Deprecated: Will be removed in a future release, please use the SetIf receiver
166 //function of the SyncStorage type.
167 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) (bool, error) {
168         return s.storage.SetIf(s.nameSpace, key, oldData, newData)
169 }
170
171 //SetIfNotExistsAndPublish conditionally sets the value of a key. If key already exists in SDL,
172 //then it's value is not changed. Checking the key existence and potential set operation
173 //is done atomically. If the set operation was done successfully, an event is published to a
174 //given channel.
175 //Deprecated: Will be removed in a future release, please use the SetIfNotExistsAndPublish
176 //receiver function of the SyncStorage type.
177 func (s *SdlInstance) SetIfNotExistsAndPublish(channelsAndEvents []string, key string, data interface{}) (bool, error) {
178         return s.storage.SetIfNotExistsAndPublish(s.nameSpace, channelsAndEvents, key, data)
179 }
180
181 //SetIfNotExists conditionally sets the value of a key. If key already exists in SDL,
182 //then it's value is not changed. Checking the key existence and potential set operation
183 //is done atomically.
184 //Deprecated: Will be removed in a future release, please use the SetIfNotExists
185 //receiver function of the SyncStorage type.
186 func (s *SdlInstance) SetIfNotExists(key string, data interface{}) (bool, error) {
187         return s.storage.SetIfNotExists(s.nameSpace, key, data)
188 }
189
190 //RemoveAndPublish removes data from SDL. Operation is done atomically, i.e. either all succeeds or fails.
191 //Trying to remove a nonexisting key is not considered as an error.
192 //An event is published into a given channel if remove operation is successfull and
193 //at least one key is removed (if several keys given). If the given key(s) doesn't exist
194 //when trying to remove, no event is published.
195 //Deprecated: Will be removed in a future release, please use the RemoveAndPublish
196 //receiver function of the SyncStorage type.
197 func (s *SdlInstance) RemoveAndPublish(channelsAndEvents []string, keys []string) error {
198         return s.storage.RemoveAndPublish(s.nameSpace, channelsAndEvents, keys)
199 }
200
201 //Remove data from SDL. Operation is done atomically, i.e. either all succeeds or fails.
202 //Deprecated: Will be removed in a future release, please use the Remove receiver
203 //function of the SyncStorage type.
204 func (s *SdlInstance) Remove(keys []string) error {
205         return s.storage.Remove(s.nameSpace, keys)
206 }
207
208 //RemoveIfAndPublish removes data from SDL conditionally and if remove was done successfully,
209 //a given event is published to channel. If existing data matches given data,
210 //key and data are removed from SDL. If remove was done successfully, true is returned.
211 //Deprecated: Will be removed in a future release, please use the RemoveIfAndPublish
212 //receiver function of the SyncStorage type.
213 func (s *SdlInstance) RemoveIfAndPublish(channelsAndEvents []string, key string, data interface{}) (bool, error) {
214         return s.storage.RemoveIfAndPublish(s.nameSpace, channelsAndEvents, key, data)
215 }
216
217 //RemoveIf removes data from SDL conditionally. If existing data matches given data,
218 //key and data are removed from SDL. If remove was done successfully, true is returned.
219 //Deprecated: Will be removed in a future release, please use the RemoveIf receiver
220 //function of the SyncStorage type.
221 func (s *SdlInstance) RemoveIf(key string, data interface{}) (bool, error) {
222         return s.storage.RemoveIf(s.nameSpace, key, data)
223 }
224
225 //GetAll returns all keys under the namespace. No prior knowledge about the keys in the
226 //given namespace exists, thus operation is not guaranteed to be atomic or isolated.
227 //Deprecated: Will be removed in a future release, please use the GetAll receiver
228 //function of the SyncStorage type.
229 func (s *SdlInstance) GetAll() ([]string, error) {
230         return s.storage.GetAll(s.nameSpace)
231 }
232
233 //RemoveAll removes all keys under the namespace. Remove operation is not atomic, thus
234 //it is not guaranteed that all keys are removed.
235 //Deprecated: Will be removed in a future release, please use the RemoveAll receiver
236 //function of the SyncStorage type.
237 func (s *SdlInstance) RemoveAll() error {
238         return s.storage.RemoveAll(s.nameSpace)
239 }
240
241 //RemoveAllAndPublish removes all keys under the namespace and if successfull, it
242 //will publish an event to given channel. This operation is not atomic, thus it is
243 //not guaranteed that all keys are removed.
244 //Deprecated: Will be removed in a future release, please use the RemoveAllAndPublish
245 //receiver function of the SyncStorage type.
246 func (s *SdlInstance) RemoveAllAndPublish(channelsAndEvents []string) error {
247         return s.storage.RemoveAllAndPublish(s.nameSpace, channelsAndEvents)
248 }
249
250 //AddMember adds a new members to a group.
251 //
252 //SDL groups are unordered collections of members where each member is
253 //unique. It is possible to add the same member several times without the
254 //need to check if it already exists.
255 //Deprecated: Will be removed in a future release, please use the AddMember
256 //receiver function of the SyncStorage type.
257 func (s *SdlInstance) AddMember(group string, member ...interface{}) error {
258         return s.storage.AddMember(s.nameSpace, group, member...)
259 }
260
261 //RemoveMember removes members from a group.
262 //Deprecated: Will be removed in a future release, please use the RemoveMember
263 //receiver function of the SyncStorage type.
264 func (s *SdlInstance) RemoveMember(group string, member ...interface{}) error {
265         return s.storage.RemoveMember(s.nameSpace, group, member...)
266 }
267
268 //RemoveGroup removes the whole group along with it's members.
269 //Deprecated: Will be removed in a future release, please use the RemoveGroup
270 //receiver function of the SyncStorage type.
271 func (s *SdlInstance) RemoveGroup(group string) error {
272         return s.storage.RemoveGroup(s.nameSpace, group)
273 }
274
275 //GetMembers returns all the members from a group.
276 //Deprecated: Will be removed in a future release, please use the GetMembers
277 //receiver function of the SyncStorage type.
278 func (s *SdlInstance) GetMembers(group string) ([]string, error) {
279         return s.storage.GetMembers(s.nameSpace, group)
280 }
281
282 //IsMember returns true if given member is found from a group.
283 func (s *SdlInstance) IsMember(group string, member interface{}) (bool, error) {
284         return s.storage.IsMember(s.nameSpace, group, member)
285 }
286
287 //GroupSize returns the number of members in a group.
288 //Deprecated: Will be removed in a future release, please use the GroupSize
289 //receiver function of the SyncStorage type.
290 func (s *SdlInstance) GroupSize(group string) (int64, error) {
291         return s.storage.GroupSize(s.nameSpace, group)
292 }
293
294 //LockResource function is used for locking a resource. The resource lock in
295 //practice is a key with random value that is set to expire after a time
296 //period. The value written to key is a random value, thus only the instance
297 //created a lock, can release it. Resource locks are per namespace.
298 //Deprecated: Will be removed in a future release, please use the LockResource
299 //receiver function of the SyncStorage type.
300 func (s *SdlInstance) LockResource(resource string, expiration time.Duration, opt *Options) (*Lock, error) {
301         l, err := s.storage.LockResource(s.nameSpace, resource, expiration, opt)
302         if l != nil {
303                 return &Lock{
304                         s:           s,
305                         storageLock: l,
306                 }, err
307         }
308         return nil, err
309 }
310
311 //ReleaseResource removes the lock from a resource. If lock is already
312 //expired or some other instance is keeping the lock (lock taken after expiration),
313 //an error is returned.
314 //Deprecated: Will be removed in a future release, please use the ReleaseResource
315 //receiver function of the SyncStorageLock type.
316 func (l *Lock) ReleaseResource() error {
317         return l.storageLock.ReleaseResource(l.s.nameSpace)
318 }
319
320 //RefreshResource function can be used to set a new expiration time for the
321 //resource lock (if the lock still exists). The old remaining expiration
322 //time is overwritten with the given new expiration time.
323 //Deprecated: Will be removed in a future release, please use the RefreshResource
324 //receiver function of the SyncStorageLock type.
325 func (l *Lock) RefreshResource(expiration time.Duration) error {
326         return l.storageLock.RefreshResource(l.s.nameSpace, expiration)
327 }
328
329 //CheckResource returns the expiration time left for a resource.
330 //If the resource doesn't exist, -2 is returned.
331 //Deprecated: Will be removed in a future release, please use the CheckResource
332 //receiver function of the SyncStorage type.
333 func (s *SdlInstance) CheckResource(resource string) (time.Duration, error) {
334         return s.storage.CheckResource(s.nameSpace, resource)
335 }
336
337 //Options struct defines the behaviour for getting the resource lock.
338 type Options struct {
339         //The number of time the lock will be tried.
340         //Default: 0 = no retry
341         RetryCount int
342
343         //Wait between the retries.
344         //Default: 100ms
345         RetryWait time.Duration
346 }
347
348 func (o *Options) getRetryCount() int {
349         if o != nil && o.RetryCount > 0 {
350                 return o.RetryCount
351         }
352         return 0
353 }
354
355 func (o *Options) getRetryWait() time.Duration {
356         if o != nil && o.RetryWait > 0 {
357                 return o.RetryWait
358         }
359         return 100 * time.Millisecond
360 }
361
362 //Lock struct identifies the resource lock instance. Releasing and adjusting the
363 //expirations are done using the methods defined for this struct.
364 //Deprecated: Will be removed in a future release, please use instead the SyncStorageLock
365 //type defined in syncstorage.go.
366 type Lock struct {
367         s           *SdlInstance
368         storageLock *SyncStorageLock
369 }