diff --git a/http.go b/http.go index 3a6fea20116994fd8834346d26189e3c47156f2d..f84f231a065ab99fe235149b346fbc5c707f637c 100644 --- a/http.go +++ b/http.go @@ -2,44 +2,136 @@ package gosdn import ( "context" + "flag" "fmt" "net/http" "time" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + + cgw "code.fbi.h-da.de/danet/api/go/gosdn/core" + pgw "code.fbi.h-da.de/danet/api/go/gosdn/pnd" +) + +var ( + // command-line options: + // gRPC server endpoint + grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:55055", "gRPC server endpoint") ) func stopHttpServer() error { + log.Info("shutting down http server") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - log.Info("shutting down http server") - return c.httpServer.Shutdown(ctx) + err := c.httpServer.Shutdown(ctx) + return err +} + +func run() error { + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + // Register gRPC server endpoint + // Note: Make sure the gRPC server is running properly and accessible + mux := runtime.NewServeMux() + + err := registerHttpHandler(mux) + + if err != nil { + return err + } + + opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} + err = cgw.RegisterCoreServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts) + if err != nil { + return err + } + + err = pgw.RegisterPndServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts) + if err != nil { + return err + } + + // Set the HTTP server of core to the new server + c.httpServer = &http.Server{Addr: ":8080", Handler: mux} + // Start HTTP server (and proxy calls to gRPC server endpoint) + return c.httpServer.ListenAndServe() +} + +func startHttpServer() { + go func() { + if err := run(); err != nil { + log.Info(err) + } + }() + + log.Info("Server exiting") } -func registerHttpHandler() { +func registerHttpHandler(mux *runtime.ServeMux) error { defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }() - http.HandleFunc("/livez", healthCheck) - http.HandleFunc("/readyz", readynessCheck) - http.Handle("/metrics", promhttp.Handler()) + + err := liveCheckHandler(mux) + if err != nil { + return err + } + + err = readyCheckHandler(mux) + if err != nil { + return err + } + + err = metricsHandler(mux) + if err != nil { + return err + } + + return nil } -func startHttpServer() { - registerHttpHandler() - c.httpServer = &http.Server{Addr: ":8080"} - go func() { - log.Info(c.httpServer.ListenAndServe()) - }() +func liveCheckHandler(mux *runtime.ServeMux) error { + err := mux.HandlePath("GET", "/livez", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { + w.WriteHeader(http.StatusOK) + }) + + if err != nil { + return status.Errorf(codes.Internal, "%v", err) + } + + return nil } -func healthCheck(writer http.ResponseWriter, request *http.Request) { - writer.WriteHeader(http.StatusOK) +func readyCheckHandler(mux *runtime.ServeMux) error { + err := mux.HandlePath("GET", "/readyz", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { + w.WriteHeader(http.StatusOK) + }) + + if err != nil { + return status.Errorf(codes.Internal, "%v", err) + } + + return nil } -func readynessCheck(writer http.ResponseWriter, request *http.Request) { - writer.WriteHeader(http.StatusOK) +func metricsHandler(mux *runtime.ServeMux) error { + err := mux.HandlePath("GET", "/metrics", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { + promhttp.Handler() + }) + + if err != nil { + return status.Errorf(codes.Internal, "%v", err) + } + + return nil } diff --git a/northbound/server/pnd.go b/northbound/server/pnd.go index 5597f2e9a3d82f459690b13abb490143c495f2ab..ec1201a39c6ffa021d2a8abe0715f06e324e1dab 100644 --- a/northbound/server/pnd.go +++ b/northbound/server/pnd.go @@ -42,7 +42,12 @@ func (p pndServer) GetOnd(ctx context.Context, request *ppb.GetOndRequest) (*ppb } return &ppb.GetOndResponse{ Timestamp: time.Now().UnixNano(), - Ond: onds, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Ond: onds, }, nil } @@ -60,14 +65,19 @@ func (p pndServer) GetOndList(ctx context.Context, request *ppb.GetOndListReques log.Error(err) return nil, status.Errorf(codes.Aborted, "%v", err) } - onds, err := fillOnds(pnd, true, "") + onds, err := fillOnds(pnd, true) if err != nil { log.Error(err) return nil, status.Errorf(codes.Aborted, "%v", err) } return &ppb.GetOndListResponse{ Timestamp: time.Now().UnixNano(), - Ond: onds, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Ond: onds, }, nil } @@ -140,7 +150,12 @@ func (p pndServer) GetSbi(ctx context.Context, request *ppb.GetSbiRequest) (*ppb } return &ppb.GetSbiResponse{ Timestamp: time.Now().UnixNano(), - Sbi: sbis, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Sbi: sbis, }, nil } @@ -165,7 +180,12 @@ func (p pndServer) GetSbiList(ctx context.Context, request *ppb.GetSbiListReques } return &ppb.GetSbiListResponse{ Timestamp: time.Now().UnixNano(), - Sbi: sbis, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Sbi: sbis, }, nil } @@ -218,6 +238,7 @@ func stringToUUID(sid []string) ([]uuid.UUID, error) { return UUIDs, nil } +//TODO: add body to request tod eal with / problem func (p pndServer) GetPath(ctx context.Context, request *ppb.GetPathRequest) (*ppb.GetPathResponse, error) { labels := prometheus.Labels{"service": "pnd", "rpc": "get"} start := metrics.StartHook(labels, grpcRequestsTotal) @@ -249,7 +270,12 @@ func (p pndServer) GetPath(ctx context.Context, request *ppb.GetPathRequest) (*p } return &ppb.GetPathResponse{ Timestamp: time.Now().UnixNano(), - Device: ond[0].Device, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Device: ond[0].Device, }, nil } @@ -275,7 +301,12 @@ func (p pndServer) GetChange(ctx context.Context, request *ppb.GetChangeRequest) } return &ppb.GetChangeResponse{ Timestamp: time.Now().UnixNano(), - Change: changes, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Change: changes, }, nil } @@ -300,7 +331,12 @@ func (p pndServer) GetChangeList(ctx context.Context, request *ppb.GetChangeList } return &ppb.GetChangeListResponse{ Timestamp: time.Now().UnixNano(), - Change: changes, + Pnd: &ppb.PrincipalNetworkDomain{ + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), + }, + Change: changes, }, nil } @@ -372,7 +408,7 @@ func (p pndServer) SetOndList(ctx context.Context, request *ppb.SetOndListReques Timestamp: time.Now().UnixNano(), Status: ppb.Status_STATUS_OK, Responses: []*ppb.SetResponse{ - &ppb.SetResponse{ + { Status: ppb.Status_STATUS_OK, }, }, @@ -420,7 +456,7 @@ func (p pndServer) SetChangeList(ctx context.Context, request *ppb.SetChangeList Timestamp: time.Now().UnixNano(), Status: ppb.Status_STATUS_OK, Responses: []*ppb.SetResponse{ - &ppb.SetResponse{ + { Status: ppb.Status_STATUS_OK, }, }, @@ -458,7 +494,7 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ Timestamp: time.Now().UnixNano(), Status: ppb.Status_STATUS_OK, Responses: []*ppb.SetResponse{ - &ppb.SetResponse{ + { Status: ppb.Status_STATUS_OK, }, }, @@ -466,6 +502,27 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ } +//TODO: update proto for setSBI and add sbiList after! +// func (p pndServer) SetSbiList(ctx context.Context, request *ppb.SetSbiListRequest) (*ppb.SetSbiListResponse, error) { +// labels := prometheus.Labels{"service": "pnd", "rpc": "set"} +// start := metrics.StartHook(labels, grpcRequestsTotal) +// defer metrics.FinishHook(labels, start, grpcRequestDurationSecondsTotal, grpcRequestDurationSeconds) +// pid, err := uuid.Parse(request.Pid) +// if err != nil { +// return nil, handleRPCError(labels, err) +// } + +// pnd, err := pndc.GetPND(pid) +// if err != nil { +// return nil, handleRPCError(labels, err) +// } + +// for _, r := range request.Sbi { +// err := pnd.AddSbi() +// } + +// } + func (p pndServer) DeleteOnd(ctx context.Context, request *ppb.DeleteOndRequest) (*ppb.DeleteOndResponse, error) { pid, err := uuid.Parse(request.Pid) if err != nil {