Newer
Older
"source": [
"#### Subscribing to application termination"
]
},
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def send_subscribe_termination(sandbox_name: str, app_inst_id: swagger_client.models.application_info.ApplicationInfo) -> int:\n",
" global MEC_PLTF, logger\n",
"\n",
" logger.debug('>>> send_subscribe_termination: ' + app_inst_id.id)\n",
" try:\n",
" url = '/{sandbox_name}/{mec_pltf}/mec_app_support/v2/applications/{app_inst_id}/subscriptions'\n",
" logger.debug('send_subscribe_termination: url: ' + url)\n",
" path_params = {}\n",
" path_params['sandbox_name'] = sandbox_name\n",
" path_params['mec_pltf'] = MEC_PLTF\n",
" path_params['app_inst_id'] = app_inst_id.id\n",
" header_params = {}\n",
" # HTTP header `Accept`\n",
" header_params['Accept'] = 'application/json' # noqa: E501\n",
" # HTTP header `Content-Type`\n",
" header_params['Content-Type'] = 'application/json' # noqa: E501\n",
" # Body\n",
" dict_body = {}\n",
" dict_body['subscriptionType'] = 'AppTerminationNotificationSubscription'\n",
" dict_body['callbackReference'] = 'http://yanngarcia.ddns.net/mec011/v2/termination' # FIXME To be parameterized\n",
" dict_body['appInstanceId'] = app_inst_id.id\n",
" (result, status, headers) = service_api.call_api(url, 'POST', header_params=header_params, path_params = path_params, body=dict_body, async_req=False)\n",
" return (extract_sub_id(headers['Location']), headers['Location'])\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
" return None\n",
" # End of function send_subscribe_termination"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extracting subscription identifier"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def extract_sub_id(resource_url: str) -> str:\n",
" global logger\n",
"\n",
" logger.debug('>>> extract_sub_id: resource_url: ' + resource_url)\n",
"\n",
" res = urllib3.util.parse_url(resource_url)\n",
" if res is not None and res.path is not None and res.path != '':\n",
" id = res.path.rsplit('/', 1)[-1]\n",
" if id is not None:\n",
" return id\n",
"\n",
" return None\n",
" # End of function extract_sub_id"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Delete subscription to application termination"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def delete_subscribe_termination(sandbox_name: str, app_inst_id: swagger_client.models.application_info.ApplicationInfo, sub_id: str) -> int:\n",
" global MEC_PLTF, logger\n",
" logger.debug('>>> delete_subscribe_termination: ' + app_inst_id.id)\n",
" url = '/{sandbox_name}/{mec_pltf}/mec_app_support/v2/applications/{app_inst_id}/subscriptions/{sub_id}'\n",
" logger.debug('delete_subscribe_termination: url: ' + url)\n",
" path_params = {}\n",
" path_params['sandbox_name'] = sandbox_name\n",
" path_params['mec_pltf'] = MEC_PLTF\n",
" path_params['app_inst_id'] = app_inst_id.id\n",
" path_params['sub_id'] = sub_id\n",
" header_params = {}\n",
" # HTTP header `Accept`\n",
" header_params['Accept'] = 'application/json' # noqa: E501\n",
" # HTTP header `Content-Type`\n",
" header_params['Content-Type'] = 'application/json' # noqa: E501\n",
" service_api.call_api(url, 'DELETE', header_params=header_params, path_params = path_params, async_req=False)\n",
" return 0\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
" # End of function delete_subscribe_termination"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, it is time now to create the our fifth iteration of our MEC application.\n",
"\n",
"The sequence is the following:\n",
"- Login\n",
"- Print sandbox identifier\n",
"- Print available network scenarios\n",
"- Activate a network scenario\n",
"- Request for a new application instance identifier\n",
"- Subscribe to AppTerminationNotificationSubscription\n",
"- Check list of services\n",
"- Delete AppTerminationNotification subscription\n",
"- Delete our application instance identifier\n",
"- Deactivate a network scenario\n",
"- Logout\n",
"- Check that logout is effective\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def process_main():\n",
" \"\"\"\n",
" This is the second sprint of our skeleton of our MEC application:\n",
" - Login\n",
" - Print sandbox identifier\n",
" - Print available network scenarios\n",
" - Activate a network scenario\n",
" - Request for a new application instance identifier\n",
" \n",
" - Subscribe to AppTermination Notification\n",
" - Delete AppTerminationNotification subscription\n",
" - Delete our application instance identifier\n",
" - Deactivate a network scenario\n",
" - Logout\n",
" - Check that logout is effective\n",
" \"\"\" \n",
" global logger, nw_scenarios\n",
" logger.debug('Starting at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" logger.debug('\\t pwd= ' + os.getcwd())\n",
"\n",
" # Login\n",
" sandbox = process_login()\n",
" if sandbox is None:\n",
" logger.error('Failed to instanciate a MEC Sandbox')\n",
" return\n",
"\n",
" # Print sandbox identifier\n",
" logger.info('Sandbox created: ' + sandbox)\n",
" # Wait for the MEC Sandbox is running\n",
" time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
"\n",
" # Print available network scenarios\n",
" nw_scenarios = get_network_scenarios(sandbox)\n",
" if nw_scenarios is None:\n",
" logger.error('Failed to retrieve the list of network scenarios')\n",
" logger.info('nw_scenarios: %s', str(type(nw_scenarios[0])))\n",
" logger.info('nw_scenarios: %s', str(nw_scenarios))\n",
" # Wait for the MEC Sandbox is running\n",
" time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
" else:\n",
" logger.info('nw_scenarios: No scenario available')\n",
"\n",
" # Activate a network scenario based on a list of criterias (hard coded!!!)\n",
" if activate_network_scenario(sandbox) == -1:\n",
" logger.error('Failed to activate network scenario')\n",
" logger.info('Network scenario activated: ' + nw_scenarios[nw_scenario_idx].id)\n",
" time.sleep(2 * STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
" # Request for a new application instance identifier\n",
" app_inst_id = request_application_instance_id(sandbox)\n",
" if app_inst_id == None:\n",
" logger.error('Failed to request an application instance identifier')\n",
" logger.info('app_inst_id: %s', str(app_inst_id))\n",
" time.sleep(STABLE_TIME_OUT)\n",
" sub_id = None\n",
" if send_ready_confirmation(sandbox, app_inst_id) == -1:\n",
" logger.error('Failed to send confirm_ready')\n",
" else:\n",
" # Subscribe to AppTerminationNotificationSubscription\n",
" sub_id, res_url = send_subscribe_termination(sandbox, app_inst_id)\n",
" if sub_id == None:\n",
" logger.error('Failed to do the subscription')\n",
" else:\n",
" logger.info('sub_id: %s', sub_id)\n",
"\n",
" # Any processing here\n",
" time.sleep(STABLE_TIME_OUT)\n",
"\n",
" # Delete AppTerminationNotification subscription\n",
" if sub_id is not None:\n",
" if delete_subscribe_termination(sandbox, app_inst_id, sub_id) == -1:\n",
" logger.error('Failed to delete the application instance identifier')\n",
" else:\n",
" logger.info('app_inst_id deleted: ' + app_inst_id.id)\n",
" # Delete the application instance identifier\n",
" if delete_application_instance_id(sandbox, app_inst_id.id) == -1:\n",
" logger.error('Failed to delete the application instance identifier')\n",
" logger.info('app_inst_id deleted: ' + app_inst_id.id)\n",
"\n",
" # Deactivate a network scenario based on a list of criterias (hard coded!!!)\n",
" if deactivate_network_scenario(sandbox) == -1:\n",
" logger.error('Failed to deactivate network scenario')\n",
" logger.info('Network scenario deactivated: ' + nw_scenarios[nw_scenario_idx].id)\n",
" time.sleep(2 * STABLE_TIME_OUT)\n",
"\n",
" # Logout\n",
" process_logout(sandbox)\n",
"\n",
" # Check that logout is effective\n",
" logger.debug('To check that logout is effective, verify on the MEC Sandbox server that the MEC Sandbox is removed (kubectl get pods -A)')\n",
" logger.debug('Stopped at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" # End of function process_main\n",
"\n",
"if __name__ == '__main__':\n",
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conclusion: Create two procedures for the setup and the termination of our MEC application\n"
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The procedure for the setup of a MEC application\n",
"This function provides the steps to setup a MEC application and to be ready to use the MEC service exposed by the created MEC Sandbox.\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def mec_app_setup():\n",
" This function provides the steps to setup a MEC application:\n",
" - Login\n",
" - Print sandbox identifier\n",
" - Print available network scenarios\n",
" - Activate a network scenario\n",
" - Request for a new application instance identifier\n",
" - Send READY confirmation\n",
" - Subscribe to AppTermination Notification\n",
" \"\"\"\n",
" global logger, nw_scenarios\n",
"\n",
" # Login\n",
" sandbox = process_login()\n",
" if sandbox is None:\n",
" logger.error('Failed to instanciate a MEC Sandbox')\n",
" return\n",
" # Wait for the MEC Sandbox is running\n",
" time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
"\n",
" # Print available network scenarios\n",
" nw_scenarios = get_network_scenarios(sandbox)\n",
" if nw_scenarios is None:\n",
" logger.error('Failed to retrieve the list of network scenarios')\n",
" elif len(nw_scenarios) != 0:\n",
" # Wait for the MEC Sandbox is running\n",
" time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
" else:\n",
" logger.info('nw_scenarios: No scenario available')\n",
"\n",
" # Activate a network scenario based on a list of criterias (hard coded!!!)\n",
" if activate_network_scenario(sandbox) == -1:\n",
" logger.error('Failed to activate network scenario')\n",
" else:\n",
" # Wait for the MEC services are running\n",
" time.sleep(2 * STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
"\n",
" # Request for a new application instance identifier\n",
" app_inst_id = request_application_instance_id(sandbox)\n",
" if app_inst_id == None:\n",
" logger.error('Failed to request an application instance identifier')\n",
" # Wait for the MEC services are terminated\n",
" time.sleep(STABLE_TIME_OUT)\n",
" # Send READY confirmation\n",
" sub_id = None\n",
" if send_ready_confirmation(sandbox, app_inst_id) == -1:\n",
" logger.error('Failed to send confirm_ready')\n",
" else:\n",
" # Subscribe to AppTerminationNotificationSubscription\n",
" sub_id, res_url = send_subscribe_termination(sandbox, app_inst_id)\n",
" if sub_id == None:\n",
" logger.error('Failed to do the subscription')\n",
"\n",
" return (sandbox, app_inst_id, sub_id)\n",
" # End of function mec_app_setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The procedure for the termination of a MEC application\n",
"\n",
"This function provides the steps to terminate a MEC application.\n",
"NOTE: All subscriptions done outside of the mec_app_setup function are not deleted."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def mec_app_termination(sandbox: str, app_inst_id:swagger_client.models.ApplicationInfo, sub_id: str):\n",
" \"\"\"\n",
" This function provides the steps to setup a MEC application:\n",
" - Login\n",
" - Print sandbox identifier\n",
" - Print available network scenarios\n",
" - Activate a network scenario\n",
" - Request for a new application instance identifier\n",
" - Send READY confirmation\n",
" - Subscribe to AppTermination Notification\n",
" \"\"\"\n",
" # Delete AppTerminationNotification subscription\n",
" if sub_id is not None:\n",
" if delete_subscribe_termination(sandbox, app_inst_id, sub_id) == -1:\n",
" logger.error('Failed to delete the application instance identifier')\n",
"\n",
" # Delete the application instance identifier\n",
" if delete_application_instance_id(sandbox, app_inst_id.id) == -1:\n",
" logger.error('Failed to delete the application instance identifier')\n",
" # Wait for the MEC services are terminated\n",
" time.sleep(STABLE_TIME_OUT)\n",
"\n",
" # Deactivate a network scenario based on a list of criterias (hard coded!!!)\n",
" if deactivate_network_scenario(sandbox) == -1:\n",
" logger.error('Failed to deactivate network scenario')\n",
" else:\n",
" # Wait for the MEC services are terminated\n",
" time.sleep(2 * STABLE_TIME_OUT)\n",
"\n",
" # Logout\n",
" process_logout(sandbox)\n",
"\n",
" # End of function mec_app_termination"
]
},
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell descrbes the new basic MEC application architecture. It will be used in the rest of this titorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def process_main():\n",
" \"\"\"\n",
" This is the second sprint of our skeleton of our MEC application:\n",
" - Mec application setup\n",
" - Get UU unicast provisioning information\n",
" - Mec application termination\n",
" \"\"\" \n",
" global logger, nw_scenarios\n",
"\n",
" logger.debug('Starting at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" logger.debug('\\t pwd= ' + os.getcwd())\n",
"\n",
" # Setup the MEC application\n",
" (sandbox_name, app_inst_id, sub_id) = mec_app_setup()\n",
"\n",
" # Any processing here\n",
" logger.info('sandbox_name: ' + sandbox_name)\n",
" logger.info('app_inst_id: ' + app_inst_id.id)\n",
" if sub_id is not None:\n",
" logger.info('sub_id: ' + sub_id)\n",
" time.sleep(STABLE_TIME_OUT)\n",
"\n",
" # Terminate the MEC application\n",
" mec_app_termination(sandbox_name, app_inst_id, sub_id)\n",
"\n",
" logger.debug('Stopped at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" # End of function process_main\n",
"\n",
"if __name__ == '__main__':\n",
" process_main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create our second MEC application: how to use MEC Services\n",
"\n",
"After doing the logging, network scenario activation, MEC application instance creation steps, we are ready to exploit the MEC services exposed by the MEC Sandbox.\n",
"\n",
"In this clause, we use the following functionalities provided by MEC-030:\n",
"- Getting UU unicast provisioning information (ETSI GS MEC 030 Clause 5.5.1)\n",
"- Subscribe to the V2X message distribution server (ETSI GS MEC 030 Clause 5.5.7)\n",
"- Delete subscription\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Getting UU unicast provisioning information\n",
"\n",
"The purpose is to query provisioning information for V2X communication over Uu unicast."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def send_uu_unicast_provisioning_info(sandbox_name: str, ecgi: str) -> str:\n",
" global MEC_PLTF, logger\n",
" logger.debug('>>> send_uu_unicast_provisioning_info: ' + ecgi)\n",
" url = '/{sandbox_name}/{mec_pltf}/vis/v2/queries/uu_unicast_provisioning_info'\n",
" logger.debug('send_uu_unicast_provisioning_info: url: ' + url)\n",
" path_params = {}\n",
" path_params['sandbox_name'] = sandbox_name\n",
" path_params['mec_pltf'] = MEC_PLTF\n",
" query_params = []\n",
" query_params.append(('location_info', 'ecgi,' + ecgi))\n",
" header_params = {}\n",
" # HTTP header `Accept`\n",
" header_params['Accept'] = 'application/json' # noqa: E501\n",
" # HTTP header `Content-Type`\n",
" header_params['Content-Type'] = 'application/json' # noqa: E501\n",
" result = service_api.call_api(url, 'GET', header_params=header_params, path_params=path_params, query_params=query_params, async_req=False)\n",
" return result\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
" # End of function send_uu_unicast_provisioning_info"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create the our second MEC application.\n",
"The sequence is the following:\n",
"- Mec application setup\n",
"- Get UU unicast provisioning information\n",
"- Mec application termination\n",
"\n",
"Note that the UU unicast provisioning information is returned as a JSON string. To de-serialized it into a Python data structure, please refer to clause [Subscribing to V2X message distribution server](#subscribing_to_v2x_message_distribution_server)."
"cell_type": "code",
"execution_count": null,
"source": [
"def process_main():\n",
" \"\"\"\n",
" This is the second sprint of our skeleton of our MEC application:\n",
" - Mec application setup\n",
" - Get UU unicast provisioning information\n",
" - Mec application termination\n",
" \"\"\" \n",
" global logger, nw_scenarios\n",
"\n",
" logger.debug('Starting at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" logger.debug('\\t pwd= ' + os.getcwd())\n",
"\n",
" # Setup the MEC application\n",
" (sandbox_name, app_inst_id, sub_id) = mec_app_setup()\n",
" # Get UU unicast provisioning information\n",
" ecgi = \"268708941961,268711972264\" # List of ecgi spearated by a ','\n",
" result = send_uu_unicast_provisioning_info(sandbox_name, ecgi)\n",
" if result is None:\n",
" logger.error('Failed to get UU unicast provisioning information')\n",
" else:\n",
" logger.info('UU unicast provisioning information: ', str(result))\n",
" # Terminate the MEC application\n",
" mec_app_termination(sandbox_name, app_inst_id, sub_id)\n",
"\n",
" logger.debug('Stopped at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" # End of function process_main\n",
"\n",
"if __name__ == '__main__':\n",
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Subscribing to V2X message distribution server\n",
"\n",
"Here, we need to come back to the MEC 030 standard to create the type V2xMsgSubscription. It involves the creation of a set of basic types described below.\n",
"\n",
"Note: These new type shall be 'JSON\"serializable. It means that they have to implement the following methods:\n",
"```python\n",
"to_dict()\n",
"to_str()\n",
"__repr__()\n",
"__eq__()\n",
"__ne__()\n",
"```\n",
"`\r\n",
"}\r\n",
"\n"
]
},
"metadata": {},
"outputs": [],
"source": [
"class LinkType(object):\n",
" swagger_types = {'href': 'str'}\n",
" attribute_map = {'href': 'href'}\n",
" def __init__(self, href=None): # noqa: E501\n",
" self._href = None\n",
" if href is not None:\n",
" self._href = href\n",
" @property\n",
" def href(self):\n",
" return self._href\n",
" @href.setter\n",
" def href(self, href):\n",
" self._href = href\n",
" def to_dict(self):\n",
" result = {}\n",
" for attr, _ in six.iteritems(self.swagger_types):\n",
" value = getattr(self, attr)\n",
" if isinstance(value, list):\n",
" result[attr] = list(map(\n",
" lambda x: x.to_dict() if hasattr(x, 'to_dict') else x,\n",
" value\n",
" ))\n",
" elif hasattr(value, 'to_dict'):\n",
" result[attr] = value.to_dict()\n",
" elif isinstance(value, dict):\n",
" result[attr] = dict(map(\n",
" lambda item: (item[0], item[1].to_dict())\n",
" if hasattr(item[1], 'to_dict') else item,\n",
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
" value.items()\n",
" ))\n",
" else:\n",
" result[attr] = value\n",
" if issubclass(LinkType, dict):\n",
" for key, value in self.items():\n",
" result[key] = value\n",
" return result\n",
" def to_str(self):\n",
" return pprint.pformat(self.to_dict())\n",
" def __repr__(self):\n",
" return self.to_str()\n",
" def __eq__(self, other):\n",
" if not isinstance(other, LinkType):\n",
" return False\n",
" return self.__dict__ == other.__dict__\n",
" def __ne__(self, other):\n",
" return not self == other\n",
"\n",
"class Links(object):\n",
" swagger_types = {'self': 'LinkType'}\n",
" attribute_map = {'self': 'self'}\n",
" def __init__(self, self_=None): # noqa: E501\n",
" self._self = None\n",
" if self_ is not None:\n",
" self._self = self_\n",
" @property\n",
" def self_(self):\n",
" return self._self\n",
" @self_.setter\n",
" def self_(self, self_):\n",
" self._self = self_\n",
" def to_dict(self):\n",
" result = {}\n",
" for attr, _ in six.iteritems(self.swagger_types):\n",
" value = getattr(self, attr)\n",
" if isinstance(value, list):\n",
" result[attr] = list(map(\n",
" lambda x: x.to_dict() if hasattr(x, 'to_dict') else x,\n",
" value\n",
" ))\n",
" elif hasattr(value, 'to_dict'):\n",
" result[attr] = value.to_dict()\n",
" elif isinstance(value, dict):\n",
" result[attr] = dict(map(\n",
" lambda item: (item[0], item[1].to_dict())\n",
" if hasattr(item[1], 'to_dict') else item,\n",
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
" value.items()\n",
" ))\n",
" else:\n",
" result[attr] = value\n",
" if issubclass(Links, dict):\n",
" for key, value in self.items():\n",
" result[key] = value\n",
" return result\n",
" def to_str(self):\n",
" return pprint.pformat(self.to_dict())\n",
" def __repr__(self):\n",
" return self.to_str()\n",
" def __eq__(self, other):\n",
" if not isinstance(other, Links):\n",
" return False\n",
" return self.__dict__ == other.__dict__\n",
" def __ne__(self, other):\n",
" return not self == other\n",
"\n",
"class TimeStamp(object):\n",
" swagger_types = {'seconds': 'int', 'nano_seconds': 'int'}\n",
" attribute_map = {'seconds': 'seconds', 'nano_seconds': 'nanoSeconds'}\n",
" def __init__(self, seconds=None, nano_seconds=None): # noqa: E501\n",
" self._seconds = None\n",
" self._nano_seconds = None\n",
" if seconds is not None:\n",
" self._seconds = seconds\n",
" if nano_seconds is not None:\n",
" self._nano_seconds = nano_seconds\n",
" @property\n",
" def seconds(self):\n",
" return self._seconds\n",
" @seconds.setter\n",
" def seconds(self, seconds):\n",
" self._seconds = seconds\n",
" @property\n",
" def nano_seconds(self):\n",
" return self._nano_seconds\n",
" @nano_seconds.setter\n",
" def nano_seconds(self, nano_seconds):\n",
" self._nano_seconds = nano_seconds\n",
" def to_dict(self):\n",
" result = {}\n",
" for attr, _ in six.iteritems(self.swagger_types):\n",
" value = getattr(self, attr)\n",
" if isinstance(value, list):\n",
" result[attr] = list(map(\n",
" lambda x: x.to_dict() if hasattr(x, 'to_dict') else x,\n",
" value\n",
" ))\n",
" elif hasattr(value, 'to_dict'):\n",
" result[attr] = value.to_dict()\n",
" elif isinstance(value, dict):\n",
" result[attr] = dict(map(\n",
" lambda item: (item[0], item[1].to_dict())\n",
" if hasattr(item[1], 'to_dict') else item,\n",
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
" value.items()\n",
" ))\n",
" else:\n",
" result[attr] = value\n",
" if issubclass(TimeStamp, dict):\n",
" for key, value in self.items():\n",
" result[key] = value\n",
" return result\n",
" def to_str(self):\n",
" return pprint.pformat(self.to_dict())\n",
" def __repr__(self):\n",
" return self.to_str()\n",
" def __eq__(self, other):\n",
" if not isinstance(other, TimeStamp):\n",
" return False\n",
" return self.__dict__ == other.__dict__\n",
" def __ne__(self, other):\n",
" return not self == other"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Subscribing to V2X message distribution server\n",
"\n",
"The cell bellow implements the V2xMsgSubscription data structure.\"`\r\n",
"}\r\n",
{
"cell_type": "code",
"execution_count": 23,
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
"metadata": {},
"outputs": [],
"source": [
"class V2xMsgSubscription(object):\n",
" swagger_types = {'links': 'Links', 'callback_reference': 'str', 'filter_criteria': 'V2xMsgSubscriptionFilterCriteria', 'request_test_notification': 'bool', 'subscription_type': 'str'}\n",
" attribute_map = {'links': 'Links', 'callback_reference': 'callbackReference', 'filter_criteria': 'filterCriteria', 'request_test_notification': 'requestTestNotification', 'subscription_type': 'subscriptionType'}\n",
" def __init__(self, links=None, callback_reference=None, filter_criteria=None, request_test_notification=None): # noqa: E501\n",
" self._links = None\n",
" self._callback_reference = None\n",
" self._filter_criteria = None\n",
" self._request_test_notification = None\n",
" self._subscription_type = \"V2xMsgSubscription\"\n",
" if links is not None:\n",
" self.links = links\n",
" if callback_reference is not None:\n",
" self.callback_reference = callback_reference\n",
" if filter_criteria is not None:\n",
" self.filter_criteria = filter_criteria\n",
" if request_test_notification is not None:\n",
" self.request_test_notification = request_test_notification\n",
" @property\n",
" def links(self):\n",
" return self._links\n",
" @links.setter\n",
" def links(self, links):\n",
" self_.links = links\n",
" @property\n",
" def callback_reference(self):\n",
" return self._callback_reference\n",
" @callback_reference.setter\n",
" def callback_reference(self, callback_reference):\n",
" self._callback_reference = callback_reference\n",
" @property\n",
" def links(self):\n",
" return self._links\n",
" @links.setter\n",
" def links(self, links):\n",
" self._links = links\n",
" @property\n",
" def filter_criteria(self):\n",
" return self._filter_criteria\n",
" @filter_criteria.setter\n",
" def filter_criteria(self, filter_criteria):\n",
" self._filter_criteria = filter_criteria\n",
" @property\n",
" def request_test_notification(self):\n",
" return self._request_test_notification\n",
" @request_test_notification.setter\n",
" def request_test_notification(self, request_test_notification):\n",
" self._request_test_notification = request_test_notification\n",
" @property\n",
" def subscription_type(self):\n",
" return self._subscription_type\n",
" def to_dict(self):\n",
" result = {}\n",
" for attr, _ in six.iteritems(self.swagger_types):\n",
" value = getattr(self, attr)\n",
" if isinstance(value, list):\n",
" result[attr] = list(map(\n",
" lambda x: x.to_dict() if hasattr(x, 'to_dict') else x,\n",
" value\n",
" ))\n",
" elif hasattr(value, 'to_dict'):\n",
" result[attr] = value.to_dict()\n",
" elif isinstance(value, dict):\n",
" result[attr] = dict(map(\n",
" lambda item: (item[0], item[1].to_dict())\n",
" if hasattr(item[1], 'to_dict') else item,\n",
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
" value.items()\n",
" ))\n",
" else:\n",
" result[attr] = value\n",
" if issubclass(V2xMsgSubscription, dict):\n",
" for key, value in self.items():\n",
" result[key] = value\n",
" return result\n",
" def to_str(self):\n",
" return pprint.pformat(self.to_dict())\n",
" def __repr__(self):\n",
" return self.to_str()\n",
" def __eq__(self, other):\n",
" if not isinstance(other, V2xMsgSubscription):\n",
" return False\n",
" return self.__dict__ == other.__dict__\n",
" def __ne__(self, other):\n",
" return not self == other\n",
"\n",
"class V2xMsgSubscriptionFilterCriteria(object):\n",
" swagger_types = {'msg_type': 'list[str]', 'std_organization': 'str'}\n",
" attribute_map = {'msg_type': 'MsgType', 'std_organization': 'stdOrganization'}\n",
" def __init__(self, msg_type, std_organization): # noqa: E501\n",
" self._msg_type = None\n",
" self._std_organization = None\n",
" self.msg_type = msg_type\n",
" self.std_organization = std_organization\n",
" @property\n",
" def msg_type(self):\n",
" return self._msg_type\n",
" @msg_type.setter\n",
" def msg_type(self, msg_type):\n",
" self._msg_type = msg_type\n",
" @property\n",
" def std_organization(self):\n",
" return self._std_organization\n",
" @std_organization.setter\n",
" def std_organization(self, std_organization):\n",
" self._std_organization = std_organization\n",
" def to_dict(self):\n",
" result = {}\n",
" for attr, _ in six.iteritems(self.swagger_types):\n",
" value = getattr(self, attr)\n",
" if isinstance(value, list):\n",
" result[attr] = list(map(\n",
" lambda x: x.to_dict() if hasattr(x, 'to_dict') else x,\n",
" value\n",
" ))\n",
" elif hasattr(value, 'to_dict'):\n",
" result[attr] = value.to_dict()\n",
" elif isinstance(value, dict):\n",
" result[attr] = dict(map(\n",
" lambda item: (item[0], item[1].to_dict())\n",
" if hasattr(item[1], 'to_dict') else item,\n",
" value.items()\n",
" ))\n",
" else:\n",
" result[attr] = value\n",
" if issubclass(V2xMsgSubscriptionFilterCriteria, dict):\n",
" for key, value in self.items():\n",
" result[key] = value\n",
" return result\n",
" def to_str(self):\n",
" return pprint.pformat(self.to_dict())\n",
" def __repr__(self):\n",
" return self.to_str()\n",
" def __eq__(self, other):\n",
" if not isinstance(other, V2xMsgSubscriptionFilterCriteria):\n",
" return False\n",
" return self.__dict__ == other.__dict__\n",
" def __ne__(self, other):\n",
" return not self == other"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the V2X message subscription function. The body contains a 'JSON' serialized instance of the class V2xMsgSubscription."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def subscribe_v2x_message(sandbox_name: str, v2xMsgSubscription: V2xMsgSubscription) -> int:\n",
" global MEC_SANDBOX_URL, MEC_PLTF, CALLBACK_URI, logger\n",
"\n",
" logger.debug('>>> subscribe_v2x_message: v2xMsgSubscription: ' + str(v2xMsgSubscription))\n",
" url = '/{sandbox_name}/{mec_pltf}/vis/v2/subscriptions'\n",
" logger.debug('subscribe_v2x_message: url: ' + url)\n",
" path_params = {}\n",
" path_params['sandbox_name'] = sandbox_name\n",
" path_params['mec_pltf'] = MEC_PLTF\n",
" header_params = {}\n",
" # HTTP header `Accept`\n",
" header_params['Accept'] = 'application/json' # noqa: E501\n",
" # HTTP header `Content-Type`\n",
" header_params['Content-Type'] = 'application/json' # noqa: E501\n",
" (result, status, headers) = service_api.call_api(url, 'POST', header_params=header_params, path_params=path_params, body=v2xMsgSubscription, async_req=False)\n",
" return (extract_sub_id(headers['Location']), headers['Location'])\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
" # End of function subscribe_v2x_message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a generic function to delete any MEC service subscription based on the subscription resource URL provided in the Location header of the subscription creation response."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
"source": [
"def delete_mec_subscription(resource_url: str) -> int:\n",
" global logger\n",
"\n",
" logger.debug('>>> delete_mec_subscription: resource_url: ' + resource_url)\n",
" try:\n",
" res = urllib3.util.parse_url(resource_url)\n",
" if res is None:\n",
" logger.error('delete_mec_subscription: Failed to paerse URL')\n",
" return -1\n",
" header_params = {}\n",
" # HTTP header `Accept`\n",
" header_params['Accept'] = 'application/json' # noqa: E501\n",
" # HTTP header `Content-Type`\n",
" header_params['Content-Type'] = 'application/json' # noqa: E501\n",
" service_api.call_api(res.path, 'DELETE', header_params=header_params, async_req=False)\n",
" return 0\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
" return -1\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finaly, here is how to implement the V2X message subscription:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-10-01 12:51:59,320 - __main__ - DEBUG - Starting at 20241001-125159\n",
"2024-10-01 12:51:59,321 - __main__ - DEBUG - \t pwd= /home/yann/dev/jupyter/Sandbox/mecapp\n",
"2024-10-01 12:51:59,322 - __main__ - DEBUG - >>> process_login\n",
"2024-10-01 12:51:59,322 DEBUG Resetting dropped connection: mec-platform2.etsi.org\n",
"2024-10-01 12:51:59,505 DEBUG https://mec-platform2.etsi.org:443 \"POST /sandbox-api/v1/login?provider=Jupyter2024 HTTP/1.1\" 201 48\n",
"2024-10-01 12:51:59,506 DEBUG response body: b'{\"user_code\":\"sbx5bl4at0\",\"verification_uri\":\"\"}'\n",
"2024-10-01 12:51:59,507 - __main__ - DEBUG - process_login (step1): oauth: {'user_code': 'sbx5bl4at0', 'verification_uri': ''}\n",
"2024-10-01 12:51:59,507 - __main__ - DEBUG - =======================> DO AUTHORIZATION WITH CODE : sbx5bl4at0\n",
"2024-10-01 12:51:59,508 - __main__ - DEBUG - =======================> DO AUTHORIZATION HERE : \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"send: b'POST /sandbox-api/v1/login?provider=Jupyter2024 HTTP/1.1\\r\\nHost: mec-platform2.etsi.org\\r\\nAccept-Encoding: identity\\r\\nContent-Length: 2\\r\\nAccept: application/json\\r\\nContent-Type: application/json\\r\\nUser-Agent: Swagger-Codegen/1.0.0/python\\r\\n\\r\\n'\n",
"send: b'{}'\n",
"reply: 'HTTP/1.1 201 Created\\r\\n'\n",
"header: Date: Tue, 01 Oct 2024 10:51:58 GMT\n",
"header: Content-Type: application/json; charset=UTF-8\n",
"header: Content-Length: 48\n",
"header: Connection: keep-alive\n",
"header: Strict-Transport-Security: max-age=15724800; includeSubDomains\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-10-01 12:52:02,541 DEBUG https://mec-platform2.etsi.org:443 \"GET /sandbox-api/v1/namespace?user_code=sbx5bl4at0 HTTP/1.1\" 200 29\n",
"2024-10-01 12:52:02,543 DEBUG response body: b'{\"sandbox_name\":\"sbx5bl4at0\"}'\n",
"2024-10-01 12:52:02,546 - __main__ - DEBUG - process_login (step2): result: {'sandbox_name': 'sbx5bl4at0'}\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"send: b'GET /sandbox-api/v1/namespace?user_code=sbx5bl4at0 HTTP/1.1\\r\\nHost: mec-platform2.etsi.org\\r\\nAccept-Encoding: identity\\r\\nAccept: application/json\\r\\nContent-Type: application/json\\r\\nUser-Agent: Swagger-Codegen/1.0.0/python\\r\\n\\r\\n'\n",
"reply: 'HTTP/1.1 200 OK\\r\\n'\n",
"header: Date: Tue, 01 Oct 2024 10:52:01 GMT\n",
"header: Content-Type: application/json; charset=UTF-8\n",
"header: Content-Length: 29\n",
"header: Connection: keep-alive\n",
"header: Strict-Transport-Security: max-age=15724800; includeSubDomains\n"
]
},
{
"name": "stderr",
"output_type": "stream",