Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 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 | |
| 17 | package webserver |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 20 | "encoding/json" |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 21 | "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 Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 27 | "os" |
| 28 | "path/filepath" |
| 29 | "strings" |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 30 | "sync" |
| 31 | ) |
| 32 | |
| 33 | var logger = log.WithFields(log.Fields{ |
| 34 | "module": "WEBSERVER", |
| 35 | }) |
| 36 | |
Matteo Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 37 | type imageRequestCount struct { |
| 38 | Requests int `json:"requests"` |
| 39 | } |
| 40 | |
| 41 | var imageRequests = 0 |
| 42 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 43 | // StartRestServer starts REST server which esposes: |
| 44 | // - a SADIS configuration for the currently simulated OLT |
| 45 | // - static files for software image download |
| 46 | func 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 Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 56 | 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 Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 60 | |
Matteo Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 61 | // 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 Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 80 | // Choose the folder to serve (this is the location inside the container) |
| 81 | staticDir := "/app/configs/" |
Matteo Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 82 | fileServer := http.FileServer(http.Dir(staticDir)) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 83 | |
| 84 | // Create the route |
Matteo Scandolo | 2216d17 | 2021-10-07 14:48:06 -0700 | [diff] [blame^] | 85 | 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 Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 113 | |
| 114 | log.Fatal(http.ListenAndServe(addr, router)) |
| 115 | |
| 116 | wg.Done() |
| 117 | } |