-
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
ce1be96
commit 888dea1
Showing
21 changed files
with
1,022 additions
and
466 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
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,88 @@ | ||
// 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 amf | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/netip" | ||
"time" | ||
|
||
"github.com/nextmn/cp-lite/internal/smf" | ||
|
||
"github.com/nextmn/json-api/healthcheck" | ||
"github.com/nextmn/json-api/jsonapi" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Amf struct { | ||
control jsonapi.ControlURI | ||
client http.Client | ||
userAgent string | ||
smf *smf.Smf | ||
srv *http.Server | ||
} | ||
|
||
func NewAmf(bindAddr netip.AddrPort, control jsonapi.ControlURI, userAgent string, smf *smf.Smf) *Amf { | ||
amf := Amf{ | ||
control: control, | ||
client: http.Client{}, | ||
userAgent: userAgent, | ||
smf: smf, | ||
} | ||
// TODO: gin.SetMode(gin.DebugMode) / gin.SetMode(gin.ReleaseMode) depending on log level | ||
r := gin.Default() | ||
r.GET("/status", Status) | ||
|
||
// PDU Sessions | ||
r.POST("/ps/establishment-request", amf.EstablishmentRequest) | ||
r.POST("/ps/n2-establishment-response", amf.N2EstablishmentResponse) | ||
|
||
logrus.WithFields(logrus.Fields{"http-addr": bindAddr}).Info("HTTP Server created") | ||
amf.srv = &http.Server{ | ||
Addr: bindAddr.String(), | ||
Handler: r, | ||
} | ||
|
||
return &amf | ||
} | ||
|
||
func (amf *Amf) Start(ctx context.Context) error { | ||
l, err := net.Listen("tcp", amf.srv.Addr) | ||
if err != nil { | ||
return err | ||
} | ||
go func(ln net.Listener) { | ||
logrus.Info("Starting HTTP Server") | ||
if err := amf.srv.Serve(ln); err != nil && err != http.ErrServerClosed { | ||
logrus.WithError(err).Error("Http Server error") | ||
} | ||
}(l) | ||
go func(ctx context.Context) { | ||
select { | ||
case <-ctx.Done(): | ||
ctxShutdown, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
if err := amf.srv.Shutdown(ctxShutdown); err != nil { | ||
logrus.WithError(err).Info("HTTP Server Shutdown") | ||
} | ||
} | ||
}(ctx) | ||
|
||
return nil | ||
} | ||
|
||
// get status of the controller | ||
func Status(c *gin.Context) { | ||
status := healthcheck.Status{ | ||
Ready: true, | ||
} | ||
c.Header("Cache-Control", "no-cache") | ||
c.JSON(http.StatusOK, status) | ||
} |
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,67 @@ | ||
// 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 amf | ||
|
||
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" | ||
) | ||
|
||
func (amf *Amf) EstablishmentRequest(c *gin.Context) { | ||
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(), | ||
"gnb": ps.Gnb.String(), | ||
"dnn": ps.Dnn, | ||
}).Info("New PDU Session establishment Request") | ||
|
||
pduSession, err := amf.smf.CreateSessionUplink(c, ps.Ue, ps.Gnb, ps.Dnn) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not create pdu session uplink", Error: err}) | ||
return | ||
} | ||
|
||
// send PseAccept to UE | ||
n2PsReq := n1n2.N2PduSessionReqMsg{ | ||
Cp: amf.control, | ||
UeInfo: n1n2.PduSessionEstabAcceptMsg{ | ||
Header: ps, | ||
Addr: pduSession.UeIpAddr, | ||
}, | ||
Upf: pduSession.UplinkFteid.Addr, | ||
UplinkTeid: pduSession.UplinkFteid.Teid, | ||
} | ||
reqBody, err := json.Marshal(n2PsReq) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not marshal json", Error: err}) | ||
return | ||
} | ||
req, err := http.NewRequestWithContext(c, http.MethodPost, ps.Gnb.JoinPath("ps/n2-establishment-request").String(), bytes.NewBuffer(reqBody)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not create request", Error: err}) | ||
return | ||
} | ||
req.Header.Set("User-Agent", amf.userAgent) | ||
req.Header.Set("Content-Type", "application/json; charset=UTF-8") | ||
resp, err := amf.client.Do(req) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "no http response", Error: err}) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
} |
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,46 @@ | ||
// 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 amf | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/nextmn/json-api/jsonapi" | ||
"github.com/nextmn/json-api/jsonapi/n1n2" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func (amf *Amf) N2EstablishmentResponse(c *gin.Context) { | ||
var ps n1n2.N2PduSessionRespMsg | ||
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 | ||
} | ||
pduSession, err := amf.smf.CreateSessionDownlink(c, ps.UeInfo.Header.Ue, ps.UeInfo.Header.Dnn, ps.Gnb, ps.DownlinkTeid) | ||
if err != nil { | ||
logrus.WithError(err).WithFields(logrus.Fields{ | ||
"ue-ip-addr": ps.UeInfo.Addr, | ||
"ue": ps.UeInfo.Header.Ue, | ||
"gnb": ps.UeInfo.Header.Gnb, | ||
"dnn": ps.UeInfo.Header.Dnn, | ||
}).Error("could not create downlink path") | ||
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not create downlink path", Error: err}) | ||
return | ||
} | ||
logrus.WithFields(logrus.Fields{ | ||
"ue": ps.UeInfo.Header.Ue.String(), | ||
"gnb": ps.UeInfo.Header.Gnb.String(), | ||
"ip-addr": ps.UeInfo.Addr, | ||
"gtp-upf": pduSession.UplinkFteid.Addr, | ||
"gtp-uplink-teid": pduSession.UplinkFteid.Teid, | ||
"gtp-gnb": pduSession.DownlinkFteid.Addr, | ||
"gtp-downlink-teid": pduSession.DownlinkFteid.Teid, | ||
"dnn": ps.UeInfo.Header.Dnn, | ||
}).Info("New PDU Session Established") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.