DB connection for service layer 06/13506/2
authorsubhash kumar singh <subh.singh@samsung.com>
Mon, 7 Oct 2024 09:24:15 +0000 (09:24 +0000)
committersubhash kumar singh <subh.singh@samsung.com>
Mon, 7 Oct 2024 09:24:15 +0000 (09:24 +0000)
DB connection for service layer.
Note: Intially DB connection started using sqllite3 connection
later the connection would be updated to postgress.

Change-Id: I106fa3e8fdc16b762ed8203390a37375fef996e1
Signed-off-by: subhash kumar singh <subh.singh@samsung.com>
apis/mmes_apis.go
apis_test/mmes_apis_test.go
go.mod
go.sum
main.go

index 3b9f383..44b7397 100644 (file)
@@ -24,6 +24,7 @@ import (
        "os"
 
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/core"
+       "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/db"
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/logging"
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/models"
        "github.com/gin-gonic/gin"
@@ -32,11 +33,13 @@ import (
 
 type MmeApiHandler struct {
        dbmgr core.DBMgr
+       iDB   db.IDB
 }
 
-func NewMmeApiHandler(dbMgr core.DBMgr) *MmeApiHandler {
+func NewMmeApiHandler(dbMgr core.DBMgr, iDB db.IDB) *MmeApiHandler {
        handler := &MmeApiHandler{
                dbmgr: dbMgr,
+               iDB:   iDB,
        }
        return handler
 }
index 8116367..d9772b9 100644 (file)
@@ -71,7 +71,7 @@ func TestRegisterModel(t *testing.T) {
        os.Setenv("LOG_FILE_NAME", "testing")
        dbMgrMockInst := new(dbMgrMock)
        dbMgrMockInst.On("CreateBucket", "test-model").Return(nil)
-       handler := apis.NewMmeApiHandler(dbMgrMockInst)
+       handler := apis.NewMmeApiHandler(dbMgrMockInst, nil)
        router := routers.InitRouter(handler)
        w := httptest.NewRecorder()
        req, _ := http.NewRequest("POST", "/registerModel", strings.NewReader(registerModelBody))
@@ -92,7 +92,7 @@ func TestWhenSuccessGetModelInfoList(t *testing.T) {
                },
        }, nil)
 
-       handler := apis.NewMmeApiHandler(dbMgrMockInst)
+       handler := apis.NewMmeApiHandler(dbMgrMockInst, nil)
        router := routers.InitRouter(handler)
        responseRecorder := httptest.NewRecorder()
 
@@ -121,7 +121,7 @@ func TestWhenFailGetModelInfoList(t *testing.T) {
        dbMgrMockInst := new(dbMgrMock)
        dbMgrMockInst.On("ListBucket").Return([]core.Bucket{}, errors.New("Test: Fail GetModelInfoList"))
 
-       handler := apis.NewMmeApiHandler(dbMgrMockInst)
+       handler := apis.NewMmeApiHandler(dbMgrMockInst, nil)
        router := routers.InitRouter(handler)
        responseRecorder := httptest.NewRecorder()
 
diff --git a/go.mod b/go.mod
index 9cf07e3..72f7c83 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -26,6 +26,7 @@ require (
        github.com/klauspost/cpuid/v2 v2.2.4 // indirect
        github.com/leodido/go-urn v1.2.4 // indirect
        github.com/mattn/go-isatty v0.0.19 // indirect
+       github.com/mattn/go-sqlite3 v1.14.22 // indirect
        github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
        github.com/modern-go/reflect2 v1.0.2 // indirect
        github.com/pelletier/go-toml/v2 v2.0.8 // indirect
@@ -40,5 +41,6 @@ require (
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/protobuf v1.30.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
+       gorm.io/driver/sqlite v1.5.6 // indirect
        gorm.io/gorm v1.25.12 // indirect
 )
diff --git a/go.sum b/go.sum
index 8d676bd..d3925ff 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -48,6 +48,8 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
 github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
 github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
 github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
+github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -100,6 +102,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
+gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
 gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
 gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
 rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
diff --git a/main.go b/main.go
index 4135c8c..548897c 100644 (file)
--- a/main.go
+++ b/main.go
@@ -24,14 +24,31 @@ import (
 
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/apis"
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/core"
+       modelDB "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/db"
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/logging"
+       "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/models"
        "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/routers"
+       "gorm.io/driver/sqlite"
+       "gorm.io/gorm"
 )
 
 func main() {
+
+       // setup the database connection
+       db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
+       if err != nil {
+               logging.ERROR("database not available")
+               os.Exit(-1)
+       }
+
+       // Auto migrate the scheme
+       db.AutoMigrate(&models.ModelInfo{})
+       repo := modelDB.NewModelInfoRepository(db)
+
        router := routers.InitRouter(
                apis.NewMmeApiHandler(
                        core.GetDBManagerInstance(),
+                       repo,
                ))
        server := http.Server{
                Addr:         os.Getenv("MMES_URL"),