feat: basic cwmp implementation
This commit is contained in:
parent
28a0e3e491
commit
9fef8849a1
|
|
@ -14,7 +14,7 @@ func main() {
|
||||||
|
|
||||||
natsActions := nats.StartNatsClient(c.Nats)
|
natsActions := nats.StartNatsClient(c.Nats)
|
||||||
|
|
||||||
h := handler.NewHandler(natsActions.Publish, natsActions.Subscribe)
|
h := handler.NewHandler(natsActions.Publish, natsActions.Subscribe, c.Acs)
|
||||||
|
|
||||||
b := bridge.NewBridge(
|
b := bridge.NewBridge(
|
||||||
natsActions.Publish,
|
natsActions.Publish,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
@ -20,13 +21,15 @@ type Nats struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Acs struct {
|
type Acs struct {
|
||||||
Port string
|
Port string
|
||||||
Tls bool
|
Tls bool
|
||||||
TlsPort bool
|
TlsPort bool
|
||||||
NoTls bool
|
NoTls bool
|
||||||
Username string
|
KeepAliveInterval time.Duration
|
||||||
Password string
|
Username string
|
||||||
Route string
|
Password string
|
||||||
|
Route string
|
||||||
|
DebugMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
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")
|
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")
|
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")
|
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")
|
flHelp := flag.Bool("help", false, "Help")
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -70,8 +75,10 @@ func NewConfig() *Config {
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
},
|
},
|
||||||
Acs: Acs{
|
Acs: Acs{
|
||||||
Port: *acsPort,
|
Port: *acsPort,
|
||||||
Route: *acsRoute,
|
Route: *acsRoute,
|
||||||
|
KeepAliveInterval: time.Duration(*acsKeepAliveInterval),
|
||||||
|
DebugMode: *cwmpDebugMode,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -99,6 +106,17 @@ func lookupEnvOrString(key string, defaultVal string) string {
|
||||||
return defaultVal
|
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 {
|
func lookupEnvOrBool(key string, defaultVal bool) bool {
|
||||||
if val, _ := os.LookupEnv(key); val != "" {
|
if val, _ := os.LookupEnv(key); val != "" {
|
||||||
v, err := strconv.ParseBool(val)
|
v, err := strconv.ParseBool(val)
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ func (h *Handler) CwmpHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
tmp, _ := ioutil.ReadAll(r.Body)
|
tmp, _ := ioutil.ReadAll(r.Body)
|
||||||
body := string(tmp)
|
body := string(tmp)
|
||||||
|
|
||||||
|
if h.acsConfig.DebugMode {
|
||||||
|
log.Println("Received message: ", body)
|
||||||
|
}
|
||||||
|
|
||||||
var envelope cwmp.SoapEnvelope
|
var envelope cwmp.SoapEnvelope
|
||||||
xml.Unmarshal(tmp, &envelope)
|
xml.Unmarshal(tmp, &envelope)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"oktopUSP/backend/services/acs/internal/config"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
|
@ -53,9 +54,10 @@ type MsgCPEs struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
pub func(string, []byte) error
|
pub func(string, []byte) error
|
||||||
sub func(string, func(*nats.Msg)) error
|
sub func(string, func(*nats.Msg)) error
|
||||||
Cpes map[string]CPE
|
Cpes map[string]CPE
|
||||||
|
acsConfig config.Acs
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -64,10 +66,15 @@ const (
|
||||||
NATS_ADAPTER_SUBJECT_PREFIX = "adapter.v1."
|
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{
|
return &Handler{
|
||||||
pub: pub,
|
pub: pub,
|
||||||
sub: sub,
|
sub: sub,
|
||||||
Cpes: make(map[string]CPE),
|
Cpes: make(map[string]CPE),
|
||||||
|
acsConfig: cAcs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,13 @@ import (
|
||||||
"time"
|
"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) {
|
func (h *Handler) handleCpeStatus(cpe string) {
|
||||||
for {
|
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)
|
delete(h.Cpes, cpe)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(CHECK_STATUS_INTERVAL)
|
time.Sleep(h.acsConfig.KeepAliveInterval)
|
||||||
}
|
}
|
||||||
log.Println("CPE", cpe, "is offline")
|
log.Println("CPE", cpe, "is offline")
|
||||||
h.pub("cwmp.v1."+cpe+".status", []byte("0"))
|
h.pub("cwmp.v1."+cpe+".status", []byte("0"))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user