Skip to content

Commit

Permalink
Use HTTP 202 (Accepted) for API instead of HTTP 200 (Ok)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Dec 17, 2024
1 parent 3162f5b commit ab6e30a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 19 deletions.
32 changes: 20 additions & 12 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ func (cli *Cli) RadioPeer(c *gin.Context) {
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
if err := cli.Radio.InitPeer(c, peer.Gnb); err != nil {
logrus.WithError(err).Error("could not perform radio peer init")
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not perform radio peer init", Error: err})
go cli.HandleRadioPeer(peer)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})
}

func (cli *Cli) HandleRadioPeer(peer CliPeerMsg) {
if err := cli.Radio.InitPeer(peer.Gnb); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"gnb": peer.Gnb,
"dnn": peer.Dnn,
}).Error("Could not perform Radio Peer Init")
return
}

// TODO: handle gnb failure

c.Status(http.StatusNoContent)
}

func (cli *Cli) PsEstablish(c *gin.Context) {
Expand All @@ -60,15 +64,19 @@ func (cli *Cli) PsEstablish(c *gin.Context) {
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
// TODO: first, check if radio link is established
go cli.HandlePsEstablish(peer)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})
}

if err := cli.PduSessions.InitEstablish(c, peer.Gnb, peer.Dnn); err != nil {
logrus.WithError(err).Error("could not perform pdu session establishment")
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not perform pdu session establishment", Error: err})
func (cli *Cli) HandlePsEstablish(peer CliPeerMsg) {
// TODO: first, check if radio link is established
if err := cli.PduSessions.InitEstablish(peer.Gnb, peer.Dnn); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"gnb": peer.Gnb,
"dnn": peer.Dnn,
}).Error("Could not perform PDU Session Establishment")
return
}

// TODO: handle gnb failure

c.Status(http.StatusNoContent)
}
14 changes: 14 additions & 0 deletions internal/radio/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT

package radio

import (
"errors"
)

var (
ErrNilCtx = errors.New("nil context")
)
26 changes: 24 additions & 2 deletions internal/radio/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Radio struct {
Control jsonapi.ControlURI
Data netip.AddrPort
UserAgent string

// not exported because must not be modified
ctx context.Context
}

func NewRadio(control jsonapi.ControlURI, data netip.AddrPort, userAgent string) *Radio {
Expand All @@ -52,7 +55,8 @@ func (r *Radio) Write(pkt []byte, srv *net.UDPConn, gnb jsonapi.ControlURI) erro
return err
}

func (r *Radio) InitPeer(ctx context.Context, gnb jsonapi.ControlURI) error {
func (r *Radio) InitPeer(gnb jsonapi.ControlURI) error {
ctx := r.Context()
logrus.WithFields(logrus.Fields{
"gnb": gnb.String(),
}).Info("Creating radio link with a new gNB")
Expand Down Expand Up @@ -88,11 +92,29 @@ func (r *Radio) Peer(c *gin.Context) {
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
go r.HandlePeer(peer)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})

}

func (r *Radio) HandlePeer(peer n1n2.RadioPeerMsg) {
r.peerMap.Store(peer.Control, peer.Data)
logrus.WithFields(logrus.Fields{
"peer-control": peer.Control.String(),
"peer-ran": peer.Data,
}).Info("New peer radio link")
}

c.Status(http.StatusNoContent)
func (r *Radio) Context() context.Context {
if r.ctx != nil {
return r.ctx
}
return context.Background()
}
func (r *Radio) Init(ctx context.Context) error {
if ctx == nil {
return ErrNilCtx
}
r.ctx = ctx
return nil
}
5 changes: 4 additions & 1 deletion internal/radio/radio_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (r *RadioDaemon) runUplinkDaemon(ctx context.Context, srv *net.UDPConn, ifa
}

func (r *RadioDaemon) Start(ctx context.Context) error {
if err := r.Radio.Init(ctx); err != nil {
return err
}
ifacetun := r.tunMan.OpenTun()
defer func(ctx context.Context) {
defer r.tunMan.CloseTun()
Expand Down Expand Up @@ -146,7 +149,7 @@ func (r *RadioDaemon) Start(ctx context.Context) error {
case <-ctx.Done():
return ctx.Err()
default:
if err := r.Radio.InitPeer(ctx, gnb); err != nil {
if err := r.Radio.InitPeer(gnb); err != nil {
return err
}
}
Expand Down
14 changes: 14 additions & 0 deletions internal/session/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT

package session

import (
"errors"
)

var (
ErrNilCtx = errors.New("nil context")
)
23 changes: 19 additions & 4 deletions internal/session/pdu_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type PduSessions struct {
UserAgent string
psMan *PduSessionsManager
reqPs []config.PDUSession

// not exported because must not be modified
ctx context.Context
}

func NewPduSessions(control jsonapi.ControlURI, psMan *PduSessionsManager, reqPs []config.PDUSession, userAgent string) *PduSessions {
Expand All @@ -38,7 +41,8 @@ func NewPduSessions(control jsonapi.ControlURI, psMan *PduSessionsManager, reqPs
}
}

func (p *PduSessions) InitEstablish(ctx context.Context, gnb jsonapi.ControlURI, dnn string) error {
func (p *PduSessions) InitEstablish(gnb jsonapi.ControlURI, dnn string) error {
ctx := p.Context()
logrus.WithFields(logrus.Fields{
"gnb": gnb.String(),
}).Info("Creating new PDU Session")
Expand Down Expand Up @@ -80,14 +84,18 @@ func (p *PduSessions) EstablishmentAccept(c *gin.Context) {
"ip-addr": ps.Addr,
}).Info("New PDU Session")

p.psMan.CreatePduSession(ps.Addr, ps.Header.Gnb)
go p.psMan.CreatePduSession(ps.Addr, ps.Header.Gnb)

c.Status(http.StatusNoContent)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})
}

func (p *PduSessions) Start(ctx context.Context) error {
if ctx == nil {
return ErrNilCtx
}
p.ctx = ctx
for _, ps := range p.reqPs {
if err := p.InitEstablish(ctx, ps.Gnb, ps.Dnn); err != nil {
if err := p.InitEstablish(ps.Gnb, ps.Dnn); err != nil {
return err
}
}
Expand All @@ -98,3 +106,10 @@ func (p *PduSessions) WaitShutdown(ctx context.Context) error {
// nothing to do
return nil
}

func (p *PduSessions) Context() context.Context {
if p.ctx != nil {
return p.ctx
}
return context.Background()
}

0 comments on commit ab6e30a

Please sign in to comment.