aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Multanen <eric.w.multanen@intel.com>2020-04-27 17:33:25 -0700
committerEric Multanen <eric.w.multanen@intel.com>2020-04-30 10:44:03 -0700
commit4100bfb764c40e2a788d28691b0f891e2ae86b74 (patch)
tree40ea142bf10236b53ca1479e99193094e06ce7c9 /src
parent7f6d2717e367c007ad836c39626c9eacc572f068 (diff)
Add API support for network chain intent
Adds API for managing Network Chain intents for composite applications. Issue-ID: MULTICLOUD-1029 Signed-off-by: Eric Multanen <eric.w.multanen@intel.com> Change-Id: Ie3d4e8f1e3179397d86829d951e2bb877597b38d
Diffstat (limited to 'src')
-rw-r--r--src/ncm/api/api.go16
-rw-r--r--src/ncm/api/chainhandler.go290
-rw-r--r--src/ncm/api/chainhandler_test.go56
-rw-r--r--src/ncm/pkg/module/chaining.go209
-rw-r--r--src/ncm/pkg/module/module.go2
5 files changed, 573 insertions, 0 deletions
diff --git a/src/ncm/api/api.go b/src/ncm/api/api.go
index 2b105716..29ed46bd 100644
--- a/src/ncm/api/api.go
+++ b/src/ncm/api/api.go
@@ -72,6 +72,13 @@ func setClient(client, testClient interface{}) interface{} {
return c
}
}
+ case *moduleLib.ChainClient:
+ if testClient != nil && reflect.TypeOf(testClient).Implements(reflect.TypeOf((*moduleLib.ChainManager)(nil)).Elem()) {
+ c, ok := testClient.(moduleLib.ChainManager)
+ if ok {
+ return c
+ }
+ }
default:
fmt.Printf("unknown type %T\n", cl)
}
@@ -155,5 +162,14 @@ func NewRouter(testClient interface{}) *mux.Router {
router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/workload-intents/{workload-intent}/interfaces/{name}", workloadifintentHandler.getHandler).Methods("GET")
router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/workload-intents/{workload-intent}/interfaces/{name}", workloadifintentHandler.deleteHandler).Methods("DELETE")
+ chainHandler := chainHandler{
+ client: setClient(moduleClient.Chain, testClient).(moduleLib.ChainManager),
+ }
+ router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/network-chains", chainHandler.createHandler).Methods("POST")
+ router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/network-chains", chainHandler.getHandler).Methods("GET")
+ router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/network-chains/{name}", chainHandler.putHandler).Methods("PUT")
+ router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/network-chains/{name}", chainHandler.getHandler).Methods("GET")
+ router.HandleFunc("/projects/{project}/composite-apps/{composite-app-name}/{version}/network-controller-intent/{net-control-intent}/network-chains/{name}", chainHandler.deleteHandler).Methods("DELETE")
+
return router
}
diff --git a/src/ncm/api/chainhandler.go b/src/ncm/api/chainhandler.go
new file mode 100644
index 00000000..7169014b
--- /dev/null
+++ b/src/ncm/api/chainhandler.go
@@ -0,0 +1,290 @@
+/*
+ * Copyright 2020 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"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+
+ moduleLib "github.com/onap/multicloud-k8s/src/ncm/pkg/module"
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation"
+ pkgerrors "github.com/pkg/errors"
+
+ "github.com/gorilla/mux"
+)
+
+// Used to store backend implementations objects
+// Also simplifies mocking for unit testing purposes
+type chainHandler struct {
+ // Interface that implements workload intent operations
+ // We will set this variable with a mock interface for testing
+ client moduleLib.ChainManager
+}
+
+func validateRoutingNetwork(r moduleLib.RoutingNetwork) error {
+ errs := validation.IsValidName(r.NetworkName)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid routing network name: %v", errs)
+ }
+
+ err := validation.IsIpv4Cidr(r.Subnet)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Invalid routing network subnet")
+ }
+
+ err = validation.IsIpv4(r.GatewayIP)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Invalid routing network gateway IP")
+ }
+
+ return nil
+}
+
+// validateNetworkChain checks that the network chain string input follows the
+// generic format: "app=app1,net1,app=app2,net2, ..... ,netN-1,app=appN"
+// assume "app=app1" can conform to validation.IsValidLabel() with an "="
+func validateNetworkChain(chain string) error {
+ elems := strings.Split(chain, ",")
+
+ // chain needs at least two apps and a network
+ if len(elems) < 3 {
+ return pkgerrors.Errorf("Network chain is too short")
+ }
+
+ // chain needs to have an odd number of elements
+ if len(elems)%2 == 0 {
+ return pkgerrors.Errorf("Invalid network chain - even number of elements")
+ }
+
+ for i, s := range elems {
+ // allows whitespace in comma separated elements
+ t := strings.TrimSpace(s)
+ // if even element, verify a=b format
+ if i%2 == 0 {
+ if strings.Index(t, "=") < 1 {
+ return pkgerrors.Errorf("Invalid deployment label element of network chain")
+ }
+ errs := validation.IsValidLabel(t)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid deployment label element: %v", errs)
+ }
+ } else {
+ errs := validation.IsValidName(t)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid network element of network chain: %v", errs)
+ }
+ }
+ }
+ return nil
+}
+
+// Check for valid format of input parameters
+func validateChainInputs(ch moduleLib.Chain) error {
+ // validate metadata
+ err := moduleLib.IsValidMetadata(ch.Metadata)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Invalid network chain metadata")
+ }
+
+ if strings.ToLower(ch.Spec.ChainType) != moduleLib.RoutingChainType {
+ return pkgerrors.Wrap(err, "Invalid network chain type")
+ }
+
+ for _, r := range ch.Spec.RoutingSpec.LeftNetwork {
+ err = validateRoutingNetwork(r)
+ if err != nil {
+ return err
+ }
+ }
+
+ for _, r := range ch.Spec.RoutingSpec.RightNetwork {
+ err = validateRoutingNetwork(r)
+ if err != nil {
+ return err
+ }
+ }
+
+ err = validateNetworkChain(ch.Spec.RoutingSpec.NetworkChain)
+ if err != nil {
+ return err
+ }
+
+ errs := validation.IsValidName(ch.Spec.RoutingSpec.Namespace)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid network chain route spec namespace: %v", errs)
+ }
+
+ return nil
+}
+
+// Create handles creation of the Chain entry in the database
+func (h chainHandler) createHandler(w http.ResponseWriter, r *http.Request) {
+ var ch moduleLib.Chain
+ vars := mux.Vars(r)
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+ netControlIntent := vars["net-control-intent"]
+
+ err := json.NewDecoder(r.Body).Decode(&ch)
+
+ switch {
+ case err == io.EOF:
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ case err != nil:
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ // Name is required.
+ if ch.Metadata.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ err = validateChainInputs(ch)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateChain(ch, project, compositeApp, compositeAppVersion, netControlIntent, false)
+ 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
+ }
+}
+
+// Put handles creation/update of the Chain entry in the database
+func (h chainHandler) putHandler(w http.ResponseWriter, r *http.Request) {
+ var ch moduleLib.Chain
+ vars := mux.Vars(r)
+ name := vars["name"]
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+ netControlIntent := vars["net-control-intent"]
+
+ err := json.NewDecoder(r.Body).Decode(&ch)
+
+ switch {
+ case err == io.EOF:
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ case err != nil:
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ // Name is required.
+ if ch.Metadata.Name == "" {
+ http.Error(w, "Missing name in PUT request", http.StatusBadRequest)
+ return
+ }
+
+ // Name in URL should match name in body
+ if ch.Metadata.Name != name {
+ fmt.Printf("bodyname = %v, name= %v\n", ch.Metadata.Name, name)
+ http.Error(w, "Mismatched name in PUT request", http.StatusBadRequest)
+ return
+ }
+
+ err = validateChainInputs(ch)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateChain(ch, project, compositeApp, compositeAppVersion, netControlIntent, true)
+ 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
+ }
+}
+
+// Get handles GET operations on a particular Chain Name
+// Returns a Chain
+func (h chainHandler) getHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["name"]
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+ netControlIntent := vars["net-control-intent"]
+ var ret interface{}
+ var err error
+
+ if len(name) == 0 {
+ ret, err = h.client.GetChains(project, compositeApp, compositeAppVersion, netControlIntent)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+ ret, err = h.client.GetChain(name, project, compositeApp, compositeAppVersion, netControlIntent)
+ 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
+ }
+}
+
+// Delete handles DELETE operations on a particular Chain
+func (h chainHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["name"]
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+ netControlIntent := vars["net-control-intent"]
+
+ err := h.client.DeleteChain(name, project, compositeApp, compositeAppVersion, netControlIntent)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
diff --git a/src/ncm/api/chainhandler_test.go b/src/ncm/api/chainhandler_test.go
new file mode 100644
index 00000000..f13a90c4
--- /dev/null
+++ b/src/ncm/api/chainhandler_test.go
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2020 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 (
+ "testing"
+)
+
+func TestIsValidNetworkChain(t *testing.T) {
+ t.Run("Valid Chains", func(t *testing.T) {
+ validchains := []string{
+ "app=abc,net1,app=xyz",
+ "app=abc, net1, app=xyz",
+ " app=abc , net1 , app=xyz ",
+ "app.kubernets.io/name=abc,net1,app.kubernets.io/name=xyz",
+ "app.kubernets.io/name=abc,net1,app.kubernets.io/name=xyz, net2, anotherlabel=wex",
+ }
+ for _, chain := range validchains {
+ err := validateNetworkChain(chain)
+ if err != nil {
+ t.Errorf("Valid network chain failed to pass: %v %v", chain, err)
+ }
+ }
+ })
+
+ t.Run("Invalid Chains", func(t *testing.T) {
+ invalidchains := []string{
+ "",
+ "app=abc,net1,app= xyz",
+ "app=abc,net1,xyz",
+ "app=abc,net1",
+ "app.kubernets.io/name=abc,net1,=xyz",
+ "abcdefg",
+ }
+ for _, chain := range invalidchains {
+ err := validateNetworkChain(chain)
+ if err == nil {
+ t.Errorf("Invalid network chain passed: %v", chain)
+ }
+ }
+ })
+}
diff --git a/src/ncm/pkg/module/chaining.go b/src/ncm/pkg/module/chaining.go
new file mode 100644
index 00000000..45f061fa
--- /dev/null
+++ b/src/ncm/pkg/module/chaining.go
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2020 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 module
+
+import (
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
+
+ pkgerrors "github.com/pkg/errors"
+)
+
+// Chain defines the high level structure of a network chain document
+type Chain struct {
+ Metadata Metadata `json:"metadata" yaml:"metadata"`
+ Spec NetworkChainingSpec `json:"spec" yaml:"spec"`
+}
+
+// NetworkChainingSpec contains the specification of a network chain
+type NetworkChainingSpec struct {
+ ChainType string `json:"type"`
+ RoutingSpec RouteSpec `json:"routingSpec"`
+}
+
+// RouteSpec contains the routing specificaiton of a network chain
+type RouteSpec struct {
+ LeftNetwork []RoutingNetwork `json:"leftNetwork"`
+ RightNetwork []RoutingNetwork `json:"rightNetwork"`
+ NetworkChain string `json:"networkChain"`
+ Namespace string `json:"namespace"`
+}
+
+// RoutingNetwork contains the route networkroute network details for en element of a network chain
+type RoutingNetwork struct {
+ NetworkName string `json:"networkName"`
+ GatewayIP string `json:"gatewayIp"`
+ Subnet string `json:"subnet"`
+}
+
+// ChainKey is the key structure that is used in the database
+type ChainKey struct {
+ Project string `json:"project"`
+ CompositeApp string `json:"compositeapp"`
+ CompositeAppVersion string `json:"compositeappversion"`
+ NetControlIntent string `json:"netcontrolintent"`
+ NetworkChain string `json:"networkchain"`
+}
+
+// CrChain is the structure for the Network Chain Custom Resource
+type CrChain struct {
+ APIVersion string `yaml:"apiVersion"`
+ Kind string `yaml:"kind"`
+ Chain Chain
+}
+
+// RoutingChainType is currently only defined chaining type
+const RoutingChainType = "routing"
+
+// ChainingAPIVersion is the kubernetes version of a network chain custom resource
+const ChainingAPIVersion = "k8s.plugin.opnfv.org/v1"
+
+// ChainingKind is the Kind string for a network chain
+const ChainingKind = "NetworkChaining"
+
+// ChainManager is an interface exposing the Chain functionality
+type ChainManager interface {
+ CreateChain(ch Chain, pr, ca, caver, netctrlint string, exists bool) (Chain, error)
+ GetChain(name, pr, ca, caver, netctrlint string) (Chain, error)
+ GetChains(pr, ca, caver, netctrlint string) ([]Chain, error)
+ DeleteChain(name, pr, ca, caver, netctrlint string) error
+}
+
+// ChainClient implements the Manager
+// It will also be used to maintain some localized state
+type ChainClient struct {
+ db ClientDbInfo
+}
+
+// NewChainClient returns an instance of the ChainClient
+// which implements the Manager
+func NewChainClient() *ChainClient {
+ return &ChainClient{
+ db: ClientDbInfo{
+ storeName: "orchestrator",
+ tagMeta: "chainmetadata",
+ },
+ }
+}
+
+// CreateChain - create a new Chain
+func (v *ChainClient) CreateChain(ch Chain, pr, ca, caver, netctrlint string, exists bool) (Chain, error) {
+ //Construct key and tag to select the entry
+ key := ChainKey{
+ Project: pr,
+ CompositeApp: ca,
+ CompositeAppVersion: caver,
+ NetControlIntent: netctrlint,
+ NetworkChain: ch.Metadata.Name,
+ }
+
+ //Check if the Network Control Intent exists
+ _, err := NewNetControlIntentClient().GetNetControlIntent(netctrlint, pr, ca, caver)
+ if err != nil {
+ return Chain{}, pkgerrors.Errorf("Network Control Intent %v does not exist", netctrlint)
+ }
+
+ //Check if this Chain already exists
+ _, err = v.GetChain(ch.Metadata.Name, pr, ca, caver, netctrlint)
+ if err == nil && !exists {
+ return Chain{}, pkgerrors.New("Chain already exists")
+ }
+
+ err = db.DBconn.Insert(v.db.storeName, key, nil, v.db.tagMeta, ch)
+ if err != nil {
+ return Chain{}, pkgerrors.Wrap(err, "Creating DB Entry")
+ }
+
+ return ch, nil
+}
+
+// GetChain returns the Chain for corresponding name
+func (v *ChainClient) GetChain(name, pr, ca, caver, netctrlint string) (Chain, error) {
+ //Construct key and tag to select the entry
+ key := ChainKey{
+ Project: pr,
+ CompositeApp: ca,
+ CompositeAppVersion: caver,
+ NetControlIntent: netctrlint,
+ NetworkChain: name,
+ }
+
+ value, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
+ if err != nil {
+ return Chain{}, pkgerrors.Wrap(err, "Get Chain")
+ }
+
+ //value is a byte array
+ if value != nil {
+ ch := Chain{}
+ err = db.DBconn.Unmarshal(value[0], &ch)
+ if err != nil {
+ return Chain{}, pkgerrors.Wrap(err, "Unmarshalling Value")
+ }
+ return ch, nil
+ }
+
+ return Chain{}, pkgerrors.New("Error getting Chain")
+}
+
+// GetChains returns all of the Chains for for the given network control intent
+func (v *ChainClient) GetChains(pr, ca, caver, netctrlint string) ([]Chain, error) {
+ //Construct key and tag to select the entry
+ key := ChainKey{
+ Project: pr,
+ CompositeApp: ca,
+ CompositeAppVersion: caver,
+ NetControlIntent: netctrlint,
+ NetworkChain: "",
+ }
+
+ var resp []Chain
+ values, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
+ if err != nil {
+ return []Chain{}, pkgerrors.Wrap(err, "Get Chains")
+ }
+
+ for _, value := range values {
+ cp := Chain{}
+ err = db.DBconn.Unmarshal(value, &cp)
+ if err != nil {
+ return []Chain{}, pkgerrors.Wrap(err, "Unmarshalling Value")
+ }
+ resp = append(resp, cp)
+ }
+
+ return resp, nil
+}
+
+// DeleteChain deletes the Chain from the database
+func (v *ChainClient) DeleteChain(name, pr, ca, caver, netctrlint string) error {
+
+ //Construct key and tag to select the entry
+ key := ChainKey{
+ Project: pr,
+ CompositeApp: ca,
+ CompositeAppVersion: caver,
+ NetControlIntent: netctrlint,
+ NetworkChain: name,
+ }
+
+ err := db.DBconn.Remove(v.db.storeName, key)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Delete Chain Entry;")
+ }
+
+ return nil
+}
diff --git a/src/ncm/pkg/module/module.go b/src/ncm/pkg/module/module.go
index 9655c1de..21adaf9d 100644
--- a/src/ncm/pkg/module/module.go
+++ b/src/ncm/pkg/module/module.go
@@ -24,6 +24,7 @@ type Client struct {
NetControlIntent *NetControlIntentClient
WorkloadIntent *WorkloadIntentClient
WorkloadIfIntent *WorkloadIfIntentClient
+ Chain *ChainClient
// Add Clients for API's here
}
@@ -36,6 +37,7 @@ func NewClient() *Client {
c.NetControlIntent = NewNetControlIntentClient()
c.WorkloadIntent = NewWorkloadIntentClient()
c.WorkloadIfIntent = NewWorkloadIfIntentClient()
+ c.Chain = NewChainClient()
// Add Client API handlers here
return c
}