Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI Pipeline

on:
push:
branches: [ main, "feature/**" ]
pull_request:
branches: [ main ]

jobs:
build-and-test:
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'

- name: Build
run: go build ./...

- name: Run tests
run: go test -v ./...
7 changes: 7 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/free5gc/openapi/models"
)

type Subscriber struct {
IMSI string `json:"imsi"`
Name string `json:"name"`
}

type NFContext struct {
NfId string
Name string
Expand All @@ -18,6 +23,7 @@ type NFContext struct {
SBIPort int

SpyFamilyData map[string]string
Subscribers []Subscriber
}

var nfContext = NFContext{}
Expand All @@ -40,6 +46,7 @@ func InitNfContext() {
nfContext.BindingIPv4 = "0.0.0.0"
}
}
nfContext.Subscribers = []Subscriber{}
nfContext.SpyFamilyData = map[string]string{
"Loid": "Forger",
"Anya": "Forger",
Expand Down
39 changes: 39 additions & 0 deletions internal/sbi/api_subscriber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sbi

import (
"net/http"

"github.com/Alonza0314/nf-example/internal/logger"
"github.com/gin-gonic/gin"
)

func (s *Server) getSubscriberRoute() []Route {
return []Route{
{
Name: "Get All Subscribers",
Method: http.MethodGet,
Pattern: "/",
APIFunc: s.HTTPGetAllSubscribers,
// Use: curl -X GET http://127.0.0.163:8000/subscriber/ -w "\n"
},
{
Name: "Add Subscriber",
Method: http.MethodPost,
Pattern: "/",
APIFunc: s.HTTPAddSubscriber,
// Use: curl -X POST http://127.0.0.163:8000/subscriber/ \
// -H "Content-Type: application/json" \
// -d '{"imsi":"208930000000001","name":"UE-Test"}' -w "\n"
},
}
}

func (s *Server) HTTPGetAllSubscribers(c *gin.Context) {
logger.SBILog.Infof("HTTPGetAllSubscribers")
s.Processor().GetAllSubscribers(c)
}

func (s *Server) HTTPAddSubscriber(c *gin.Context) {
logger.SBILog.Infof("HTTPAddSubscriber")
s.Processor().AddSubscriber(c)
}
31 changes: 31 additions & 0 deletions internal/sbi/processor/subscriber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package processor

import (
"net/http"

context "github.com/Alonza0314/nf-example/internal/context"
"github.com/gin-gonic/gin"
)

func (p *Processor) GetAllSubscribers(c *gin.Context) {
subscribers := p.Context().Subscribers
if len(subscribers) == 0 {
c.JSON(http.StatusOK, []context.Subscriber{})
return
}
c.JSON(http.StatusOK, subscribers)
}

func (p *Processor) AddSubscriber(c *gin.Context) {
var sub context.Subscriber
if err := c.ShouldBindJSON(&sub); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if sub.IMSI == "" || sub.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "imsi and name are required"})
return
}
p.Context().Subscribers = append(p.Context().Subscribers, sub)
c.JSON(http.StatusCreated, sub)
}
3 changes: 3 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func newRouter(s *Server) *gin.Engine {
spyFamilyGroup := router.Group("/spyfamily")
applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute())

subscriberGroup := router.Group("/subscriber")
applyRoutes(subscriberGroup, s.getSubscriberRoute())

return router
}

Expand Down