blob: 8bed1f913492428db6f365cb0afbbea554341663 [file] [log] [blame]
elinuxhenrikba96d842021-09-06 16:05:01 +02001// -
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
21package server
22
23import (
elinuxhenrik63a42ca2021-09-06 22:16:24 +020024 "encoding/json"
elinuxhenrikba96d842021-09-06 16:05:01 +020025 "fmt"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020026 "io/ioutil"
elinuxhenrikba96d842021-09-06 16:05:01 +020027 "net/http"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020028
elinuxhenrikfe61c612021-09-24 15:08:47 +020029 "github.com/gorilla/mux"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020030 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
elinuxhenrikba96d842021-09-06 16:05:01 +020031)
32
elinuxhenrikfe61c612021-09-24 15:08:47 +020033const StatusPath = "/status"
34const AddJobPath = "/jobs"
35const jobIdToken = "infoJobId"
36const deleteJobPath = AddJobPath + "/{" + jobIdToken + "}"
elinuxhenrik382870d2021-09-23 11:09:09 +020037
elinuxhenrik65a53d22021-09-29 15:41:26 +020038type ProducerCallbackHandler struct {
elinuxhenrik6e0d5842021-11-04 10:29:02 +010039 jobsManager jobs.JobsManager
elinuxhenrik65a53d22021-09-29 15:41:26 +020040}
41
elinuxhenrik6e0d5842021-11-04 10:29:02 +010042func NewProducerCallbackHandler(jm jobs.JobsManager) *ProducerCallbackHandler {
elinuxhenrik65a53d22021-09-29 15:41:26 +020043 return &ProducerCallbackHandler{
elinuxhenrik6e0d5842021-11-04 10:29:02 +010044 jobsManager: jm,
elinuxhenrik65a53d22021-09-29 15:41:26 +020045 }
46}
47
elinuxhenrik6e0d5842021-11-04 10:29:02 +010048func NewRouter(jm jobs.JobsManager) *mux.Router {
49 callbackHandler := NewProducerCallbackHandler(jm)
elinuxhenrikfe61c612021-09-24 15:08:47 +020050 r := mux.NewRouter()
51 r.HandleFunc(StatusPath, statusHandler).Methods(http.MethodGet).Name("status")
elinuxhenrik65a53d22021-09-29 15:41:26 +020052 r.HandleFunc(AddJobPath, callbackHandler.addInfoJobHandler).Methods(http.MethodPost).Name("add")
53 r.HandleFunc(deleteJobPath, callbackHandler.deleteInfoJobHandler).Methods(http.MethodDelete).Name("delete")
elinuxhenrikfe61c612021-09-24 15:08:47 +020054 r.NotFoundHandler = &notFoundHandler{}
55 r.MethodNotAllowedHandler = &methodNotAllowedHandler{}
56 return r
elinuxhenrikba96d842021-09-06 16:05:01 +020057}
elinuxhenrik63a42ca2021-09-06 22:16:24 +020058
elinuxhenrik65a53d22021-09-29 15:41:26 +020059func statusHandler(w http.ResponseWriter, r *http.Request) {
60 // Just respond OK to show the server is alive for now. Might be extended later.
61}
elinuxhenrik63a42ca2021-09-06 22:16:24 +020062
elinuxhenrik65a53d22021-09-29 15:41:26 +020063func (h *ProducerCallbackHandler) addInfoJobHandler(w http.ResponseWriter, r *http.Request) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020064 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 }
elinuxhenrik6e0d5842021-11-04 10:29:02 +010074 if err := h.jobsManager.AddJob(jobInfo); err != nil {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020075 http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest)
76 }
77}
elinuxhenrikfe61c612021-09-24 15:08:47 +020078
elinuxhenrik65a53d22021-09-29 15:41:26 +020079func (h *ProducerCallbackHandler) deleteInfoJobHandler(w http.ResponseWriter, r *http.Request) {
elinuxhenrikfe61c612021-09-24 15:08:47 +020080 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
elinuxhenrik6e0d5842021-11-04 10:29:02 +010087 h.jobsManager.DeleteJob(id)
elinuxhenrikfe61c612021-09-24 15:08:47 +020088}
89
90type notFoundHandler struct{}
91
92func (h *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
93 http.Error(w, "404 not found.", http.StatusNotFound)
94}
95
96type methodNotAllowedHandler struct{}
97
98func (h *methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
99 http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)
100}