From d194f9801b27e5a8020ebe7280c102f05123cc14 Mon Sep 17 00:00:00 2001 From: Antonio Francisco Date: Mon, 4 Oct 2021 18:29:22 -0300 Subject: [PATCH] Add methods to check if path is working Using SDNTrace in Control Plane to verify if the path an EVC was using previously is still deployed to the switches. --- models.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- settings.py | 3 +++ utils.py | 7 +++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/models.py b/models.py index 85cee249..d2295252 100644 --- a/models.py +++ b/models.py @@ -4,6 +4,7 @@ from uuid import uuid4 import requests +from glom import glom from kytos.core import log from kytos.core.common import EntityStatus, GenericEntity @@ -14,7 +15,7 @@ from napps.kytos.mef_eline import settings from napps.kytos.mef_eline.exceptions import FlowModException from napps.kytos.mef_eline.storehouse import StoreHouse -from napps.kytos.mef_eline.utils import emit_event +from napps.kytos.mef_eline.utils import compare_endpoint_trace, emit_event class Path(list, GenericEntity): @@ -838,6 +839,48 @@ def _prepare_pop_flow(self, in_interface, out_interface, in_vlan, flow_mod["actions"].insert(0, new_action) return flow_mod + @staticmethod + def run_sdntrace(uni): + """Run SDN trace on control plane starting from EVC UNIs.""" + endpoint = f'{settings.SDN_TRACE_CP_URL}/trace' + data_uni = { + 'trace': { + 'switch': { + 'dpid': uni.interface.switch.dpid, + 'in_port': uni.interface.port_number + } + } + } + if uni.user_tag: + data_uni['trace']['eth'] = { + 'dl_type': 0x8100, + 'dl_vlan': uni.user_tag.value + } + return requests.put(endpoint, json=data_uni) + + def check_traces(self): + """Check if current_path is deployed comparing with SDN traces.""" + trace_a = self.run_sdntrace(self.uni_a).json()['result'] + if len(trace_a) != len(self.current_path) + 1: + return False + trace_z = self.run_sdntrace(self.uni_z).json()['result'] + if len(trace_z) != len(self.current_path) + 1: + return False + + for link, trace1, trace2 in zip(self.current_path, + trace_a[1:], + trace_z[:0:-1]): + if compare_endpoint_trace( + link.endpoint_a, + glom(link.metadata, 's_vlan.value'), trace2) is False: + return False + if compare_endpoint_trace( + link.endpoint_b, + glom(link.metadata, 's_vlan.value'), trace1) is False: + return False + + return True + class LinkProtection(EVCDeploy): """Class to handle link protection.""" diff --git a/settings.py b/settings.py index 4178b2f0..ca2d19ad 100644 --- a/settings.py +++ b/settings.py @@ -9,6 +9,9 @@ # Base URL of the Pathfinder endpoint TOPOLOGY_URL = 'http://localhost:8181/api/kytos/topology/v3' +# Base URL of SDN trace CP +SDN_TRACE_CP_URL = 'http://localhost:8181/api/amlight/sdntrace_cp' + # EVC consistency interval DEPLOY_EVCS_INTERVAL = 60 diff --git a/utils.py b/utils.py index ac3a355c..b4f2f3f1 100644 --- a/utils.py +++ b/utils.py @@ -7,3 +7,10 @@ def emit_event(controller, name, **kwargs): event_name = f'kytos/mef_eline.{name}' event = KytosEvent(name=event_name, content=kwargs) controller.buffers.app.put(event) + + +def compare_endpoint_trace(endpoint, vlan, trace): + """Compare and endpoint with a trace step.""" + return endpoint.switch.dpid == trace['dpid']\ + and endpoint.port_number == trace['port']\ + and vlan == trace['vlan']