X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cmd%2Fappmgr%2Fdb.go;fp=cmd%2Fappmgr%2Fdb.go;h=6f772c0f6f190522cca837be1d92490cc75203e5;hb=193caf9d7e08b84a0b9c1f0352924a7efd77e77c;hp=0000000000000000000000000000000000000000;hpb=cb4b2ad8e2f99eadea145e480da556c58b0a47b5;p=ric-plt%2Fappmgr.git diff --git a/cmd/appmgr/db.go b/cmd/appmgr/db.go new file mode 100755 index 0000000..6f772c0 --- /dev/null +++ b/cmd/appmgr/db.go @@ -0,0 +1,91 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package main + +import ( + "encoding/json" + sdl "gerrit.oran-osc.org/r/ric-plt/sdlgo" + cmap "github.com/orcaman/concurrent-map" + "github.com/spf13/viper" + "time" +) + +type DB struct { + session *sdl.SdlInstance +} + +func (d *DB) Create() { + ns := viper.GetString("db.sessionNamespace") + d.session = sdl.Create(ns) + + // Test DB connection, and wait until ready! + for { + if _, err := d.session.GetAll(); err == nil { + return + } + mdclog(MdclogErr, "Database connection not ready, waiting ...") + time.Sleep(time.Duration(5 * time.Second)) + } +} + +func (d *DB) StoreSubscriptions(m cmap.ConcurrentMap) { + for v := range m.Iter() { + s := v.Val.(Subscription) + + data, err := json.Marshal(s.req) + if err != nil { + mdclog(MdclogErr, "json.marshal failed: "+err.Error()) + return + } + + if err := d.session.Set(s.req.Id, data); err != nil { + mdclog(MdclogErr, "DB.session.Set failed: "+err.Error()) + } + } +} + +func (d *DB) RestoreSubscriptions() (m cmap.ConcurrentMap) { + m = cmap.New() + + keys, err := d.session.GetAll() + if err != nil { + mdclog(MdclogErr, "DB.session.GetAll failed: "+err.Error()) + return + } + + for _, key := range keys { + value, err := d.session.Get([]string{key}) + if err != nil { + mdclog(MdclogErr, "DB.session.Get failed: "+err.Error()) + return + } + + var item SubscriptionReq + if err = json.Unmarshal([]byte(value[key].(string)), &item); err != nil { + mdclog(MdclogErr, "json.Unmarshal failed: "+err.Error()) + return + } + + resp := SubscriptionResp{key, 0, item.EventType} + m.Set(key, Subscription{item, resp}) + } + + return m +}