blob: 26f6ec3e84d05323660b729006ef722543a001c8 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
Joey Armstrongaadcaa42024-02-01 19:54:17 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandoloa4285862020-12-01 18:10:10 -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 core
18
19import (
20 "context"
21 "encoding/json"
22 "fmt"
23 "github.com/gorilla/mux"
David K. Bainbridge06631892021-08-19 13:07:00 +000024 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Matteo Scandoloa4285862020-12-01 18:10:10 -080025 "net/http"
26 "sync"
27)
28
29type Server struct {
30 store *Store
31}
32
33func NewServer(store *Store) *Server {
34 return &Server{
35 store: store,
36 }
37}
38
39func (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
52func (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
84func (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}