Newer
Older
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/errors"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/types"
André Sterba
committed
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
var apiOpmap = map[string]types.Operation{
"update": types.TransportUpdate,
"replace": types.TransportReplace,
"delete": types.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 httpApi(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 httpPnd nucleus.PrincipalNetworkDomain
var httpSbi nucleus.SouthboundInterface
if query.Get("q") != "init" && query.Get("q") != "getIDs" {
sbic := httpPnd.GetSBIs()
httpSbi, err = sbic.(*nucleus.SbiStore).Get(sid)
d, err := nucleus.NewDevice(httpSbi, &nucleus.GnmiTransportOptions{
Config: gnmi.Config{
Addr: query.Get("address"),
Password: query.Get("password"),
Username: query.Get("username"),
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: httpSbi.SetNode(),
Unmarshal: httpSbi.(*nucleus.OpenConfig).Unmarshal(),
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
log.Error(err)
return
}
writer.WriteHeader(http.StatusCreated)
fmt.Fprintf(writer, "device added\n")
fmt.Fprintf(writer, "Name: %s\n", d.Name)
fmt.Fprintf(writer, "UUID: %v\n", d.UUID)
err = httpPnd.Request(id, query.Get("path"))
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
}
writer.WriteHeader(http.StatusOK)
case "requestAll":
err = httpPnd.RequestAll(query.Get("path"))
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
log.Error(err)
return
}
writer.WriteHeader(http.StatusOK)
case "getDevice":
deviceIdentifier := query.Get("identifier")
device, err := httpPnd.MarshalDevice(deviceIdentifier)
if err != nil {
switch err.(type) {
case *errors.ErrNotFound:
writer.WriteHeader(http.StatusNotFound)
default:
writer.WriteHeader(http.StatusInternalServerError)
writer.Header().Set("Content-Type", "application/json")
fmt.Fprintf(writer, "%v", device)
writeIDs(writer, "PNDs", pnds)
writeIDs(writer, "SBIs", c.sbic.UUIDs())
writeIDs(writer, "Devices", p.Devices())
writeIDs(writer, "PNDs", c.pndc.UUIDs())
writeIDs(writer, "SBIs", c.sbic.UUIDs())
case "update", "replace":
if err := httpPnd.ChangeOND(id, apiOpmap[q], query.Get("path"), query.Get("value")); err != nil {
if err := httpPnd.ChangeOND(id, types.TransportDelete, query.Get("path")); err != nil {
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 := httpPnd.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 := httpPnd.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)
}