blob: 7f4c13aeef49f1f081e35da15fd91433b809fcff [file] [log] [blame]
Matteo Scandolocedde462021-03-09 17:37:16 -08001/*
Joey Armstrong14628cd2023-01-10 08:38:31 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolocedde462021-03-09 17:37:16 -08003
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package webserver
18
19import (
Matteo Scandolo2216d172021-10-07 14:48:06 -070020 "encoding/json"
Matteo Scandolocedde462021-03-09 17:37:16 -080021 "github.com/gorilla/mux"
22 "github.com/opencord/bbsim/internal/bbsim/devices"
23 "github.com/opencord/bbsim/internal/bbsim/responders/sadis"
24 "github.com/opencord/bbsim/internal/common"
25 log "github.com/sirupsen/logrus"
26 "net/http"
Matteo Scandolo2216d172021-10-07 14:48:06 -070027 "os"
28 "path/filepath"
29 "strings"
Matteo Scandolocedde462021-03-09 17:37:16 -080030 "sync"
31)
32
33var logger = log.WithFields(log.Fields{
34 "module": "WEBSERVER",
35})
36
Matteo Scandolo2216d172021-10-07 14:48:06 -070037type imageRequestCount struct {
38 Requests int `json:"requests"`
39}
40
41var imageRequests = 0
42
Matteo Scandolocedde462021-03-09 17:37:16 -080043// StartRestServer starts REST server which esposes:
44// - a SADIS configuration for the currently simulated OLT
45// - static files for software image download
46func StartRestServer(olt *devices.OltDevice, wg *sync.WaitGroup) {
47 addr := common.Config.BBSim.SadisRestAddress
48 logger.Infof("WEBSERVER server listening on %s", addr)
49 s := &sadis.SadisServer{
50 Olt: olt,
51 }
52
53 router := mux.NewRouter().StrictSlash(true)
54
55 // sadis routes
Matteo Scandolo8a574812021-05-20 15:18:53 -070056 router.HandleFunc(sadis.BaseConfigUrl, s.ServeBaseConfig)
57 router.HandleFunc(sadis.StaticConfigUrl, s.ServeStaticConfig)
58 router.HandleFunc(sadis.SadisEntryUrl, s.ServeEntry)
59 router.HandleFunc(sadis.SadisBwUrl, s.ServeBWPEntry)
Matteo Scandolocedde462021-03-09 17:37:16 -080060
Matteo Scandolo2216d172021-10-07 14:48:06 -070061 // expose the requests counter
62 router.HandleFunc("/images-count", func(w http.ResponseWriter, r *http.Request) {
63 c := imageRequestCount{Requests: imageRequests}
64 response, err := json.Marshal(c)
65
66 if err != nil {
67 logger.WithFields(log.Fields{
68 "err": err.Error(),
69 }).Error("Cannot parse imageRequestCount to JSON")
70 w.WriteHeader(http.StatusInternalServerError)
71 _, _ = w.Write([]byte(err.Error()))
72 return
73 }
74
75 w.Header().Set("Content-Type", "application/json")
76 w.WriteHeader(http.StatusOK)
77 _, _ = w.Write(response)
78 })
79
Matteo Scandolocedde462021-03-09 17:37:16 -080080 // Choose the folder to serve (this is the location inside the container)
81 staticDir := "/app/configs/"
Matteo Scandolo2216d172021-10-07 14:48:06 -070082 fileServer := http.FileServer(http.Dir(staticDir))
Matteo Scandolocedde462021-03-09 17:37:16 -080083
84 // Create the route
Matteo Scandolo2216d172021-10-07 14:48:06 -070085 router.PathPrefix("/images/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86 path, err := filepath.Abs(r.URL.Path)
87 if err != nil {
88 http.Error(w, err.Error(), http.StatusBadRequest)
89 return
90 }
91
92 path = strings.Replace(path, "/images/", "", 1)
93
94 path = filepath.Join(staticDir, path)
95
96 _, err = os.Stat(path)
97 if os.IsNotExist(err) {
98 // file does not exist, return 404
99 http.Error(w, "file-not-found", http.StatusNotFound)
100 return
101 } else if err != nil {
102 http.Error(w, err.Error(), http.StatusInternalServerError)
103 return
104 }
105
106 imageRequests = imageRequests + 1
107 logger.WithFields(log.Fields{
108 "count": imageRequests,
109 }).Info("Got image request")
110
111 http.StripPrefix("/images/", fileServer).ServeHTTP(w, r)
112 })
Matteo Scandolocedde462021-03-09 17:37:16 -0800113
114 log.Fatal(http.ListenAndServe(addr, router))
115
116 wg.Done()
117}