| Total Complexity | 233 | 
| Total Lines | 1223 | 
| Duplicated Lines | 8.5 % | 
| Coverage | 90.98% | 
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like build.main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Main module of kytos/topology Kytos Network Application.  | 
            ||
| 2 | |||
| 3 | Manage the network topology  | 
            ||
| 4 | """  | 
            ||
| 5 | # pylint: disable=wrong-import-order  | 
            ||
| 6 | |||
| 7 | 1 | import time  | 
            |
| 8 | 1 | from collections import defaultdict  | 
            |
| 9 | 1 | from datetime import timezone  | 
            |
| 10 | 1 | from threading import Lock  | 
            |
| 11 | 1 | from typing import List, Optional  | 
            |
| 12 | |||
| 13 | 1 | from kytos.core import KytosEvent, KytosNApp, log, rest  | 
            |
| 14 | 1 | from kytos.core.common import EntityStatus  | 
            |
| 15 | 1 | from kytos.core.exceptions import KytosLinkCreationError  | 
            |
| 16 | 1 | from kytos.core.helpers import listen_to, now  | 
            |
| 17 | 1 | from kytos.core.interface import Interface  | 
            |
| 18 | 1 | from kytos.core.link import Link  | 
            |
| 19 | 1 | from kytos.core.rest_api import (HTTPException, JSONResponse, Request,  | 
            |
| 20 | content_type_json_or_415, get_json_or_400)  | 
            ||
| 21 | 1 | from kytos.core.switch import Switch  | 
            |
| 22 | 1 | from napps.kytos.topology import settings  | 
            |
| 23 | |||
| 24 | 1 | from .controllers import TopoController  | 
            |
| 25 | 1 | from .exceptions import RestoreError  | 
            |
| 26 | 1 | from .models import Topology  | 
            |
| 27 | |||
| 28 | 1 | DEFAULT_LINK_UP_TIMER = 10  | 
            |
| 29 | |||
| 30 | |||
| 31 | 1 | class Main(KytosNApp): # pylint: disable=too-many-public-methods  | 
            |
| 32 | """Main class of kytos/topology NApp.  | 
            ||
| 33 | |||
| 34 | This class is the entry point for this napp.  | 
            ||
| 35 | """  | 
            ||
| 36 | |||
| 37 | 1 | def setup(self):  | 
            |
| 38 | """Initialize the NApp's links list."""  | 
            ||
| 39 | 1 |         self.links = {} | 
            |
| 40 | 1 |         self.intf_available_tags = {} | 
            |
| 41 | 1 | self.link_up_timer = getattr(settings, 'LINK_UP_TIMER',  | 
            |
| 42 | DEFAULT_LINK_UP_TIMER)  | 
            ||
| 43 | |||
| 44 | 1 | self._links_lock = Lock()  | 
            |
| 45 | 1 | self._links_notify_lock = defaultdict(Lock)  | 
            |
| 46 | # to keep track of potential unorded scheduled interface events  | 
            ||
| 47 | 1 | self._intfs_lock = defaultdict(Lock)  | 
            |
| 48 | 1 |         self._intfs_updated_at = {} | 
            |
| 49 | 1 | self.link_up = set()  | 
            |
| 50 | 1 | self.link_status_lock = Lock()  | 
            |
| 51 | 1 | self.topo_controller = self.get_topo_controller()  | 
            |
| 52 | 1 |         Link.register_status_func(f"{self.napp_id}_link_up_timer", | 
            |
| 53 | self.link_status_hook_link_up_timer)  | 
            ||
| 54 | 1 | self.topo_controller.bootstrap_indexes()  | 
            |
| 55 | 1 | self.load_topology()  | 
            |
| 56 | |||
| 57 | 1 | @staticmethod  | 
            |
| 58 | 1 | def get_topo_controller() -> TopoController:  | 
            |
| 59 | """Get TopoController."""  | 
            ||
| 60 | return TopoController()  | 
            ||
| 61 | |||
| 62 | 1 | def execute(self):  | 
            |
| 63 | """Execute once when the napp is running."""  | 
            ||
| 64 | pass  | 
            ||
| 65 | |||
| 66 | 1 | def shutdown(self):  | 
            |
| 67 | """Do nothing."""  | 
            ||
| 68 |         log.info('NApp kytos/topology shutting down.') | 
            ||
| 69 | |||
| 70 | 1 | def _get_metadata(self, request: Request) -> dict:  | 
            |
| 71 | """Return a JSON with metadata."""  | 
            ||
| 72 | 1 | content_type_json_or_415(request)  | 
            |
| 73 | 1 | metadata = get_json_or_400(request, self.controller.loop)  | 
            |
| 74 | 1 | if not isinstance(metadata, dict):  | 
            |
| 75 | 1 |             raise HTTPException(400, "Invalid metadata value: {metadata}") | 
            |
| 76 | 1 | return metadata  | 
            |
| 77 | |||
| 78 | 1 | def _get_link_or_create(self, endpoint_a, endpoint_b):  | 
            |
| 79 | """Get an existing link or create a new one.  | 
            ||
| 80 | |||
| 81 | Returns:  | 
            ||
| 82 | Tuple(Link, bool): Link and a boolean whether it has been created.  | 
            ||
| 83 | """  | 
            ||
| 84 | 1 | new_link = Link(endpoint_a, endpoint_b)  | 
            |
| 85 | |||
| 86 | 1 | for link in self.links.values():  | 
            |
| 87 | 1 | if new_link == link:  | 
            |
| 88 | 1 | return (link, False)  | 
            |
| 89 | |||
| 90 | 1 | self.links[new_link.id] = new_link  | 
            |
| 91 | 1 | return (new_link, True)  | 
            |
| 92 | |||
| 93 | 1 | def _get_switches_dict(self):  | 
            |
| 94 | """Return a dictionary with the known switches."""  | 
            ||
| 95 | 1 |         switches = {'switches': {}} | 
            |
| 96 | 1 | for idx, switch in enumerate(self.controller.switches.copy().values()):  | 
            |
| 97 | 1 | switch_data = switch.as_dict()  | 
            |
| 98 | 1 | if not all(key in switch_data['metadata']  | 
            |
| 99 |                        for key in ('lat', 'lng')): | 
            ||
| 100 | # Switches are initialized somewhere in the ocean  | 
            ||
| 101 | switch_data['metadata']['lat'] = str(0.0)  | 
            ||
| 102 | switch_data['metadata']['lng'] = str(-30.0+idx*10.0)  | 
            ||
| 103 | 1 | switches['switches'][switch.id] = switch_data  | 
            |
| 104 | 1 | return switches  | 
            |
| 105 | |||
| 106 | 1 | def _get_links_dict(self):  | 
            |
| 107 | """Return a dictionary with the known links."""  | 
            ||
| 108 | 1 |         return {'links': {link.id: link.as_dict() for link in | 
            |
| 109 | self.links.copy().values()}}  | 
            ||
| 110 | |||
| 111 | 1 | def _get_topology_dict(self):  | 
            |
| 112 | """Return a dictionary with the known topology."""  | 
            ||
| 113 | 1 |         return {'topology': {**self._get_switches_dict(), | 
            |
| 114 | **self._get_links_dict()}}  | 
            ||
| 115 | |||
| 116 | 1 | def _get_topology(self):  | 
            |
| 117 | """Return an object representing the topology."""  | 
            ||
| 118 | 1 | return Topology(self.controller.switches.copy(), self.links.copy())  | 
            |
| 119 | |||
| 120 | 1 | def _get_link_from_interface(self, interface):  | 
            |
| 121 | """Return the link of the interface, or None if it does not exist."""  | 
            ||
| 122 | 1 | for link in list(self.links.values()):  | 
            |
| 123 | 1 | if interface in (link.endpoint_a, link.endpoint_b):  | 
            |
| 124 | 1 | return link  | 
            |
| 125 | 1 | return None  | 
            |
| 126 | |||
| 127 | 1 | def _load_link(self, link_att):  | 
            |
| 128 | 1 | endpoint_a = link_att['endpoint_a']['id']  | 
            |
| 129 | 1 | endpoint_b = link_att['endpoint_b']['id']  | 
            |
| 130 | 1 | link_str = link_att['id']  | 
            |
| 131 | 1 |         log.info(f"Loading link: {link_str}") | 
            |
| 132 | 1 | interface_a = self.controller.get_interface_by_id(endpoint_a)  | 
            |
| 133 | 1 | interface_b = self.controller.get_interface_by_id(endpoint_b)  | 
            |
| 134 | |||
| 135 | 1 |         error = f"Fail to load endpoints for link {link_str}. " | 
            |
| 136 | 1 | if not interface_a:  | 
            |
| 137 | 1 |             raise RestoreError(f"{error}, endpoint_a {endpoint_a} not found") | 
            |
| 138 | 1 | if not interface_b:  | 
            |
| 139 |             raise RestoreError(f"{error}, endpoint_b {endpoint_b} not found") | 
            ||
| 140 | |||
| 141 | 1 | with self._links_lock:  | 
            |
| 142 | 1 | link, _ = self._get_link_or_create(interface_a, interface_b)  | 
            |
| 143 | |||
| 144 | 1 | if link_att['enabled']:  | 
            |
| 145 | 1 | link.enable()  | 
            |
| 146 | else:  | 
            ||
| 147 | 1 | link.disable()  | 
            |
| 148 | |||
| 149 | # These ones are just runtime active southbound protocol data  | 
            ||
| 150 | # It won't be stored in the future, only kept in the runtime.  | 
            ||
| 151 | # Also network operators can follow logs to track this state changes  | 
            ||
| 152 | 1 | for key in (  | 
            |
| 153 | "last_status_is_active", "last_status_change", "notified_up_at"  | 
            ||
| 154 | ):  | 
            ||
| 155 | 1 | link_att["metadata"].pop(key, None)  | 
            |
| 156 | |||
| 157 | 1 | link.extend_metadata(link_att["metadata"])  | 
            |
| 158 | 1 | interface_a.update_link(link)  | 
            |
| 159 | 1 | interface_b.update_link(link)  | 
            |
| 160 | 1 | interface_a.nni = True  | 
            |
| 161 | 1 | interface_b.nni = True  | 
            |
| 162 | |||
| 163 | 1 | def _load_switch(self, switch_id, switch_att):  | 
            |
| 164 | 1 |         log.info(f'Loading switch dpid: {switch_id}') | 
            |
| 165 | 1 | switch = self.controller.get_switch_or_create(switch_id)  | 
            |
| 166 | 1 | if switch_att['enabled']:  | 
            |
| 167 | 1 | switch.enable()  | 
            |
| 168 | else:  | 
            ||
| 169 | 1 | switch.disable()  | 
            |
| 170 | 1 |         switch.description['manufacturer'] = switch_att.get('manufacturer', '') | 
            |
| 171 | 1 |         switch.description['hardware'] = switch_att.get('hardware', '') | 
            |
| 172 | 1 |         switch.description['software'] = switch_att.get('software') | 
            |
| 173 | 1 |         switch.description['serial'] = switch_att.get('serial', '') | 
            |
| 174 | 1 |         switch.description['data_path'] = switch_att.get('data_path', '') | 
            |
| 175 | 1 | switch.extend_metadata(switch_att["metadata"])  | 
            |
| 176 | |||
| 177 | 1 |         for iface_id, iface_att in switch_att.get('interfaces', {}).items(): | 
            |
| 178 | 1 |             log.info(f'Loading interface iface_id={iface_id}') | 
            |
| 179 | 1 | interface = switch.update_or_create_interface(  | 
            |
| 180 | port_no=iface_att['port_number'],  | 
            ||
| 181 | name=iface_att['name'],  | 
            ||
| 182 |                             address=iface_att.get('mac', None), | 
            ||
| 183 |                             speed=iface_att.get('speed', None)) | 
            ||
| 184 | 1 | if iface_att['enabled']:  | 
            |
| 185 | 1 | interface.enable()  | 
            |
| 186 | else:  | 
            ||
| 187 | 1 | interface.disable()  | 
            |
| 188 | 1 | interface.lldp = iface_att['lldp']  | 
            |
| 189 | 1 | interface.extend_metadata(iface_att["metadata"])  | 
            |
| 190 | 1 | interface.deactivate()  | 
            |
| 191 | 1 | name = 'kytos/topology.port.created'  | 
            |
| 192 | 1 |             event = KytosEvent(name=name, content={ | 
            |
| 193 | 'switch': switch_id,  | 
            ||
| 194 | 'port': interface.port_number,  | 
            ||
| 195 |                                               'port_description': { | 
            ||
| 196 | 'alias': interface.name,  | 
            ||
| 197 | 'mac': interface.address,  | 
            ||
| 198 | 'state': interface.state  | 
            ||
| 199 | }  | 
            ||
| 200 | })  | 
            ||
| 201 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 202 | |||
| 203 | 1 |         intf_ids = [v["id"] for v in switch_att.get("interfaces", {}).values()] | 
            |
| 204 | 1 | intf_details = self.topo_controller.get_interfaces_details(intf_ids)  | 
            |
| 205 | 1 | with self._links_lock:  | 
            |
| 206 | 1 | self.load_interfaces_available_tags(switch, intf_details)  | 
            |
| 207 | |||
| 208 | # pylint: disable=attribute-defined-outside-init  | 
            ||
| 209 | 1 | def load_topology(self):  | 
            |
| 210 | """Load network topology from DB."""  | 
            ||
| 211 | 1 | topology = self.topo_controller.get_topology()  | 
            |
| 212 | 1 | switches = topology["topology"]["switches"]  | 
            |
| 213 | 1 | links = topology["topology"]["links"]  | 
            |
| 214 | |||
| 215 | 1 |         failed_switches = {} | 
            |
| 216 | 1 |         log.debug(f"_load_network_status switches={switches}") | 
            |
| 217 | 1 | for switch_id, switch_att in switches.items():  | 
            |
| 218 | 1 | try:  | 
            |
| 219 | 1 | self._load_switch(switch_id, switch_att)  | 
            |
| 220 | # pylint: disable=broad-except  | 
            ||
| 221 | 1 | except Exception as err:  | 
            |
| 222 | 1 | failed_switches[switch_id] = err  | 
            |
| 223 | 1 |                 log.error(f'Error loading switch: {err}') | 
            |
| 224 | |||
| 225 | 1 |         failed_links = {} | 
            |
| 226 | 1 |         log.debug(f"_load_network_status links={links}") | 
            |
| 227 | 1 | for link_id, link_att in links.items():  | 
            |
| 228 | 1 | try:  | 
            |
| 229 | 1 | self._load_link(link_att)  | 
            |
| 230 | # pylint: disable=broad-except  | 
            ||
| 231 | 1 | except Exception as err:  | 
            |
| 232 | 1 | failed_links[link_id] = err  | 
            |
| 233 | 1 |                 log.error(f'Error loading link {link_id}: {err}') | 
            |
| 234 | |||
| 235 | 1 | name = 'kytos/topology.topology_loaded'  | 
            |
| 236 | 1 | event = KytosEvent(  | 
            |
| 237 | name=name,  | 
            ||
| 238 |             content={ | 
            ||
| 239 | 'topology': self._get_topology(),  | 
            ||
| 240 | 'failed_switches': failed_switches,  | 
            ||
| 241 | 'failed_links': failed_links  | 
            ||
| 242 | })  | 
            ||
| 243 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 244 | |||
| 245 | 1 |     @rest('v3/') | 
            |
| 246 | 1 | def get_topology(self, _request: Request) -> JSONResponse:  | 
            |
| 247 | """Return the latest known topology.  | 
            ||
| 248 | |||
| 249 | This topology is updated when there are network events.  | 
            ||
| 250 | """  | 
            ||
| 251 | 1 | return JSONResponse(self._get_topology_dict())  | 
            |
| 252 | |||
| 253 | # Switch related methods  | 
            ||
| 254 | 1 |     @rest('v3/switches') | 
            |
| 255 | 1 | def get_switches(self, _request: Request) -> JSONResponse:  | 
            |
| 256 | """Return a json with all the switches in the topology."""  | 
            ||
| 257 | return JSONResponse(self._get_switches_dict())  | 
            ||
| 258 | |||
| 259 | 1 |     @rest('v3/switches/{dpid}/enable', methods=['POST']) | 
            |
| 260 | 1 | def enable_switch(self, request: Request) -> JSONResponse:  | 
            |
| 261 | """Administratively enable a switch in the topology."""  | 
            ||
| 262 | 1 | dpid = request.path_params["dpid"]  | 
            |
| 263 | 1 | try:  | 
            |
| 264 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 265 | 1 | self.topo_controller.enable_switch(dpid)  | 
            |
| 266 | 1 | switch.enable()  | 
            |
| 267 | 1 | except KeyError:  | 
            |
| 268 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 269 | |||
| 270 | 1 | self.notify_switch_enabled(dpid)  | 
            |
| 271 | 1 | self.notify_topology_update()  | 
            |
| 272 | 1 | self.notify_switch_links_status(switch, "link enabled")  | 
            |
| 273 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 274 | |||
| 275 | 1 |     @rest('v3/switches/{dpid}/disable', methods=['POST']) | 
            |
| 276 | 1 | def disable_switch(self, request: Request) -> JSONResponse:  | 
            |
| 277 | """Administratively disable a switch in the topology."""  | 
            ||
| 278 | 1 | dpid = request.path_params["dpid"]  | 
            |
| 279 | 1 | try:  | 
            |
| 280 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 281 | 1 | self.topo_controller.disable_switch(dpid)  | 
            |
| 282 | 1 | switch.disable()  | 
            |
| 283 | 1 | except KeyError:  | 
            |
| 284 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 285 | |||
| 286 | 1 | self.notify_switch_disabled(dpid)  | 
            |
| 287 | 1 | self.notify_topology_update()  | 
            |
| 288 | 1 | self.notify_switch_links_status(switch, "link disabled")  | 
            |
| 289 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 290 | |||
| 291 | 1 |     @rest('v3/switches/{dpid}/metadata') | 
            |
| 292 | 1 | def get_switch_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 293 | """Get metadata from a switch."""  | 
            ||
| 294 | 1 | dpid = request.path_params["dpid"]  | 
            |
| 295 | 1 | try:  | 
            |
| 296 | 1 | metadata = self.controller.switches[dpid].metadata  | 
            |
| 297 | 1 |             return JSONResponse({"metadata": metadata}) | 
            |
| 298 | 1 | except KeyError:  | 
            |
| 299 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 300 | |||
| 301 | 1 |     @rest('v3/switches/{dpid}/metadata', methods=['POST']) | 
            |
| 302 | 1 | def add_switch_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 303 | """Add metadata to a switch."""  | 
            ||
| 304 | 1 | dpid = request.path_params["dpid"]  | 
            |
| 305 | 1 | metadata = self._get_metadata(request)  | 
            |
| 306 | 1 | try:  | 
            |
| 307 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 308 | 1 | except KeyError:  | 
            |
| 309 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 310 | |||
| 311 | 1 | self.topo_controller.add_switch_metadata(dpid, metadata)  | 
            |
| 312 | 1 | switch.extend_metadata(metadata)  | 
            |
| 313 | 1 | self.notify_metadata_changes(switch, 'added')  | 
            |
| 314 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 315 | |||
| 316 | 1 |     @rest('v3/switches/{dpid}/metadata/{key}', methods=['DELETE']) | 
            |
| 317 | 1 | def delete_switch_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 318 | """Delete metadata from a switch."""  | 
            ||
| 319 | 1 | dpid = request.path_params["dpid"]  | 
            |
| 320 | 1 | key = request.path_params["key"]  | 
            |
| 321 | 1 | try:  | 
            |
| 322 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 323 | 1 | except KeyError:  | 
            |
| 324 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 325 | |||
| 326 | 1 | try:  | 
            |
| 327 | 1 | _ = switch.metadata[key]  | 
            |
| 328 | except KeyError:  | 
            ||
| 329 | raise HTTPException(404, "Metadata not found")  | 
            ||
| 330 | |||
| 331 | 1 | self.topo_controller.delete_switch_metadata_key(dpid, key)  | 
            |
| 332 | 1 | switch.remove_metadata(key)  | 
            |
| 333 | 1 | self.notify_metadata_changes(switch, 'removed')  | 
            |
| 334 | 1 |         return JSONResponse("Operation successful") | 
            |
| 335 | |||
| 336 | # Interface related methods  | 
            ||
| 337 | 1 |     @rest('v3/interfaces') | 
            |
| 338 | 1 | def get_interfaces(self, _request: Request) -> JSONResponse:  | 
            |
| 339 | """Return a json with all the interfaces in the topology."""  | 
            ||
| 340 | 1 |         interfaces = {} | 
            |
| 341 | 1 | switches = self._get_switches_dict()  | 
            |
| 342 | 1 | for switch in switches['switches'].values():  | 
            |
| 343 | 1 | for interface_id, interface in switch['interfaces'].items():  | 
            |
| 344 | 1 | interfaces[interface_id] = interface  | 
            |
| 345 | |||
| 346 | 1 |         return JSONResponse({'interfaces': interfaces}) | 
            |
| 347 | |||
| 348 | 1 | View Code Duplication |     @rest('v3/interfaces/switch/{dpid}/enable', methods=['POST']) | 
            
| 
                                                                                                    
                        
                         | 
                |||
| 349 | 1 |     @rest('v3/interfaces/{interface_enable_id}/enable', methods=['POST']) | 
            |
| 350 | 1 | def enable_interface(self, request: Request) -> JSONResponse:  | 
            |
| 351 | """Administratively enable interfaces in the topology."""  | 
            ||
| 352 | 1 |         interface_enable_id = request.path_params.get("interface_enable_id") | 
            |
| 353 | 1 |         dpid = request.path_params.get("dpid") | 
            |
| 354 | 1 | if dpid is None:  | 
            |
| 355 | 1 |             dpid = ":".join(interface_enable_id.split(":")[:-1]) | 
            |
| 356 | 1 | try:  | 
            |
| 357 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 358 | except KeyError:  | 
            ||
| 359 | raise HTTPException(404, detail="Switch not found")  | 
            ||
| 360 | |||
| 361 | 1 | if interface_enable_id:  | 
            |
| 362 | 1 |             interface_number = int(interface_enable_id.split(":")[-1]) | 
            |
| 363 | |||
| 364 | 1 | try:  | 
            |
| 365 | 1 | interface = switch.interfaces[interface_number]  | 
            |
| 366 | 1 | self.topo_controller.enable_interface(interface.id)  | 
            |
| 367 | 1 | interface.enable()  | 
            |
| 368 | 1 | self.notify_interface_link_status(interface, "link enabled")  | 
            |
| 369 | 1 | except KeyError:  | 
            |
| 370 | 1 |                 msg = f"Switch {dpid} interface {interface_number} not found" | 
            |
| 371 | 1 | raise HTTPException(404, detail=msg)  | 
            |
| 372 | else:  | 
            ||
| 373 | 1 | for interface in switch.interfaces.copy().values():  | 
            |
| 374 | 1 | interface.enable()  | 
            |
| 375 | 1 | self.notify_interface_link_status(interface, "link enabled")  | 
            |
| 376 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict())  | 
            |
| 377 | 1 | self.notify_topology_update()  | 
            |
| 378 | 1 |         return JSONResponse("Operation successful") | 
            |
| 379 | |||
| 380 | 1 | View Code Duplication |     @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST']) | 
            
| 381 | 1 |     @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST']) | 
            |
| 382 | 1 | def disable_interface(self, request: Request) -> JSONResponse:  | 
            |
| 383 | """Administratively disable interfaces in the topology."""  | 
            ||
| 384 | 1 |         interface_disable_id = request.path_params.get("interface_disable_id") | 
            |
| 385 | 1 |         dpid = request.path_params.get("dpid") | 
            |
| 386 | 1 | if dpid is None:  | 
            |
| 387 | 1 |             dpid = ":".join(interface_disable_id.split(":")[:-1]) | 
            |
| 388 | 1 | try:  | 
            |
| 389 | 1 | switch = self.controller.switches[dpid]  | 
            |
| 390 | 1 | except KeyError:  | 
            |
| 391 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 392 | |||
| 393 | 1 | if interface_disable_id:  | 
            |
| 394 | 1 |             interface_number = int(interface_disable_id.split(":")[-1]) | 
            |
| 395 | |||
| 396 | 1 | try:  | 
            |
| 397 | 1 | interface = switch.interfaces[interface_number]  | 
            |
| 398 | 1 | self.topo_controller.disable_interface(interface.id)  | 
            |
| 399 | 1 | interface.disable()  | 
            |
| 400 | 1 | self.notify_interface_link_status(interface, "link disabled")  | 
            |
| 401 | 1 | except KeyError:  | 
            |
| 402 | 1 |                 msg = f"Switch {dpid} interface {interface_number} not found" | 
            |
| 403 | 1 | raise HTTPException(404, detail=msg)  | 
            |
| 404 | else:  | 
            ||
| 405 | 1 | for interface in switch.interfaces.copy().values():  | 
            |
| 406 | 1 | interface.disable()  | 
            |
| 407 | 1 | self.notify_interface_link_status(interface, "link disabled")  | 
            |
| 408 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict())  | 
            |
| 409 | 1 | self.notify_topology_update()  | 
            |
| 410 | 1 |         return JSONResponse("Operation successful") | 
            |
| 411 | |||
| 412 | 1 |     @rest('v3/interfaces/{interface_id}/metadata') | 
            |
| 413 | 1 | def get_interface_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 414 | """Get metadata from an interface."""  | 
            ||
| 415 | 1 | interface_id = request.path_params["interface_id"]  | 
            |
| 416 | 1 |         switch_id = ":".join(interface_id.split(":")[:-1]) | 
            |
| 417 | 1 |         interface_number = int(interface_id.split(":")[-1]) | 
            |
| 418 | 1 | try:  | 
            |
| 419 | 1 | switch = self.controller.switches[switch_id]  | 
            |
| 420 | 1 | except KeyError:  | 
            |
| 421 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 422 | |||
| 423 | 1 | try:  | 
            |
| 424 | 1 | interface = switch.interfaces[interface_number]  | 
            |
| 425 | 1 | except KeyError:  | 
            |
| 426 | 1 | raise HTTPException(404, detail="Interface not found")  | 
            |
| 427 | |||
| 428 | 1 |         return JSONResponse({"metadata": interface.metadata}) | 
            |
| 429 | |||
| 430 | 1 |     @rest('v3/interfaces/{interface_id}/metadata', methods=['POST']) | 
            |
| 431 | 1 | def add_interface_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 432 | """Add metadata to an interface."""  | 
            ||
| 433 | 1 | interface_id = request.path_params["interface_id"]  | 
            |
| 434 | 1 | metadata = self._get_metadata(request)  | 
            |
| 435 | 1 |         switch_id = ":".join(interface_id.split(":")[:-1]) | 
            |
| 436 | 1 |         interface_number = int(interface_id.split(":")[-1]) | 
            |
| 437 | 1 | try:  | 
            |
| 438 | 1 | switch = self.controller.switches[switch_id]  | 
            |
| 439 | 1 | except KeyError:  | 
            |
| 440 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 441 | |||
| 442 | 1 | try:  | 
            |
| 443 | 1 | interface = switch.interfaces[interface_number]  | 
            |
| 444 | 1 | self.topo_controller.add_interface_metadata(interface.id, metadata)  | 
            |
| 445 | 1 | except KeyError:  | 
            |
| 446 | 1 | raise HTTPException(404, detail="Interface not found")  | 
            |
| 447 | |||
| 448 | 1 | interface.extend_metadata(metadata)  | 
            |
| 449 | 1 | self.notify_metadata_changes(interface, 'added')  | 
            |
| 450 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 451 | |||
| 452 | 1 |     @rest('v3/interfaces/{interface_id}/metadata/{key}', methods=['DELETE']) | 
            |
| 453 | 1 | def delete_interface_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 454 | """Delete metadata from an interface."""  | 
            ||
| 455 | 1 | interface_id = request.path_params["interface_id"]  | 
            |
| 456 | 1 | key = request.path_params["key"]  | 
            |
| 457 | 1 |         switch_id = ":".join(interface_id.split(":")[:-1]) | 
            |
| 458 | 1 | try:  | 
            |
| 459 | 1 |             interface_number = int(interface_id.split(":")[-1]) | 
            |
| 460 | except ValueError:  | 
            ||
| 461 |             detail = f"Invalid interface_id {interface_id}" | 
            ||
| 462 | raise HTTPException(400, detail=detail)  | 
            ||
| 463 | |||
| 464 | 1 | try:  | 
            |
| 465 | 1 | switch = self.controller.switches[switch_id]  | 
            |
| 466 | 1 | except KeyError:  | 
            |
| 467 | 1 | raise HTTPException(404, detail="Switch not found")  | 
            |
| 468 | |||
| 469 | 1 | try:  | 
            |
| 470 | 1 | interface = switch.interfaces[interface_number]  | 
            |
| 471 | 1 | except KeyError:  | 
            |
| 472 | 1 | raise HTTPException(404, detail="Interface not found")  | 
            |
| 473 | |||
| 474 | 1 | try:  | 
            |
| 475 | 1 | _ = interface.metadata[key]  | 
            |
| 476 | 1 | except KeyError:  | 
            |
| 477 | 1 | raise HTTPException(404, detail="Metadata not found")  | 
            |
| 478 | |||
| 479 | 1 | self.topo_controller.delete_interface_metadata_key(interface.id, key)  | 
            |
| 480 | 1 | interface.remove_metadata(key)  | 
            |
| 481 | 1 | self.notify_metadata_changes(interface, 'removed')  | 
            |
| 482 | 1 |         return JSONResponse("Operation successful") | 
            |
| 483 | |||
| 484 | # Link related methods  | 
            ||
| 485 | 1 |     @rest('v3/links') | 
            |
| 486 | 1 | def get_links(self, _request: Request) -> JSONResponse:  | 
            |
| 487 | """Return a json with all the links in the topology.  | 
            ||
| 488 | |||
| 489 | Links are connections between interfaces.  | 
            ||
| 490 | """  | 
            ||
| 491 | return JSONResponse(self._get_links_dict())  | 
            ||
| 492 | |||
| 493 | 1 |     @rest('v3/links/{link_id}/enable', methods=['POST']) | 
            |
| 494 | 1 | def enable_link(self, request: Request) -> JSONResponse:  | 
            |
| 495 | """Administratively enable a link in the topology."""  | 
            ||
| 496 | 1 | link_id = request.path_params["link_id"]  | 
            |
| 497 | 1 | try:  | 
            |
| 498 | 1 | with self._links_lock:  | 
            |
| 499 | 1 | link = self.links[link_id]  | 
            |
| 500 | 1 | self.topo_controller.enable_link(link_id)  | 
            |
| 501 | 1 | link.enable()  | 
            |
| 502 | 1 | except KeyError:  | 
            |
| 503 | 1 | raise HTTPException(404, detail="Link not found")  | 
            |
| 504 | 1 | self.notify_link_status_change(  | 
            |
| 505 | self.links[link_id],  | 
            ||
| 506 | reason='link enabled'  | 
            ||
| 507 | )  | 
            ||
| 508 | 1 | self.notify_topology_update()  | 
            |
| 509 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 510 | |||
| 511 | 1 |     @rest('v3/links/{link_id}/disable', methods=['POST']) | 
            |
| 512 | 1 | def disable_link(self, request: Request) -> JSONResponse:  | 
            |
| 513 | """Administratively disable a link in the topology."""  | 
            ||
| 514 | 1 | link_id = request.path_params["link_id"]  | 
            |
| 515 | 1 | try:  | 
            |
| 516 | 1 | with self._links_lock:  | 
            |
| 517 | 1 | link = self.links[link_id]  | 
            |
| 518 | 1 | self.topo_controller.disable_link(link_id)  | 
            |
| 519 | 1 | link.disable()  | 
            |
| 520 | 1 | except KeyError:  | 
            |
| 521 | 1 | raise HTTPException(404, detail="Link not found")  | 
            |
| 522 | 1 | self.notify_link_status_change(  | 
            |
| 523 | self.links[link_id],  | 
            ||
| 524 | reason='link disabled'  | 
            ||
| 525 | )  | 
            ||
| 526 | 1 | self.notify_topology_update()  | 
            |
| 527 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 528 | |||
| 529 | 1 |     @rest('v3/links/{link_id}/metadata') | 
            |
| 530 | 1 | def get_link_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 531 | """Get metadata from a link."""  | 
            ||
| 532 | 1 | link_id = request.path_params["link_id"]  | 
            |
| 533 | 1 | try:  | 
            |
| 534 | 1 |             return JSONResponse({"metadata": self.links[link_id].metadata}) | 
            |
| 535 | 1 | except KeyError:  | 
            |
| 536 | 1 | raise HTTPException(404, detail="Link not found")  | 
            |
| 537 | |||
| 538 | 1 |     @rest('v3/links/{link_id}/metadata', methods=['POST']) | 
            |
| 539 | 1 | def add_link_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 540 | """Add metadata to a link."""  | 
            ||
| 541 | 1 | link_id = request.path_params["link_id"]  | 
            |
| 542 | 1 | metadata = self._get_metadata(request)  | 
            |
| 543 | 1 | try:  | 
            |
| 544 | 1 | link = self.links[link_id]  | 
            |
| 545 | 1 | except KeyError:  | 
            |
| 546 | 1 | raise HTTPException(404, detail="Link not found")  | 
            |
| 547 | |||
| 548 | 1 | self.topo_controller.add_link_metadata(link_id, metadata)  | 
            |
| 549 | 1 | link.extend_metadata(metadata)  | 
            |
| 550 | 1 | self.notify_metadata_changes(link, 'added')  | 
            |
| 551 | 1 |         return JSONResponse("Operation successful", status_code=201) | 
            |
| 552 | |||
| 553 | 1 |     @rest('v3/links/{link_id}/metadata/{key}', methods=['DELETE']) | 
            |
| 554 | 1 | def delete_link_metadata(self, request: Request) -> JSONResponse:  | 
            |
| 555 | """Delete metadata from a link."""  | 
            ||
| 556 | 1 | link_id = request.path_params["link_id"]  | 
            |
| 557 | 1 | key = request.path_params["key"]  | 
            |
| 558 | 1 | try:  | 
            |
| 559 | 1 | link = self.links[link_id]  | 
            |
| 560 | 1 | except KeyError:  | 
            |
| 561 | 1 | raise HTTPException(404, detail="Link not found")  | 
            |
| 562 | |||
| 563 | 1 | try:  | 
            |
| 564 | 1 | _ = link.metadata[key]  | 
            |
| 565 | 1 | except KeyError:  | 
            |
| 566 | 1 | raise HTTPException(404, detail="Metadata not found")  | 
            |
| 567 | |||
| 568 | 1 | self.topo_controller.delete_link_metadata_key(link.id, key)  | 
            |
| 569 | 1 | link.remove_metadata(key)  | 
            |
| 570 | 1 | self.notify_metadata_changes(link, 'removed')  | 
            |
| 571 | 1 |         return JSONResponse("Operation successful") | 
            |
| 572 | |||
| 573 | 1 | def notify_current_topology(self) -> None:  | 
            |
| 574 | """Notify current topology."""  | 
            ||
| 575 | 1 | name = "kytos/topology.current"  | 
            |
| 576 | 1 |         event = KytosEvent(name=name, content={"topology": | 
            |
| 577 | self._get_topology()})  | 
            ||
| 578 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 579 | |||
| 580 | 1 |     @listen_to("kytos/topology.get") | 
            |
| 581 | 1 | def on_get_topology(self, _event) -> None:  | 
            |
| 582 | """Handle kytos/topology.get."""  | 
            ||
| 583 | self.notify_current_topology()  | 
            ||
| 584 | |||
| 585 | 1 |     @listen_to("kytos/.*.liveness.(up|down)") | 
            |
| 586 | 1 | def on_link_liveness_status(self, event) -> None:  | 
            |
| 587 | """Handle link liveness up|down status event."""  | 
            ||
| 588 | link = Link(event.content["interface_a"], event.content["interface_b"])  | 
            ||
| 589 | try:  | 
            ||
| 590 | link = self.links[link.id]  | 
            ||
| 591 | except KeyError:  | 
            ||
| 592 |             log.error(f"Link id {link.id} not found, {link}") | 
            ||
| 593 | return  | 
            ||
| 594 |         liveness_status = event.name.split(".")[-1] | 
            ||
| 595 | self.handle_link_liveness_status(self.links[link.id], liveness_status)  | 
            ||
| 596 | |||
| 597 | 1 | def handle_link_liveness_status(self, link, liveness_status) -> None:  | 
            |
| 598 | """Handle link liveness."""  | 
            ||
| 599 | 1 |         metadata = {"liveness_status": liveness_status} | 
            |
| 600 | 1 |         log.info(f"Link liveness {liveness_status}: {link}") | 
            |
| 601 | 1 | self.topo_controller.add_link_metadata(link.id, metadata)  | 
            |
| 602 | 1 | link.extend_metadata(metadata)  | 
            |
| 603 | 1 | self.notify_topology_update()  | 
            |
| 604 | 1 | if link.status == EntityStatus.UP and liveness_status == "up":  | 
            |
| 605 | 1 | self.notify_link_status_change(link, reason="liveness_up")  | 
            |
| 606 | 1 | if link.status == EntityStatus.DOWN and liveness_status == "down":  | 
            |
| 607 | 1 | self.notify_link_status_change(link, reason="liveness_down")  | 
            |
| 608 | |||
| 609 | 1 |     @listen_to("kytos/.*.liveness.disabled") | 
            |
| 610 | 1 | def on_link_liveness_disabled(self, event) -> None:  | 
            |
| 611 | """Handle link liveness disabled event."""  | 
            ||
| 612 | interfaces = event.content["interfaces"]  | 
            ||
| 613 | self.handle_link_liveness_disabled(interfaces)  | 
            ||
| 614 | |||
| 615 | 1 | def get_links_from_interfaces(self, interfaces) -> dict:  | 
            |
| 616 | """Get links from interfaces."""  | 
            ||
| 617 | 1 |         links_found = {} | 
            |
| 618 | 1 | with self._links_lock:  | 
            |
| 619 | 1 | for interface in interfaces:  | 
            |
| 620 | 1 | for link in self.links.values():  | 
            |
| 621 | 1 | if any((  | 
            |
| 622 | interface.id == link.endpoint_a.id,  | 
            ||
| 623 | interface.id == link.endpoint_b.id,  | 
            ||
| 624 | )):  | 
            ||
| 625 | 1 | links_found[link.id] = link  | 
            |
| 626 | 1 | return links_found  | 
            |
| 627 | |||
| 628 | 1 | def handle_link_liveness_disabled(self, interfaces) -> None:  | 
            |
| 629 | """Handle link liveness disabled."""  | 
            ||
| 630 | 1 |         log.info(f"Link liveness disabled interfaces: {interfaces}") | 
            |
| 631 | |||
| 632 | 1 | key = "liveness_status"  | 
            |
| 633 | 1 | links = self.get_links_from_interfaces(interfaces)  | 
            |
| 634 | 1 | for link in links.values():  | 
            |
| 635 | 1 | link.remove_metadata(key)  | 
            |
| 636 | 1 | link_ids = list(links.keys())  | 
            |
| 637 | 1 | self.topo_controller.bulk_delete_link_metadata_key(link_ids, key)  | 
            |
| 638 | 1 | self.notify_topology_update()  | 
            |
| 639 | 1 | for link in links.values():  | 
            |
| 640 | 1 | self.notify_link_status_change(link, reason="liveness_disabled")  | 
            |
| 641 | |||
| 642 | 1 |     @listen_to("kytos/.*.link_available_tags") | 
            |
| 643 | 1 | def on_link_available_tags(self, event):  | 
            |
| 644 | """Handle on_link_available_tags."""  | 
            ||
| 645 | with self._links_lock:  | 
            ||
| 646 |             self.handle_on_link_available_tags(event.content.get("link")) | 
            ||
| 647 | |||
| 648 | 1 | def handle_on_link_available_tags(self, link):  | 
            |
| 649 | """Handle on_link_available_tags."""  | 
            ||
| 650 | 1 | if link.id not in self.links:  | 
            |
| 651 | return  | 
            ||
| 652 | 1 | endpoint_a = self.links[link.id].endpoint_a  | 
            |
| 653 | 1 | endpoint_b = self.links[link.id].endpoint_b  | 
            |
| 654 | 1 | values_a = [tag.value for tag in endpoint_a.available_tags]  | 
            |
| 655 | 1 | values_b = [tag.value for tag in endpoint_b.available_tags]  | 
            |
| 656 | 1 | ids_details = [  | 
            |
| 657 |             (endpoint_a.id, {"_id": endpoint_a.id, | 
            ||
| 658 | "available_vlans": values_a}),  | 
            ||
| 659 |             (endpoint_b.id, {"_id": endpoint_b.id, | 
            ||
| 660 | "available_vlans": values_b})  | 
            ||
| 661 | ]  | 
            ||
| 662 | 1 | self.topo_controller.bulk_upsert_interface_details(ids_details)  | 
            |
| 663 | |||
| 664 | 1 |     @listen_to('.*.switch.(new|reconnected)') | 
            |
| 665 | 1 | def on_new_switch(self, event):  | 
            |
| 666 | """Create a new Device on the Topology.  | 
            ||
| 667 | |||
| 668 | Handle the event of a new created switch and update the topology with  | 
            ||
| 669 | this new device. Also notify if the switch is enabled.  | 
            ||
| 670 | """  | 
            ||
| 671 | self.handle_new_switch(event)  | 
            ||
| 672 | |||
| 673 | 1 | def handle_new_switch(self, event):  | 
            |
| 674 | """Create a new Device on the Topology."""  | 
            ||
| 675 | 1 | switch = event.content['switch']  | 
            |
| 676 | 1 | switch.activate()  | 
            |
| 677 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict())  | 
            |
| 678 | 1 |         log.debug('Switch %s added to the Topology.', switch.id) | 
            |
| 679 | 1 | self.notify_topology_update()  | 
            |
| 680 | 1 | if switch.is_enabled():  | 
            |
| 681 | 1 | self.notify_switch_enabled(switch.id)  | 
            |
| 682 | |||
| 683 | 1 |     @listen_to('.*.connection.lost') | 
            |
| 684 | 1 | def on_connection_lost(self, event):  | 
            |
| 685 | """Remove a Device from the topology.  | 
            ||
| 686 | |||
| 687 | Remove the disconnected Device and every link that has one of its  | 
            ||
| 688 | interfaces.  | 
            ||
| 689 | """  | 
            ||
| 690 | self.handle_connection_lost(event)  | 
            ||
| 691 | |||
| 692 | 1 | def handle_connection_lost(self, event):  | 
            |
| 693 | """Remove a Device from the topology."""  | 
            ||
| 694 | 1 | switch = event.content['source'].switch  | 
            |
| 695 | 1 | if switch:  | 
            |
| 696 | 1 | switch.deactivate()  | 
            |
| 697 | 1 |             log.debug('Switch %s removed from the Topology.', switch.id) | 
            |
| 698 | 1 | self.notify_topology_update()  | 
            |
| 699 | |||
| 700 | 1 | def handle_interfaces_created(self, event):  | 
            |
| 701 | """Update the topology based on the interfaces created."""  | 
            ||
| 702 | 1 | interfaces = event.content["interfaces"]  | 
            |
| 703 | 1 | if not interfaces:  | 
            |
| 704 | return  | 
            ||
| 705 | 1 | switch = interfaces[0].switch  | 
            |
| 706 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict())  | 
            |
| 707 | 1 | name = "kytos/topology.switch.interface.created"  | 
            |
| 708 | 1 | for interface in interfaces:  | 
            |
| 709 | 1 |             event = KytosEvent(name=name, content={'interface': interface}) | 
            |
| 710 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 711 | |||
| 712 | 1 | def handle_interface_created(self, event):  | 
            |
| 713 | """Update the topology based on an interface created event.  | 
            ||
| 714 | |||
| 715 | It's handled as a link_up in case a switch send a  | 
            ||
| 716 | created event again and it can be belong to a link.  | 
            ||
| 717 | """  | 
            ||
| 718 | 1 | interface = event.content['interface']  | 
            |
| 719 | 1 | if not interface.is_active():  | 
            |
| 720 | 1 | return  | 
            |
| 721 | 1 | self.handle_interface_link_up(interface, event)  | 
            |
| 722 | |||
| 723 | 1 |     @listen_to('.*.topology.switch.interface.created') | 
            |
| 724 | 1 | def on_interface_created(self, event):  | 
            |
| 725 | """Handle individual interface create event.  | 
            ||
| 726 | |||
| 727 | It's handled as a link_up in case a switch send a  | 
            ||
| 728 | created event it can belong to an existign link.  | 
            ||
| 729 | """  | 
            ||
| 730 | self.handle_interface_created(event)  | 
            ||
| 731 | |||
| 732 | 1 |     @listen_to('.*.switch.interfaces.created') | 
            |
| 733 | 1 | def on_interfaces_created(self, event):  | 
            |
| 734 | """Update the topology based on a list of created interfaces."""  | 
            ||
| 735 | self.handle_interfaces_created(event)  | 
            ||
| 736 | |||
| 737 | 1 | def handle_interface_down(self, event):  | 
            |
| 738 | """Update the topology based on a Port Modify event.  | 
            ||
| 739 | |||
| 740 | The event notifies that an interface was changed to 'down'.  | 
            ||
| 741 | """  | 
            ||
| 742 | 1 | interface = event.content['interface']  | 
            |
| 743 | 1 | with self._intfs_lock[interface.id]:  | 
            |
| 744 | 1 | if (  | 
            |
| 745 | interface.id in self._intfs_updated_at  | 
            ||
| 746 | and self._intfs_updated_at[interface.id] > event.timestamp  | 
            ||
| 747 | ):  | 
            ||
| 748 | return  | 
            ||
| 749 | 1 | self._intfs_updated_at[interface.id] = event.timestamp  | 
            |
| 750 | 1 | interface.deactivate()  | 
            |
| 751 | 1 | self.handle_interface_link_down(interface, event)  | 
            |
| 752 | |||
| 753 | 1 |     @listen_to('.*.switch.interface.deleted') | 
            |
| 754 | 1 | def on_interface_deleted(self, event):  | 
            |
| 755 | """Update the topology based on a Port Delete event."""  | 
            ||
| 756 | self.handle_interface_deleted(event)  | 
            ||
| 757 | |||
| 758 | 1 | def handle_interface_deleted(self, event):  | 
            |
| 759 | """Update the topology based on a Port Delete event."""  | 
            ||
| 760 | 1 | self.handle_interface_down(event)  | 
            |
| 761 | |||
| 762 | 1 |     @listen_to('.*.switch.interface.link_up') | 
            |
| 763 | 1 | def on_interface_link_up(self, event):  | 
            |
| 764 | """Update the topology based on a Port Modify event.  | 
            ||
| 765 | |||
| 766 | The event notifies that an interface's link was changed to 'up'.  | 
            ||
| 767 | """  | 
            ||
| 768 | interface = event.content['interface']  | 
            ||
| 769 | self.handle_interface_link_up(interface, event)  | 
            ||
| 770 | |||
| 771 | 1 | def handle_interface_link_up(self, interface, event):  | 
            |
| 772 | """Update the topology based on a Port Modify event."""  | 
            ||
| 773 | 1 | with self._intfs_lock[interface.id]:  | 
            |
| 774 | 1 | if (  | 
            |
| 775 | interface.id in self._intfs_updated_at  | 
            ||
| 776 | and self._intfs_updated_at[interface.id] > event.timestamp  | 
            ||
| 777 | ):  | 
            ||
| 778 | 1 | return  | 
            |
| 779 | 1 | self._intfs_updated_at[interface.id] = event.timestamp  | 
            |
| 780 | 1 | self.handle_link_up(interface)  | 
            |
| 781 | |||
| 782 | 1 |     @listen_to('kytos/maintenance.end_switch') | 
            |
| 783 | 1 | def on_switch_maintenance_end(self, event):  | 
            |
| 784 | """Handle the end of the maintenance of a switch."""  | 
            ||
| 785 | self.handle_switch_maintenance_end(event)  | 
            ||
| 786 | |||
| 787 | 1 | def handle_switch_maintenance_end(self, event):  | 
            |
| 788 | """Handle the end of the maintenance of a switch."""  | 
            ||
| 789 | 1 | switches = event.content['switches']  | 
            |
| 790 | 1 | for switch_id in switches:  | 
            |
| 791 | 1 | try:  | 
            |
| 792 | 1 | switch = self.controller.switches[switch_id]  | 
            |
| 793 | except KeyError:  | 
            ||
| 794 | continue  | 
            ||
| 795 | 1 | switch.enable()  | 
            |
| 796 | 1 | switch.activate()  | 
            |
| 797 | 1 | for interface in switch.interfaces.values():  | 
            |
| 798 | 1 | interface.enable()  | 
            |
| 799 | 1 | self.handle_link_up(interface)  | 
            |
| 800 | |||
| 801 | 1 | def link_status_hook_link_up_timer(self, link) -> Optional[EntityStatus]:  | 
            |
| 802 | """Link status hook link up timer."""  | 
            ||
| 803 | 1 | tnow = time.time()  | 
            |
| 804 | 1 | if (  | 
            |
| 805 | link.is_active()  | 
            ||
| 806 | and link.is_enabled()  | 
            ||
| 807 | and "last_status_change" in link.metadata  | 
            ||
| 808 | and tnow - link.metadata['last_status_change'] < self.link_up_timer  | 
            ||
| 809 | ):  | 
            ||
| 810 | 1 | return EntityStatus.DOWN  | 
            |
| 811 | 1 | return None  | 
            |
| 812 | |||
| 813 | 1 | def notify_link_up_if_status(self, link, reason="link up") -> None:  | 
            |
| 814 | """Tries to notify link up and topology changes based on its status  | 
            ||
| 815 | |||
| 816 | Currently, it needs to wait up to a timer."""  | 
            ||
| 817 | 1 | time.sleep(self.link_up_timer)  | 
            |
| 818 | 1 | if link.status != EntityStatus.UP:  | 
            |
| 819 | return  | 
            ||
| 820 | 1 | with self._links_notify_lock[link.id]:  | 
            |
| 821 | 1 |             notified_at = link.get_metadata("notified_up_at") | 
            |
| 822 | 1 | if (  | 
            |
| 823 | notified_at  | 
            ||
| 824 | and (now() - notified_at.replace(tzinfo=timezone.utc)).seconds  | 
            ||
| 825 | < self.link_up_timer  | 
            ||
| 826 | ):  | 
            ||
| 827 | 1 | return  | 
            |
| 828 | 1 | key, notified_at = "notified_up_at", now()  | 
            |
| 829 | 1 | link.update_metadata(key, now())  | 
            |
| 830 | 1 | self.notify_topology_update()  | 
            |
| 831 | 1 | self.notify_link_status_change(link, reason)  | 
            |
| 832 | |||
| 833 | 1 | def handle_link_up(self, interface):  | 
            |
| 834 | """Handle link up for an interface."""  | 
            ||
| 835 | 1 | with self._links_lock:  | 
            |
| 836 | 1 | link = self._get_link_from_interface(interface)  | 
            |
| 837 | 1 | if not link:  | 
            |
| 838 | self.notify_topology_update()  | 
            ||
| 839 | return  | 
            ||
| 840 | 1 | other_interface = (  | 
            |
| 841 | link.endpoint_b if link.endpoint_a == interface  | 
            ||
| 842 | else link.endpoint_a  | 
            ||
| 843 | )  | 
            ||
| 844 | 1 | if other_interface.is_active() is False:  | 
            |
| 845 | 1 | self.notify_topology_update()  | 
            |
| 846 | 1 | return  | 
            |
| 847 | 1 |             metadata = { | 
            |
| 848 | 'last_status_change': time.time(),  | 
            ||
| 849 | 'last_status_is_active': True  | 
            ||
| 850 | }  | 
            ||
| 851 | 1 | link.extend_metadata(metadata)  | 
            |
| 852 | 1 | link.activate()  | 
            |
| 853 | 1 | self.notify_topology_update()  | 
            |
| 854 | 1 | self.notify_link_up_if_status(link, "link up")  | 
            |
| 855 | |||
| 856 | 1 |     @listen_to('.*.switch.interface.link_down') | 
            |
| 857 | 1 | def on_interface_link_down(self, event):  | 
            |
| 858 | """Update the topology based on a Port Modify event.  | 
            ||
| 859 | |||
| 860 | The event notifies that an interface's link was changed to 'down'.  | 
            ||
| 861 | """  | 
            ||
| 862 | interface = event.content['interface']  | 
            ||
| 863 | self.handle_interface_link_down(interface, event)  | 
            ||
| 864 | |||
| 865 | 1 | def handle_interface_link_down(self, interface, event):  | 
            |
| 866 | """Update the topology based on an interface."""  | 
            ||
| 867 | 1 | with self._intfs_lock[interface.id]:  | 
            |
| 868 | 1 | if (  | 
            |
| 869 | interface.id in self._intfs_updated_at  | 
            ||
| 870 | and self._intfs_updated_at[interface.id] > event.timestamp  | 
            ||
| 871 | ):  | 
            ||
| 872 | 1 | return  | 
            |
| 873 | 1 | self._intfs_updated_at[interface.id] = event.timestamp  | 
            |
| 874 | 1 | self.handle_link_down(interface)  | 
            |
| 875 | |||
| 876 | 1 |     @listen_to('kytos/maintenance.start_switch') | 
            |
| 877 | 1 | def on_switch_maintenance_start(self, event):  | 
            |
| 878 | """Handle the start of the maintenance of a switch."""  | 
            ||
| 879 | self.handle_switch_maintenance_start(event)  | 
            ||
| 880 | |||
| 881 | 1 | def handle_switch_maintenance_start(self, event):  | 
            |
| 882 | """Handle the start of the maintenance of a switch."""  | 
            ||
| 883 | 1 | switches = event.content['switches']  | 
            |
| 884 | 1 | for switch_id in switches:  | 
            |
| 885 | 1 | try:  | 
            |
| 886 | 1 | switch = self.controller.switches[switch_id]  | 
            |
| 887 | except KeyError:  | 
            ||
| 888 | continue  | 
            ||
| 889 | 1 | switch.disable()  | 
            |
| 890 | 1 | switch.deactivate()  | 
            |
| 891 | 1 | for interface in switch.interfaces.values():  | 
            |
| 892 | 1 | interface.disable()  | 
            |
| 893 | 1 | if interface.is_active():  | 
            |
| 894 | 1 | self.handle_link_down(interface)  | 
            |
| 895 | |||
| 896 | 1 | def handle_link_down(self, interface):  | 
            |
| 897 | """Notify a link is down."""  | 
            ||
| 898 | 1 | with self._links_lock:  | 
            |
| 899 | 1 | link = self._get_link_from_interface(interface)  | 
            |
| 900 | 1 |             if not link or not link.get_metadata("last_status_is_active"): | 
            |
| 901 | 1 | self.notify_topology_update()  | 
            |
| 902 | 1 | return  | 
            |
| 903 | 1 | link.deactivate()  | 
            |
| 904 | 1 |             metadata = { | 
            |
| 905 | "last_status_change": time.time(),  | 
            ||
| 906 | "last_status_is_active": False,  | 
            ||
| 907 | }  | 
            ||
| 908 | 1 | link.extend_metadata(metadata)  | 
            |
| 909 | 1 | self.notify_link_status_change(link, reason="link down")  | 
            |
| 910 | 1 | self.notify_topology_update()  | 
            |
| 911 | |||
| 912 | 1 |     @listen_to('.*.interface.is.nni') | 
            |
| 913 | 1 | def on_add_links(self, event):  | 
            |
| 914 | """Update the topology with links related to the NNI interfaces."""  | 
            ||
| 915 | self.add_links(event)  | 
            ||
| 916 | |||
| 917 | 1 | def add_links(self, event):  | 
            |
| 918 | """Update the topology with links related to the NNI interfaces."""  | 
            ||
| 919 | 1 | interface_a = event.content['interface_a']  | 
            |
| 920 | 1 | interface_b = event.content['interface_b']  | 
            |
| 921 | |||
| 922 | 1 | try:  | 
            |
| 923 | 1 | with self._links_lock:  | 
            |
| 924 | 1 | link, created = self._get_link_or_create(interface_a,  | 
            |
| 925 | interface_b)  | 
            ||
| 926 | 1 | interface_a.update_link(link)  | 
            |
| 927 | 1 | interface_b.update_link(link)  | 
            |
| 928 | |||
| 929 | 1 | link.endpoint_a = interface_a  | 
            |
| 930 | 1 | link.endpoint_b = interface_b  | 
            |
| 931 | |||
| 932 | 1 | interface_a.nni = True  | 
            |
| 933 | 1 | interface_b.nni = True  | 
            |
| 934 | |||
| 935 | except KytosLinkCreationError as err:  | 
            ||
| 936 |             log.error(f'Error creating link: {err}.') | 
            ||
| 937 | return  | 
            ||
| 938 | |||
| 939 | 1 | if not created:  | 
            |
| 940 | return  | 
            ||
| 941 | |||
| 942 | 1 | self.notify_topology_update()  | 
            |
| 943 | 1 | if not link.is_active():  | 
            |
| 944 | return  | 
            ||
| 945 | |||
| 946 | 1 |         metadata = { | 
            |
| 947 | 'last_status_change': time.time(),  | 
            ||
| 948 | 'last_status_is_active': True  | 
            ||
| 949 | }  | 
            ||
| 950 | 1 | link.extend_metadata(metadata)  | 
            |
| 951 | 1 | self.topo_controller.upsert_link(link.id, link.as_dict())  | 
            |
| 952 | 1 | self.notify_link_up_if_status(link, "link up")  | 
            |
| 953 | |||
| 954 | 1 |     @listen_to('.*.of_lldp.network_status.updated') | 
            |
| 955 | 1 | def on_lldp_status_updated(self, event):  | 
            |
| 956 | """Handle of_lldp.network_status.updated from of_lldp."""  | 
            ||
| 957 | self.handle_lldp_status_updated(event)  | 
            ||
| 958 | |||
| 959 | 1 |     @listen_to(".*.topo_controller.upsert_switch") | 
            |
| 960 | 1 | def on_topo_controller_upsert_switch(self, event) -> None:  | 
            |
| 961 | """Listen to topo_controller_upsert_switch."""  | 
            ||
| 962 | self.handle_topo_controller_upsert_switch(event.content["switch"])  | 
            ||
| 963 | |||
| 964 | 1 | def handle_topo_controller_upsert_switch(self, switch) -> Optional[dict]:  | 
            |
| 965 | """Handle topo_controller_upsert_switch."""  | 
            ||
| 966 | 1 | return self.topo_controller.upsert_switch(switch.id, switch.as_dict())  | 
            |
| 967 | |||
| 968 | 1 | def handle_lldp_status_updated(self, event) -> None:  | 
            |
| 969 | """Handle .*.network_status.updated events from of_lldp."""  | 
            ||
| 970 | 1 | content = event.content  | 
            |
| 971 | 1 | interface_ids = content["interface_ids"]  | 
            |
| 972 | 1 | switches = set()  | 
            |
| 973 | 1 | for interface_id in interface_ids:  | 
            |
| 974 | 1 |             dpid = ":".join(interface_id.split(":")[:-1]) | 
            |
| 975 | 1 | switch = self.controller.get_switch_by_dpid(dpid)  | 
            |
| 976 | 1 | if switch:  | 
            |
| 977 | 1 | switches.add(switch)  | 
            |
| 978 | |||
| 979 | 1 | name = "kytos/topology.topo_controller.upsert_switch"  | 
            |
| 980 | 1 | for switch in switches:  | 
            |
| 981 | 1 |             event = KytosEvent(name=name, content={"switch": switch}) | 
            |
| 982 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 983 | |||
| 984 | 1 | def notify_switch_enabled(self, dpid):  | 
            |
| 985 | """Send an event to notify that a switch is enabled."""  | 
            ||
| 986 | 1 | name = 'kytos/topology.switch.enabled'  | 
            |
| 987 | 1 |         event = KytosEvent(name=name, content={'dpid': dpid}) | 
            |
| 988 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 989 | |||
| 990 | 1 | def notify_switch_links_status(self, switch, reason):  | 
            |
| 991 | """Send an event to notify the status of a link in a switch"""  | 
            ||
| 992 | 1 | with self._links_lock:  | 
            |
| 993 | 1 | for link in self.links.values():  | 
            |
| 994 | 1 | if switch in (link.endpoint_a.switch, link.endpoint_b.switch):  | 
            |
| 995 | 1 | if reason == "link enabled":  | 
            |
| 996 | 1 | name = 'kytos/topology.notify_link_up_if_status'  | 
            |
| 997 | 1 |                         content = {'reason': reason, "link": link} | 
            |
| 998 | 1 | event = KytosEvent(name=name, content=content)  | 
            |
| 999 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1000 | else:  | 
            ||
| 1001 | 1 | self.notify_link_status_change(link, reason)  | 
            |
| 1002 | |||
| 1003 | 1 | def notify_switch_disabled(self, dpid):  | 
            |
| 1004 | """Send an event to notify that a switch is disabled."""  | 
            ||
| 1005 | 1 | name = 'kytos/topology.switch.disabled'  | 
            |
| 1006 | 1 |         event = KytosEvent(name=name, content={'dpid': dpid}) | 
            |
| 1007 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1008 | |||
| 1009 | 1 | def notify_topology_update(self):  | 
            |
| 1010 | """Send an event to notify about updates on the topology."""  | 
            ||
| 1011 | 1 | name = 'kytos/topology.updated'  | 
            |
| 1012 | 1 |         event = KytosEvent(name=name, content={'topology': | 
            |
| 1013 | self._get_topology()})  | 
            ||
| 1014 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1015 | |||
| 1016 | 1 | def notify_interface_link_status(self, interface, reason):  | 
            |
| 1017 | """Send an event to notify the status of a link from  | 
            ||
| 1018 | an interface."""  | 
            ||
| 1019 | 1 | link = self._get_link_from_interface(interface)  | 
            |
| 1020 | 1 | if link:  | 
            |
| 1021 | 1 | if reason == "link enabled":  | 
            |
| 1022 | 1 | name = 'kytos/topology.notify_link_up_if_status'  | 
            |
| 1023 | 1 |                 content = {'reason': reason, "link": link} | 
            |
| 1024 | 1 | event = KytosEvent(name=name, content=content)  | 
            |
| 1025 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1026 | else:  | 
            ||
| 1027 | 1 | self.notify_link_status_change(link, reason)  | 
            |
| 1028 | |||
| 1029 | 1 | def notify_link_status_change(self, link, reason='not given'):  | 
            |
| 1030 | """Send an event to notify about a status change on a link."""  | 
            ||
| 1031 | 1 | link_id = link.id  | 
            |
| 1032 | 1 | with self.link_status_lock:  | 
            |
| 1033 | 1 | if (  | 
            |
| 1034 | (not link.status_reason or link.status == EntityStatus.UP)  | 
            ||
| 1035 | and link_id not in self.link_up  | 
            ||
| 1036 | ):  | 
            ||
| 1037 | 1 | self.link_up.add(link_id)  | 
            |
| 1038 | 1 | event = KytosEvent(  | 
            |
| 1039 | name='kytos/topology.link_up',  | 
            ||
| 1040 |                     content={ | 
            ||
| 1041 | 'link': link,  | 
            ||
| 1042 | 'reason': reason  | 
            ||
| 1043 | },  | 
            ||
| 1044 | )  | 
            ||
| 1045 | 1 | elif (  | 
            |
| 1046 | (link.status_reason or link.status != EntityStatus.UP)  | 
            ||
| 1047 | and link_id in self.link_up  | 
            ||
| 1048 | ):  | 
            ||
| 1049 | 1 | self.link_up.remove(link_id)  | 
            |
| 1050 | 1 | event = KytosEvent(  | 
            |
| 1051 | name='kytos/topology.link_down',  | 
            ||
| 1052 |                     content={ | 
            ||
| 1053 | 'link': link,  | 
            ||
| 1054 | 'reason': reason  | 
            ||
| 1055 | },  | 
            ||
| 1056 | )  | 
            ||
| 1057 | else:  | 
            ||
| 1058 | 1 | return  | 
            |
| 1059 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1060 | |||
| 1061 | 1 | def notify_metadata_changes(self, obj, action):  | 
            |
| 1062 | """Send an event to notify about metadata changes."""  | 
            ||
| 1063 | 1 | if isinstance(obj, Switch):  | 
            |
| 1064 | 1 | entity = 'switch'  | 
            |
| 1065 | 1 | entities = 'switches'  | 
            |
| 1066 | 1 | elif isinstance(obj, Interface):  | 
            |
| 1067 | 1 | entity = 'interface'  | 
            |
| 1068 | 1 | entities = 'interfaces'  | 
            |
| 1069 | 1 | elif isinstance(obj, Link):  | 
            |
| 1070 | 1 | entity = 'link'  | 
            |
| 1071 | 1 | entities = 'links'  | 
            |
| 1072 | else:  | 
            ||
| 1073 | 1 | raise ValueError(  | 
            |
| 1074 | 'Invalid object, supported: Switch, Interface, Link'  | 
            ||
| 1075 | )  | 
            ||
| 1076 | |||
| 1077 | 1 |         name = f'kytos/topology.{entities}.metadata.{action}' | 
            |
| 1078 | 1 |         content = {entity: obj, 'metadata': obj.metadata.copy()} | 
            |
| 1079 | 1 | event = KytosEvent(name=name, content=content)  | 
            |
| 1080 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1081 | 1 |         log.debug(f'Metadata from {obj.id} was {action}.') | 
            |
| 1082 | |||
| 1083 | 1 |     @listen_to('kytos/topology.notify_link_up_if_status') | 
            |
| 1084 | 1 | def on_notify_link_up_if_status(self, event):  | 
            |
| 1085 | """Tries to notify link up and topology changes"""  | 
            ||
| 1086 | link = event.content["link"]  | 
            ||
| 1087 | reason = event.content["reason"]  | 
            ||
| 1088 | self.notify_link_up_if_status(link, reason)  | 
            ||
| 1089 | |||
| 1090 | 1 |     @listen_to('.*.switch.port.created') | 
            |
| 1091 | 1 | def on_notify_port_created(self, event):  | 
            |
| 1092 | """Notify when a port is created."""  | 
            ||
| 1093 | self.notify_port_created(event)  | 
            ||
| 1094 | |||
| 1095 | 1 | def notify_port_created(self, event):  | 
            |
| 1096 | """Notify when a port is created."""  | 
            ||
| 1097 | 1 | name = 'kytos/topology.port.created'  | 
            |
| 1098 | 1 | event = KytosEvent(name=name, content=event.content)  | 
            |
| 1099 | 1 | self.controller.buffers.app.put(event)  | 
            |
| 1100 | |||
| 1101 | 1 | @staticmethod  | 
            |
| 1102 | 1 | def load_interfaces_available_tags(switch: Switch,  | 
            |
| 1103 | interfaces_details: List[dict]) -> None:  | 
            ||
| 1104 | """Load interfaces available tags (vlans)."""  | 
            ||
| 1105 | 1 | if not interfaces_details:  | 
            |
| 1106 | return  | 
            ||
| 1107 | 1 | for interface_details in interfaces_details:  | 
            |
| 1108 | 1 | available_vlans = interface_details["available_vlans"]  | 
            |
| 1109 | 1 | if not available_vlans:  | 
            |
| 1110 | continue  | 
            ||
| 1111 | 1 |             log.debug(f"Interface id {interface_details['id']} loading " | 
            |
| 1112 |                       f"{len(interface_details['available_vlans'])} " | 
            ||
| 1113 | "available tags")  | 
            ||
| 1114 | 1 |             port_number = int(interface_details["id"].split(":")[-1]) | 
            |
| 1115 | 1 | interface = switch.interfaces[port_number]  | 
            |
| 1116 | 1 | interface.set_available_tags(interface_details['available_vlans'])  | 
            |
| 1117 | |||
| 1118 | 1 |     @listen_to('kytos/maintenance.start_link') | 
            |
| 1119 | 1 | def on_link_maintenance_start(self, event):  | 
            |
| 1120 | """Deals with the start of links maintenance."""  | 
            ||
| 1121 | with self._links_lock:  | 
            ||
| 1122 | self.handle_link_maintenance_start(event)  | 
            ||
| 1123 | |||
| 1124 | 1 | def handle_link_maintenance_start(self, event):  | 
            |
| 1125 | """Deals with the start of links maintenance."""  | 
            ||
| 1126 | 1 | notify_links = []  | 
            |
| 1127 | 1 | maintenance_links = event.content['links']  | 
            |
| 1128 | 1 | for maintenance_link_id in maintenance_links:  | 
            |
| 1129 | 1 | try:  | 
            |
| 1130 | 1 | link = self.links[maintenance_link_id]  | 
            |
| 1131 | 1 | except KeyError:  | 
            |
| 1132 | 1 | continue  | 
            |
| 1133 | 1 | notify_links.append(link)  | 
            |
| 1134 | 1 | for link in notify_links:  | 
            |
| 1135 | 1 | link.disable()  | 
            |
| 1136 | 1 | link.deactivate()  | 
            |
| 1137 | 1 | link.endpoint_a.deactivate()  | 
            |
| 1138 | 1 | link.endpoint_b.deactivate()  | 
            |
| 1139 | 1 | link.endpoint_a.disable()  | 
            |
| 1140 | 1 | link.endpoint_b.disable()  | 
            |
| 1141 | 1 | self.notify_link_status_change(link, reason='maintenance')  | 
            |
| 1142 | |||
| 1143 | 1 |     @listen_to('kytos/maintenance.end_link') | 
            |
| 1144 | 1 | def on_link_maintenance_end(self, event):  | 
            |
| 1145 | """Deals with the end of links maintenance."""  | 
            ||
| 1146 | with self._links_lock:  | 
            ||
| 1147 | self.handle_link_maintenance_end(event)  | 
            ||
| 1148 | |||
| 1149 | 1 | def handle_link_maintenance_end(self, event):  | 
            |
| 1150 | """Deals with the end of links maintenance."""  | 
            ||
| 1151 | 1 | notify_links = []  | 
            |
| 1152 | 1 | maintenance_links = event.content['links']  | 
            |
| 1153 | 1 | for maintenance_link_id in maintenance_links:  | 
            |
| 1154 | 1 | try:  | 
            |
| 1155 | 1 | link = self.links[maintenance_link_id]  | 
            |
| 1156 | 1 | except KeyError:  | 
            |
| 1157 | 1 | continue  | 
            |
| 1158 | 1 | notify_links.append(link)  | 
            |
| 1159 | 1 | for link in notify_links:  | 
            |
| 1160 | 1 | link.enable()  | 
            |
| 1161 | 1 | link.activate()  | 
            |
| 1162 | 1 | link.endpoint_a.activate()  | 
            |
| 1163 | 1 | link.endpoint_b.activate()  | 
            |
| 1164 | 1 | link.endpoint_a.enable()  | 
            |
| 1165 | 1 | link.endpoint_b.enable()  | 
            |
| 1166 | 1 | self.notify_link_status_change(link, reason='maintenance')  | 
            |
| 1167 | |||
| 1168 | 1 |     @listen_to('topology.interruption.start') | 
            |
| 1169 | 1 | def on_interruption_start(self, event: KytosEvent):  | 
            |
| 1170 | """Deals with the start of service interruption."""  | 
            ||
| 1171 | with self._links_lock:  | 
            ||
| 1172 | self.handle_interruption_start(event)  | 
            ||
| 1173 | |||
| 1174 | 1 | View Code Duplication | def handle_interruption_start(self, event: KytosEvent):  | 
            
| 1175 | """Deals with the start of service interruption."""  | 
            ||
| 1176 | 1 | interrupt_type = event.content['type']  | 
            |
| 1177 |         # switches = event.content.get('switches', []) | 
            ||
| 1178 |         # interfaces = event.content.get('interfaces', []) | 
            ||
| 1179 | 1 |         links = event.content.get('links', []) | 
            |
| 1180 | # for switch_id in switches:  | 
            ||
| 1181 | # pass  | 
            ||
| 1182 | # for interface_id in interfaces:  | 
            ||
| 1183 | # pass  | 
            ||
| 1184 | 1 | for link_id in links:  | 
            |
| 1185 | 1 | link = self.links.get(link_id)  | 
            |
| 1186 | 1 | if link is None:  | 
            |
| 1187 | log.error(  | 
            ||
| 1188 | "Invalid link id '%s' for interruption of type '%s;",  | 
            ||
| 1189 | link_id,  | 
            ||
| 1190 | interrupt_type  | 
            ||
| 1191 | )  | 
            ||
| 1192 | else:  | 
            ||
| 1193 | 1 | self.notify_link_status_change(link, interrupt_type)  | 
            |
| 1194 | 1 | self.notify_topology_update()  | 
            |
| 1195 | |||
| 1196 | 1 |     @listen_to('topology.interruption.end') | 
            |
| 1197 | 1 | def on_interruption_end(self, event: KytosEvent):  | 
            |
| 1198 | """Deals with the end of service interruption."""  | 
            ||
| 1199 | with self._links_lock:  | 
            ||
| 1200 | self.handle_interruption_end(event)  | 
            ||
| 1201 | |||
| 1202 | 1 | View Code Duplication | def handle_interruption_end(self, event: KytosEvent):  | 
            
| 1203 | """Deals with the end of service interruption."""  | 
            ||
| 1204 | 1 | interrupt_type = event.content['type']  | 
            |
| 1205 |         # switches = event.content.get('switches', []) | 
            ||
| 1206 |         # interfaces = event.content.get('interfaces', []) | 
            ||
| 1207 | 1 |         links = event.content.get('links', []) | 
            |
| 1208 | # for switch_id in switches:  | 
            ||
| 1209 | # pass  | 
            ||
| 1210 | # for interface_id in interfaces:  | 
            ||
| 1211 | # pass  | 
            ||
| 1212 | 1 | for link_id in links:  | 
            |
| 1213 | 1 | link = self.links.get(link_id)  | 
            |
| 1214 | 1 | if link is None:  | 
            |
| 1215 | log.error(  | 
            ||
| 1216 | "Invalid link id '%s' for interruption of type '%s;",  | 
            ||
| 1217 | link_id,  | 
            ||
| 1218 | interrupt_type  | 
            ||
| 1219 | )  | 
            ||
| 1220 | else:  | 
            ||
| 1221 | 1 | self.notify_link_status_change(link, interrupt_type)  | 
            |
| 1222 | self.notify_topology_update()  | 
            ||
| 1223 |