2 * Copyright 2018 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 "github.com/gorilla/mux"
28 // Used to store backend implementations objects
29 // Also simplifies mocking for unit testing purposes
30 type vnfdHandler struct {
31 // Interface that implements vnfDefinition operations
32 // We will set this variable with a mock interface for testing
33 vnfdClient vnfd.VNFDefinitionInterface
36 // vnfdCreateHandler handles creation of the vnfd entry in the database
37 func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) {
38 var v vnfd.VNFDefinition
41 http.Error(w, "Empty body", http.StatusBadRequest)
45 err := json.NewDecoder(r.Body).Decode(&v)
47 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
53 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
57 ret, err := h.vnfdClient.Create(v)
59 http.Error(w, err.Error(), http.StatusInternalServerError)
63 w.Header().Set("Content-Type", "application/json")
64 w.WriteHeader(http.StatusCreated)
65 err = json.NewEncoder(w).Encode(ret)
67 http.Error(w, err.Error(), http.StatusInternalServerError)
72 // vnfdUploadHandler handles upload of the vnf tar file into the database
73 // Note: This will be implemented in a different patch
74 func (h vnfdHandler) vnfdUploadHandler(w http.ResponseWriter, r *http.Request) {
77 // vnfdListHandler handles GET (list) operations on the /v1/vnfd endpoint
78 // Returns a list of vnfd.VNFDefinitions
79 func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) {
80 ret, err := h.vnfdClient.List()
82 http.Error(w, err.Error(), http.StatusInternalServerError)
86 w.Header().Set("Content-Type", "application/json")
87 w.WriteHeader(http.StatusOK)
88 err = json.NewEncoder(w).Encode(ret)
90 http.Error(w, err.Error(), http.StatusInternalServerError)
95 // vnfdGetHandler handles GET operations on a particular VNFID
96 // Returns a vnfd.VNFDefinition
97 func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) {
99 vnfdID := vars["vnfdID"]
101 ret, err := h.vnfdClient.Get(vnfdID)
103 http.Error(w, err.Error(), http.StatusInternalServerError)
107 w.Header().Set("Content-Type", "application/json")
108 w.WriteHeader(http.StatusOK)
109 err = json.NewEncoder(w).Encode(ret)
111 http.Error(w, err.Error(), http.StatusInternalServerError)
116 // vnfdDeleteHandler handles DELETE operations on a particular VNFID
117 func (h vnfdHandler) vnfdDeleteHandler(w http.ResponseWriter, r *http.Request) {
119 vnfdID := vars["vnfdID"]
121 err := h.vnfdClient.Delete(vnfdID)
123 http.Error(w, err.Error(), http.StatusInternalServerError)
127 w.WriteHeader(http.StatusNoContent)