using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json.Serialization;
namespace CoreAgent.Domain.Entities
{
///
/// 网络配置聚合根
///
public class NetworkConfiguration : IValidatableObject
{
///
/// 配置键值
///
[Required(ErrorMessage = "配置键值不能为空")]
[JsonPropertyName("configKey")]
public string ConfigKey { get; set; }
///
/// RAN配置文件路径
///
[Required(ErrorMessage = "RAN配置路径不能为空")]
[JsonPropertyName("ragConfig")]
public string RagConfig { get; set; }
///
/// 核心网和IMS配置列表
///
[JsonPropertyName("coreOrImsConfigs")]
public List CoreOrImsConfigs { get; set; }
///
/// APN配置
///
[Required(ErrorMessage = "APN配置不能为空")]
[JsonPropertyName("apn")]
public string Apn { get; set; }
///
/// 频段配置
///
[Required(ErrorMessage = "频段配置不能为空")]
[MinLength(1, ErrorMessage = "至少需要一个频段配置")]
[JsonPropertyName("band")]
public List Band { get; set; }
///
/// 配置说明
///
[JsonPropertyName("comment")]
public string Comment { get; set; }
///
/// 构造函数
///
[JsonConstructor]
public NetworkConfiguration()
{
CoreOrImsConfigs = new List();
Band = new List();
}
///
/// 创建新的网络配置
///
public static NetworkConfiguration Create(
string configKey,
string ragConfig,
List coreOrImsConfigs,
string apn,
List band,
string comment = null)
{
var config = new NetworkConfiguration
{
ConfigKey = configKey,
RagConfig = ragConfig,
Apn = apn,
Comment = comment,
CoreOrImsConfigs = coreOrImsConfigs ?? new List(),
Band = band ?? new List()
};
var validationResults = new List();
if (!Validator.TryValidateObject(config, new ValidationContext(config), validationResults, true))
{
throw new ValidationException(string.Join(Environment.NewLine, validationResults.Select(r => r.ErrorMessage)));
}
return config;
}
///
/// 验证配置
///
public IEnumerable Validate(ValidationContext validationContext)
{
// 验证PLMN是否重复
var plmnGroups = CoreOrImsConfigs.GroupBy(x => x.Plmn);
foreach (var group in plmnGroups)
{
if (group.Count() > 1)
{
yield return new ValidationResult($"PLMN {group.Key} 重复配置");
}
}
// 验证索引是否连续
var orderedConfigs = CoreOrImsConfigs.OrderBy(x => x.Index).ToList();
for (int i = 0; i < orderedConfigs.Count; i++)
{
if (orderedConfigs[i].Index != i + 1)
{
yield return new ValidationResult($"配置索引不连续,期望 {i + 1},实际 {orderedConfigs[i].Index}");
}
}
// 验证频段格式
foreach (var band in Band)
{
if (!band.StartsWith("B") || !int.TryParse(band.Substring(1), out _))
{
yield return new ValidationResult($"频段格式不正确: {band},应为 B1、B2 等格式");
}
}
}
///
/// 获取指定PLMN的配置
///
public CoreImsConfig GetConfigByPlmn(string plmn)
{
return CoreOrImsConfigs.FirstOrDefault(x => x.Plmn == plmn);
}
///
/// 获取指定索引的配置
///
public CoreImsConfig GetConfigByIndex(int index)
{
return CoreOrImsConfigs.FirstOrDefault(x => x.Index == index);
}
}
///
/// 网络配置包装类
///
public class NetworkConfigurationsWrapper
{
///
/// 网络配置列表
///
[JsonPropertyName("networkConfigurations")]
public List NetworkConfigurations { get; set; } = new List();
}
///
/// 核心网和IMS配置实体
///
public class CoreImsConfig
{
///
/// 配置索引
///
[Required(ErrorMessage = "配置索引不能为空")]
[Range(1, int.MaxValue, ErrorMessage = "配置索引必须大于0")]
[JsonPropertyName("index")]
public int Index { get; set; }
///
/// PLMN码
///
[Required(ErrorMessage = "PLMN码不能为空")]
[RegularExpression(@"^\d{5,6}$", ErrorMessage = "PLMN码必须是5-6位数字")]
[JsonPropertyName("plmn")]
public string Plmn { get; set; }
///
/// 核心网配置文件路径
///
[Required(ErrorMessage = "核心网配置路径不能为空")]
[RegularExpression(@"^Config/CoreNetwork/.*\.cfg$", ErrorMessage = "核心网配置路径格式不正确")]
[JsonPropertyName("coreNetworkConfig")]
public string CoreNetworkConfig { get; set; }
///
/// IMS配置文件路径
///
[Required(ErrorMessage = "IMS配置路径不能为空")]
[RegularExpression(@"^Config/Ims/.*\.cfg$", ErrorMessage = "IMS配置路径格式不正确")]
[JsonPropertyName("imsConfig")]
public string ImsConfig { get; set; }
}
}