Commit 223a2df8 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'feat/316-p2mp' into 'develop'

(CNIT) Basic point-to-multipoint optical connections

Closes #316

See merge request !506
parents ddece538 22dad1b6
Loading
Loading
Loading
Loading
+39 −17
Changes for src/context/service/database/OpticalConfig.py: 39 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,27 @@ from context.service.database.uuids.OpticalConfig import (
from .Events import notify_event_opticalconfig
from .OpticalBand import set_optical_band


def _as_int(value, default : int = 0) -> int:
    """int(value), tolerating a key that is present but carries None.

    A ROADM's optical configuration legitimately leaves numeric fields unset:
    a media channel has no single frequency, only a band. The key is still
    present in the configuration, so testing for its presence is not enough
    and int(None) raises. Non-numeric or absent values fall back to default.
    """
    if value is None:
        return default
    try:
        return int(value)
    except (TypeError, ValueError):
        pass
    try:
        return int(float(value))
    except (TypeError, ValueError):
        return default


LOGGER = logging.getLogger(__name__)

def get_opticalconfig(db_engine : Engine):
@@ -114,8 +135,8 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "transponder_uuid"   : transponder_get_uuid(device_id),
                            "channel_uuid"       : channel_get_uuid(channel_params['name']['index'],device_uuid),
                            "channel_name"       : channel_params['name']['index'],
                            "frequency"          : int(channel_params["frequency"]) if "frequency" in channel_params  else 0,
                            "operational_mode"   : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0,
                            "frequency"          : _as_int(channel_params.get("frequency")),
                            "operational_mode"   : _as_int(channel_params.get("operational-mode")),
                            "target_output_power": channel_params["target-output-power"] if "target-output-power" in channel_params else '',
                            "status"             : channel_params["status"] if "status" in channel_params else ""
                        })
@@ -140,8 +161,8 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "roadm_uuid"          : roadm_get_uuid(device_id),
                            "channel_uuid"        : channel_get_uuid(f'media_channel_{channel_index}',device_uuid),
                            "band_name"           : channel_params['band_name'],
                            "lower_frequency"     : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params  else 0,
                            "upper_frequency"     : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params  else 0,
                            "lower_frequency"     : _as_int(channel_params.get("lower_frequency")),
                            "upper_frequency"     : _as_int(channel_params.get("upper_frequency")),
                            "dest_port"           : channel_params["dest_port"] if "dest_port" in channel_params else '',
                            "src_port"            : channel_params["src_port"] if "src_port" in channel_params else '',
                            "status"              : channel_params["status"] if "status" in channel_params else "",
@@ -159,8 +180,8 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "roadm_uuid"         : roadm_get_uuid(device_id),
                            "channel_uuid"       : channel_get_uuid(f'optical_bands_{channel_num}',device_uuid),
                            "band_name"          : channel_params['band_name'],
                            "lower_frequency"    : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params  else 0,
                            "upper_frequency"    : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params  else 0,
                            "lower_frequency"    : _as_int(channel_params.get("lower_frequency")),
                            "upper_frequency"    : _as_int(channel_params.get("upper_frequency")),
                            "dest_port"          : channel_params["dest_port"] if "dest_port" in channel_params else '',
                            "src_port"           : channel_params["src_port"] if "src_port" in channel_params else '',
                            "status"             : channel_params["status"] if "status" in channel_params else "",
@@ -186,7 +207,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "port"                : interface["port"],
                            "interface_list"      : interface["interface_list"],
                            "frequency"           : interface["frequency"],
                            "width"               :  int(float(interface["width"])) if 'width' in interface else 0,
                            "width"               :  _as_int(interface.get("width")),
                            "roadm_uuid"          : roadm_get_uuid(device_id),
                        })
                if 'circuits' in config:
@@ -206,7 +227,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "port"                : ports_g,
                            "interface_list"      : interface["interface_list"] if 'interface_list' in interface else "Null",
                            "frequency"           : interface["frequency"] if 'frequency' in interface else 0.0,
                            "width"               :  int(float(interface["width"])) if 'width' in interface else 0,
                            "width"               :  _as_int(interface.get("width")),
                            "roadm_uuid"          : roadm_get_uuid(device_id),
                        })
                roadms.append({
@@ -419,8 +440,8 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            "transponder_uuid"    : transponder_get_uuid(device_id),
                            "channel_uuid"        : channel_get_uuid(channel_params['name']['index'],device_uuid),
                            "channel_name"        : channel_params['name']['index'],
                            "frequency"           : int(channel_params["frequency"]) if "frequency" in channel_params  else 0,
                            "operational_mode"    : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0,
                            "frequency"           : _as_int(channel_params.get("frequency")),
                            "operational_mode"    : _as_int(channel_params.get("operational-mode")),
                            "target_output_power" : channel_params["target-output-power"] if "target-output-power" in channel_params else '',
                            "status"              : channel_params["status"] if "status" in channel_params else ""
                        })
@@ -442,8 +463,8 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                        "transponder_uuid"    : transponder_get_uuid(device_id),
                        "channel_uuid"        : channel_get_uuid(channel_name,device_uuid),
                        "channel_name"        : channel_name,
                        "frequency"           : int(target_config["frequency"]) if "frequency" in target_config  else 0,
                        "operational_mode"    : int(target_config["operational-mode"]) if "operational-mode" in target_config else 0,
                        "frequency"           : _as_int(target_config.get("frequency")),
                        "operational_mode"    : _as_int(target_config.get("operational-mode")),
                        "target_output_power" : target_config["target-output-power"] if "target-output-power" in target_config else '',
                        "status"              : target_config["status"] if "status" in target_config else ""
                    })
@@ -480,8 +501,8 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                                "roadm_uuid"          : roadm_get_uuid(device_id),
                                "channel_uuid"        : channel_get_uuid(f'media_channel_{channel_index}',device_uuid),
                                "band_name"           : config['new_config']['band_type'],
                                "lower_frequency"     : int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)) if "frequency" in config['new_config']  else 0,
                                "upper_frequency"     : int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) if "frequency" in config['new_config']  else 0,
                                "lower_frequency"     : _as_int(_as_int(config['new_config'].get('frequency')) - _as_int(config['new_config'].get('band')) / 2),
                                "upper_frequency"     : _as_int(_as_int(config['new_config'].get('frequency')) + _as_int(config['new_config'].get('band')) / 2),
                                "dest_port"           : dest_port,
                                "src_port"            : src_port,
                                "status"              : config['new_config']["status"] if "status" in config['new_config'] else "",
@@ -508,8 +529,8 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                                "roadm_uuid"      : roadm_get_uuid(device_id),
                                "channel_uuid"    : channel_get_uuid(f'optical_bands_{channel_index}',device_uuid),
                                "band_name"       : config['new_config']['band_type'],
                                "lower_frequency" : int(config['new_config']["low-freq"]) if "low-freq" in config['new_config']  else 0,
                                "upper_frequency" : int(config['new_config']["up-freq"]) if "up-freq" in config['new_config']  else 0,
                                "lower_frequency" : _as_int(config['new_config'].get("low-freq")),
                                "upper_frequency" : _as_int(config['new_config'].get("up-freq")),
                                "dest_port"       : dest_port,
                                "src_port"        : src_port,
                                "status"          : config['new_config']["status"] if "status" in config['new_config'] else "",
@@ -664,7 +685,8 @@ def delete_opticalchannel(db_engine : Engine, messagebroker : MessageBroker, req
        if 'flow' in config:
            if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_:
                for flow in config['flow']:
                    src,dest = flow
                    # point-to-multipoint flows also carry their slots: (src, dest, slots[, op_mode])
                    src, dest = flow[0], flow[1]
                    channel_index= src if src is not None and  src!='0' else dest
                    channel_name= f"channel-{channel_index}"
                    channels.append({
+16 −0
Changes for src/opticalcontroller/OpticalController.py: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ from context.client.ContextClient import ContextClient
from opticalcontroller.tools import *
from opticalcontroller.variables import *
from opticalcontroller.RSA import RSA
from opticalcontroller import p2mp_state
from opticalcontroller.service.ConnectivityCandidates import (
    OpticalCandidateError, compute_optical_connectivity_candidates
)
@@ -471,6 +472,12 @@ class GetTopology(Resource):
                DeviceTypeEnum.OPTICAL_TRANSPONDER.value,
                DeviceTypeEnum.EMULATED_OPTICAL_TRANSPONDER.value,
                DeviceTypeEnum.NETWORK.value,
                # Pluggable transponders are registered as packet-router so the
                # telemetry collector filter can find them; without these two the
                # P2MP graph has no vertex for them and every link touching one
                # is dropped below.
                DeviceTypeEnum.PACKET_ROUTER.value,
                DeviceTypeEnum.EMULATED_PACKET_ROUTER.value,
            }
            added_device_uuids = set()
            for device in nodes:
@@ -488,6 +495,8 @@ class GetTopology(Resource):
                    #"ip":f"10.30.2.{207+i}",
                    #"port":"50001",
                    "type": dev_type,
                    # RSA keys off "type", RSA_P2MP off "device_type".
                    "device_type": "optical-roadm" if dev_type == "OC-ROADM" else "optical-transponder",
                    "driver": "OpticalOC"
                }
                node_dict[device.name] = dev_dic
@@ -519,6 +528,7 @@ class GetTopology(Resource):
                    
                links_dict["optical_links"].append(link_dict_type)
    
            p2mp_state.publish(node_dict, links_dict)
            new_rsa = RSA(node_dict, links_dict)
            slot_counts = new_rsa.init_link_slots2()
            rsa = new_rsa
@@ -567,5 +577,11 @@ class ComputeOpticalConnectivityCandidates(Resource):
        finally:
            ctx_client.close()

# Point-to-multipoint resources. Imported last so that the module, which
# reads the topology snapshot published above, binds onto a fully built
# namespace.
from opticalcontroller import p2mp_routes          # noqa: E402
p2mp_routes.register(api, optical)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=10060, debug=True)
+2005 −0

File added.

Preview size limit exceeded, changes collapsed.

+205 −0
Changes for src/opticalcontroller/heuristicMST.py: 205 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright 2022-2026 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.

# TODO: migrate to NetworkX:
# https://networkx.org/documentation/stable/index.html
# https://networkx.org/documentation/stable/reference/algorithms/shortest_paths.html

import sys
import heapq
from collections import defaultdict

class Graph:
    """Undirected adjacency-list graph used for P2MP/point-to-point path
    computation (see spf_find_path / heuristic_mst below).

    Storage: self.graph is a dict keyed by node name -> list of
    (neighbor, src_port, dst_port, weight) tuples. add_edge() appends the
    edge in BOTH directions (src->dst and dst->src, with ports swapped) so
    the graph can be walked either way -- there is no separate "reverse"
    structure.

    NOTE: this class used to define add_vertex/add_edge/printGraph twice.
    Because a class body executes top-to-bottom and each `def` simply
    rebinds the method name, the second definition of each silently
    replaced the first -- the first add_vertex/add_edge (which referenced
    self.vertices/self.edges, attributes that are never initialized
    anywhere in __init__) were dead code, unreachable from the moment the
    class was defined. They have been removed; only the versions that were
    actually ever called (the ones operating on self.graph) remain.
    """
    def __init__(self):
        self.graph = defaultdict(list)
        self.visited = {}  # Track visited nodes

    def add_vertex(self, node):
        if node not in self.graph:
            self.graph[node] = []

    def add_edge(self, src, dst, src_port, dst_port, weight):
        # Store port information along with the edges
        self.graph[src].append((dst, src_port, dst_port, weight))
        self.graph[dst].append((src, dst_port, src_port, weight))  # Assuming undirected graph

    def printGraph(self):
        print("Vertices:")
        for vertex, edges in self.graph.items():
            print(f"{vertex}: {edges}")
        print("Edges:")
        for vertex, edges in self.graph.items():
            for edge in edges:
                print(f"{vertex} -> {edge[0]} (src_port: {edge[1]}, dst_port: {edge[2]}, weight: {edge[3]})")

    def get_edge_weight(self, src, dst):
        # Return the weight of the edge between src and dst nodes
        for neighbor, _, _, weight in self.graph.get(src, []):
            if neighbor == dst:
                return weight

    def reset_graph(self):
        # Reset the graph state (e.g., clear visited nodes)
        self.visited.clear()  # Clear the visited nodes
        for node in self.graph:
            # Reset any other properties as necessary (e.g., distances, parents)
            # Example: self.graph[node].clear() if needed
            pass

    def get_neighbors(self, node):
        return self.graph.get(node, [])

def spf_find_path(graph, start, end, visited_edges=None):
    """Dijkstra shortest path from start to end over `graph` (a Graph
    instance). Returns a list of (from_node, to_node, src_port, dst_port)
    hops, or None if end is unreachable. visited_edges, when given, is a
    set of (from_node, to_node) pairs -- an edge NOT in that set is skipped
    (used to keep a path from immediately walking back over an edge
    reserved by a different direction of the same computation)."""
    priority_queue = [(0, start, [])]  # (cost, node, path)
    visited_nodes = set()

    while priority_queue:
        cost, current_node, path = heapq.heappop(priority_queue)

        if current_node == end:
            return path  # Return the shortest path if destination is reached

        if current_node not in visited_nodes:
            visited_nodes.add(current_node)
            for neighbor, src_port, dst_port, weight in graph.get_neighbors(current_node):

                # Avoid reusing edges in reverse if visited_edges is provided
                if visited_edges and (current_node, neighbor) not in visited_edges:
                    continue

                if neighbor not in visited_nodes:
                    new_path = path + [(current_node, neighbor, src_port, dst_port)]
                    heapq.heappush(priority_queue, (cost + weight, neighbor, new_path))
    return None  # No path found


def heuristic_mst(graph, start):
    """Build a minimum-spanning-tree-like Graph rooted at `start` via a
    Prim's-algorithm-style expansion (cheapest-edge-first from the visited
    set). Used by compute_path_pmp (RSA_P2MP.py) as a cheap way to get a
    single tree that spf_find_path can then extract a src->dst route from,
    rather than re-running Dijkstra from scratch for every destination."""
    mst = Graph()
    visited = set()
    min_heap = [(0, start, None, None)]  # (cost, node, parent, port)

    while min_heap:
        cost, node, parent, parent_port = heapq.heappop(min_heap)

        if node not in visited:
            visited.add(node)
            if parent is not None:
                mst.add_edge(parent, node, parent_port, cost, cost)  # Store port info in MST

            for neighbor, src_port, dst_port, weight in graph.get_neighbors(node):
                if neighbor not in visited:
                    heapq.heappush(min_heap, (weight, neighbor, node, src_port))

    return mst

def find_paths_from_sources_to_destination(graph, sources, destination):
    # Find paths from multiple sources to a single destination
    paths = {}
    for source in sources:
        path = spf_find_path(graph, source, destination)
        if path:
            paths[source] = path
    return paths

def find_paths_from_destination_to_sources(graph, destination, sources):
    # Find paths from a single destination to multiple sources
    paths = {}
    for source in sources:
        path = spf_find_path(graph, destination, source)
        if path:
            paths[source] = path
    return paths


def find_paths_from_source_to_destinations(graph, source, destinations):
    # Find paths from a single source to multiple destinations
    paths = {}
    for destination in destinations:
        path = spf_find_path(graph, source, destination)
        if path:
            paths[destination] = path
    return paths

def find_paths_from_destinations_to_source(graph, destinations, source):
    # Find paths from multiple destinations to a single source
    paths = {}
    for destination in destinations:
        path = spf_find_path(graph, destination, source)
        if path:
            paths[destination] = path
    return paths

def extract_links_with_ports_from_path(path, links_dict):
    links_with_ports = []

    for i in range(len(path)):
        src = path[i][0]  # Extract source node
        dst = path[i][1]  # Extract destination node

        # Search for the correct link in the topology file
        for link in links_dict["optical_links"]:
            if link["name"] == f"{src}-{dst}":
                src_port = link["optical_details"]["src_port"]
                dst_port = link["optical_details"]["dst_port"]
                links_with_ports.append(f"{src}-{dst} (Ports: {src_port} -> {dst_port})")
                break  # Stop searching once found

    return links_with_ports

def initGraph2(links_dict, nodes_dict):
    # Initializes a graph with vertices and edges from topology data
    g = Graph()

    # Add vertices (nodes)
    for node in nodes_dict:
        g.add_vertex(node)

    # Add edges (links)
    for l in links_dict["optical_links"]:
        [s, d] = l["name"].split('-')  # Extract source and destination node names
        ps = l["optical_details"]["src_port"]  # Source port
        pd = l["optical_details"]["dst_port"]  # Destination port
        weight = 1  # Set a default weight (or use the actual weight if available)
        g.add_edge(s, d, ps, pd, weight)

    return g
+1139 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading