Build related cleanup. Entrypoint to read config from correct place.
[ric-plt/appmgr.git] / cmd / appmgr / db.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package main
21
22 import (
23         "encoding/json"
24         sdl "gerrit.oran-osc.org/r/ric-plt/sdlgo"
25         cmap "github.com/orcaman/concurrent-map"
26         "github.com/spf13/viper"
27         "time"
28 )
29
30 type DB struct {
31         session *sdl.SdlInstance
32 }
33
34 func (d *DB) Create() {
35         ns := viper.GetString("db.sessionNamespace")
36         d.session = sdl.NewSdlInstance(ns, sdl.NewDatabase())
37
38         // Test DB connection, and wait until ready!
39         for {
40                 if _, err := d.session.GetAll(); err == nil {
41                         return
42                 }
43                 Logger.Error("Database connection not ready, waiting ...")
44                 time.Sleep(time.Duration(5 * time.Second))
45         }
46 }
47
48 func (d *DB) StoreSubscriptions(m cmap.ConcurrentMap) {
49         for v := range m.Iter() {
50                 s := v.Val.(Subscription)
51
52                 data, err := json.Marshal(s.req)
53                 if err != nil {
54                         Logger.Error("json.marshal failed: %v ", err.Error())
55                         return
56                 }
57
58                 if err := d.session.Set(s.req.Id, data); err != nil {
59                         Logger.Error("DB.session.Set failed: %v ", err.Error())
60                 }
61         }
62 }
63
64 func (d *DB) RestoreSubscriptions() (m cmap.ConcurrentMap) {
65         m = cmap.New()
66
67         keys, err := d.session.GetAll()
68         if err != nil {
69                 Logger.Error("DB.session.GetAll failed: %v ", err.Error())
70                 return
71         }
72
73         for _, key := range keys {
74                 value, err := d.session.Get([]string{key})
75                 if err != nil {
76                         Logger.Error("DB.session.Get failed: %v ", err.Error())
77                         return
78                 }
79
80                 var item SubscriptionReq
81                 if err = json.Unmarshal([]byte(value[key].(string)), &item); err != nil {
82                         Logger.Error("json.Unmarshal failed: %v ", err.Error())
83                         return
84                 }
85
86                 resp := SubscriptionResp{key, 0, item.EventType}
87                 m.Set(key, Subscription{item, resp})
88         }
89
90         return m
91 }