diff --git a/internal/context/context.go b/internal/context/context.go index 2bfe8b2..fcd1164 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -16,7 +16,7 @@ type NFContext struct { UriScheme models.UriScheme BindingIPv4 string SBIPort int - + NetworkData map[string]string SpyFamilyData map[string]string } @@ -57,6 +57,16 @@ func InitNfContext() { "Henry": "Henderson", "Martha": "Marriott", } + nfContext.NetworkData = map[string]string{ + "Router-Core-01": "Core Router", + "Switch-Access-01": "Access Switch", + "Switch-Dist-01": "Distribution Switch", + "AP-Floor-01": "Access Point", + "Firewall-01": "Firewall", + "gNB-Site-01": "5G Base Station", + "UPF-Node-01": "User Plane Function", + "AMF-Node-01": "Access Mobility Function", + } } func GetSelf() *NFContext { diff --git a/internal/sbi/api_network.go b/internal/sbi/api_network.go new file mode 100644 index 0000000..c462d29 --- /dev/null +++ b/internal/sbi/api_network.go @@ -0,0 +1,65 @@ +package sbi + +import ( + "net/http" + + "github.com/Alonza0314/nf-example/internal/logger" + "github.com/gin-gonic/gin" +) + +func (s *Server) getNetworkRoute() []Route { + return []Route{ + { + Name: "Hello Network!", + Method: http.MethodGet, + Pattern: "/", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, "Network Device API") + }, + }, + { + Name: "Get Network Device", + Method: http.MethodGet, + Pattern: "/device/:Name", + APIFunc: s.HTTPGetNetworkDevice, + }, + { + Name: "Add Network Device", + Method: http.MethodPost, + Pattern: "/device", + APIFunc: s.HTTPPostNetworkDevice, + }, + } +} + +func (s *Server) HTTPGetNetworkDevice(c *gin.Context) { + logger.SBILog.Infof("In HTTPGetNetworkDevice") + + targetName := c.Param("Name") + if targetName == "" { + c.String(http.StatusBadRequest, "No device name provided") + return + } + + s.Processor().FindNetworkDevice(c, targetName) +} + +func (s *Server) HTTPPostNetworkDevice(c *gin.Context) { + logger.SBILog.Infof("In HTTPPostNetworkDevice") + + type RequestBody struct { + Name string `json:"name"` + Type string `json:"type"` + } + var body RequestBody + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + if body.Name == "" || body.Type == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "name and type are required"}) + return + } + + s.Processor().AddNetworkDevice(c, body.Name, body.Type) +} diff --git a/internal/sbi/processor/network.go b/internal/sbi/processor/network.go new file mode 100644 index 0000000..4956e07 --- /dev/null +++ b/internal/sbi/processor/network.go @@ -0,0 +1,35 @@ +package processor + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func (p *Processor) FindNetworkDevice(c *gin.Context, targetName string) { + if deviceType, ok := p.Context().NetworkData[targetName]; ok { + c.JSON(http.StatusOK, gin.H{ + "name": targetName, + "type": deviceType, + }) + return + } + c.JSON(http.StatusNotFound, gin.H{ + "error": fmt.Sprintf("device [%s] not found", targetName), + }) +} + +func (p *Processor) AddNetworkDevice(c *gin.Context, name string, deviceType string) { + if _, ok := p.Context().NetworkData[name]; ok { + c.JSON(http.StatusConflict, gin.H{ + "error": fmt.Sprintf("device [%s] already exists", name), + }) + return + } + p.Context().NetworkData[name] = deviceType + c.JSON(http.StatusCreated, gin.H{ + "name": name, + "type": deviceType, + }) +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index b62139f..56a8de1 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -44,6 +44,9 @@ func newRouter(s *Server) *gin.Engine { spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + + networkGroup := router.Group("/network") + applyRoutes(networkGroup, s.getNetworkRoute()) return router }