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 vlan range on vlan_pool migration script #213

Closed
italovalcy opened this issue Sep 24, 2024 · 0 comments · Fixed by #214
Closed

add support for custom vlan range on vlan_pool migration script #213

italovalcy opened this issue Sep 24, 2024 · 0 comments · Fixed by #214

Comments

@italovalcy
Copy link

Hi,

Currently the vlan_pool migration script assumes all VLAN range defaults to [1, 4095], eventually overwriting the vlan range defined by the network operator. It would be nice if the network operator had the option to provide a custom vlan range per interface ID.

Below is a change proposal that allows the network operator to provide such customization:

diff --git a/scripts/vlan_pool.py b/scripts/vlan_pool.py
index c319489..ffc917d 100644
--- a/scripts/vlan_pool.py
+++ b/scripts/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

The README could be enhanced to provide instructions like this:

CMD=update_database CUSTOM_TAG_RANGE='{"00:00:00:00:00:00:00:10:25": [[1101, 1399]], "00:00:00:00:00:40:00:04:32": [[1101, 1399]]}' python3 vlan_pool.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant