Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom tag range on vlan pool migration script #214

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions scripts/db/2023.2.0/000_vlan_pool.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import datetime
import json
import sys
import os
from collections import defaultdict
from kytos.core.db import Mongo

DEFAULT_TAG_RANGES = [[1, 4095]]
custom_tag_range = json.loads(os.environ.get("CUSTOM_TAG_RANGE", "{}"))


def get_tag_range(intf_id) -> [list[list[int]]]:
return custom_tag_range.get(intf_id, DEFAULT_TAG_RANGES)

def get_range(vlans, avoid) -> list[list[int]]:
"""Convert available_vlans to available_tags.
Expand Down Expand Up @@ -34,10 +40,10 @@ def get_range(vlans, avoid) -> list[list[int]]:
result.append([start, end])
return result

def generate_ranges(avoid) -> [list[list[int]]]:
def generate_ranges(avoid, intf_id) -> [list[list[int]]]:
"""Generate available_tags only from avoid"""
if not avoid:
return DEFAULT_TAG_RANGES
return get_tag_range(intf_id)

avoid.sort()
ranges = []
Expand Down Expand Up @@ -93,10 +99,10 @@ def update_database(mongo: Mongo):
result = db.interface_details.update_one(
{"id": document["id"]},
{
"$set":
"$set":
{
"available_tags": {"vlan": ranges},
"tag_ranges": {"vlan": DEFAULT_TAG_RANGES}
"tag_ranges": {"vlan": get_tag_range(document["id"])}
},
"$unset": {"available_vlans": ""}
}
Expand All @@ -105,15 +111,15 @@ def update_database(mongo: Mongo):

evc_intf_count = 0
for intf_id, avoid_tags in evc_tags.items():
available_tags = generate_ranges(list(avoid_tags))
available_tags = generate_ranges(list(avoid_tags), intf_id)
utc_now = datetime.datetime.utcnow()
result = db.interface_details.insert_one({
"_id": intf_id,
"id": intf_id,
"inserted_at": utc_now,
"updated_at": utc_now,
"available_tags": {"vlan": available_tags},
"tag_ranges": {"vlan": DEFAULT_TAG_RANGES},
"tag_ranges": {"vlan": get_tag_range(intf_id)},
})
if result:
evc_intf_count += 1
Expand Down
8 changes: 7 additions & 1 deletion scripts/db/2023.2.0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ An `ERROR` can be shown if a duplicated `TAG` is detected in different `EVC`s. A
Error: Detected duplicated 200 TAG in EVCs 861a11d8fce148 and d74e18464d524b in interface 00:00:00:00:00:00:00:01:1
```

If needed, you can also specify a custom VLAN tag range per interface:

```
CMD=update_database CUSTOM_TAG_RANGE='{"00:00:00:00:00:00:00:01:1": [[2100, 2999]], "00:00:00:00:00:00:00:02:32": [[1000, 1399]]}' python3 scripts/db/2023.2.0/000_vlan_pool.py
```

</details>

<details><summary><h3>Add <code>special_available_tags</code> and <code>special_tags</code> field to each Interface document in <code>interface_details</code> collection </h3></summary>
Expand Down Expand Up @@ -146,4 +152,4 @@ An `ERROR` can be shown if a duplicated `TAG` is detected in different `EVC`s. A
Error: Detected duplicated vlan 'any' TAG in EVCs d68eb033688a48 and 861a11d8fce148 in interface 00:00:00:00:00:00:00:01:1
```

</details>
</details>