Skip to content

Commit

Permalink
Register in gin.Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Dec 19, 2024
1 parent 6edcdaf commit d412e64
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 128 deletions.
5 changes: 2 additions & 3 deletions internal/app/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func NewHttpServerEntity(bindAddr netip.AddrPort, r *radio.Radio, ps *session.Pd
h.GET("/status", Status)

// Radio
h.POST("/radio/peer", r.Peer)
r.Register(h)

// Pdu Sessions
h.POST("/ps/establishment-request", ps.EstablishmentRequest)
h.POST("/ps/n2-establishment-request", ps.N2EstablishmentRequest)
ps.Register(h)

logrus.WithFields(logrus.Fields{"http-addr": bindAddr}).Info("HTTP Server created")
e := HttpServerEntity{
Expand Down
61 changes: 61 additions & 0 deletions internal/radio/peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 (
"bytes"
"encoding/json"
"net/http"

"github.com/nextmn/json-api/jsonapi"
"github.com/nextmn/json-api/jsonapi/n1n2"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

// allow to peer to ue
func (r *Radio) Peer(c *gin.Context) {
var peer n1n2.RadioPeerMsg
if err := c.BindJSON(&peer); err != nil {
logrus.WithError(err).Error("could not deserialize")
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) {
ctx := r.Context()
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")
msg := n1n2.RadioPeerMsg{
Control: r.Control,
Data: r.Data,
}

reqBody, err := json.Marshal(msg)
if err != nil {
logrus.WithError(err).Error("Could not Marshal n1n2.RadioPeerMsg")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, peer.Control.JoinPath("radio/peer").String(), bytes.NewBuffer(reqBody))
if err != nil {
logrus.WithError(err).Error("Could not create radio/peer request")
return
}
req.Header.Set("User-Agent", r.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
if _, err := r.Client.Do(req); err != nil {
logrus.WithError(err).Error("Could not send radio/peer request")
return
}
// TODO: handle ue failure
}
46 changes: 2 additions & 44 deletions internal/radio/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
package radio

import (
"bytes"
"context"
"encoding/json"
"net"
"net/http"
"net/netip"
"sync"

"github.com/nextmn/json-api/jsonapi"
"github.com/nextmn/json-api/jsonapi/n1n2"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,45 +65,6 @@ func (r *Radio) Write(pkt []byte, srv *net.UDPConn, ue jsonapi.ControlURI) error
return err
}

// allow to peer to ue
func (r *Radio) Peer(c *gin.Context) {
var peer n1n2.RadioPeerMsg
if err := c.BindJSON(&peer); err != nil {
logrus.WithError(err).Error("could not deserialize")
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) {
ctx := r.Context()
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")
msg := n1n2.RadioPeerMsg{
Control: r.Control,
Data: r.Data,
}

reqBody, err := json.Marshal(msg)
if err != nil {
logrus.WithError(err).Error("Could not Marshal n1n2.RadioPeerMsg")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, peer.Control.JoinPath("radio/peer").String(), bytes.NewBuffer(reqBody))
if err != nil {
logrus.WithError(err).Error("Could not create radio/peer request")
return
}
req.Header.Set("User-Agent", r.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
if _, err := r.Client.Do(req); err != nil {
logrus.WithError(err).Error("Could not send radio/peer request")
return
}
// TODO: handle ue failure
func (r *Radio) Register(e *gin.Engine) {
e.POST("/radio/peer", r.Peer)
}
55 changes: 55 additions & 0 deletions internal/session/establishment-request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 (
"bytes"
"encoding/json"
"net/http"

"github.com/nextmn/json-api/jsonapi"
"github.com/nextmn/json-api/jsonapi/n1n2"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

// request from UE
func (p *PduSessions) EstablishmentRequest(c *gin.Context) {
// get PseReq
var ps n1n2.PduSessionEstabReqMsg
if err := c.BindJSON(&ps); err != nil {
logrus.WithError(err).Error("could not deserialize")
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
logrus.WithFields(logrus.Fields{
"ue": ps.Ue.String(),
}).Info("New PDU Session establishment Request")
go p.HandleEstablishmentRequest(ps)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})
}

func (p *PduSessions) HandleEstablishmentRequest(ps n1n2.PduSessionEstabReqMsg) {
ctx := p.Context()
// forward to cp
reqBody, err := json.Marshal(ps)
if err != nil {
logrus.WithError(err).Error("Could not marshal n1n2.PduSessionEstabReqMsg")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.Cp.JoinPath("ps/establishment-request").String(), bytes.NewBuffer(reqBody))
if err != nil {
logrus.WithError(err).Error("Could not create ps/establishment-request")
return
}
req.Header.Set("User-Agent", p.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
if _, err := p.Client.Do(req); err != nil {
logrus.WithError(err).Error("Could not send ps/establishment-request")
return
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ package session

import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/netip"
"sync"

"github.com/nextmn/json-api/jsonapi"
"github.com/nextmn/json-api/jsonapi/n1n2"
Expand All @@ -20,84 +17,6 @@ import (
"github.com/sirupsen/logrus"
)

type PduSessions struct {
PduSessionsMap sync.Map // key : UE 5G ip address; value: UE Control URI
UserAgent string
Client http.Client
Control jsonapi.ControlURI
Cp jsonapi.ControlURI
GnbGtp netip.Addr
manager *PduSessionsManager

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

func NewPduSessions(control jsonapi.ControlURI, cp jsonapi.ControlURI, manager *PduSessionsManager, userAgent string, gnbGtp netip.Addr) *PduSessions {
return &PduSessions{
Client: http.Client{},
PduSessionsMap: sync.Map{},
UserAgent: userAgent,
Control: control,
Cp: cp,
GnbGtp: gnbGtp,
manager: manager,
}

}

func (p *PduSessions) Init(ctx context.Context) error {
if ctx == nil {
return ErrNilCtx
}
p.ctx = ctx
return nil
}

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

// request from UE
func (p *PduSessions) EstablishmentRequest(c *gin.Context) {
// get PseReq
var ps n1n2.PduSessionEstabReqMsg
if err := c.BindJSON(&ps); err != nil {
logrus.WithError(err).Error("could not deserialize")
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
logrus.WithFields(logrus.Fields{
"ue": ps.Ue.String(),
}).Info("New PDU Session establishment Request")
go p.HandleEstablishmentRequest(ps)
c.JSON(http.StatusAccepted, jsonapi.Message{Message: "please refer to logs for more information"})
}

func (p *PduSessions) HandleEstablishmentRequest(ps n1n2.PduSessionEstabReqMsg) {
ctx := p.Context()
// forward to cp
reqBody, err := json.Marshal(ps)
if err != nil {
logrus.WithError(err).Error("Could not marshal n1n2.PduSessionEstabReqMsg")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.Cp.JoinPath("ps/establishment-request").String(), bytes.NewBuffer(reqBody))
if err != nil {
logrus.WithError(err).Error("Could not create ps/establishment-request")
return
}
req.Header.Set("User-Agent", p.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
if _, err := p.Client.Do(req); err != nil {
logrus.WithError(err).Error("Could not send ps/establishment-request")
return
}
}

// request from CP
func (p *PduSessions) N2EstablishmentRequest(c *gin.Context) {
var ps n1n2.N2PduSessionReqMsg
Expand Down
63 changes: 63 additions & 0 deletions internal/session/pdu_sessions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 (
"context"
"net/http"
"net/netip"
"sync"

"github.com/nextmn/json-api/jsonapi"

"github.com/gin-gonic/gin"
)

type PduSessions struct {
PduSessionsMap sync.Map // key : UE 5G ip address; value: UE Control URI
UserAgent string
Client http.Client
Control jsonapi.ControlURI
Cp jsonapi.ControlURI
GnbGtp netip.Addr
manager *PduSessionsManager

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

func NewPduSessions(control jsonapi.ControlURI, cp jsonapi.ControlURI, manager *PduSessionsManager, userAgent string, gnbGtp netip.Addr) *PduSessions {
return &PduSessions{
Client: http.Client{},
PduSessionsMap: sync.Map{},
UserAgent: userAgent,
Control: control,
Cp: cp,
GnbGtp: gnbGtp,
manager: manager,
}

}

func (p *PduSessions) Init(ctx context.Context) error {
if ctx == nil {
return ErrNilCtx
}
p.ctx = ctx
return nil
}

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

func (p *PduSessions) Register(e *gin.Engine) {
e.POST("/ps/establishment-request", p.EstablishmentRequest)
e.POST("/ps/n2-establishment-request", p.N2EstablishmentRequest)
}

0 comments on commit d412e64

Please sign in to comment.