feat: 新增mml到工具模块
This commit is contained in:
98
src/modules/tool/controller/mml.go
Normal file
98
src/modules/tool/controller/mml.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
neService "be.ems/src/modules/network_element/service"
|
||||
"be.ems/src/modules/tool/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 MMLController 结构体
|
||||
var NewMML = &MMLController{
|
||||
neInfoService: neService.NewNeInfo,
|
||||
mmlSystemService: service.NewMMLSystem,
|
||||
mmlSubscriberService: service.NewMMLSubscriber,
|
||||
}
|
||||
|
||||
// MML 网元MML
|
||||
//
|
||||
// PATH /tool/mml
|
||||
type MMLController struct {
|
||||
neInfoService *neService.NeInfo // 网元信息服务
|
||||
mmlSystemService *service.MMLSystem
|
||||
mmlSubscriberService *service.MMLSubscriber
|
||||
}
|
||||
|
||||
// SystemList MML网元列表
|
||||
//
|
||||
// GET /system/list
|
||||
func (s MMLController) SystemList(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.mmlSystemService.FindByPage(query)
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// SubscriberList MML网元UDM列表
|
||||
//
|
||||
// GET /subscriber/list
|
||||
func (s MMLController) SubscriberList(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.mmlSubscriberService.FindByPage(query)
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// Command MML命令执行
|
||||
//
|
||||
// POST /command
|
||||
func (s MMLController) Command(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
Command []string `json:"command" binding:"required"` // 命令
|
||||
Type string `json:"type" binding:"required,oneof=General Standard"` // 类型UPF标准版 General Standard
|
||||
}
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.FindByNeTypeAndNeID(body.NeType, body.NeId)
|
||||
if neInfo.NeId != body.NeId || neInfo.IP == "" {
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
// 网元主机的Telnet客户端
|
||||
num := 1
|
||||
if body.Type == "Standard" {
|
||||
num = 2
|
||||
}
|
||||
telnetClient, err := s.neInfoService.NeRunTelnetClient(neInfo.NeType, neInfo.NeId, num)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer telnetClient.Close()
|
||||
if body.Type == "Standard" {
|
||||
telnetClient.WindowChange(1024, 1024)
|
||||
}
|
||||
// 发送MML
|
||||
result := []string{}
|
||||
for _, v := range body.Command {
|
||||
output, err := telnetClient.RunCMD(v + "\r\r")
|
||||
if err != nil {
|
||||
result = append(result, err.Error())
|
||||
continue
|
||||
}
|
||||
result = append(result, strings.TrimSpace(output))
|
||||
}
|
||||
c.JSON(200, resp.OkData(result))
|
||||
}
|
||||
Reference in New Issue
Block a user