2 * Copyright 2020 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 moduleLib "github.com/onap/multicloud-k8s/src/ncm/pkg/module"
26 pkgerrors "github.com/pkg/errors"
28 "github.com/gorilla/mux"
31 // Used to store backend implementations objects
32 // Also simplifies mocking for unit testing purposes
33 type netcontrolintentHandler struct {
34 // Interface that implements Cluster operations
35 // We will set this variable with a mock interface for testing
36 client moduleLib.NetControlIntentManager
39 // Check for valid format of input parameters
40 func validateNetControlIntentInputs(nci moduleLib.NetControlIntent) error {
42 err := moduleLib.IsValidMetadata(nci.Metadata)
44 return pkgerrors.Wrap(err, "Invalid network controller intent metadata")
49 // Create handles creation of the NetControlIntent entry in the database
50 func (h netcontrolintentHandler) createHandler(w http.ResponseWriter, r *http.Request) {
51 var nci moduleLib.NetControlIntent
53 project := vars["project"]
54 compositeApp := vars["composite-app-name"]
55 compositeAppVersion := vars["version"]
57 err := json.NewDecoder(r.Body).Decode(&nci)
61 http.Error(w, "Empty body", http.StatusBadRequest)
64 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
69 if nci.Metadata.Name == "" {
70 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
74 err = validateNetControlIntentInputs(nci)
76 http.Error(w, err.Error(), http.StatusBadRequest)
80 ret, err := h.client.CreateNetControlIntent(nci, project, compositeApp, compositeAppVersion, false)
82 http.Error(w, err.Error(), http.StatusInternalServerError)
86 w.Header().Set("Content-Type", "application/json")
87 w.WriteHeader(http.StatusCreated)
88 err = json.NewEncoder(w).Encode(ret)
90 http.Error(w, err.Error(), http.StatusInternalServerError)
95 // Put handles creation/update of the NetControlIntent entry in the database
96 func (h netcontrolintentHandler) putHandler(w http.ResponseWriter, r *http.Request) {
97 var nci moduleLib.NetControlIntent
100 project := vars["project"]
101 compositeApp := vars["composite-app-name"]
102 compositeAppVersion := vars["version"]
104 err := json.NewDecoder(r.Body).Decode(&nci)
108 http.Error(w, "Empty body", http.StatusBadRequest)
111 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
116 if nci.Metadata.Name == "" {
117 http.Error(w, "Missing name in PUT request", http.StatusBadRequest)
121 // Name in URL should match name in body
122 if nci.Metadata.Name != name {
123 fmt.Printf("bodyname = %v, name= %v\n", nci.Metadata.Name, name)
124 http.Error(w, "Mismatched name in PUT request", http.StatusBadRequest)
128 err = validateNetControlIntentInputs(nci)
130 http.Error(w, err.Error(), http.StatusBadRequest)
134 ret, err := h.client.CreateNetControlIntent(nci, project, compositeApp, compositeAppVersion, true)
136 http.Error(w, err.Error(), http.StatusInternalServerError)
140 w.Header().Set("Content-Type", "application/json")
141 w.WriteHeader(http.StatusCreated)
142 err = json.NewEncoder(w).Encode(ret)
144 http.Error(w, err.Error(), http.StatusInternalServerError)
149 // Get handles GET operations on a particular NetControlIntent Name
150 // Returns a NetControlIntent
151 func (h netcontrolintentHandler) getHandler(w http.ResponseWriter, r *http.Request) {
154 project := vars["project"]
155 compositeApp := vars["composite-app-name"]
156 compositeAppVersion := vars["version"]
161 ret, err = h.client.GetNetControlIntents(project, compositeApp, compositeAppVersion)
163 http.Error(w, err.Error(), http.StatusInternalServerError)
167 ret, err = h.client.GetNetControlIntent(name, project, compositeApp, compositeAppVersion)
169 http.Error(w, err.Error(), http.StatusInternalServerError)
174 w.Header().Set("Content-Type", "application/json")
175 w.WriteHeader(http.StatusOK)
176 err = json.NewEncoder(w).Encode(ret)
178 http.Error(w, err.Error(), http.StatusInternalServerError)
183 // Delete handles DELETE operations on a particular NetControlIntent Name
184 func (h netcontrolintentHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
187 project := vars["project"]
188 compositeApp := vars["composite-app-name"]
189 compositeAppVersion := vars["version"]
191 err := h.client.DeleteNetControlIntent(name, project, compositeApp, compositeAppVersion)
193 http.Error(w, err.Error(), http.StatusInternalServerError)
197 w.WriteHeader(http.StatusNoContent)