-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6edcdaf
commit d412e64
Showing
6 changed files
with
183 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |