2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
29 "github.com/gorilla/mux"
30 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
33 const StatusPath = "/status"
34 const AddJobPath = "/jobs"
35 const jobIdToken = "infoJobId"
36 const deleteJobPath = AddJobPath + "/{" + jobIdToken + "}"
38 type ProducerCallbackHandler struct {
39 jobHandler jobs.JobHandler
42 func NewProducerCallbackHandler(jh jobs.JobHandler) *ProducerCallbackHandler {
43 return &ProducerCallbackHandler{
48 func NewRouter(jh jobs.JobHandler) *mux.Router {
49 callbackHandler := NewProducerCallbackHandler(jh)
51 r.HandleFunc(StatusPath, statusHandler).Methods(http.MethodGet).Name("status")
52 r.HandleFunc(AddJobPath, callbackHandler.addInfoJobHandler).Methods(http.MethodPost).Name("add")
53 r.HandleFunc(deleteJobPath, callbackHandler.deleteInfoJobHandler).Methods(http.MethodDelete).Name("delete")
54 r.NotFoundHandler = ¬FoundHandler{}
55 r.MethodNotAllowedHandler = &methodNotAllowedHandler{}
59 func statusHandler(w http.ResponseWriter, r *http.Request) {
60 // Just respond OK to show the server is alive for now. Might be extended later.
63 func (h *ProducerCallbackHandler) addInfoJobHandler(w http.ResponseWriter, r *http.Request) {
64 b, readErr := ioutil.ReadAll(r.Body)
66 http.Error(w, fmt.Sprintf("Unable to read body due to: %v", readErr), http.StatusBadRequest)
69 jobInfo := jobs.JobInfo{}
70 if unmarshalErr := json.Unmarshal(b, &jobInfo); unmarshalErr != nil {
71 http.Error(w, fmt.Sprintf("Invalid json body. Cause: %v", unmarshalErr), http.StatusBadRequest)
74 if err := h.jobHandler.AddJob(jobInfo); err != nil {
75 http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest)
79 func (h *ProducerCallbackHandler) deleteInfoJobHandler(w http.ResponseWriter, r *http.Request) {
81 id, ok := vars[jobIdToken]
83 http.Error(w, "Must provide infoJobId.", http.StatusBadRequest)
87 h.jobHandler.DeleteJob(id)
90 type notFoundHandler struct{}
92 func (h *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
93 http.Error(w, "404 not found.", http.StatusNotFound)
96 type methodNotAllowedHandler struct{}
98 func (h *methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
99 http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)