02db20871c3650a3d24c26991e400b911ea3c491
[icn/sdwan.git] /
1 // SPDX-License-Identifier: Apache-2.0\r
2 // Copyright (c) 2020 Intel Corporation\r
3 \r
4 package contextdb\r
5 \r
6 import (\r
7         "github.com/open-ness/EMCO/src/orchestrator/pkg/infra/config"\r
8         pkgerrors "github.com/pkg/errors"\r
9 )\r
10 \r
11 // Db interface used to talk a concrete Database connection\r
12 var Db ContextDb\r
13 \r
14 // ContextDb is an interface for accessing the context database\r
15 type ContextDb interface {\r
16         // Returns nil if db health is good\r
17         HealthCheck() error\r
18         // Puts Json Struct in db with key\r
19         Put(key string, value interface{}) error\r
20         // Delete k,v\r
21         Delete(key string) error\r
22         // Delete all keys in heirarchy\r
23         DeleteAll(key string) error\r
24         // Gets Json Struct from db\r
25         Get(key string, value interface{}) error\r
26         // Returns all keys with a prefix\r
27         GetAllKeys(path string) ([]string, error)\r
28 }\r
29 \r
30 // createContextDBClient creates the DB client\r
31 func createContextDBClient(dbType string) error {\r
32         var err error\r
33         switch dbType {\r
34         case "etcd":\r
35                 c := EtcdConfig{\r
36                         Endpoint: config.GetConfiguration().EtcdIP,\r
37                         CertFile: config.GetConfiguration().EtcdCert,\r
38                         KeyFile:  config.GetConfiguration().EtcdKey,\r
39                         CAFile:   config.GetConfiguration().EtcdCAFile,\r
40                 }\r
41                 Db, err = NewEtcdClient(nil, c)\r
42                 if err != nil {\r
43                         pkgerrors.Wrap(err, "Etcd Client Initialization failed with error")\r
44                 }\r
45         default:\r
46                 return pkgerrors.New(dbType + "DB not supported")\r
47         }\r
48         return err\r
49 }\r
50 \r
51 // InitializeContextDatabase sets up the connection to the\r
52 // configured database to allow the application to talk to it.\r
53 func InitializeContextDatabase() error {\r
54         // Only support Etcd for now\r
55         err := createContextDBClient("etcd")\r
56         if err != nil {\r
57                 return pkgerrors.Cause(err)\r
58         }\r
59         err = Db.HealthCheck()\r
60         if err != nil {\r
61                 return pkgerrors.Cause(err)\r
62         }\r
63         return nil\r
64 }\r