summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/k8splugin/api/api.go9
-rw-r--r--src/k8splugin/api/handler.go7
-rw-r--r--src/k8splugin/api/handler_test.go3
-rw-r--r--src/k8splugin/api/vnfdhandler.go128
-rw-r--r--src/k8splugin/api/vnfdhandler_test.go336
-rw-r--r--src/k8splugin/db/DB.go21
-rw-r--r--src/k8splugin/db/consul.go3
-rw-r--r--src/k8splugin/db/db_test.go57
-rw-r--r--src/k8splugin/go.mod1
-rw-r--r--src/k8splugin/go.sum3
-rw-r--r--src/k8splugin/mock_files/mock_json/create_vnfd.json6
-rw-r--r--src/k8splugin/vnfd/vnfd.go134
-rw-r--r--src/k8splugin/vnfd/vnfd_test.go305
13 files changed, 1008 insertions, 5 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go
index 53db3fb5..e90c994e 100644
--- a/src/k8splugin/api/api.go
+++ b/src/k8splugin/api/api.go
@@ -14,6 +14,7 @@ limitations under the License.
package api
import (
+ "k8splugin/vnfd"
"os"
"path/filepath"
"plugin"
@@ -110,6 +111,14 @@ 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")
+
// (TODO): Fix update method
// vnfInstanceHandler.HandleFunc("/{vnfInstanceId}", UpdateHandler).Methods("PUT")
diff --git a/src/k8splugin/api/handler.go b/src/k8splugin/api/handler.go
index 4635e7ba..b4828b76 100644
--- a/src/k8splugin/api/handler.go
+++ b/src/k8splugin/api/handler.go
@@ -118,13 +118,12 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) {
// krd.AddNetworkAnnotationsToPod(kubeData, resource.Networks)
// "{"deployment":<>,"service":<>}"
- out, err := json.Marshal(resourceNameMap)
+ serializedResourceNameMap, err := db.Serialize(resourceNameMap)
if err != nil {
werr := pkgerrors.Wrap(err, "Create VNF deployment JSON Marshalling error")
http.Error(w, werr.Error(), http.StatusInternalServerError)
return
}
- serializedResourceNameMap := string(out)
// key: cloud1-default-uuid
// value: "{"deployment":<>,"service":<>}"
@@ -232,7 +231,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
},
*/
deserializedResourceNameMap := make(map[string][]string)
- err = json.Unmarshal([]byte(serializedResourceNameMap), &deserializedResourceNameMap)
+ err = db.DeSerialize(serializedResourceNameMap, &deserializedResourceNameMap)
if err != nil {
werr := pkgerrors.Wrap(err, "Delete VNF error")
http.Error(w, werr.Error(), http.StatusInternalServerError)
@@ -357,7 +356,7 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
},
*/
deserializedResourceNameMap := make(map[string][]string)
- err = json.Unmarshal([]byte(serializedResourceNameMap), &deserializedResourceNameMap)
+ err = db.DeSerialize(serializedResourceNameMap, &deserializedResourceNameMap)
if err != nil {
werr := pkgerrors.Wrap(err, "Get VNF error")
http.Error(w, werr.Error(), http.StatusInternalServerError)
diff --git a/src/k8splugin/api/handler_test.go b/src/k8splugin/api/handler_test.go
index 8d990daa..ac97d011 100644
--- a/src/k8splugin/api/handler_test.go
+++ b/src/k8splugin/api/handler_test.go
@@ -29,6 +29,9 @@ import (
"k8splugin/db"
)
+//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 mockDB struct {
db.DatabaseConnection
}
diff --git a/src/k8splugin/api/vnfdhandler.go b/src/k8splugin/api/vnfdhandler.go
new file mode 100644
index 00000000..ff777826
--- /dev/null
+++ b/src/k8splugin/api/vnfdhandler.go
@@ -0,0 +1,128 @@
+/*
+ * 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 api
+
+import (
+ "encoding/json"
+ "net/http"
+
+ "k8splugin/vnfd"
+
+ "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
+ // We will set this variable with a mock interface for testing
+ vnfdClient vnfd.VNFDefinitionInterface
+}
+
+// vnfdCreateHandler handles creation of the vnfd entry in the database
+func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) {
+ var v vnfd.VNFDefinition
+
+ if r.Body == nil {
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ }
+
+ err := json.NewDecoder(r.Body).Decode(&v)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ // Name is required.
+ if v.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.vnfdClient.Create(v)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// vnfdUploadHandler handles upload of the vnf tar file into the database
+// Note: This will be implemented in a different patch
+func (h vnfdHandler) vnfdUploadHandler(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()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// vnfdGetHandler handles GET operations on a particular VNFID
+// Returns a vnfd.VNFDefinition
+func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ vnfdID := vars["vnfdID"]
+
+ ret, err := h.vnfdClient.Get(vnfdID)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// vnfdDeleteHandler handles DELETE operations on a particular VNFID
+func (h vnfdHandler) vnfdDeleteHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ vnfdID := vars["vnfdID"]
+
+ err := h.vnfdClient.Delete(vnfdID)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
diff --git a/src/k8splugin/api/vnfdhandler_test.go b/src/k8splugin/api/vnfdhandler_test.go
new file mode 100644
index 00000000..e393be6f
--- /dev/null
+++ b/src/k8splugin/api/vnfdhandler_test.go
@@ -0,0 +1,336 @@
+/*
+ * 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 api
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "k8splugin/vnfd"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "testing"
+
+ pkgerrors "github.com/pkg/errors"
+)
+
+//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
+ // Items and err will be used to customize each test
+ // via a localized instantiation of mockVNFDefinition
+ Items []vnfd.VNFDefinition
+ Err error
+}
+
+func (m *mockVNFDefinition) Create(inp vnfd.VNFDefinition) (vnfd.VNFDefinition, error) {
+ if m.Err != nil {
+ return vnfd.VNFDefinition{}, m.Err
+ }
+
+ return m.Items[0], nil
+}
+
+func (m *mockVNFDefinition) List() ([]vnfd.VNFDefinition, error) {
+ if m.Err != nil {
+ return []vnfd.VNFDefinition{}, m.Err
+ }
+
+ return m.Items, nil
+}
+
+func (m *mockVNFDefinition) Get(vnfID string) (vnfd.VNFDefinition, error) {
+ if m.Err != nil {
+ return vnfd.VNFDefinition{}, m.Err
+ }
+
+ return m.Items[0], nil
+}
+
+func (m *mockVNFDefinition) Delete(vnfID string) error {
+ return m.Err
+}
+
+func TestVnfdCreateHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ reader io.Reader
+ expected vnfd.VNFDefinition
+ expectedCode int
+ vnfdClient *mockVNFDefinition
+ }{
+ {
+ label: "Missing Body Failure",
+ expectedCode: http.StatusBadRequest,
+ vnfdClient: &mockVNFDefinition{},
+ },
+ {
+ label: "Create without UUID",
+ expectedCode: http.StatusCreated,
+ reader: bytes.NewBuffer([]byte(`{
+ "name":"testdomain",
+ "description":"test description",
+ "service-type":"firewall"
+ }`)),
+ expected: vnfd.VNFDefinition{
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "test description",
+ ServiceType: "firewall",
+ },
+ vnfdClient: &mockVNFDefinition{
+ //Items that will be returned by the mocked Client
+ Items: []vnfd.VNFDefinition{
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "test description",
+ ServiceType: "firewall",
+ },
+ },
+ },
+ },
+ }
+
+ 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)
+
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ hr := http.HandlerFunc(vh.vnfdCreateHandler)
+ hr.ServeHTTP(rr, req)
+
+ //Check returned code
+ if rr.Code != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ }
+
+ //Check returned body only if statusCreated
+ if rr.Code == http.StatusCreated {
+ got := vnfd.VNFDefinition{}
+ json.NewDecoder(rr.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("vnfdCreateHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestVnfdListHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected []vnfd.VNFDefinition
+ expectedCode int
+ vnfdClient *mockVNFDefinition
+ }{
+ {
+ label: "List VNF Definitions",
+ expectedCode: http.StatusOK,
+ expected: []vnfd.VNFDefinition{
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "test description",
+ ServiceType: "firewall",
+ },
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655441111",
+ Name: "testvnf2",
+ Description: "test description",
+ ServiceType: "dns",
+ },
+ },
+ vnfdClient: &mockVNFDefinition{
+ // list of definitions that will be returned by the mockclient
+ Items: []vnfd.VNFDefinition{
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "test description",
+ ServiceType: "firewall",
+ },
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655441111",
+ Name: "testvnf2",
+ Description: "test description",
+ ServiceType: "dns",
+ },
+ },
+ },
+ },
+ }
+
+ 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)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ hr := http.HandlerFunc(vh.vnfdListHandler)
+
+ hr.ServeHTTP(rr, req)
+ //Check returned code
+ if rr.Code != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ }
+
+ //Check returned body only if statusOK
+ if rr.Code == http.StatusOK {
+ got := []vnfd.VNFDefinition{}
+ json.NewDecoder(rr.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("vnfdListHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestVnfdGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected vnfd.VNFDefinition
+ inpUUID string
+ expectedCode int
+ vnfdClient *mockVNFDefinition
+ }{
+ {
+ label: "Get VNF Definition",
+ expectedCode: http.StatusOK,
+ expected: vnfd.VNFDefinition{
+ UUID: "123e4567-e89b-12d3-a456-426655441111",
+ Name: "testvnf2",
+ Description: "test description",
+ ServiceType: "dns",
+ },
+ inpUUID: "123e4567-e89b-12d3-a456-426655441111",
+ vnfdClient: &mockVNFDefinition{
+ // list of definitions that will be returned by the mockclient
+ Items: []vnfd.VNFDefinition{
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655441111",
+ Name: "testvnf2",
+ Description: "test description",
+ ServiceType: "dns",
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Exiting VNF Definition",
+ expectedCode: http.StatusInternalServerError,
+ inpUUID: "123e4567-e89b-12d3-a456-426655440000",
+ vnfdClient: &mockVNFDefinition{
+ // list of definitions that will be returned by the mockclient
+ Items: []vnfd.VNFDefinition{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ 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)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ hr := http.HandlerFunc(vh.vnfdGetHandler)
+
+ hr.ServeHTTP(rr, req)
+ //Check returned code
+ if rr.Code != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ }
+
+ //Check returned body only if statusOK
+ if rr.Code == http.StatusOK {
+ got := vnfd.VNFDefinition{}
+ json.NewDecoder(rr.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("vnfdListHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestVnfdDeleteHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ inpUUID string
+ expectedCode int
+ vnfdClient *mockVNFDefinition
+ }{
+ {
+ label: "Delete VNF Definition",
+ expectedCode: http.StatusNoContent,
+ inpUUID: "123e4567-e89b-12d3-a456-426655441111",
+ vnfdClient: &mockVNFDefinition{},
+ },
+ {
+ label: "Delete Non-Exiting VNF Definition",
+ expectedCode: http.StatusInternalServerError,
+ inpUUID: "123e4567-e89b-12d3-a456-426655440000",
+ vnfdClient: &mockVNFDefinition{
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ 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)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ hr := http.HandlerFunc(vh.vnfdDeleteHandler)
+
+ hr.ServeHTTP(rr, req)
+ //Check returned code
+ if rr.Code != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ }
+ })
+ }
+}
diff --git a/src/k8splugin/db/DB.go b/src/k8splugin/db/DB.go
index c8895088..d92b5953 100644
--- a/src/k8splugin/db/DB.go
+++ b/src/k8splugin/db/DB.go
@@ -14,6 +14,9 @@ limitations under the License.
package db
import (
+ "encoding/json"
+ "reflect"
+
pkgerrors "github.com/pkg/errors"
)
@@ -40,3 +43,21 @@ var CreateDBClient = func(dbType string) error {
return pkgerrors.New(dbType + "DB not supported")
}
}
+
+// Serialize converts given data into a JSON string
+func Serialize(v interface{}) (string, error) {
+ out, err := json.Marshal(v)
+ if err != nil {
+ return "", pkgerrors.Wrap(err, "Error serializing "+reflect.TypeOf(v).String())
+ }
+ return string(out), nil
+}
+
+// DeSerialize converts string to a json object specified by type
+func DeSerialize(str string, v interface{}) error {
+ err := json.Unmarshal([]byte(str), &v)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error deSerializing "+str)
+ }
+ return nil
+}
diff --git a/src/k8splugin/db/consul.go b/src/k8splugin/db/consul.go
index 686d9348..950eea34 100644
--- a/src/k8splugin/db/consul.go
+++ b/src/k8splugin/db/consul.go
@@ -14,9 +14,10 @@ limitations under the License.
package db
import (
+ "os"
+
consulapi "github.com/hashicorp/consul/api"
pkgerrors "github.com/pkg/errors"
- "os"
)
// ConsulDB is an implementation of the DatabaseConnection interface
diff --git a/src/k8splugin/db/db_test.go b/src/k8splugin/db/db_test.go
index a5dc0eb8..d37cd7ae 100644
--- a/src/k8splugin/db/db_test.go
+++ b/src/k8splugin/db/db_test.go
@@ -40,3 +40,60 @@ func TestCreateDBClient(t *testing.T) {
}
})
}
+
+func TestSerialize(t *testing.T) {
+
+ inp := map[string]interface{}{
+ "UUID": "123e4567-e89b-12d3-a456-426655440000",
+ "Data": "sdaijsdiodalkfjsdlagf",
+ "Number": 23,
+ "Float": 34.4,
+ "Map": map[string]interface{}{
+ "m1": "m1",
+ "m2": 2,
+ "m3": 3.0,
+ },
+ }
+
+ got, err := Serialize(inp)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected := "{\"Data\":\"sdaijsdiodalkfjsdlagf\"," +
+ "\"Float\":34.4,\"Map\":{\"m1\":\"m1\",\"m2\":2,\"m3\":3}," +
+ "\"Number\":23,\"UUID\":\"123e4567-e89b-12d3-a456-426655440000\"}"
+
+ if expected != got {
+ t.Errorf("Serialize returned unexpected string: %s;"+
+ " expected %sv", got, expected)
+ }
+}
+
+func TestDeSerialize(t *testing.T) {
+
+ inp := "{\"Data\":\"sdaijsdiodalkfjsdlagf\"," +
+ "\"Float\":34.4,\"Map\":{\"m1\":\"m1\",\"m3\":3}," +
+ "\"UUID\":\"123e4567-e89b-12d3-a456-426655440000\"}"
+
+ got := make(map[string]interface{})
+ err := DeSerialize(inp, &got)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected := map[string]interface{}{
+ "UUID": "123e4567-e89b-12d3-a456-426655440000",
+ "Data": "sdaijsdiodalkfjsdlagf",
+ "Float": 34.4,
+ "Map": map[string]interface{}{
+ "m1": "m1",
+ "m3": 3.0,
+ },
+ }
+
+ if reflect.DeepEqual(expected, got) == false {
+ t.Errorf("Serialize returned unexpected : %s;"+
+ " expected %s", got, expected)
+ }
+}
diff --git a/src/k8splugin/go.mod b/src/k8splugin/go.mod
index 3652afba..b4f4558b 100644
--- a/src/k8splugin/go.mod
+++ b/src/k8splugin/go.mod
@@ -13,6 +13,7 @@ require (
github.com/hashicorp/consul v1.2.2
github.com/hashicorp/go-cleanhttp v0.0.0-20171218145408-d5fe4b57a186
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90
+ github.com/hashicorp/go-uuid v1.0.0
github.com/hashicorp/serf v0.8.1
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
github.com/imdario/mergo v0.3.5
diff --git a/src/k8splugin/go.sum b/src/k8splugin/go.sum
index 56311a99..4a10051a 100644
--- a/src/k8splugin/go.sum
+++ b/src/k8splugin/go.sum
@@ -21,6 +21,8 @@ github.com/hashicorp/go-cleanhttp v0.0.0-20171218145408-d5fe4b57a186 h1:URgjUo+b
github.com/hashicorp/go-cleanhttp v0.0.0-20171218145408-d5fe4b57a186/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90 h1:9HVkPxOpo+yO93Ah4yrO67d/qh0fbLLWbKqhYjyHq9A=
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90/go.mod h1:o4zcYY1e0GEZI6eSEr+43QDYmuGglw1qSO6qdHUHCgg=
+github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
+github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/serf v0.8.1 h1:mYs6SMzu72+90OcPa5wr3nfznA4Dw9UyR791ZFNOIf4=
github.com/hashicorp/serf v0.8.1/go.mod h1:h/Ru6tmZazX7WO/GDmwdpS975F019L4t5ng5IgwbNrE=
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c h1:kQWxfPIHVLbgLzphqk3QUflDy9QdksZR4ygR807bpy0=
@@ -29,6 +31,7 @@ github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/json-iterator/go v0.0.0-20180315132816-ca39e5af3ece h1:3HJXp/18JmMk5sjBP3LDUBtWjczCvynxaeAF6b6kWp8=
github.com/json-iterator/go v0.0.0-20180315132816-ca39e5af3ece/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
+github.com/mitchellh/go-homedir v0.0.0-20180801233206-58046073cbff h1:jM4Eo4qMmmcqePS3u6X2lcEELtVuXWkWJIS/pRI3oSk=
github.com/mitchellh/go-homedir v0.0.0-20180801233206-58046073cbff/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 h1:KXZJFdun9knAVAR8tg/aHJEr5DgtcbqyvzacK+CDCaI=
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
diff --git a/src/k8splugin/mock_files/mock_json/create_vnfd.json b/src/k8splugin/mock_files/mock_json/create_vnfd.json
new file mode 100644
index 00000000..64a186b2
--- /dev/null
+++ b/src/k8splugin/mock_files/mock_json/create_vnfd.json
@@ -0,0 +1,6 @@
+{
+ "name": "test-vnfd",
+ "description": "testing vnfd creation api",
+ "uuid": "",
+ "service-type": "firewall"
+} \ No newline at end of file
diff --git a/src/k8splugin/vnfd/vnfd.go b/src/k8splugin/vnfd/vnfd.go
new file mode 100644
index 00000000..322b2d78
--- /dev/null
+++ b/src/k8splugin/vnfd/vnfd.go
@@ -0,0 +1,134 @@
+/*
+ * 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.CreateEntry(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, ok, err := db.DBconn.ReadEntry(key)
+ if err != nil {
+ log.Printf("Error Reading Key: %s", key)
+ continue
+ }
+ if ok {
+ 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, ok, err := db.DBconn.ReadEntry(v.keyPrefix + vnfID)
+ if err != nil {
+ return VNFDefinition{}, pkgerrors.Wrap(err, "Get VNF Definitions")
+ }
+
+ if ok {
+ 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.DeleteEntry(v.keyPrefix + vnfID)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Delete VNF Definitions")
+ }
+
+ return nil
+}
diff --git a/src/k8splugin/vnfd/vnfd_test.go b/src/k8splugin/vnfd/vnfd_test.go
new file mode 100644
index 00000000..54ab5f49
--- /dev/null
+++ b/src/k8splugin/vnfd/vnfd_test.go
@@ -0,0 +1,305 @@
+/*
+ * 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"
+ "reflect"
+ "strings"
+ "testing"
+
+ "github.com/hashicorp/consul/api"
+ pkgerrors "github.com/pkg/errors"
+)
+
+//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 mockDB struct {
+ db.DatabaseConnection
+ Items api.KVPairs
+ Err error
+}
+
+func (m *mockDB) CreateEntry(key string, value string) error {
+ return m.Err
+}
+
+func (m *mockDB) ReadEntry(key string) (string, bool, error) {
+ if m.Err != nil {
+ return "", false, m.Err
+ }
+
+ for _, kvpair := range m.Items {
+ if kvpair.Key == key {
+ return string(kvpair.Value), true, nil
+ }
+ }
+
+ return "", false, nil
+}
+
+func (m *mockDB) DeleteEntry(key string) error {
+ return m.Err
+}
+
+func (m *mockDB) ReadAll(prefix string) ([]string, error) {
+ if m.Err != nil {
+ return []string{}, m.Err
+ }
+
+ var res []string
+
+ for _, keypair := range m.Items {
+ res = append(res, keypair.Key)
+ }
+
+ return res, nil
+}
+
+func TestCreate(t *testing.T) {
+ testCases := []struct {
+ label string
+ inp VNFDefinition
+ expectedError string
+ mockdb *mockDB
+ expected VNFDefinition
+ }{
+ {
+ label: "Create VNF Definition",
+ inp: VNFDefinition{
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "testvnf",
+ ServiceType: "firewall",
+ },
+ expected: VNFDefinition{
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "testvnf",
+ ServiceType: "firewall",
+ },
+ expectedError: "",
+ mockdb: &mockDB{},
+ },
+ {
+ label: "Failed Create VNF Definition",
+ expectedError: "Error Creating Definition",
+ mockdb: &mockDB{
+ Err: pkgerrors.New("Error Creating Definition"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ db.DBconn = testCase.mockdb
+ vimpl := GetVNFDClient()
+ got, err := vimpl.Create(testCase.inp)
+ if err != nil {
+ if testCase.expectedError == "" {
+ t.Fatalf("Create returned an unexpected error %s", err)
+ }
+ if strings.Contains(err.Error(), testCase.expectedError) == false {
+ t.Fatalf("Create returned an unexpected error %s", err)
+ }
+ } else {
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("Create VNF returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestList(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expectedError string
+ mockdb *mockDB
+ expected []VNFDefinition
+ }{
+ {
+ label: "List VNF Definition",
+ expected: []VNFDefinition{
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "testvnf",
+ ServiceType: "firewall",
+ },
+ {
+ UUID: "123e4567-e89b-12d3-a456-426655441111",
+ Name: "testvnf2",
+ Description: "testvnf2",
+ ServiceType: "dns",
+ },
+ },
+ expectedError: "",
+ mockdb: &mockDB{
+ Items: api.KVPairs{
+ &api.KVPair{
+ Key: "vnfd/123e4567-e89b-12d3-a456-426655440000",
+ Value: []byte("{\"name\":\"testvnf\"," +
+ "\"description\":\"testvnf\"," +
+ "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
+ "\"service-type\":\"firewall\"}"),
+ },
+ &api.KVPair{
+ Key: "vnfd/123e4567-e89b-12d3-a456-426655441111",
+ Value: []byte("{\"name\":\"testvnf2\"," +
+ "\"description\":\"testvnf2\"," +
+ "\"uuid\":\"123e4567-e89b-12d3-a456-426655441111\"," +
+ "\"service-type\":\"dns\"}"),
+ },
+ },
+ },
+ },
+ {
+ label: "List Error",
+ expectedError: "DB Error",
+ mockdb: &mockDB{
+ Err: pkgerrors.New("DB Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ db.DBconn = testCase.mockdb
+ vimpl := GetVNFDClient()
+ got, err := vimpl.List()
+ if err != nil {
+ if testCase.expectedError == "" {
+ t.Fatalf("List returned an unexpected error %s", err)
+ }
+ if strings.Contains(err.Error(), testCase.expectedError) == false {
+ t.Fatalf("List returned an unexpected error %s", err)
+ }
+ } else {
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("List VNF returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestGet(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expectedError string
+ mockdb *mockDB
+ inp string
+ expected VNFDefinition
+ }{
+ {
+ label: "Get VNF Definition",
+ inp: "123e4567-e89b-12d3-a456-426655440000",
+ expected: VNFDefinition{
+ UUID: "123e4567-e89b-12d3-a456-426655440000",
+ Name: "testvnf",
+ Description: "testvnf",
+ ServiceType: "firewall",
+ },
+ expectedError: "",
+ mockdb: &mockDB{
+ Items: api.KVPairs{
+ &api.KVPair{
+ Key: "vnfd/123e4567-e89b-12d3-a456-426655440000",
+ Value: []byte("{\"name\":\"testvnf\"," +
+ "\"description\":\"testvnf\"," +
+ "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
+ "\"service-type\":\"firewall\"}"),
+ },
+ },
+ },
+ },
+ {
+ label: "Get Error",
+ expectedError: "DB Error",
+ mockdb: &mockDB{
+ Err: pkgerrors.New("DB Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ db.DBconn = testCase.mockdb
+ vimpl := GetVNFDClient()
+ got, err := vimpl.Get(testCase.inp)
+ if err != nil {
+ if testCase.expectedError == "" {
+ t.Fatalf("Get returned an unexpected error %s", err)
+ }
+ if strings.Contains(err.Error(), testCase.expectedError) == false {
+ t.Fatalf("Get returned an unexpected error %s", err)
+ }
+ } else {
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("Get VNF returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestDelete(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ inp string
+ expectedError string
+ mockdb *mockDB
+ expected []VNFDefinition
+ }{
+ {
+ label: "Delete VNF Definition",
+ inp: "123e4567-e89b-12d3-a456-426655440000",
+ mockdb: &mockDB{},
+ },
+ {
+ label: "Delete Error",
+ expectedError: "DB Error",
+ mockdb: &mockDB{
+ Err: pkgerrors.New("DB Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ db.DBconn = testCase.mockdb
+ vimpl := GetVNFDClient()
+ err := vimpl.Delete(testCase.inp)
+ if err != nil {
+ if testCase.expectedError == "" {
+ t.Fatalf("Delete returned an unexpected error %s", err)
+ }
+ if strings.Contains(err.Error(), testCase.expectedError) == false {
+ t.Fatalf("Delete returned an unexpected error %s", err)
+ }
+ }
+ })
+ }
+}