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 log "github.com/sirupsen/logrus"
31 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
34 const StatusPath = "/status"
35 const AddJobPath = "/jobs"
36 const jobIdToken = "infoJobId"
37 const deleteJobPath = AddJobPath + "/{" + jobIdToken + "}"
38 const logLevelToken = "level"
39 const logAdminPath = "/admin/log"
41 type ProducerCallbackHandler struct {
42 jobsManager jobs.JobsManager
45 func NewProducerCallbackHandler(jm jobs.JobsManager) *ProducerCallbackHandler {
46 return &ProducerCallbackHandler{
51 func NewRouter(jm jobs.JobsManager) *mux.Router {
52 callbackHandler := NewProducerCallbackHandler(jm)
54 r.HandleFunc(StatusPath, statusHandler).Methods(http.MethodGet).Name("status")
55 r.HandleFunc(AddJobPath, callbackHandler.addInfoJobHandler).Methods(http.MethodPost).Name("add")
56 r.HandleFunc(deleteJobPath, callbackHandler.deleteInfoJobHandler).Methods(http.MethodDelete).Name("delete")
57 r.HandleFunc(logAdminPath, callbackHandler.setLogLevel).Methods(http.MethodPut).Name("setLogLevel")
58 r.NotFoundHandler = ¬FoundHandler{}
59 r.MethodNotAllowedHandler = &methodNotAllowedHandler{}
63 func statusHandler(w http.ResponseWriter, r *http.Request) {
64 // Just respond OK to show the server is alive for now. Might be extended later.
67 func (h *ProducerCallbackHandler) addInfoJobHandler(w http.ResponseWriter, r *http.Request) {
68 b, readErr := ioutil.ReadAll(r.Body)
70 http.Error(w, fmt.Sprintf("Unable to read body due to: %v", readErr), http.StatusBadRequest)
73 jobInfo := jobs.JobInfo{}
74 if unmarshalErr := json.Unmarshal(b, &jobInfo); unmarshalErr != nil {
75 http.Error(w, fmt.Sprintf("Invalid json body. Cause: %v", unmarshalErr), http.StatusBadRequest)
78 if err := h.jobsManager.AddJobFromRESTCall(jobInfo); err != nil {
79 http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest)
83 func (h *ProducerCallbackHandler) deleteInfoJobHandler(w http.ResponseWriter, r *http.Request) {
85 id, ok := vars[jobIdToken]
87 http.Error(w, "Must provide infoJobId.", http.StatusBadRequest)
91 h.jobsManager.DeleteJobFromRESTCall(id)
94 func (h *ProducerCallbackHandler) setLogLevel(w http.ResponseWriter, r *http.Request) {
95 query := r.URL.Query()
96 logLevelStr := query.Get(logLevelToken)
97 if loglevel, err := log.ParseLevel(logLevelStr); err == nil {
98 log.SetLevel(loglevel)
100 http.Error(w, fmt.Sprintf("Invalid log level: %v. Log level will not be changed!", logLevelStr), http.StatusBadRequest)
105 type notFoundHandler struct{}
107 func (h *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
108 http.Error(w, "404 not found.", http.StatusNotFound)
111 type methodNotAllowedHandler struct{}
113 func (h *methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
114 http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)