Newer
Older
package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"fmt"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
"update": TransportUpdate,
"replace": TransportReplace,
"delete": TransportDelete,
func stopHttpServer() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Info("shutting down http server")
return c.httpServer.Shutdown(ctx)
}
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
http.HandleFunc("/livez", healthCheck)
c.httpServer = &http.Server{Addr: ":8080"}
func healthCheck(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
}
func readynessCheck(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
}
func httpHandler(writer http.ResponseWriter, request *http.Request) {
log.WithFields(log.Fields{
"request": request,
}).Debug("incoming request")
query, err := url.ParseQuery(request.URL.RawQuery)
if err.Error() != "invalid UUID length: 0" {
log.Error(err)
}
if err.Error() != "invalid UUID length: 0" {
log.Error(err)
}
sid, err := uuid.Parse(query.Get("sbi"))
if err != nil {
if err.Error() != "invalid UUID length: 0" {
log.Error(err)
}
var pnd PrincipalNetworkDomain
var sbi SouthboundInterface
if query.Get("q") != "init" && query.Get("q") != "getIDs" {
pnd, err = c.pndc.get(pid)
if err != nil {
}
sbic := pnd.GetSBIs()
sbi, err = sbic.(*sbiStore).get(sid)
case "addDevice":
d, err := NewDevice(sbi, &GnmiTransportOptions{
Config: gnmi.Config{
Addr: query.Get("address"),
Password: query.Get("password"),
Username: query.Get("username"),
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: sbi.SetNode(),
Unmarshal: sbi.(*OpenConfig).Unmarshal(),
RespChan: make(chan *gpb.SubscribeResponse),
})
err = pnd.AddDevice(d)
writer.WriteHeader(http.StatusCreated)
fmt.Fprintf(writer, "device added\n")
fmt.Fprintf(writer, "UUID: %v\n", d.UUID)
case "request":
err = pnd.Request(id, query.Get("path"))
switch err.(type) {
case *ErrNotFound:
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
}
writer.WriteHeader(http.StatusOK)
case "requestAll":
err = pnd.RequestAll(query.Get("path"))
if err != nil {
switch err.(type) {
case *ErrNotFound:
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
log.Error(err)
return
}
writer.WriteHeader(http.StatusOK)
case "getDevice":
device, err := pnd.MarshalDevice(id)
if err != nil {
switch err.(type) {
case *ErrNotFound:
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
}
writer.Header().Set("Content-Type", "application/json")
fmt.Fprintf(writer, "%v", device)
case "getIDs":
writeIDs(writer, "PNDs", pnds)
writeIDs(writer, "SBIs", c.sbic.UUIDs())
for _, id := range pnds {
writeIDs(writer, "Devices", p.Devices())
writeIDs(writer, "PNDs", c.pndc.UUIDs())
writeIDs(writer, "SBIs", c.sbic.UUIDs())
case "update", "replace":
if err := pnd.ChangeOND(id, apiOpmap[q], query.Get("path"), query.Get("value")); err != nil {
handleServerError(writer, err)
return
if err := pnd.ChangeOND(id, TransportDelete, query.Get("path")); err != nil {
changes := pnd.Committed()
writeIDs(writer, "Tentative changes", changes)
case "change-list-pending":
writeIDs(writer, "Pending changes", changes)
case "change-commit":
cuid, err := uuid.Parse(query.Get("cuid"))
if err != nil {
handleServerError(writer, err)
return
}
if err := pnd.Commit(cuid); err != nil {
writer.WriteHeader(http.StatusAccepted)
case "change-confirm":
cuid, err := uuid.Parse(query.Get("cuid"))
if err != nil {
handleServerError(writer, err)
return
}
if err := pnd.Confirm(cuid); err != nil {
default:
writer.WriteHeader(http.StatusBadRequest)
}
func writeIDs(w io.Writer, typ string, ids []uuid.UUID) {
fmt.Fprintf(w, "%v:\n", typ)
for i, id := range ids {
fmt.Fprintf(w, "%v: %v\n", i+1, id)
}
}
func handleServerError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error: %v", err)
log.Error(err)
}