Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Jul 16, 2024
1 parent 46508b7 commit aeb98f7
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 288 deletions.
3 changes: 3 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ indent_unit = tab

[sqlfluff:indentation]
indent_unit = tab

[sqlfluff:rules:L057]
ignore_words = uuid-ossp
4 changes: 0 additions & 4 deletions internal/app/api/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package app_api

import (
"github.com/nextmn/srv6/internal/ctrl"
ctrl_api "github.com/nextmn/srv6/internal/ctrl/api"
"github.com/nextmn/srv6/internal/database"
"github.com/nextmn/srv6/internal/iproute2"
)
Expand All @@ -22,7 +21,4 @@ type Registry interface {
RegisterDB(*database.Database)
DB() (*database.Database, bool)
DeleteDB()
RegisterRulesRegistry(rr ctrl_api.RulesRegistry)
RulesRegistry() (ctrl_api.RulesRegistry, bool)
DeleteRulesRegistry()
}
19 changes: 1 addition & 18 deletions internal/app/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package app

import (
"fmt"

"github.com/nextmn/srv6/internal/ctrl"
ctrl_api "github.com/nextmn/srv6/internal/ctrl/api"
"github.com/nextmn/srv6/internal/database"
"github.com/nextmn/srv6/internal/iproute2"
)
Expand All @@ -16,15 +16,13 @@ type Registry struct {
ifaces map[string]*iproute2.TunIface
controllerRegistry *ctrl.ControllerRegistry
db *database.Database
rulesRegistry ctrl_api.RulesRegistry
}

func NewRegistry() *Registry {
return &Registry{
ifaces: make(map[string]*iproute2.TunIface),
controllerRegistry: nil,
db: nil,
rulesRegistry: nil,
}
}

Expand Down Expand Up @@ -72,18 +70,3 @@ func (r *Registry) DB() (*database.Database, bool) {
func (r *Registry) DeleteDB() {
r.db = nil
}

func (r *Registry) RegisterRulesRegistry(rr ctrl_api.RulesRegistry) {
r.rulesRegistry = rr
}

func (r *Registry) RulesRegistry() (ctrl_api.RulesRegistry, bool) {
if r.rulesRegistry == nil {
return nil, false
}
return r.rulesRegistry, true
}

func (r *Registry) DeleteRulesRegistry() {
r.rulesRegistry = nil
}
133 changes: 23 additions & 110 deletions internal/ctrl/rules-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"fmt"
"log"
"net/http"
"net/netip"
"sync"

"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
Expand All @@ -19,65 +17,12 @@ import (

// A RulesRegistry contains rules for an headend
type RulesRegistry struct {
sync.RWMutex
rules jsonapi.RuleMap
db *database.Database
db *database.Database
}

func NewRulesRegistry(db *database.Database) *RulesRegistry {
return &RulesRegistry{
rules: make(jsonapi.RuleMap),
db: db,
}
}

func (rr *RulesRegistry) UplinkAction(UEIp netip.Addr, GnbIp netip.Addr) (uuid.UUID, jsonapi.Action, error) {
rr.RLock()
defer rr.RUnlock()
for id, r := range rr.rules {
if !r.Enabled {
continue
}
if r.Type != "uplink" {
continue
}
if !r.Match.GNBIpPrefix.Contains(GnbIp) {
continue
}
if r.Match.UEIpPrefix.Contains(UEIp) {
return id, r.Action, nil
}
}
return uuid.UUID{}, jsonapi.Action{}, fmt.Errorf("Not found")
}

func (rr *RulesRegistry) DownlinkAction(UEIp netip.Addr) (uuid.UUID, jsonapi.Action, error) {
rr.RLock()
defer rr.RUnlock()
for id, r := range rr.rules {
if !r.Enabled {
continue
}
if r.Type != "downlink" {
continue
}
if r.Match.UEIpPrefix.Contains(UEIp) {
return id, r.Action, nil
}
}
return uuid.UUID{}, jsonapi.Action{}, fmt.Errorf("Not found")
}

func (rr *RulesRegistry) ByUUID(uuid uuid.UUID) (jsonapi.Action, error) {
rr.RLock()
defer rr.RUnlock()
if rule, ok := rr.rules[uuid]; !ok {
return jsonapi.Action{}, fmt.Errorf("Not found")
} else {
if !rule.Enabled {
return rule.Action, fmt.Errorf("Disabled")
}
return rule.Action, nil
db: db,
}
}

Expand All @@ -89,17 +34,15 @@ func (rr *RulesRegistry) GetRule(c *gin.Context) {
return
}
c.Header("Cache-Control", "no-cache")
rr.Lock()
defer rr.Unlock()
if val, ok := rr.rules[iduuid]; ok {
c.JSON(http.StatusOK, val)
return
}
c.JSON(http.StatusNotFound, gin.H{"message": "rule not found"})
_ = iduuid
c.Status(http.StatusNotImplemented)
}

func (rr *RulesRegistry) GetRules(c *gin.Context) {
c.JSON(http.StatusOK, rr.rules)
//FIXME

c.Status(http.StatusNotImplemented)
//c.JSON(http.StatusOK, rr.rules)
}

func (rr *RulesRegistry) DeleteRule(c *gin.Context) {
Expand All @@ -110,13 +53,12 @@ func (rr *RulesRegistry) DeleteRule(c *gin.Context) {
return
}
c.Header("Cache-Control", "no-cache")
rr.Lock()
defer rr.Unlock()
if _, exists := rr.rules[iduuid]; !exists {
c.JSON(http.StatusNotFound, gin.H{"message": "rule not found"})
err = rr.db.DeleteRule(iduuid)
if err != nil {
log.Printf("Could not delete rule in the database: %s\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "could not delete rule in the database"})
return
}
delete(rr.rules, iduuid)
c.Status(http.StatusNoContent) // successful deletion
}

Expand All @@ -128,19 +70,14 @@ func (rr *RulesRegistry) EnableRule(c *gin.Context) {
return
}
c.Header("Cache-Control", "no-cache")
rr.Lock()
defer rr.Unlock()
if val, ok := rr.rules[iduuid]; ok {
val.Enabled = true
rr.rules[iduuid] = val // rules is not a map of pointers
c.Status(http.StatusNoContent)
return
}
err = rr.db.EnableRule(iduuid)
if err != nil {
log.Printf("Could not enable rule in the database: %s\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "could not enable rule in the database"})
return
//TODO: check if rule not found
}
c.JSON(http.StatusNotFound, gin.H{"message": "rule not found"})
c.Status(http.StatusNoContent)
}

func (rr *RulesRegistry) DisableRule(c *gin.Context) {
Expand All @@ -151,19 +88,14 @@ func (rr *RulesRegistry) DisableRule(c *gin.Context) {
return
}
c.Header("Cache-Control", "no-cache")
rr.Lock()
defer rr.Unlock()
if val, ok := rr.rules[iduuid]; ok {
val.Enabled = false
rr.rules[iduuid] = val // rules is not a map of pointers
c.Status(http.StatusNoContent)
return
}
err = rr.db.DisableRule(iduuid)
if err != nil {
log.Printf("Could not disable rule in the database: %s\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "could not disable rule in the database"})
return
//TODO: check if rule not found
}
c.JSON(http.StatusNotFound, gin.H{"message": "rule not found"})
c.Status(http.StatusNoContent)
}

// Post a new rule
Expand All @@ -174,30 +106,11 @@ func (rr *RulesRegistry) PostRule(c *gin.Context) {
return
}
c.Header("Cache-Control", "no-cache")
rr.Lock()
defer rr.Unlock()

// TODO: perform checks

id, err := uuid.NewV4()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed to generate UUID"})
}
for {
if _, exists := rr.rules[id]; !exists {
break
} else {
id, err = uuid.NewV4()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed to generate UUID"})
}
}
}
rr.rules[id] = rule
err = rr.db.InsertRule(id, rule)
id, err := rr.db.InsertRule(rule)
if err != nil {
log.Printf("Could not insert rule in the database: %s\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed to insert rule"})
}
c.Header("Location", fmt.Sprintf("/rules/%s", id))
c.JSON(http.StatusCreated, rr.rules[id])
c.JSON(http.StatusCreated, rule)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// 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 ctrl_api
package database_api

import (
"github.com/gofrs/uuid"
"github.com/nextmn/json-api/jsonapi"
"net/netip"

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

type RulesRegistry interface {
UplinkAction(UEIp netip.Addr, GnbIp netip.Addr) (uuid.UUID, jsonapi.Action, error)
DownlinkAction(UEIp netip.Addr) (uuid.UUID, jsonapi.Action, error)
ByUUID(uuid uuid.UUID) (jsonapi.Action, error)
type Downlink interface {
GetDownlinkAction(ueIp netip.Addr) (jsonapi.Action, error)
}
8 changes: 4 additions & 4 deletions internal/database/api/uplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package database_api

import (
"github.com/gofrs/uuid"
"net/netip"

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

type Uplink interface {
InsertAction(uplinkTeid uint32, SrgwIp netip.Addr, GnbIp netip.Addr, actionUuid uuid.UUID) error
UpdateAction(uplinkTeid uint32, SrgwIp netip.Addr, GnbIp netip.Addr, actionUuid uuid.UUID) error
GetUplinkAction(UplinkTeid uint32, SrgwIp netip.Addr, GnbIp netip.Addr) (uuid.UUID, error)
GetUplinkAction(UplinkTeid uint32, SrgwIp netip.Addr, GnbIp netip.Addr) (jsonapi.Action, error)
SetUplinkAction(UplinkTeid uint32, SrgwIp netip.Addr, GnbIp netip.Addr, UeIpAddress netip.Addr) (jsonapi.Action, error)
}
Loading

0 comments on commit aeb98f7

Please sign in to comment.