用python实现: "dataplanes_config": [ {"name": "default", "network_type": "DATAPLANE", "subnets": [{"start": "166.166.16.10", "end": "166.166.16.166", "cidr": "166.166.16.0/24", "gateway": "", "vlan_id": ""}] }], "host-192-168-103-110": { "nics": { "eno2": { "physical_network": { "default": { "network_type": "DATAPLANE", "vtep_ip": "1.1.1.1", "segmentation_type": "vxlan" } }, "vswitch_type": "dvs", "dvs_nic_form": "", "exist_netplanes": [ "DATAPLANE" ], } } } network_scenarios= sdn 或者 network_scenarios=sdn-hierarchical and sdn_manage_vswitch=yes时: segmentation_type = vxlan时,vswitch_type =dvs或ovs时这里主要检查如下: (1)vtep_ip是否配置(必填) (2)vtep_ip是否在subnets中所属网段且在start 和end之间 (3) vtep_ip是否有重复
时间: 2024-02-10 21:11:28 浏览: 106
可以使用Python中的字典和列表来实现该配置信息的存储和检查。具体实现如下:
```python
config = {
"dataplanes_config": [
{
"name": "default",
"network_type": "DATAPLANE",
"subnets": [
{
"start": "166.166.16.10",
"end": "166.166.16.166",
"cidr": "166.166.16.0/24",
"gateway": "",
"vlan_id": ""
}
]
}
],
"host-192-168-103-110": {
"nics": {
"eno2": {
"physical_network": {
"default": {
"network_type": "DATAPLANE",
"vtep_ip": "1.1.1.1",
"segmentation_type": "vxlan"
}
},
"vswitch_type": "dvs",
"dvs_nic_form": "",
"exist_netplanes": [
"DATAPLANE"
]
}
}
}
}
network_scenarios = "sdn-hierarchical"
sdn_manage_vswitch = True
if network_scenarios == "sdn" or (network_scenarios == "sdn-hierarchical" and sdn_manage_vswitch):
nic_config = config["host-192-168-103-110"]["nics"]["eno2"]
if nic_config["physical_network"]["default"]["segmentation_type"] == "vxlan" and nic_config["vswitch_type"] in ["dvs", "ovs"]:
vtep_ip = nic_config["physical_network"]["default"]["vtep_ip"]
subnets = config["dataplanes_config"][0]["subnets"]
cidr = None
for subnet in subnets:
if vtep_ip in subnet["cidr"]:
cidr = subnet["cidr"]
start_ip = subnet["start"]
end_ip = subnet["end"]
break
if cidr and start_ip <= vtep_ip <= end_ip:
# vtep_ip is within the subnet range, check for duplicates
vtep_ips = [nic["physical_network"]["default"]["vtep_ip"] for nic in config["host-192-168-103-110"]["nics"].values()]
if vtep_ips.count(vtep_ip) > 1:
print("Error: Duplicate vtep_ip detected.")
else:
print("Error: vtep_ip is not within the specified subnet range.")
else:
print("Error: Invalid segmentation_type or vswitch_type.")
else:
print("Error: Invalid network_scenarios or sdn_manage_vswitch setting.")
```
上述代码可以进行如下检查:
1. 检查`network_scenarios`是否为"sdn"或者"sdn-hierarchical"且`sdn_manage_vswitch`为True;
2. 检查`segmentation_type`是否为"vxlan"且`vswitch_type`为"dvs"或者"ovs";
3. 检查`vtep_ip`是否配置,且在指定的`subnets`中;
4. 检查`vtep_ip`是否有重复。
阅读全文