From b034ed7669d3083fdeb14a9e79e0a32708079394 Mon Sep 17 00:00:00 2001 From: velazquez Date: Fri, 26 Sep 2025 14:52:13 +0200 Subject: [PATCH] Change code to get realization method by tag-type in ietf intent --- .gitignore | 2 +- src/mapper/main.py | 1 - src/realizer/main.py | 7 ++++--- src/realizer/tfs/main.py | 6 +++--- src/templates/ietf_template_empty.json | 10 +++++++--- src/utils/safe_get.py | 26 ++++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 src/utils/safe_get.py diff --git a/.gitignore b/.gitignore index f16afd3..d4f1865 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ swagger/__pycache__/ src/__pycache__/ venv/ .env - +slice.db diff --git a/src/mapper/main.py b/src/mapper/main.py index 84be572..b738935 100644 --- a/src/mapper/main.py +++ b/src/mapper/main.py @@ -43,7 +43,6 @@ def mapper(ietf_intent): # Extract Service Level Objectives (SLOs) from the intent slos = ietf_intent["ietf-network-slice-service:network-slice-services"]["slo-sle-templates"]["slo-sle-template"][0]["slo-policy"]["metric-bound"] - print(f"SLOs: {slos}") if slos: # Find candidate NRPs that can meet the SLO requirements candidates = [ diff --git a/src/realizer/main.py b/src/realizer/main.py index 2bd8983..e1f08a0 100644 --- a/src/realizer/main.py +++ b/src/realizer/main.py @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This file is an original contribution from Telefonica Innovación Digital S.L. +# This file includes original contributions from Telefonica Innovación Digital S.L. +import logging from .select_way import select_way from .nrp_handler import nrp_handler +from src.utils.safe_get import safe_get def realizer(ietf_intent, need_nrp=False, order=None, nrp=None, controller_type=None, response=None): """ @@ -37,7 +39,6 @@ def realizer(ietf_intent, need_nrp=False, order=None, nrp=None, controller_type= return nrp_view else: # Select slice service method - way = ietf_intent["ietf-network-slice-service:network-slice-services"]["slice-service"][0]["service-tags"]["tag-type"]["value"] - way = "L2VPN" + way = safe_get(ietf_intent, ['ietf-network-slice-service:network-slice-services', 'slice-service', 0, 'service-tags', 'tag-type', 0, 'tag-type-value', 0]) request = select_way(controller=controller_type, way=way, ietf_intent=ietf_intent, response=response) return request diff --git a/src/realizer/tfs/main.py b/src/realizer/tfs/main.py index 9e1233e..2975127 100644 --- a/src/realizer/tfs/main.py +++ b/src/realizer/tfs/main.py @@ -19,11 +19,11 @@ from .service_types.tfs_l2vpn import tfs_l2vpn from .service_types.tfs_l3vpn import tfs_l3vpn def tfs(ietf_intent, way=None, response=None): - if way == "L2VPN": + if way == "L2": realizing_request = tfs_l2vpn(ietf_intent, response) - elif way == "L3VPN": + elif way == "L3": realizing_request = tfs_l3vpn(ietf_intent, response) else: - logging.warning(f"Unsupported way: {way}. Defaulting to L2VPN realization.") + logging.warning(f"Unsupported way: {way}. Defaulting to L2 realization.") realizing_request = tfs_l2vpn(ietf_intent, response) return realizing_request \ No newline at end of file diff --git a/src/templates/ietf_template_empty.json b/src/templates/ietf_template_empty.json index cdaf66c..c484a4a 100644 --- a/src/templates/ietf_template_empty.json +++ b/src/templates/ietf_template_empty.json @@ -29,10 +29,14 @@ "id":"5GSliceMapping", "description":"example 5G Slice mapping", "service-tags":{ - "tag-type":{ - "tag-type":"", - "value":"" + "tag-type": [ + { + "tag-type": "", + "tag-type-value": [ + "" + ] } + ] }, "slo-sle-policy":{ "slo-sle-template":"" diff --git a/src/utils/safe_get.py b/src/utils/safe_get.py new file mode 100644 index 0000000..a12d885 --- /dev/null +++ b/src/utils/safe_get.py @@ -0,0 +1,26 @@ +# Copyright 2022-2025 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/) + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file is an original contribution from Telefonica Innovación Digital S.L. + +def safe_get(dct, keys): + """Safely get a nested value from a dictionary or list.""" + for key in keys: + if isinstance(dct, dict) and key in dct: + dct = dct[key] + elif isinstance(dct, list) and isinstance(key, int) and key < len(dct): + dct = dct[key] + else: + return None + return dct -- GitLab