Callback to delete job
[nonrtric.git] / dmaap-mediator-producer / internal / server / server.go
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 (
24         "encoding/json"
25         "fmt"
26         "io/ioutil"
27         "net/http"
28
29         "github.com/gorilla/mux"
30         "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
31 )
32
33 const StatusPath = "/status"
34 const AddJobPath = "/jobs"
35 const jobIdToken = "infoJobId"
36 const deleteJobPath = AddJobPath + "/{" + jobIdToken + "}"
37
38 func NewRouter() *mux.Router {
39         r := mux.NewRouter()
40         r.HandleFunc(StatusPath, statusHandler).Methods(http.MethodGet).Name("status")
41         r.HandleFunc(AddJobPath, addInfoJobHandler).Methods(http.MethodPost).Name("add")
42         r.HandleFunc(deleteJobPath, deleteInfoJobHandler).Methods(http.MethodDelete).Name("delete")
43         r.NotFoundHandler = &notFoundHandler{}
44         r.MethodNotAllowedHandler = &methodNotAllowedHandler{}
45         return r
46 }
47
48 func statusHandler(w http.ResponseWriter, r *http.Request) {}
49
50 func addInfoJobHandler(w http.ResponseWriter, r *http.Request) {
51         b, readErr := ioutil.ReadAll(r.Body)
52         if readErr != nil {
53                 http.Error(w, fmt.Sprintf("Unable to read body due to: %v", readErr), http.StatusBadRequest)
54                 return
55         }
56         jobInfo := jobs.JobInfo{}
57         if unmarshalErr := json.Unmarshal(b, &jobInfo); unmarshalErr != nil {
58                 http.Error(w, fmt.Sprintf("Invalid json body. Cause: %v", unmarshalErr), http.StatusBadRequest)
59                 return
60         }
61         if err := jobs.AddJob(jobInfo); err != nil {
62                 http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest)
63         }
64 }
65
66 func deleteInfoJobHandler(w http.ResponseWriter, r *http.Request) {
67         vars := mux.Vars(r)
68         id, ok := vars[jobIdToken]
69         if !ok {
70                 http.Error(w, "Must provide infoJobId.", http.StatusBadRequest)
71                 return
72         }
73
74         jobs.DeleteJob(id)
75 }
76
77 type notFoundHandler struct{}
78
79 func (h *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
80         http.Error(w, "404 not found.", http.StatusNotFound)
81 }
82
83 type methodNotAllowedHandler struct{}
84
85 func (h *methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
86         http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)
87 }