From e7fd55eb53f24c407f619aef834d035e0bff7125 Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Wed, 15 Feb 2023 18:32:45 -0300 Subject: [PATCH 1/8] feat: Update dl_vlan validation to allow mask --- db/models/__init__.py | 16 +++++++++++++++- tests/unit/test_db_models.py | 25 +++++++++++++++++++++++++ tests/unit/test_utils.py | 1 - 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/db/models/__init__.py b/db/models/__init__.py index db9966e..52ac333 100644 --- a/db/models/__init__.py +++ b/db/models/__init__.py @@ -50,7 +50,7 @@ class MatchSubDoc(BaseModel): dl_src: Optional[str] dl_dst: Optional[str] dl_type: Optional[int] - dl_vlan: Optional[int] + dl_vlan: Optional[str] dl_vlan_pcp: Optional[int] nw_src: Optional[str] nw_dst: Optional[str] @@ -87,6 +87,20 @@ class MatchSubDoc(BaseModel): metadata: Optional[int] tun_id: Optional[int] + @validator("dl_vlan") + def vlan_with_mask(cls, v): + """Validate vlan format""" + try: + int(v) + except ValueError: + try: + [int(part) for part in v.split("/")] + except ValueError: + raise ValueError( + "must be an integer or an integer with a mask in format vlan/mask" + ) + return v + class FlowSubDoc(BaseModel): """Flow DB SubDocument Model.""" diff --git a/tests/unit/test_db_models.py b/tests/unit/test_db_models.py index e038210..0ec397b 100644 --- a/tests/unit/test_db_models.py +++ b/tests/unit/test_db_models.py @@ -28,3 +28,28 @@ def test_flow_doct(self) -> None: flow_doc = FlowDoc(**data) assert flow_doc assert flow_doc.flow.instructions == flow_dict["instructions"] + + def test_flow_vlan_range(self) -> None: + """Test a flow with a vlan range match.""" + flow_dict = { + "match": { + "in_port": 9, + "dl_vlan": "24/4088", + "dl_type": 2048, + "nw_proto": 6, + }, + "instructions": [ + { + "instruction_type": "apply_actions", + "actions": [{"action_type": "output", "port": 4}], + }, + ], + } + data = { + "switch": "00:00:00:00:00:00:00:02", + "flow_id": "1", + "id": "0", + "flow": flow_dict, + } + flow_doc = FlowDoc(**data) + assert flow_doc diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 35d5feb..a06b01b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -140,7 +140,6 @@ def test_validate_range_exceptions(self): test_data = [((1,), ValueError), ((2, 1), ValueError), ((1, "1"), TypeError)] for values, exception in test_data: with self.subTest(values=values, exception=exception): - with self.assertRaises(exception) as exc: _validate_range(values) From d2d13349d87802a7dd7375c0b97d07ab70e6ee4e Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Wed, 22 Feb 2023 21:34:57 -0300 Subject: [PATCH 2/8] feat: Script to upgrade dl_vlan to string in Mongo --- scripts/README.md | 14 ++++++++++++++ scripts/update_vlan_type.py | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 scripts/update_vlan_type.py diff --git a/scripts/README.md b/scripts/README.md index 4fbacdc..f4bdafc 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -49,3 +49,17 @@ And then, to insert (or update) the flows: ``` CMD=insert_flows python3 scripts/storehouse_to_mongo.py ``` + +### Migrate integer `dl_vlan` to string + +#### Pre-requisites + +Same pre-requisites as the script above. + +#### How to use + +Just run the script once to upgrade all flows with a match on `dl_vlan`. + +``` +python3 scripts/update_vlan_type.py +``` diff --git a/scripts/update_vlan_type.py b/scripts/update_vlan_type.py new file mode 100644 index 0000000..18a9e21 --- /dev/null +++ b/scripts/update_vlan_type.py @@ -0,0 +1,10 @@ +from kytos.core.db import mongo_client + +client = mongo_client() +collection = client["napps"]["flows"] +for flow in collection.find({"flow.match.dl_vlan": {"$exists": True}}): + print(f"Updating flow {flow}") + collection.update_one( + {"_id": flow["_id"]}, + {"$set": {"flow.match.dl_vlan": str(flow["flow"]["match"]["dl_vlan"])}}, + ) From 168c9c394e0564ea987462a0d2f7fd844132b6d5 Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Thu, 23 Feb 2023 17:28:01 -0300 Subject: [PATCH 3/8] chore: Update changelog --- CHANGELOG.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 73a0485..becf6ad 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,12 @@ file. [UNRELEASED] - Under development ******************************** +Added +===== + +- Added support for VLAN with mask. The type of ``dl_vlan`` changed from ``int`` to ``str``. After upgrading, run the script in ``scripts/update_vlan_type.py``. + + [2022.3.0] - 2022-12-15 *********************** From 70742390e1d70a2de1e677c05a5e10bec97cd6e3 Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Fri, 24 Feb 2023 18:05:14 -0300 Subject: [PATCH 4/8] chore: Update openapi --- openapi.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/openapi.yml b/openapi.yml index 1fe6294..a412d22 100644 --- a/openapi.yml +++ b/openapi.yml @@ -256,9 +256,8 @@ components: format: int16 example: 2048 dl_vlan: - type: integer - format: int16 - example: 300 + type: string + example: "40/4088" dl_vlan_pcp: type: integer format: int8 From c263c195452ddfc26d7f8e0f6fb480cf1e67c3e9 Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Tue, 7 Mar 2023 18:35:13 -0300 Subject: [PATCH 5/8] feat: Allow dl_vlan to be integer os string --- db/models/__init__.py | 6 +++--- openapi.yml | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/db/models/__init__.py b/db/models/__init__.py index 52ac333..d9a83d3 100644 --- a/db/models/__init__.py +++ b/db/models/__init__.py @@ -6,7 +6,7 @@ from datetime import datetime from decimal import Decimal from enum import Enum -from typing import List, Optional +from typing import List, Optional, Union from bson.decimal128 import Decimal128 from pydantic import BaseModel, Field, validator @@ -50,7 +50,7 @@ class MatchSubDoc(BaseModel): dl_src: Optional[str] dl_dst: Optional[str] dl_type: Optional[int] - dl_vlan: Optional[str] + dl_vlan: Optional[Union[int, str]] dl_vlan_pcp: Optional[int] nw_src: Optional[str] nw_dst: Optional[str] @@ -91,7 +91,7 @@ class MatchSubDoc(BaseModel): def vlan_with_mask(cls, v): """Validate vlan format""" try: - int(v) + return int(v) except ValueError: try: [int(part) for part in v.split("/")] diff --git a/openapi.yml b/openapi.yml index a412d22..b84823f 100644 --- a/openapi.yml +++ b/openapi.yml @@ -256,8 +256,11 @@ components: format: int16 example: 2048 dl_vlan: - type: string - example: "40/4088" + oneOf: + - type: string + example: "40/4088" + - type: integer + example: 40 dl_vlan_pcp: type: integer format: int8 From 0e65b937fc69ea541caeeff248e325ec78b59a51 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 8 Mar 2023 12:14:42 -0300 Subject: [PATCH 6/8] Removed scripts/update_vlan_type.py --- scripts/update_vlan_type.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 scripts/update_vlan_type.py diff --git a/scripts/update_vlan_type.py b/scripts/update_vlan_type.py deleted file mode 100644 index 18a9e21..0000000 --- a/scripts/update_vlan_type.py +++ /dev/null @@ -1,10 +0,0 @@ -from kytos.core.db import mongo_client - -client = mongo_client() -collection = client["napps"]["flows"] -for flow in collection.find({"flow.match.dl_vlan": {"$exists": True}}): - print(f"Updating flow {flow}") - collection.update_one( - {"_id": flow["_id"]}, - {"$set": {"flow.match.dl_vlan": str(flow["flow"]["match"]["dl_vlan"])}}, - ) From 2ec705f4ad90b04672a796ed8a0a12c720b22a18 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 8 Mar 2023 12:18:11 -0300 Subject: [PATCH 7/8] Reverted scripts/README.md changes --- scripts/README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/scripts/README.md b/scripts/README.md index f4bdafc..4fbacdc 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -49,17 +49,3 @@ And then, to insert (or update) the flows: ``` CMD=insert_flows python3 scripts/storehouse_to_mongo.py ``` - -### Migrate integer `dl_vlan` to string - -#### Pre-requisites - -Same pre-requisites as the script above. - -#### How to use - -Just run the script once to upgrade all flows with a match on `dl_vlan`. - -``` -python3 scripts/update_vlan_type.py -``` From a7136c0e9af916b4ae9a87a287a6f9b1a6c0f9f2 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 8 Mar 2023 12:20:27 -0300 Subject: [PATCH 8/8] Updated CHANGELOG.rst --- CHANGELOG.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 06a36cb..4b4c4cb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,7 +25,6 @@ Added - Fixed not iterable error when sending a FlowMod during OpenFlow connection. ->>>>>>> master [2022.3.0] - 2022-12-15 ***********************