aboutsummaryrefslogtreecommitdiffstats
path: root/src/ncm/api/netcontrolintenthandler.go
diff options
context:
space:
mode:
authorEric Multanen <eric.w.multanen@intel.com>2020-03-19 16:07:43 -0700
committerRitu Sood <Ritu.Sood@intel.com>2020-04-10 20:36:32 +0000
commit335c7cca38eb804c2977e4dd9af9efa0ea7ef82b (patch)
tree1e1adfa51e7dfaa9d8b57c7d5890537674e4f341 /src/ncm/api/netcontrolintenthandler.go
parent107142787e331ca565c166c72ae5605621d2d2ee (diff)
APIs for network, workload and interface intents
Includes network controller intent and underlying network workload intent and workload interface intent APIs. Issue-ID: MULTICLOUD-1029 Signed-off-by: Eric Multanen <eric.w.multanen@intel.com> Change-Id: I9bb34e42785b16f39af81335b1f94dd5bb15d931
Diffstat (limited to 'src/ncm/api/netcontrolintenthandler.go')
-rw-r--r--src/ncm/api/netcontrolintenthandler.go198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/ncm/api/netcontrolintenthandler.go b/src/ncm/api/netcontrolintenthandler.go
new file mode 100644
index 00000000..c59a389b
--- /dev/null
+++ b/src/ncm/api/netcontrolintenthandler.go
@@ -0,0 +1,198 @@
+/*
+ * 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"
+
+ moduleLib "github.com/onap/multicloud-k8s/src/ncm/pkg/module"
+ pkgerrors "github.com/pkg/errors"
+
+ "github.com/gorilla/mux"
+)
+
+// Used to store backend implementations objects
+// Also simplifies mocking for unit testing purposes
+type netcontrolintentHandler struct {
+ // Interface that implements Cluster operations
+ // We will set this variable with a mock interface for testing
+ client moduleLib.NetControlIntentManager
+}
+
+// Check for valid format of input parameters
+func validateNetControlIntentInputs(nci moduleLib.NetControlIntent) error {
+ // validate metadata
+ err := moduleLib.IsValidMetadata(nci.Metadata)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Invalid network controller intent metadata")
+ }
+ return nil
+}
+
+// Create handles creation of the NetControlIntent entry in the database
+func (h netcontrolintentHandler) createHandler(w http.ResponseWriter, r *http.Request) {
+ var nci moduleLib.NetControlIntent
+ vars := mux.Vars(r)
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+
+ err := json.NewDecoder(r.Body).Decode(&nci)
+
+ 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 nci.Metadata.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ err = validateNetControlIntentInputs(nci)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateNetControlIntent(nci, project, compositeApp, compositeAppVersion, 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 NetControlIntent entry in the database
+func (h netcontrolintentHandler) putHandler(w http.ResponseWriter, r *http.Request) {
+ var nci moduleLib.NetControlIntent
+ vars := mux.Vars(r)
+ name := vars["name"]
+ project := vars["project"]
+ compositeApp := vars["composite-app-name"]
+ compositeAppVersion := vars["version"]
+
+ err := json.NewDecoder(r.Body).Decode(&nci)
+
+ 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 nci.Metadata.Name == "" {
+ http.Error(w, "Missing name in PUT request", http.StatusBadRequest)
+ return
+ }
+
+ // Name in URL should match name in body
+ if nci.Metadata.Name != name {
+ fmt.Printf("bodyname = %v, name= %v\n", nci.Metadata.Name, name)
+ http.Error(w, "Mismatched name in PUT request", http.StatusBadRequest)
+ return
+ }
+
+ err = validateNetControlIntentInputs(nci)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateNetControlIntent(nci, project, compositeApp, compositeAppVersion, 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 NetControlIntent Name
+// Returns a NetControlIntent
+func (h netcontrolintentHandler) 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"]
+ var ret interface{}
+ var err error
+
+ if len(name) == 0 {
+ ret, err = h.client.GetNetControlIntents(project, compositeApp, compositeAppVersion)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+ ret, err = h.client.GetNetControlIntent(name, project, compositeApp, compositeAppVersion)
+ 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 NetControlIntent Name
+func (h netcontrolintentHandler) 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"]
+
+ err := h.client.DeleteNetControlIntent(name, project, compositeApp, compositeAppVersion)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}