Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -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 core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "github.com/gorilla/mux" |
| 24 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 25 | "net/http" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | type Server struct { |
| 30 | store *Store |
| 31 | } |
| 32 | |
| 33 | func NewServer(store *Store) *Server { |
| 34 | return &Server{ |
| 35 | store: store, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func (s *Server) StartSadisServer(wg *sync.WaitGroup) { |
| 40 | defer wg.Done() |
| 41 | ctx := context.Background() |
| 42 | |
| 43 | addr := "0.0.0.0:8080" |
| 44 | |
| 45 | router := mux.NewRouter().StrictSlash(true) |
| 46 | router.HandleFunc("/subscribers/{ID}", s.serveEntry) |
| 47 | router.HandleFunc("/profiles/{ID}", s.serveBWPEntry) |
| 48 | |
| 49 | logger.Fatal(ctx, http.ListenAndServe(addr, router)) |
| 50 | } |
| 51 | |
| 52 | func (s Server) serveEntry(w http.ResponseWriter, r *http.Request) { |
| 53 | vars := mux.Vars(r) |
| 54 | id := vars["ID"] |
| 55 | |
| 56 | ctx := context.TODO() |
| 57 | logger.Debugw(ctx, "received-sadis-entry-request", log.Fields{"id": id}) |
| 58 | |
| 59 | w.Header().Set("Content-Type", "application/json") |
| 60 | |
| 61 | if olt, err := s.store.getOlt(r.Context(), id); err == nil { |
| 62 | w.WriteHeader(http.StatusOK) |
| 63 | _ = json.NewEncoder(w).Encode(olt) |
| 64 | logger.Infow(ctx, "responded-to-sadis-olt-entry-request", log.Fields{"id": id}) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | if onu, err := s.store.getOnu(r.Context(), id); err == nil { |
| 69 | w.WriteHeader(http.StatusOK) |
| 70 | _ = json.NewEncoder(w).Encode(onu) |
| 71 | logger.Infow(ctx, "responded-to-sadis-onu-entry-request", log.Fields{"id": id}) |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | w.WriteHeader(http.StatusNotFound) |
| 76 | msg := make(map[string]interface{}) |
| 77 | msg["statusCode"] = http.StatusNotFound |
| 78 | msg["message"] = fmt.Sprintf("Entry with ID %s not found.", id) |
| 79 | _ = json.NewEncoder(w).Encode(msg) |
| 80 | |
| 81 | logger.Warnw(ctx, "sadis-entry-not-found", log.Fields{"id": id}) |
| 82 | } |
| 83 | |
| 84 | func (s Server) serveBWPEntry(w http.ResponseWriter, r *http.Request) { |
| 85 | vars := mux.Vars(r) |
| 86 | id := vars["ID"] |
| 87 | |
| 88 | ctx := context.TODO() |
| 89 | logger.Debugw(ctx, "received-sadis-bandwidthprofile-request", log.Fields{"id": id}) |
| 90 | |
| 91 | w.Header().Set("Content-Type", "application/json") |
| 92 | |
| 93 | if bp, err := s.store.getBp(r.Context(), id); err == nil { |
| 94 | w.WriteHeader(http.StatusOK) |
| 95 | _ = json.NewEncoder(w).Encode(bp) |
| 96 | logger.Infow(ctx, "responded-to-sadis-bandwidthprofile-request", log.Fields{"id": id}) |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | w.WriteHeader(http.StatusNotFound) |
| 101 | msg := make(map[string]interface{}) |
| 102 | msg["statusCode"] = http.StatusNotFound |
| 103 | msg["message"] = fmt.Sprintf("BandwidthProfile with ID %s not found.", id) |
| 104 | _ = json.NewEncoder(w).Encode(msg) |
| 105 | |
| 106 | logger.Warnw(ctx, "sadis-bandwidthprofile-not-found", log.Fields{"id": id}) |
| 107 | } |