diff options
-rw-r--r-- | src/k8splugin/api/api.go | 16 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler.go (renamed from src/k8splugin/api/vnfdhandler.go) | 46 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler_test.go (renamed from src/k8splugin/api/vnfdhandler_test.go) | 136 | ||||
-rw-r--r-- | src/k8splugin/rb/definition.go | 135 | ||||
-rw-r--r-- | src/k8splugin/rb/definition_test.go (renamed from src/k8splugin/vnfd/vnfd_test.go) | 90 | ||||
-rw-r--r-- | src/k8splugin/vnfd/vnfd.go | 134 |
6 files changed, 279 insertions, 278 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index f05fbb0b..571a9576 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -14,7 +14,7 @@ limitations under the License. package api import ( - "k8splugin/vnfd" + "k8splugin/rb" "os" "path/filepath" "plugin" @@ -106,13 +106,13 @@ func NewRouter(kubeconfig string) *mux.Router { vnfInstanceHandler.HandleFunc("/{cloudRegionID}/{namespace}/{externalVNFID}", DeleteHandler).Methods("DELETE") vnfInstanceHandler.HandleFunc("/{cloudRegionID}/{namespace}/{externalVNFID}", GetHandler).Methods("GET") - vnfdRouter := router.PathPrefix("/v1/vnfd").Subrouter() - vh := vnfdHandler{vnfdClient: vnfd.GetVNFDClient()} - vnfdRouter.HandleFunc("", vh.vnfdCreateHandler).Methods("POST") - vnfdRouter.HandleFunc("/{vnfdID}/upload", vh.vnfdUploadHandler).Methods("POST") - vnfdRouter.HandleFunc("", vh.vnfdListHandler).Methods("GET") - vnfdRouter.HandleFunc("/{vnfdID}", vh.vnfdGetHandler).Methods("GET") - vnfdRouter.HandleFunc("/{vnfdID}", vh.vnfdDeleteHandler).Methods("DELETE") + resRouter := router.PathPrefix("/v1/rb").Subrouter() + rbdef := rbDefinitionHandler{client: rb.NewDefinitionClient()} + resRouter.HandleFunc("/definition", rbdef.createHandler).Methods("POST") + resRouter.HandleFunc("/definition/{rbdID}/upload", rbdef.uploadHandler).Methods("POST") + resRouter.HandleFunc("/definition", rbdef.listHandler).Methods("GET") + resRouter.HandleFunc("/definition/{rbdID}", rbdef.getHandler).Methods("GET") + resRouter.HandleFunc("/definition/{rbdID}", rbdef.deleteHandler).Methods("DELETE") // (TODO): Fix update method // vnfInstanceHandler.HandleFunc("/{vnfInstanceId}", UpdateHandler).Methods("PUT") diff --git a/src/k8splugin/api/vnfdhandler.go b/src/k8splugin/api/defhandler.go index ff777826..c8c03496 100644 --- a/src/k8splugin/api/vnfdhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -20,22 +20,22 @@ import ( "encoding/json" "net/http" - "k8splugin/vnfd" + "k8splugin/rb" "github.com/gorilla/mux" ) // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes -type vnfdHandler struct { - // Interface that implements vnfDefinition operations +type rbDefinitionHandler struct { + // Interface that implements bundle Definition operations // We will set this variable with a mock interface for testing - vnfdClient vnfd.VNFDefinitionInterface + client rb.DefinitionManager } -// vnfdCreateHandler handles creation of the vnfd entry in the database -func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { - var v vnfd.VNFDefinition +// createHandler handles creation of the definition entry in the database +func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Request) { + var v rb.Definition if r.Body == nil { http.Error(w, "Empty body", http.StatusBadRequest) @@ -54,7 +54,7 @@ func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { return } - ret, err := h.vnfdClient.Create(v) + ret, err := h.client.Create(v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -69,15 +69,15 @@ func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdUploadHandler handles upload of the vnf tar file into the database +// uploadHandler handles upload of the bundle tar file into the database // Note: This will be implemented in a different patch -func (h vnfdHandler) vnfdUploadHandler(w http.ResponseWriter, r *http.Request) { +func (h rbDefinitionHandler) uploadHandler(w http.ResponseWriter, r *http.Request) { } -// vnfdListHandler handles GET (list) operations on the /v1/vnfd endpoint -// Returns a list of vnfd.VNFDefinitions -func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) { - ret, err := h.vnfdClient.List() +// listHandler handles GET (list) operations on the endpoint +// Returns a list of rb.Definitions +func (h rbDefinitionHandler) listHandler(w http.ResponseWriter, r *http.Request) { + ret, err := h.client.List() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -92,13 +92,13 @@ func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdGetHandler handles GET operations on a particular VNFID -// Returns a vnfd.VNFDefinition -func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) { +// getHandler handles GET operations on a particular ids +// Returns a rb.Definition +func (h rbDefinitionHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - vnfdID := vars["vnfdID"] + id := vars["rbdID"] - ret, err := h.vnfdClient.Get(vnfdID) + ret, err := h.client.Get(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -113,12 +113,12 @@ func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdDeleteHandler handles DELETE operations on a particular VNFID -func (h vnfdHandler) vnfdDeleteHandler(w http.ResponseWriter, r *http.Request) { +// deleteHandler handles DELETE operations on a particular bundle definition id +func (h rbDefinitionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - vnfdID := vars["vnfdID"] + id := vars["rbdID"] - err := h.vnfdClient.Delete(vnfdID) + err := h.client.Delete(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/k8splugin/api/vnfdhandler_test.go b/src/k8splugin/api/defhandler_test.go index e393be6f..b83f0b7a 100644 --- a/src/k8splugin/api/vnfdhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "io" - "k8splugin/vnfd" + "k8splugin/rb" "net/http" "net/http/httptest" "reflect" @@ -32,54 +32,54 @@ import ( //Creating an embedded interface via anonymous variable //This allows us to make mockDB satisfy the DatabaseConnection //interface even if we are not implementing all the methods in it -type mockVNFDefinition struct { - vnfd.VNFDefinitionInterface +type mockRBDefinition struct { + rb.DefinitionManager // Items and err will be used to customize each test - // via a localized instantiation of mockVNFDefinition - Items []vnfd.VNFDefinition + // via a localized instantiation of mockRBDefinition + Items []rb.Definition Err error } -func (m *mockVNFDefinition) Create(inp vnfd.VNFDefinition) (vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) Create(inp rb.Definition) (rb.Definition, error) { if m.Err != nil { - return vnfd.VNFDefinition{}, m.Err + return rb.Definition{}, m.Err } return m.Items[0], nil } -func (m *mockVNFDefinition) List() ([]vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) List() ([]rb.Definition, error) { if m.Err != nil { - return []vnfd.VNFDefinition{}, m.Err + return []rb.Definition{}, m.Err } return m.Items, nil } -func (m *mockVNFDefinition) Get(vnfID string) (vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) Get(id string) (rb.Definition, error) { if m.Err != nil { - return vnfd.VNFDefinition{}, m.Err + return rb.Definition{}, m.Err } return m.Items[0], nil } -func (m *mockVNFDefinition) Delete(vnfID string) error { +func (m *mockRBDefinition) Delete(id string) error { return m.Err } -func TestVnfdCreateHandler(t *testing.T) { +func TestRBDefCreateHandler(t *testing.T) { testCases := []struct { label string reader io.Reader - expected vnfd.VNFDefinition + expected rb.Definition expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { label: "Missing Body Failure", expectedCode: http.StatusBadRequest, - vnfdClient: &mockVNFDefinition{}, + rbDefClient: &mockRBDefinition{}, }, { label: "Create without UUID", @@ -89,18 +89,18 @@ func TestVnfdCreateHandler(t *testing.T) { "description":"test description", "service-type":"firewall" }`)), - expected: vnfd.VNFDefinition{ + expected: rb.Definition{ UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ //Items that will be returned by the mocked Client - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, @@ -111,15 +111,15 @@ func TestVnfdCreateHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("POST", "/v1/vnfd", testCase.reader) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("POST", "/v1/resource/definition", testCase.reader) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdCreateHandler) + hr := http.HandlerFunc(vh.createHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -129,11 +129,11 @@ func TestVnfdCreateHandler(t *testing.T) { //Check returned body only if statusCreated if rr.Code == http.StatusCreated { - got := vnfd.VNFDefinition{} + got := rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdCreateHandler returned unexpected body: got %v;"+ + t.Errorf("createHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -141,43 +141,43 @@ func TestVnfdCreateHandler(t *testing.T) { } } -func TestVnfdListHandler(t *testing.T) { +func TestRBDefListHandler(t *testing.T) { testCases := []struct { label string - expected []vnfd.VNFDefinition + expected []rb.Definition expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "List VNF Definitions", + label: "List Bundle Definitions", expectedCode: http.StatusOK, - expected: []vnfd.VNFDefinition{ + expected: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, }, - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, @@ -188,14 +188,14 @@ func TestVnfdListHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd", nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition", nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdListHandler) + hr := http.HandlerFunc(vh.listHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -205,11 +205,11 @@ func TestVnfdListHandler(t *testing.T) { //Check returned body only if statusOK if rr.Code == http.StatusOK { - got := []vnfd.VNFDefinition{} + got := []rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdListHandler returned unexpected body: got %v;"+ + t.Errorf("listHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -217,31 +217,31 @@ func TestVnfdListHandler(t *testing.T) { } } -func TestVnfdGetHandler(t *testing.T) { +func TestRBDefGetHandler(t *testing.T) { testCases := []struct { label string - expected vnfd.VNFDefinition + expected rb.Definition inpUUID string expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "Get VNF Definition", + label: "Get Bundle Definition", expectedCode: http.StatusOK, - expected: vnfd.VNFDefinition{ + expected: rb.Definition{ UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, inpUUID: "123e4567-e89b-12d3-a456-426655441111", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, @@ -249,12 +249,12 @@ func TestVnfdGetHandler(t *testing.T) { }, }, { - label: "Get Non-Exiting VNF Definition", + label: "Get Non-Exiting Bundle Definition", expectedCode: http.StatusInternalServerError, inpUUID: "123e4567-e89b-12d3-a456-426655440000", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{}, + Items: []rb.Definition{}, Err: pkgerrors.New("Internal Error"), }, }, @@ -262,14 +262,14 @@ func TestVnfdGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd/"+testCase.inpUUID, nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition/"+testCase.inpUUID, nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdGetHandler) + hr := http.HandlerFunc(vh.getHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -279,11 +279,11 @@ func TestVnfdGetHandler(t *testing.T) { //Check returned body only if statusOK if rr.Code == http.StatusOK { - got := vnfd.VNFDefinition{} + got := rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdListHandler returned unexpected body: got %v;"+ + t.Errorf("listHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -291,25 +291,25 @@ func TestVnfdGetHandler(t *testing.T) { } } -func TestVnfdDeleteHandler(t *testing.T) { +func TestRBDefDeleteHandler(t *testing.T) { testCases := []struct { label string inpUUID string expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "Delete VNF Definition", + label: "Delete Bundle Definition", expectedCode: http.StatusNoContent, inpUUID: "123e4567-e89b-12d3-a456-426655441111", - vnfdClient: &mockVNFDefinition{}, + rbDefClient: &mockRBDefinition{}, }, { - label: "Delete Non-Exiting VNF Definition", + label: "Delete Non-Exiting Bundle Definition", expectedCode: http.StatusInternalServerError, inpUUID: "123e4567-e89b-12d3-a456-426655440000", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ Err: pkgerrors.New("Internal Error"), }, }, @@ -317,14 +317,14 @@ func TestVnfdDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd/"+testCase.inpUUID, nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition/"+testCase.inpUUID, nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdDeleteHandler) + hr := http.HandlerFunc(vh.deleteHandler) hr.ServeHTTP(rr, req) //Check returned code diff --git a/src/k8splugin/rb/definition.go b/src/k8splugin/rb/definition.go new file mode 100644 index 00000000..03fffdda --- /dev/null +++ b/src/k8splugin/rb/definition.go @@ -0,0 +1,135 @@ +/* + * Copyright 2018 Intel Corporation, Inc + * + * 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 rb + +import ( + "k8splugin/db" + "log" + + uuid "github.com/hashicorp/go-uuid" + pkgerrors "github.com/pkg/errors" +) + +// Definition contains the parameters needed for resource bundle (rb) definitions +// It implements the interface for managing the definitions +type Definition struct { + Name string `json:"name"` + Description string `json:"description"` + UUID string `json:"uuid,omitempty"` + ServiceType string `json:"service-type"` +} + +// DefinitionManager is an interface exposes the resource bundle definition functionality +type DefinitionManager interface { + Create(def Definition) (Definition, error) + List() ([]Definition, error) + Get(resID string) (Definition, error) + Delete(resID string) error +} + +// DefinitionClient implements the DefinitionManager +// It will also be used to maintain some localized state +type DefinitionClient struct { + keyPrefix string +} + +// NewDefinitionClient returns an instance of the DefinitionClient +// which implements the DefinitionManager +// Uses rb/def prefix +func NewDefinitionClient() *DefinitionClient { + return &DefinitionClient{ + keyPrefix: "rb/def/"} +} + +// Create an entry for the resource in the database +func (v *DefinitionClient) Create(def Definition) (Definition, error) { + // If UUID is empty, we will generate one + if def.UUID == "" { + def.UUID, _ = uuid.GenerateUUID() + } + key := v.keyPrefix + def.UUID + + serData, err := db.Serialize(v) + if err != nil { + return Definition{}, pkgerrors.Wrap(err, "Serialize Resource Bundle Definition") + } + + err = db.DBconn.Create(key, serData) + if err != nil { + return Definition{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return def, nil +} + +// List all resource entries in the database +func (v *DefinitionClient) List() ([]Definition, error) { + strArray, err := db.DBconn.ReadAll(v.keyPrefix) + if err != nil { + return []Definition{}, pkgerrors.Wrap(err, "Listing Resource Bundle Definitions") + } + + var retData []Definition + + for _, key := range strArray { + value, err := db.DBconn.Read(key) + if err != nil { + log.Printf("Error Reading Key: %s", key) + continue + } + if value != "" { + def := Definition{} + err = db.DeSerialize(value, &def) + if err != nil { + log.Printf("Error Deserializing Value: %s", value) + continue + } + retData = append(retData, def) + } + } + + return retData, nil +} + +// Get returns the Resource Bundle Definition for corresponding ID +func (v *DefinitionClient) Get(id string) (Definition, error) { + value, err := db.DBconn.Read(v.keyPrefix + id) + if err != nil { + return Definition{}, pkgerrors.Wrap(err, "Get Resource Bundle definition") + } + + if value != "" { + def := Definition{} + err = db.DeSerialize(value, &def) + if err != nil { + return Definition{}, pkgerrors.Wrap(err, "Deserializing Value") + } + return def, nil + } + + return Definition{}, pkgerrors.New("Error getting Resource Bundle Definition") +} + +// Delete the Resource Bundle definition from database +func (v *DefinitionClient) Delete(id string) error { + err := db.DBconn.Delete(v.keyPrefix + id) + if err != nil { + return pkgerrors.Wrap(err, "Delete Resource Bundle Definitions") + } + + return nil +} diff --git a/src/k8splugin/vnfd/vnfd_test.go b/src/k8splugin/rb/definition_test.go index 3230d3ef..a3993c8c 100644 --- a/src/k8splugin/vnfd/vnfd_test.go +++ b/src/k8splugin/rb/definition_test.go @@ -16,7 +16,7 @@ * limitations under the License. */ -package vnfd +package rb import ( "k8splugin/db" @@ -31,30 +31,30 @@ import ( func TestCreate(t *testing.T) { testCases := []struct { label string - inp VNFDefinition + inp Definition expectedError string mockdb *db.MockDB - expected VNFDefinition + expected Definition }{ { - label: "Create VNF Definition", - inp: VNFDefinition{ + label: "Create Resource Bundle Definition", + inp: Definition{ UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", - Description: "testvnf", + Name: "testresourcebundle", + Description: "testresourcebundle", ServiceType: "firewall", }, - expected: VNFDefinition{ + expected: Definition{ UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", - Description: "testvnf", + Name: "testresourcebundle", + Description: "testresourcebundle", ServiceType: "firewall", }, expectedError: "", mockdb: &db.MockDB{}, }, { - label: "Failed Create VNF Definition", + label: "Failed Create Resource Bundle Definition", expectedError: "Error Creating Definition", mockdb: &db.MockDB{ Err: pkgerrors.New("Error Creating Definition"), @@ -65,8 +65,8 @@ func TestCreate(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb - vimpl := GetVNFDClient() - got, err := vimpl.Create(testCase.inp) + impl := NewDefinitionClient() + got, err := impl.Create(testCase.inp) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) @@ -76,7 +76,7 @@ func TestCreate(t *testing.T) { } } else { if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("Create VNF returned unexpected body: got %v;"+ + t.Errorf("Create Resource Bundle returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -90,21 +90,21 @@ func TestList(t *testing.T) { label string expectedError string mockdb *db.MockDB - expected []VNFDefinition + expected []Definition }{ { - label: "List VNF Definition", - expected: []VNFDefinition{ + label: "List Resource Bundle Definition", + expected: []Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", - Description: "testvnf", + Name: "testresourcebundle", + Description: "testresourcebundle", ServiceType: "firewall", }, { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", - Description: "testvnf2", + Name: "testresourcebundle2", + Description: "testresourcebundle2", ServiceType: "dns", }, }, @@ -112,16 +112,16 @@ func TestList(t *testing.T) { mockdb: &db.MockDB{ Items: api.KVPairs{ &api.KVPair{ - Key: "vnfd/123e4567-e89b-12d3-a456-426655440000", - Value: []byte("{\"name\":\"testvnf\"," + - "\"description\":\"testvnf\"," + + Key: "rb/def/123e4567-e89b-12d3-a456-426655440000", + Value: []byte("{\"name\":\"testresourcebundle\"," + + "\"description\":\"testresourcebundle\"," + "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," + "\"service-type\":\"firewall\"}"), }, &api.KVPair{ - Key: "vnfd/123e4567-e89b-12d3-a456-426655441111", - Value: []byte("{\"name\":\"testvnf2\"," + - "\"description\":\"testvnf2\"," + + Key: "rb/def/123e4567-e89b-12d3-a456-426655441111", + Value: []byte("{\"name\":\"testresourcebundle2\"," + + "\"description\":\"testresourcebundle2\"," + "\"uuid\":\"123e4567-e89b-12d3-a456-426655441111\"," + "\"service-type\":\"dns\"}"), }, @@ -140,8 +140,8 @@ func TestList(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb - vimpl := GetVNFDClient() - got, err := vimpl.List() + impl := NewDefinitionClient() + got, err := impl.List() if err != nil { if testCase.expectedError == "" { t.Fatalf("List returned an unexpected error %s", err) @@ -151,7 +151,7 @@ func TestList(t *testing.T) { } } else { if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("List VNF returned unexpected body: got %v;"+ + t.Errorf("List Resource Bundle returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -166,24 +166,24 @@ func TestGet(t *testing.T) { expectedError string mockdb *db.MockDB inp string - expected VNFDefinition + expected Definition }{ { - label: "Get VNF Definition", + label: "Get Resource Bundle Definition", inp: "123e4567-e89b-12d3-a456-426655440000", - expected: VNFDefinition{ + expected: Definition{ UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", - Description: "testvnf", + Name: "testresourcebundle", + Description: "testresourcebundle", ServiceType: "firewall", }, expectedError: "", mockdb: &db.MockDB{ Items: api.KVPairs{ &api.KVPair{ - Key: "vnfd/123e4567-e89b-12d3-a456-426655440000", - Value: []byte("{\"name\":\"testvnf\"," + - "\"description\":\"testvnf\"," + + Key: "rb/def/123e4567-e89b-12d3-a456-426655440000", + Value: []byte("{\"name\":\"testresourcebundle\"," + + "\"description\":\"testresourcebundle\"," + "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," + "\"service-type\":\"firewall\"}"), }, @@ -202,8 +202,8 @@ func TestGet(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb - vimpl := GetVNFDClient() - got, err := vimpl.Get(testCase.inp) + impl := NewDefinitionClient() + got, err := impl.Get(testCase.inp) if err != nil { if testCase.expectedError == "" { t.Fatalf("Get returned an unexpected error %s", err) @@ -213,7 +213,7 @@ func TestGet(t *testing.T) { } } else { if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("Get VNF returned unexpected body: got %v;"+ + t.Errorf("Get Resource Bundle returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -228,10 +228,10 @@ func TestDelete(t *testing.T) { inp string expectedError string mockdb *db.MockDB - expected []VNFDefinition + expected []Definition }{ { - label: "Delete VNF Definition", + label: "Delete Resource Bundle Definition", inp: "123e4567-e89b-12d3-a456-426655440000", mockdb: &db.MockDB{}, }, @@ -247,8 +247,8 @@ func TestDelete(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb - vimpl := GetVNFDClient() - err := vimpl.Delete(testCase.inp) + impl := NewDefinitionClient() + err := impl.Delete(testCase.inp) if err != nil { if testCase.expectedError == "" { t.Fatalf("Delete returned an unexpected error %s", err) diff --git a/src/k8splugin/vnfd/vnfd.go b/src/k8splugin/vnfd/vnfd.go deleted file mode 100644 index 0fb81dbd..00000000 --- a/src/k8splugin/vnfd/vnfd.go +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2018 Intel Corporation, Inc - * - * 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 vnfd - -import ( - "k8splugin/db" - "log" - - uuid "github.com/hashicorp/go-uuid" - pkgerrors "github.com/pkg/errors" -) - -// VNFDefinition contains the parameters needed for VNF Definitions -// It implements the interface for managing the definitions -type VNFDefinition struct { - Name string `json:"name"` - Description string `json:"description"` - UUID string `json:"uuid,omitempty"` - ServiceType string `json:"service-type"` -} - -// VNFDefinitionInterface is an interface exposes the VNFDefinition functionality -type VNFDefinitionInterface interface { - Create(vnfd VNFDefinition) (VNFDefinition, error) - List() ([]VNFDefinition, error) - Get(vnfID string) (VNFDefinition, error) - Delete(vnfID string) error -} - -// VNFDefinitionClient implements the VNFDefinitionInterface -// It will also be used to maintain some localized state -type VNFDefinitionClient struct { - keyPrefix string -} - -// GetVNFDClient Returns an instance of the VNFDefinitionClient -// which implements the VNFDefinitionInterface interface -func GetVNFDClient() *VNFDefinitionClient { - return &VNFDefinitionClient{ - keyPrefix: "vnfd/"} -} - -// Create creates an entry for the VNF in the database -func (v *VNFDefinitionClient) Create(vnfd VNFDefinition) (VNFDefinition, error) { - // If UUID is empty, we will generate one - if vnfd.UUID == "" { - vnfd.UUID, _ = uuid.GenerateUUID() - } - key := v.keyPrefix + vnfd.UUID - - serData, err := db.Serialize(v) - if err != nil { - return VNFDefinition{}, pkgerrors.Wrap(err, "Serialize VNF Definition") - } - - err = db.DBconn.Create(key, serData) - if err != nil { - return VNFDefinition{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return vnfd, nil -} - -// List lists all vnf entries in the database -func (v *VNFDefinitionClient) List() ([]VNFDefinition, error) { - strArray, err := db.DBconn.ReadAll(v.keyPrefix) - if err != nil { - return []VNFDefinition{}, pkgerrors.Wrap(err, "Listing VNF Definitions") - } - - var retData []VNFDefinition - - for _, key := range strArray { - value, err := db.DBconn.Read(key) - if err != nil { - log.Printf("Error Reading Key: %s", key) - continue - } - if value != "" { - vnfd := VNFDefinition{} - err = db.DeSerialize(value, &vnfd) - if err != nil { - log.Printf("Error Deserializing Value: %s", value) - continue - } - retData = append(retData, vnfd) - } - } - - return retData, nil -} - -// Get returns the VNF Definition for corresponding ID -func (v *VNFDefinitionClient) Get(vnfID string) (VNFDefinition, error) { - value, err := db.DBconn.Read(v.keyPrefix + vnfID) - if err != nil { - return VNFDefinition{}, pkgerrors.Wrap(err, "Get VNF Definitions") - } - - if value != "" { - vnfd := VNFDefinition{} - err = db.DeSerialize(value, &vnfd) - if err != nil { - return VNFDefinition{}, pkgerrors.Wrap(err, "Deserializing Value") - } - return vnfd, nil - } - - return VNFDefinition{}, pkgerrors.New("Error getting VNF Definition") -} - -// Delete deletes the VNF Definition from database -func (v *VNFDefinitionClient) Delete(vnfID string) error { - err := db.DBconn.Delete(v.keyPrefix + vnfID) - if err != nil { - return pkgerrors.Wrap(err, "Delete VNF Definitions") - } - - return nil -} |