feat: basic cwmp implementation

This commit is contained in:
leandrofars 2024-04-24 14:06:56 -03:00
parent 28a0e3e491
commit 9fef8849a1
5 changed files with 48 additions and 25 deletions

View File

@ -14,7 +14,7 @@ func main() {
natsActions := nats.StartNatsClient(c.Nats)
h := handler.NewHandler(natsActions.Publish, natsActions.Subscribe)
h := handler.NewHandler(natsActions.Publish, natsActions.Subscribe, c.Acs)
b := bridge.NewBridge(
natsActions.Publish,

View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"time"
"github.com/joho/godotenv"
)
@ -20,13 +21,15 @@ type Nats struct {
}
type Acs struct {
Port string
Tls bool
TlsPort bool
NoTls bool
Username string
Password string
Route string
Port string
Tls bool
TlsPort bool
NoTls bool
KeepAliveInterval time.Duration
Username string
Password string
Route string
DebugMode bool
}
type Config struct {
@ -44,6 +47,8 @@ func NewConfig() *Config {
natsVerifyCertificates := flag.Bool("nats_verify_certificates", lookupEnvOrBool("NATS_VERIFY_CERTIFICATES", false), "verify validity of certificates from nats server")
acsPort := flag.String("acs_port", lookupEnvOrString("ACS_PORT", ":9292"), "port for acs server")
acsRoute := flag.String("acs_route", lookupEnvOrString("ACS_ROUTE", "/acs"), "route for acs server")
acsKeepAliveInterval := flag.Int("acs_keep_alive_interval", lookupEnvOrInt("KEEP_ALIVE_INTERVAL", 300), "keep alive interval in seconds for acs server")
cwmpDebugMode := flag.Bool("debug_mode", lookupEnvOrBool("CWMP_DEBUG", false), "enable or disable cwmp logs in debug mode")
flHelp := flag.Bool("help", false, "Help")
/*
@ -70,8 +75,10 @@ func NewConfig() *Config {
Ctx: ctx,
},
Acs: Acs{
Port: *acsPort,
Route: *acsRoute,
Port: *acsPort,
Route: *acsRoute,
KeepAliveInterval: time.Duration(*acsKeepAliveInterval),
DebugMode: *cwmpDebugMode,
},
}
}
@ -99,6 +106,17 @@ func lookupEnvOrString(key string, defaultVal string) string {
return defaultVal
}
func lookupEnvOrInt(key string, defaultVal int) int {
if val, _ := os.LookupEnv(key); val != "" {
v, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}
func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, _ := os.LookupEnv(key); val != "" {
v, err := strconv.ParseBool(val)

View File

@ -24,6 +24,10 @@ func (h *Handler) CwmpHandler(w http.ResponseWriter, r *http.Request) {
tmp, _ := ioutil.ReadAll(r.Body)
body := string(tmp)
if h.acsConfig.DebugMode {
log.Println("Received message: ", body)
}
var envelope cwmp.SoapEnvelope
xml.Unmarshal(tmp, &envelope)

View File

@ -2,6 +2,7 @@ package handler
import (
"encoding/json"
"oktopUSP/backend/services/acs/internal/config"
"time"
"github.com/nats-io/nats.go"
@ -53,9 +54,10 @@ type MsgCPEs struct {
}
type Handler struct {
pub func(string, []byte) error
sub func(string, func(*nats.Msg)) error
Cpes map[string]CPE
pub func(string, []byte) error
sub func(string, func(*nats.Msg)) error
Cpes map[string]CPE
acsConfig config.Acs
}
const (
@ -64,10 +66,15 @@ const (
NATS_ADAPTER_SUBJECT_PREFIX = "adapter.v1."
)
func NewHandler(pub func(string, []byte) error, sub func(string, func(*nats.Msg)) error) *Handler {
func NewHandler(
pub func(string, []byte) error,
sub func(string, func(*nats.Msg)) error,
cAcs config.Acs,
) *Handler {
return &Handler{
pub: pub,
sub: sub,
Cpes: make(map[string]CPE),
pub: pub,
sub: sub,
Cpes: make(map[string]CPE),
acsConfig: cAcs,
}
}

View File

@ -5,19 +5,13 @@ import (
"time"
)
// TODO: make these consts dynamic via config
const (
CHECK_STATUS_INTERVAL = 10 * time.Second
KEEP_ALIVE_INTERVAL = 600 * time.Second
)
func (h *Handler) handleCpeStatus(cpe string) {
for {
if time.Since(h.Cpes[cpe].LastConnection) > KEEP_ALIVE_INTERVAL {
if time.Since(h.Cpes[cpe].LastConnection) > h.acsConfig.KeepAliveInterval {
delete(h.Cpes, cpe)
break
}
time.Sleep(CHECK_STATUS_INTERVAL)
time.Sleep(h.acsConfig.KeepAliveInterval)
}
log.Println("CPE", cpe, "is offline")
h.pub("cwmp.v1."+cpe+".status", []byte("0"))