diff --git a/scripts/db/2023.2.0/000_vlan_pool.py b/scripts/db/2023.2.0/000_vlan_pool.py index 3d1c178..f336934 100644 --- a/scripts/db/2023.2.0/000_vlan_pool.py +++ b/scripts/db/2023.2.0/000_vlan_pool.py @@ -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. @@ -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 = [] @@ -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": ""} } @@ -105,7 +111,7 @@ 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, @@ -113,7 +119,7 @@ def update_database(mongo: Mongo): "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 diff --git a/scripts/db/2023.2.0/README.md b/scripts/db/2023.2.0/README.md index 16f0791..583b4e4 100644 --- a/scripts/db/2023.2.0/README.md +++ b/scripts/db/2023.2.0/README.md @@ -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 +``` +

Add special_available_tags and special_tags field to each Interface document in interface_details collection

@@ -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 ``` -
\ No newline at end of file +