Commit 31ef95e0 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Device omponent - TFS Optical Driver:

- Fix discovery of endpoint settings
parent eb04de62
Loading
Loading
Loading
Loading
+28 −6
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import logging, requests
from typing import Dict, List, Optional, Tuple
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import ServiceStatusEnum, ServiceTypeEnum
@@ -24,11 +24,13 @@ from common.tools.object_factory.EndPoint import json_endpoint_id
from common.tools.object_factory.Service import json_service
from device.service.driver_api.ImportTopologyEnum import ImportTopologyEnum

CONTEXT_IDS_URL = '/tfs-api/context_ids'

GET_CONTEXT_IDS_URL = '/tfs-api/context_ids'
TOPOLOGY_URL    = '/tfs-api/context/{context_uuid:s}/topology_details/{topology_uuid:s}'
SERVICES_URL    = '/tfs-api/context/{context_uuid:s}/services'
SERVICE_URL     = '/tfs-api/context/{context_uuid:s}/service/{service_uuid:s}'


MAPPING_STATUS = {
    'DEVICEOPERATIONALSTATUS_UNDEFINED': 0,
    'DEVICEOPERATIONALSTATUS_DISABLED' : 1,
@@ -60,8 +62,10 @@ MAPPING_DRIVER = {
    'DEVICEDRIVER_RESTCONF_OPENCONFIG'  : 21,
}


LOGGER = logging.getLogger(__name__)


class TfsApiClient(RestApiClient):
    def __init__(
        self, address : str, port : int, scheme : str = 'http',
@@ -73,9 +77,26 @@ class TfsApiClient(RestApiClient):
            timeout=timeout, verify_certs=False, allow_redirects=True, logger=LOGGER
        )

    def check_credentials(self) -> None:
        self.get(CONTEXT_IDS_URL)

    def check_credentials(self, raise_if_fail : bool = True) -> None:
        try:
            LOGGER.info('Checking credentials...')
            self.get(GET_CONTEXT_IDS_URL, expected_status_codes={requests.codes['OK']})
            LOGGER.info('Credentials checked')
            return True
        except requests.exceptions.Timeout as e:
            MSG = 'Timeout connecting {:s}'
            msg = MSG.format(GET_CONTEXT_IDS_URL)
            LOGGER.exception(msg)
            if raise_if_fail: raise Exception(msg) from e
            return False
        except Exception as e:
            MSG = 'Exception connecting credentials: {:s}'
            msg = MSG.format(GET_CONTEXT_IDS_URL)
            LOGGER.exception(msg)
            if raise_if_fail: raise Exception(msg) from e
            return False


    def get_devices_endpoints(
        self, import_topology : ImportTopologyEnum = ImportTopologyEnum.DEVICES
@@ -113,11 +134,12 @@ class TfsApiClient(RestApiClient):

            for json_endpoint in json_device['device_endpoints']:
                endpoint_uuid = json_endpoint['endpoint_id']['endpoint_uuid']['uuid']
                endpoint_name = json_endpoint['name']
                endpoint_url = '/endpoints/endpoint[{:s}]'.format(endpoint_uuid)
                endpoint_data = {
                    'device_uuid': device_uuid,
                    'uuid': endpoint_uuid,
                    'name': json_endpoint['name'],
                    'name': endpoint_name,
                    'type': json_endpoint['endpoint_type'],
                }
                result.append((endpoint_url, endpoint_data))