Skip to content
Snippets Groups Projects
Commit 800e4c5a authored by Fabian Seidl's avatar Fabian Seidl Committed by Fabian Seidl
Browse files

tests and and minor changes for request logic correctly, WIP

parent 7e8c1c4f
No related branches found
No related tags found
1 merge request!238Stfaseid http refactor
This commit is part of merge request !238. Comments created here will be created in the context of that merge request.
...@@ -2,44 +2,136 @@ package gosdn ...@@ -2,44 +2,136 @@ package gosdn
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus" 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 { func stopHttpServer() error {
log.Info("shutting down http server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
log.Info("shutting down http server") err := c.httpServer.Shutdown(ctx)
return 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() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Println("Recovered in f", r) fmt.Println("Recovered in f", r)
} }
}() }()
http.HandleFunc("/livez", healthCheck)
http.HandleFunc("/readyz", readynessCheck) err := liveCheckHandler(mux)
http.Handle("/metrics", promhttp.Handler()) 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() { func liveCheckHandler(mux *runtime.ServeMux) error {
registerHttpHandler() err := mux.HandlePath("GET", "/livez", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
c.httpServer = &http.Server{Addr: ":8080"} w.WriteHeader(http.StatusOK)
go func() { })
log.Info(c.httpServer.ListenAndServe())
}() if err != nil {
return status.Errorf(codes.Internal, "%v", err)
}
return nil
} }
func healthCheck(writer http.ResponseWriter, request *http.Request) { func readyCheckHandler(mux *runtime.ServeMux) error {
writer.WriteHeader(http.StatusOK) 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) { func metricsHandler(mux *runtime.ServeMux) error {
writer.WriteHeader(http.StatusOK) 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
} }
...@@ -42,7 +42,12 @@ func (p pndServer) GetOnd(ctx context.Context, request *ppb.GetOndRequest) (*ppb ...@@ -42,7 +42,12 @@ func (p pndServer) GetOnd(ctx context.Context, request *ppb.GetOndRequest) (*ppb
} }
return &ppb.GetOndResponse{ return &ppb.GetOndResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Ond: onds, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Ond: onds,
}, nil }, nil
} }
...@@ -60,14 +65,19 @@ func (p pndServer) GetOndList(ctx context.Context, request *ppb.GetOndListReques ...@@ -60,14 +65,19 @@ func (p pndServer) GetOndList(ctx context.Context, request *ppb.GetOndListReques
log.Error(err) log.Error(err)
return nil, status.Errorf(codes.Aborted, "%v", err) return nil, status.Errorf(codes.Aborted, "%v", err)
} }
onds, err := fillOnds(pnd, true, "") onds, err := fillOnds(pnd, true)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil, status.Errorf(codes.Aborted, "%v", err) return nil, status.Errorf(codes.Aborted, "%v", err)
} }
return &ppb.GetOndListResponse{ return &ppb.GetOndListResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Ond: onds, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Ond: onds,
}, nil }, nil
} }
...@@ -140,7 +150,12 @@ func (p pndServer) GetSbi(ctx context.Context, request *ppb.GetSbiRequest) (*ppb ...@@ -140,7 +150,12 @@ func (p pndServer) GetSbi(ctx context.Context, request *ppb.GetSbiRequest) (*ppb
} }
return &ppb.GetSbiResponse{ return &ppb.GetSbiResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Sbi: sbis, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Sbi: sbis,
}, nil }, nil
} }
...@@ -165,7 +180,12 @@ func (p pndServer) GetSbiList(ctx context.Context, request *ppb.GetSbiListReques ...@@ -165,7 +180,12 @@ func (p pndServer) GetSbiList(ctx context.Context, request *ppb.GetSbiListReques
} }
return &ppb.GetSbiListResponse{ return &ppb.GetSbiListResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Sbi: sbis, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Sbi: sbis,
}, nil }, nil
} }
...@@ -218,6 +238,7 @@ func stringToUUID(sid []string) ([]uuid.UUID, error) { ...@@ -218,6 +238,7 @@ func stringToUUID(sid []string) ([]uuid.UUID, error) {
return UUIDs, nil 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) { func (p pndServer) GetPath(ctx context.Context, request *ppb.GetPathRequest) (*ppb.GetPathResponse, error) {
labels := prometheus.Labels{"service": "pnd", "rpc": "get"} labels := prometheus.Labels{"service": "pnd", "rpc": "get"}
start := metrics.StartHook(labels, grpcRequestsTotal) start := metrics.StartHook(labels, grpcRequestsTotal)
...@@ -249,7 +270,12 @@ func (p pndServer) GetPath(ctx context.Context, request *ppb.GetPathRequest) (*p ...@@ -249,7 +270,12 @@ func (p pndServer) GetPath(ctx context.Context, request *ppb.GetPathRequest) (*p
} }
return &ppb.GetPathResponse{ return &ppb.GetPathResponse{
Timestamp: time.Now().UnixNano(), 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 }, nil
} }
...@@ -275,7 +301,12 @@ func (p pndServer) GetChange(ctx context.Context, request *ppb.GetChangeRequest) ...@@ -275,7 +301,12 @@ func (p pndServer) GetChange(ctx context.Context, request *ppb.GetChangeRequest)
} }
return &ppb.GetChangeResponse{ return &ppb.GetChangeResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Change: changes, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Change: changes,
}, nil }, nil
} }
...@@ -300,7 +331,12 @@ func (p pndServer) GetChangeList(ctx context.Context, request *ppb.GetChangeList ...@@ -300,7 +331,12 @@ func (p pndServer) GetChangeList(ctx context.Context, request *ppb.GetChangeList
} }
return &ppb.GetChangeListResponse{ return &ppb.GetChangeListResponse{
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Change: changes, Pnd: &ppb.PrincipalNetworkDomain{
Id: pnd.ID().String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
},
Change: changes,
}, nil }, nil
} }
...@@ -372,7 +408,7 @@ func (p pndServer) SetOndList(ctx context.Context, request *ppb.SetOndListReques ...@@ -372,7 +408,7 @@ func (p pndServer) SetOndList(ctx context.Context, request *ppb.SetOndListReques
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
Responses: []*ppb.SetResponse{ Responses: []*ppb.SetResponse{
&ppb.SetResponse{ {
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
}, },
}, },
...@@ -420,7 +456,7 @@ func (p pndServer) SetChangeList(ctx context.Context, request *ppb.SetChangeList ...@@ -420,7 +456,7 @@ func (p pndServer) SetChangeList(ctx context.Context, request *ppb.SetChangeList
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
Responses: []*ppb.SetResponse{ Responses: []*ppb.SetResponse{
&ppb.SetResponse{ {
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
}, },
}, },
...@@ -458,7 +494,7 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ ...@@ -458,7 +494,7 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ
Timestamp: time.Now().UnixNano(), Timestamp: time.Now().UnixNano(),
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
Responses: []*ppb.SetResponse{ Responses: []*ppb.SetResponse{
&ppb.SetResponse{ {
Status: ppb.Status_STATUS_OK, Status: ppb.Status_STATUS_OK,
}, },
}, },
...@@ -466,6 +502,27 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ ...@@ -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) { func (p pndServer) DeleteOnd(ctx context.Context, request *ppb.DeleteOndRequest) (*ppb.DeleteOndResponse, error) {
pid, err := uuid.Parse(request.Pid) pid, err := uuid.Parse(request.Pid)
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment