elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 1 | // - |
| 2 | // ========================LICENSE_START================================= |
| 3 | // O-RAN-SC |
| 4 | // %% |
| 5 | // Copyright (C) 2021: Nordix Foundation |
| 6 | // %% |
| 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 |
| 10 | // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | // |
| 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=================================== |
| 19 | // |
| 20 | |
| 21 | package server |
| 22 | |
| 23 | import ( |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 24 | "encoding/json" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 25 | "fmt" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 26 | "io/ioutil" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 27 | "net/http" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 28 | |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 29 | "github.com/gorilla/mux" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 30 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 33 | const StatusPath = "/status" |
| 34 | const AddJobPath = "/jobs" |
| 35 | const jobIdToken = "infoJobId" |
| 36 | const deleteJobPath = AddJobPath + "/{" + jobIdToken + "}" |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 37 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 38 | type ProducerCallbackHandler struct { |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 39 | jobsManager jobs.JobsManager |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 40 | } |
| 41 | |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 42 | func NewProducerCallbackHandler(jm jobs.JobsManager) *ProducerCallbackHandler { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 43 | return &ProducerCallbackHandler{ |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 44 | jobsManager: jm, |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 48 | func NewRouter(jm jobs.JobsManager) *mux.Router { |
| 49 | callbackHandler := NewProducerCallbackHandler(jm) |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 50 | r := mux.NewRouter() |
| 51 | r.HandleFunc(StatusPath, statusHandler).Methods(http.MethodGet).Name("status") |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 52 | r.HandleFunc(AddJobPath, callbackHandler.addInfoJobHandler).Methods(http.MethodPost).Name("add") |
| 53 | r.HandleFunc(deleteJobPath, callbackHandler.deleteInfoJobHandler).Methods(http.MethodDelete).Name("delete") |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 54 | r.NotFoundHandler = ¬FoundHandler{} |
| 55 | r.MethodNotAllowedHandler = &methodNotAllowedHandler{} |
| 56 | return r |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 57 | } |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 58 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 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. |
| 61 | } |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 62 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 63 | func (h *ProducerCallbackHandler) addInfoJobHandler(w http.ResponseWriter, r *http.Request) { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 64 | b, readErr := ioutil.ReadAll(r.Body) |
| 65 | if readErr != nil { |
| 66 | http.Error(w, fmt.Sprintf("Unable to read body due to: %v", readErr), http.StatusBadRequest) |
| 67 | return |
| 68 | } |
| 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) |
| 72 | return |
| 73 | } |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 74 | if err := h.jobsManager.AddJob(jobInfo); err != nil { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 75 | http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest) |
| 76 | } |
| 77 | } |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 78 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 79 | func (h *ProducerCallbackHandler) deleteInfoJobHandler(w http.ResponseWriter, r *http.Request) { |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 80 | vars := mux.Vars(r) |
| 81 | id, ok := vars[jobIdToken] |
| 82 | if !ok { |
| 83 | http.Error(w, "Must provide infoJobId.", http.StatusBadRequest) |
| 84 | return |
| 85 | } |
| 86 | |
elinuxhenrik | 6e0d584 | 2021-11-04 10:29:02 +0100 | [diff] [blame] | 87 | h.jobsManager.DeleteJob(id) |
elinuxhenrik | fe61c61 | 2021-09-24 15:08:47 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | type notFoundHandler struct{} |
| 91 | |
| 92 | func (h *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 93 | http.Error(w, "404 not found.", http.StatusNotFound) |
| 94 | } |
| 95 | |
| 96 | type methodNotAllowedHandler struct{} |
| 97 | |
| 98 | func (h *methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 99 | http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed) |
| 100 | } |