Commit 427d7ccc authored by Waleed Akbar's avatar Waleed Akbar
Browse files

Merge branch 'feat/370-cttc-fix-ofc25-test' of...

Merge branch 'feat/370-cttc-fix-ofc25-test' of ssh://labs.etsi.org:29419/tfs/controller into feat/370-cttc-fix-ofc25-test
parents 5d130a7a 31ef95e0
Loading
Loading
Loading
Loading
+58 −5
Original line number Diff line number Diff line
@@ -12,11 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, requests
import json, logging, requests
from typing import Dict, List, Optional
from common.tools.rest_api.client.RestApiClient import RestApiClient
from device.service.driver_api.ImportTopologyEnum import ImportTopologyEnum


GET_CONTEXT_IDS_URL = '/tfs-api/context_ids'
GET_DEVICES_URL     = '/tfs-api/devices'
GET_LINKS_URL       = '/tfs-api/links'
@@ -52,8 +53,10 @@ MAPPING_DRIVER = {
    'DEVICEDRIVER_RESTCONF_OPENCONFIG'  : 21,
}


LOGGER = logging.getLogger(__name__)


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

    def check_credentials(self) -> None:

    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
@@ -88,6 +108,10 @@ class TfsApiClient(RestApiClient):
            device_type : str = json_device['device_type']
            #if not device_type.startswith('emu-'): device_type = 'emu-' + device_type
            device_status = json_device['device_operational_status']

            ctrl_id : Dict[str, Dict] = json_device.get('controller_id', dict())
            ctrl_uuid : Optional[str] = ctrl_id.get('device_uuid', dict()).get('uuid')

            device_url = '/devices/device[{:s}]'.format(device_uuid)
            device_data = {
                'uuid': json_device['device_id']['device_uuid']['uuid'],
@@ -99,17 +123,46 @@ class TfsApiClient(RestApiClient):
                    for driver in json_device['device_drivers']
                ],
            }
            if ctrl_uuid is not None and len(ctrl_uuid) > 0:
                device_data['ctrl_uuid'] = ctrl_uuid
            result.append((device_url, device_data))

            config_rule_list : List[Dict] = (
                json_device
                .get('device_config', dict())
                .get('config_rules', list())
            )
            config_rule_dict : Dict[str, Dict] = dict()
            for cr in config_rule_list:
                if cr['action'] != 'CONFIGACTION_SET': continue
                if 'custom' not in cr: continue
                cr_rk : str = cr['custom']['resource_key']
                if not cr_rk.startswith('/endpoints/endpoint['): continue
                settings = json.loads(cr['custom']['resource_value'])
                ep_uuid = settings.get('uuid')
                if ep_uuid is not None:
                    config_rule_dict[ep_uuid] = settings
                ep_name = settings.get('name')
                if ep_name is not None:
                    config_rule_dict[ep_name] = settings

            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'],
                }
                endpoint_settings = config_rule_dict.get(endpoint_uuid)
                if endpoint_settings is not None:
                    endpoint_data['settings'] = endpoint_settings
                else:
                    endpoint_settings = config_rule_dict.get(endpoint_name)
                    if endpoint_settings is not None:
                        endpoint_data['settings'] = endpoint_settings
                result.append((endpoint_url, endpoint_data))

        if import_topology == ImportTopologyEnum.DEVICES:
+12 −4
Original line number Diff line number Diff line
@@ -138,14 +138,18 @@ class TfsApiClient(RestApiClient):
                .get('device_config', dict())
                .get('config_rules', list())
            )
            config_rule_dict = dict()
            config_rule_dict : Dict[str, Dict] = dict()
            for cr in config_rule_list:
                if cr['action'] != 'CONFIGACTION_SET': continue
                if 'custom' not in cr: continue
                cr_rk : str = cr['custom']['resource_key']
                if not cr_rk.startswith('/endpoints/endpoint['): continue
                settings = json.loads(cr['custom']['resource_value'])
                ep_name = settings['name']
                ep_uuid = settings.get('uuid')
                if ep_uuid is not None:
                    config_rule_dict[ep_uuid] = settings
                ep_name = settings.get('name')
                if ep_name is not None:
                    config_rule_dict[ep_name] = settings

            for json_endpoint in json_device['device_endpoints']:
@@ -158,6 +162,10 @@ class TfsApiClient(RestApiClient):
                    'name': endpoint_name,
                    'type': json_endpoint['endpoint_type'],
                }
                endpoint_settings = config_rule_dict.get(endpoint_uuid)
                if endpoint_settings is not None:
                    endpoint_data['settings'] = endpoint_settings
                else:
                    endpoint_settings = config_rule_dict.get(endpoint_name)
                    if endpoint_settings is not None:
                        endpoint_data['settings'] = endpoint_settings
+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))