Commit 35eb3a95 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

feat: remove extract_teraflowsdn_device_name function and update...

feat: remove extract_teraflowsdn_device_name function and update compute_optical_path with mock response
parent 5e8a5f3a
Loading
Loading
Loading
Loading
+179 −212
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ from common.proto.context_pb2 import Service, ConfigRule, ConfigActionEnum
from common.proto.pathcomp_pb2 import PathCompReply


LOGGER = logging.getLogger(__name__)

def safe_get(d, keys, default=None):
    for key in keys:
        if isinstance(d, dict):
@@ -14,37 +16,6 @@ def safe_get(d, keys, default=None):
    return d


def extract_teraflowsdn_device_name(service: Service):
    for cr in service.service_config.config_rules:
        if cr.WhichOneof('config_rule') != 'custom':
            continue

        raw_value = cr.custom.resource_value
        if not raw_value:
            continue

        try:
            data = json.loads(raw_value)
        except Exception:
            continue

        if not isinstance(data, dict):
            continue

        device_type = data.get('device_type') or data.get('type')
        if device_type != 'teraflowsdn':
            continue

        drivers = data.get('device_drivers')
        if not isinstance(drivers, list):
            continue

        if 'DEVICEDRIVER_IETF_L3VPN' in drivers:
            return data.get('device_name') or data.get('name')

    return None


def group_block(group, action, group_id_override=None, node=None):
    active = "true" if action == 'create' else "false"
    group_id = group_id_override if group_id_override is not None else group.get("group-id", group.get("digital_sub_carriers_group_id", 1))
@@ -131,185 +102,185 @@ def compute_optical_path(service: Service) -> PathCompReply:
        "Accept": "*/*"
    }

    resp = requests.post(url, headers=headers, json=payload, timeout=15)
    resp.raise_for_status()
    resp_json = resp.json()
    # resp = requests.post(url, headers=headers, json=payload, timeout=15)
    # resp.raise_for_status()
    # resp_json = resp.json()

    # MOCK RESPONSE (If the Optical Controller is down)
    # resp_json = {
    #     "tapi-connectivity:connectivity-service": {
    #         "connection": [
    #             {
    #                 "optical-connection-attributes": {
    #                     "central-frequency": 195000000000000,
    #                     "Tx-power": 0,
    #                     "modulation": {
    #                         "modulation-technique": "DP-16QAM",
    #                         "operational-mode": 9,
    #                         "port": "port-1"
    #                     },
    #                     "digital-subcarrier-spacing": 300000000,
    #                     "subcarrier-attributes": {
    #                         "digital-subcarrier-group": [
    #                             {
    #                                 "group-id": 1,
    #                                 "modulation-technique": "DP-QPSK",
    #                                 "central-frequency": 195006250,
    #                                 "operational-mode": 4,
    #                                 "Tx-power": -99,
    #                                 "group-size": 4,
    #                                 "port": "port-1",
    #                                 "subcarrier-id": [1, 2, 3, 4]
    #                             },
    #                             {
    #                                 "group-id": 2,
    #                                 "modulation-technique": "DP-QPSK",
    #                                 "central-frequency": 195018750,
    #                                 "operational-mode": 4,
    #                                 "Tx-power": -99,
    #                                 "group-size": 4,
    #                                 "port": "port-3",
    #                                 "subcarrier-id": [5, 6, 7, 8]
    #                             },
    #                             {
    #                                 "group-id": 3,
    #                                 "modulation-technique": "DP-QPSK",
    #                                 "central-frequency": 195031250,
    #                                 "operational-mode": 4,
    #                                 "Tx-power": -99,
    #                                 "group-size": 4,
    #                                 "port": "port-5",
    #                                 "subcarrier-id": [9, 10, 11, 12]
    #                             }
    #                         ]
    #                     }
    #                 },
    #                 "end-point": [
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T1.1",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T2.1"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195000000000000,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     },
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T2.1",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T1.1"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195006250,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     },
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T1.2",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T2.1"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195018750,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     },
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T2.1",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T1.2"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195018750,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     },
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T1.3",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T2.1"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195031250,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     },
    #                     {
    #                         "direction": "BIDIRECTIONAL",
    #                         "layer-protocol-name": "PHOTONIC_MEDIA",
    #                         "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
    #                         "local-id": "T2.1",
    #                         "service-interface-point": {
    #                             "service-interface-point-uuid": "T1.3"
    #                         },
    #                         "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
    #                             "mc-config": {
    #                                 "spectrum": {
    #                                     "center-frequency": 195031250,
    #                                     "frequency-constraint": {
    #                                         "adjustment-granularity": "G_6_25GHZ",
    #                                         "grid-type": "FLEX"
    #                                     }
    #                                 }
    #                             }
    #                         }
    #                     }
    #                 ]
    #             }
    #         ]
    #     }
    # }
    resp_json = {
        "tapi-connectivity:connectivity-service": {
            "connection": [
                {
                    "optical-connection-attributes": {
                        "central-frequency": 195000000000000,
                        "Tx-power": 0,
                        "modulation": {
                            "modulation-technique": "DP-16QAM",
                            "operational-mode": 9,
                            "port": "port-1"
                        },
                        "digital-subcarrier-spacing": 300000000,
                        "subcarrier-attributes": {
                            "digital-subcarrier-group": [
                                {
                                    "group-id": 1,
                                    "modulation-technique": "DP-QPSK",
                                    "central-frequency": 195006250,
                                    "operational-mode": 4,
                                    "Tx-power": -99,
                                    "group-size": 4,
                                    "port": "port-1",
                                    "subcarrier-id": [1, 2, 3, 4]
                                },
                                {
                                    "group-id": 2,
                                    "modulation-technique": "DP-QPSK",
                                    "central-frequency": 195018750,
                                    "operational-mode": 4,
                                    "Tx-power": -99,
                                    "group-size": 4,
                                    "port": "port-3",
                                    "subcarrier-id": [5, 6, 7, 8]
                                },
                                {
                                    "group-id": 3,
                                    "modulation-technique": "DP-QPSK",
                                    "central-frequency": 195031250,
                                    "operational-mode": 4,
                                    "Tx-power": -99,
                                    "group-size": 4,
                                    "port": "port-5",
                                    "subcarrier-id": [9, 10, 11, 12]
                                }
                            ]
                        }
                    },
                    "end-point": [
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T1.1",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T2.1"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195000000000000,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T2.1",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T1.1"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195006250,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T1.2",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T2.1"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195018750,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T2.1",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T1.2"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195018750,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T1.3",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T2.1"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195031250,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "direction": "BIDIRECTIONAL",
                            "layer-protocol-name": "PHOTONIC_MEDIA",
                            "layer-protocol-qualifier": "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_MC",
                            "local-id": "T2.1",
                            "service-interface-point": {
                                "service-interface-point-uuid": "T1.3"
                            },
                            "tapi-photonic-media:media-channel-connectivity-service-end-point-spec": {
                                "mc-config": {
                                    "spectrum": {
                                        "center-frequency": 195031250,
                                        "frequency-constraint": {
                                            "adjustment-granularity": "G_6_25GHZ",
                                            "grid-type": "FLEX"
                                        }
                                    }
                                }
                            }
                        }
                    ]
                }
            ]
        }
    }

    # Generate Rules
    src_name = source  # T2.1
@@ -333,15 +304,11 @@ def compute_optical_path(service: Service) -> PathCompReply:
    network_slice_uuid_str = f"{src_name}_to_{dest_str}"
    tunnel_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, network_slice_uuid_str))
    
    device_name = extract_teraflowsdn_device_name(service)
    provisionamiento = {
        "network-slice-uuid": network_slice_uuid_str,
        "viability": True,
        "actions": []
    }
    if device_name:
        provisionamiento["device_name"] = device_name

    try:
        attributes = resp_json.get("tapi-connectivity:connectivity-service", {}).get("connection", [{}])[0].get("optical-connection-attributes", {})
        groups = attributes.get("subcarrier-attributes", {}).get("digital-subcarrier-group", [])