Commit da3611de authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent a8e61fa8
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -173,6 +173,16 @@ if LOAD_ALL_DEVICE_DRIVERS:
            }
        ]))

if LOAD_ALL_DEVICE_DRIVERS:
    from .morpheus.MorpheusApiDriver import MorpheusApiDriver
    DRIVERS.append(
        (MorpheusApiDriver, [
            {
                # Close enough, it does optical switching
                FilterFieldEnum.DEVICE_TYPE: DeviceTypeEnum.MORPHEUS,
                FilterFieldEnum.DRIVER     : DeviceDriverEnum.DEVICEDRIVER_MORPHEUS,
            }
        ]))

if LOAD_ALL_DEVICE_DRIVERS:
    from .microwave.IETFApiDriver import IETFApiDriver # pylint: disable=wrong-import-position
@@ -232,14 +242,3 @@ if LOAD_ALL_DEVICE_DRIVERS:
                FilterFieldEnum.DRIVER     : DeviceDriverEnum.DEVICEDRIVER_QKD,
            }
        ]))

if LOAD_ALL_DEVICE_DRIVERS:
    from .morpheus.MorpheusApiDriver import MorpheusAPIDriver
    DRIVERS.append(
        (MorpheusAPIDriver, [
            {
                # Close enough, it does optical switching
                FilterFieldEnum.DEVICE_TYPE: DeviceTypeEnum.MORPHEUS,
                FilterFieldEnum.DRIVER     : DeviceDriverEnum.DEVICEDRIVER_MORPHEUS,
            }
        ]))
+33 −38
Original line number Diff line number Diff line
@@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, requests, threading, json, time
import json, logging, queue, requests, threading, time
from typing import Any, Iterator, List, Optional, Tuple, Union, Dict
from queue import Queue
from common.method_wrappers.Decorator import MetricsPool, metered_subclass_method
from device.service.driver_api._Driver import _Driver

@@ -38,8 +37,8 @@ class MorpheusApiDriver(_Driver):
        self.__pipeline_error_thread = None

        size = self.settings.get('queue_events_size', 10)
        self.__pipeline_error_queue = Queue(maxsize=size)
        self.__detection_queue = Queue(maxsize=size)
        self.__pipeline_error_queue = queue.Queue(maxsize=size)
        self.__detection_queue = queue.Queue(maxsize=size)

    def Connect(self) -> bool:
        url = self.__morpheus_root + '/restconf/data/naudit-morpheus:morpheus'
@@ -62,10 +61,10 @@ class MorpheusApiDriver(_Driver):
                if self.__detection_thread and self.__detection_thread.is_alive():
                    self.__unsubscribe_detection_event()

                if self.__pipeline_thread and self.__pipeline_thread.is_alive():
                if self.__pipeline_error_thread and self.__pipeline_error_thread.is_alive():
                    self.__unsubscribe_pipeline_error()
            except Exception as e:
                LOGGER.exception(f'Error during disconnect: {str(e)}')
            except Exception:  # pylint: disable=broad-except
                LOGGER.exception('Error during disconnect')

            self.__terminate.set()
            return True
@@ -84,7 +83,7 @@ class MorpheusApiDriver(_Driver):
                        result.append((key, value))

                    return  result
            except Exception as e:
            except Exception:  # pylint: disable=broad-except
                LOGGER.exception('Exception getting initial config {:s}'.format(str(self.__morpheus_root)))
            return []

@@ -105,16 +104,12 @@ class MorpheusApiDriver(_Driver):

                    for key in resource_keys:
                        try:
                            results.append(config[key])
                        except KeyError:
                            results.append(None)
                except Exception as e:
                            results.append(e)

                            results.append((key, config[key]))
                        except Exception as e:  # pylint: disable=broad-except
                            results.append((key, e))
                    return results
                return [(key, None) for key in resource_keys]
            except Exception as e:
                LOGGER.exception(f'Error getting config: {str(e)}')
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception('Error getting config')
                return [(key, e) for key in resource_keys]

    @metered_subclass_method(METRICS_POOL)
@@ -175,16 +170,16 @@ class MorpheusApiDriver(_Driver):
                (self.__subscribe_pipeline_error, self.__unsubscribe_pipeline_error),
                (self.__start_pipeline, self.__stop_pipeline),
        ]
        for i, (sub_op, unsub_op) in enumerate(operations):
        for _, (sub_op, unsub_op) in enumerate(operations):
            result = sub_op()
            reuslts.append(result)
            results.append(result)
            if isinstance(result, Exception):
                while rollback_stack:
                    rollback_op = rollback_stack.pop()
                    try:
                        rollback_op()
                    except Exception as e:
                        LOGGER.exception(f'Error during subscription rollback operation: {e}')
                    except Exception as e:  # pylint: disable=broad-except
                        LOGGER.exception('Error during subscription rollback operation')

                return results

@@ -206,8 +201,8 @@ class MorpheusApiDriver(_Driver):
                response = requests.post(url, headers=self.__headers, timeout=self.__timeout, verify=False)
                response.raise_for_status()
                return True
            except Exception as e:
                LOGGER.exception(f'Error starting pipeline: {e}')
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception('Error starting pipeline')
                return e

    def __stop_pipeline(self) -> Union[bool, Exception]:
@@ -217,8 +212,8 @@ class MorpheusApiDriver(_Driver):
                response = requests.post(url, headers=self.__headers, timeout=self.__timeout, verify=False)
                response.raise_for_status()
                return True
            except Exception as e:
                LOGGER.exception(f'Error stopping pipeline: {e}')
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception('Error stopping pipeline')
                return e

    def __subscribe_detection_event(self) -> Union[bool, Exception]:
@@ -232,8 +227,8 @@ class MorpheusApiDriver(_Driver):
                        )
                self.__detection_thread.start()
                return True
            except Exception as e:
                LOGGER.exception(f'Error subscribing to detection events: {str(e)}')
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception('Error subscribing to detection events')
                return e

    def __unsubscribe_detection_event(self) -> Union[bool, Exception]:
@@ -241,8 +236,8 @@ class MorpheusApiDriver(_Driver):
            if self.__detection_thread and self.__detection_thread.is_alive():
                self.__detection_thread.join(timeout=5)
            return True
        except Exception as e:
            LOGGER.exception(f'Error unsubscribing from detection events: {str(e)}')
        except Exception as e:  # pylint: disable=broad-except
            LOGGER.exception('Error unsubscribing from detection events')
            return e

    def __subscribe_pipeline_error(self) -> Union[bool, Exception]:
@@ -256,8 +251,8 @@ class MorpheusApiDriver(_Driver):
                        )
                self.__pipeline_error_thread.start()
                return True
            except Exception as e:
                LOGGER.exception(f'Error subscribing to pipeline errors: {str(e)}')
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception('Error subscribing to pipeline errors')
                return e

    def __unsubscribe_pipeline_error(self) -> Union[bool, Exception]:
@@ -265,8 +260,8 @@ class MorpheusApiDriver(_Driver):
            if self.__pipeline_error_thread and self.__pipeline_error_thread.is_alive():
                self.__pipeline_error_thread.join(timeout=5)
            return True
        except Exception as e:
            LOGGER.exception(f'Error unsubscribing from pipeline errors: {str(e)}')
        except Exception as e:  # pylint: disable=broad-except
            LOGGER.exception('Error unsubscribing from pipeline errors')
            return e

    def __get_state(self) -> Dict:
@@ -277,12 +272,12 @@ class MorpheusApiDriver(_Driver):
                if response.ok:
                    state = response.json()
                    return state
            except Exception as e:
                LOGGER.exception(f'Error getting internal state: {e}')
            except Exception:  # pylint: disable=broad-except
                LOGGER.exception('Error getting internal state')
        return None


    def __handle_notification_stream(self, url: str, queue: Queue[Any]) -> None:
    def __handle_notification_stream(self, url: str, queue: queue.Queue[Any]) -> None:
        try:
            with requests.get(url,
                              headers=self.__headers,
@@ -298,5 +293,5 @@ class MorpheusApiDriver(_Driver):
                    queue.put(event['data']['ietf-restconf:notification'])
                except json.JSONDecodeError as e:
                    LOGGER.error(f'Error parsing event: {e}')
        except Exception as e:
            LOGGER.exception(f'Error in notification stream handler: {str(e)}')
        except Exception as e:  # pylint: disable=broad-except
            LOGGER.exception('Error in notification stream handler')
+13 −0
Original line number Diff line number Diff line
# Copyright 2022-2024 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.