Newer
Older
" dict_body = {}\n",
" dict_body['indication'] = 'READY'\n",
" service_api.call_api(url, 'POST', header_params=header_params, path_params = path_params, body=dict_body, 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",
" # End of function send_ready_confirmation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition, our MEC application is registering to AppTerminationNotificationSubscription and it needs to delete its subscription when terminating.\n",
"\n",
"At this stage, it is important to note that all subscription deletion use the same format: <subscription URL>/<subscription identifier> (see ETSI MEC GS 003 [16]). \n",
"In this case, it the AppTerminationNotificationSubscription is 'sub-1234', the URIs to do the susbscription and to delete it are:\n",
"- MEC_SANDBOX_URL + '/' + sandbox_name + '/' + MEC_PLTF + '/mec_app_support/v2/applications/' + app_inst_id + '/subscriptions'\n",
"- MEC_SANDBOX_URL + '/' + sandbox_name + '/' + MEC_PLTF + '/mec_app_support/v2/applications/' + app_inst_id + '/subscriptions/sub-1234'\n",
"\n",
"So, it will be usefull to create a small function to extract the subscription identifier from either the HTTP Location header or from the Link field found into the reponse body data structure. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Subscribing to application termination\n",
"\n",
"The purpose is to create a new subscription to \n",
"the MEC application termination notification as describe in ETSI GS MEC 011 V3.2.1 (2024-04) Clause 5.2.6b Receiving event notifications on MEC application instance \n",
"terminations"
"metadata": {},
"outputs": [],
"source": [
"def send_subscribe_termination(sandbox_name: str, app_inst_id: swagger_client.models.application_info.ApplicationInfo) -> object:\n",
" \"\"\"\n",
" Subscribe to the MEC application termination notifications.\n",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :param app_inst_id: The MEC application instance identifier\n",
" :return: The HTTP respone, the subscription ID and the resource URL on success, None otherwise\n",
" :see ETSI GS MEC 011 V3.2.1 (2024-04) Clause 5.2.6b Receiving event notifications on MEC application instance termination\n",
" \"\"\"\n",
" global MEC_PLTF, logger, service_api\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'] = CALLBACK_URI + '/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 (result, 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 send_subscribe_termination"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extracting subscription identifier\n",
"\n",
"This helper function extracts the subscription identifier from any subscription URL."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"def extract_sub_id(resource_url: str) -> str:\n",
" \"\"\"\n",
" Extract the subscription identifier from the specified subscription URL.\n",
" :param resource_url: The subscription URL\n",
" :return: The subscription identifier on success, None otherwise\n",
" \"\"\"\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",
" return None\n",
" # End of function extract_sub_id"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Delete subscription to application termination"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"def delete_subscribe_termination(sandbox_name: str, app_inst_id: swagger_client.models.application_info.ApplicationInfo, sub_id: str) -> int:\n",
" \"\"\"\n",
" Delete the subscrition to the AppTermination notification.\n",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :param app_inst_id: The MEC application instance identifier\n",
" :param sub_id: The subscription identifier\n",
" :return: 0 on success, -1 otherwise\n",
" \"\"\"\n",
" global MEC_PLTF, logger, service_api\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": [
"When the MEC application instance is notified to gracefully terminate, it provides to the MEC platform \n",
"that the application has completed its application level related terminate/stop actiono\n",
"\n",
"Reference: ETSI GS MEC 011 V3.2.1 (2024-04) Clause 5.2.3 MEC application graceful termination/stopp."
]
},
{
"cell_type": "code",
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
"metadata": {},
"outputs": [],
"source": [
"def send_termination_confirmation(sandbox_name: str, app_inst_id: swagger_client.models.application_info.ApplicationInfo) -> int:\n",
" \"\"\"\n",
" Send the confirm_termination to indicate that the MEC application is terminating gracefully.\n",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :param app_inst_id: The MEC application instance identifier\n",
" :return: 0 on success, -1 otherwise\n",
" :see ETSI GS MEC 011 V3.2.1 (2024-04) Clause 5.2.3 MEC application graceful termination/stop\n",
" \"\"\"\n",
" global MEC_PLTF, logger, service_api\n",
"\n",
" logger.debug('>>> send_termination_confirmation: ' + app_inst_id.id)\n",
" try:\n",
" url = '/{sandbox_name}/{mec_pltf}/mec_app_support/v2/applications/{app_inst_id}/confirm_termination'\n",
" logger.debug('send_termination_confirmation: 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",
" # JSON indication READY\n",
" dict_body = {}\n",
" dict_body['operationAction'] = 'TERMINATING'\n",
" service_api.call_api(url, 'POST', header_params=header_params, path_params = path_params, body=dict_body, 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",
" # End of function send_termination_confirmation"
]
},
{
"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",
"# Uncomment the ;line above to skip execution of this cell\n",
"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, nw_scenario_idx\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",
" result, 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",
" data = json.loads(result.data)\n",
" logger.info('data: ' + str(data))\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",
"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",
" :return The MEC Sandbox instance, the MEC application instance identifier and the subscription identifier on success, None otherwise\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 None\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",
" result, 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",
"metadata": {},
"outputs": [],
"source": [
"def mec_app_termination(sandbox_name: 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",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :param app_inst_id: The MEC application instance identifier\n",
" :param sub_id: The subscription identifier\n",
" global logger\n",
"\n",
" # Delete AppTerminationNotification subscription\n",
" if sub_id is not None:\n",
" if delete_subscribe_termination(sandbox_name, 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_name, 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_name) == -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",
" process_logout(sandbox_name)\n",
" # End of function mec_app_termination"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell describes 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": [
"# Uncomment the ;line above to skip execution of this cell\n",
"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\n",
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
"\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 Location Service\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-013 LocationAPIs:\n",
"- Getting UE location lookup (ETSI GS MEC 013 Clause 5.3.2 UE Location Lookup)\n",
"- Subscribe to the UE Location changes (ETSI GS MEC 030 Clause 5.3.4)\n",
"- Delete subscription\n",
"\n",
"### First step: Getting UE location lookup\n",
"\n",
"First of all, just create a function to request the location of a specific UE. The UE identifier depends of the network scenario which was activated. In our example, we are using the 4g-5g-macro-v2x network scenario and we will run our code with the high velocity UE (i.e., the cars such as 10.100.0.1).\n"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"def get_ue_location(sandbox_name: str, ue: str) -> object:\n",
" \"\"\"\n",
" To retrieves the location information of an UE identified by its IPv4 address (e.g. 10.100.0.1)\n",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :return The HTTP response and the response status on success, None otherwise\n",
" :see ETSI GS MEC 013 V3.1.1 (2023-01) Clause 5.3.2 UE Location Lookup\n",
" \"\"\"\n",
" global MEC_PLTF, logger, service_api\n",
"\n",
" logger.debug('>>> get_ue_location: ' + ue)\n",
" try:\n",
" url = '/{sandbox_name}/{mec_pltf}/location/v3/queries/users'\n",
" logger.debug('get_ue_location: 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(('address', ue))\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, 'GET', header_params=header_params, path_params=path_params, query_params=query_params, async_req=False)\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
" # End of function get_ue_location"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create our new MEC application. The expected result looks like:\n",
"```json\n",
"{\n",
" \"userList\": {\n",
" \"resourceURL\": \"https://mec-platform2.etsi.org/xxx/mep1/location/v3/queries/users\",\n",
" \"user\": [\n",
" {\n",
" \"address\": \"10.100.0.1\",\n",
" \"accessPointId\": \"4g-macro-cell-6\",\n",
" \"zoneId\": \"zone03\",\n",
" \"resourceURL\": \"https://mec-platform.etsi.org/xxx/mep1/location/v3/queries/users?address=10.100.0.1\",\n",
" \"timestamp\": {\n",
" \"nanoSeconds\": 0,\n",
" \"seconds\": 1729245754\n",
" },\n",
" \"locationInfo\": {\n",
" \"latitude\": [\n",
" 43.73707\n",
" ],\n",
" \"longitude\": [\n",
" 7.422555\n",
" ],\n",
" \"shape\": 2\n",
" },\n",
" \"civicInfo\": {\n",
" \"country\": \"MC\"\n",
" },\n",
" \"relativeLocationInfo\": {\n",
" \"X\": 630.37036,\n",
" \"Y\": 261.92648,\n",
" \"mapInfo\": {\n",
" \"mapId\": \"324561243\",\n",
" \"origin\": {\n",
" \"latitude\": 43.7314,\n",
" \"longitude\": 7.4202\n",
" }\n",
" }\n",
" }\n",
" }\n",
" ]\n",
" }\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Putting everything together\n",
"\n",
"It is time now to create the our third iteration of our MEC application.\n",
"\n",
"The sequence is the following:\n",
"- Login\n",
"- Activate a network scenario\n",
"- Getting UE location lookup (ETSI GS MEC 013 Clause 5.3.2)\n",
"- Delete our application instance identifier\n",
"- Deactivate a network scenario\n",
"- Logout"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Uncomment the ;line above to skip execution of this cell\n",
"def process_main():\n",
" \"\"\"\n",
" This is the second sprint of our skeleton of our MEC application:\n",
" - Login\n",
" - Activate a network scenario\n",
" - Getting UE location lookup (ETSI GS MEC 013 Clause 5.3.2)\n",
" - Delete our application instance identifier\n",
" - Deactivate a network scenario\n",
" - Logout\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",
" # Getting UE location lookup\n",
" result, status = get_ue_location(sandbox_name, '10.100.0.1')\n",
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
" logger.info('UE location information: status: %s', str(status))\n",
" if status != 200:\n",
" logger.error('Failed to get UE location information')\n",
" else:\n",
" logger.info('UE location information: %s', str(result.data))\n",
"\n",
" # Any processing comes here\n",
" logger.info('body: ' + str(result.data))\n",
" data = json.loads(result.data)\n",
" logger.info('UserList: ' + str(data))\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": [
"### Second step: Adding a subscription\n",
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
"\n",
"The purpose here is to create a subscritpion when the UE (e.g. 10.100.0.1) is entering or leaving a PoA area (PoA is tanding for Piont of Access).\n",
"Accorcding to ETSI GS MEC 013 V3.1.1 Clause 7.5.3.4 POST, the 'request method is a POST and the endpoint is '/location/v3/subscriptions/users/'.\n",
"\n",
"The cell below provides the code to create our subscription."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Notification support\n",
"\n",
"To recieve notification, our MEC application is required to support an HTTP listenener to recieve POST requests from the MEC Sandbox and reply to them: this is the notification mechanism.\n",
"\n",
"This minimalistic HTTP server will also be used to implement the endpoints provided by our MEC application service: see chapter [Our third MEC application: how to create a new MEC Services](#our_third_mec_application_how_to_create_a_new_mec_services).\n",
"\n",
"The class HTTPRequestHandler (see cell below) provides the suport of such mechanism.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"class HTTPServer_RequestHandler(BaseHTTPRequestHandler):\n",
" \"\"\"\n",
" Minimal implementation of an HTTP server (http only).\n",
" \"\"\"\n",
"\n",
" def do_GET(self):\n",
" logger.info('>>> do_GET: ' + self.path)\n",
"\n",
" ctype = self.headers.get('content-type')\n",
" logger.info('do_GET: ' + ctype)\n",
"\n",
" if self.path == '/sandbox/v1/statistic/v1/quantity':\n",
" logger.info('do_GET: Computing statistic quantities for application MEC service')\n",
" # TODO Add logit to our MEC service\n",
" message = '{\"time\":20180124,\"avg\": 0.0,\"max\": 0.0,\"min\": 0.0,\"stddev\": 0.0 }'\n",
" else:\n",
" # Send error message\n",
" message = '{\"title\":\"Unknown URI\",\"type\":\"do_GET.parser\",\"status\":404 }'\n",
" logger.info('do_GET: message: ' + message)\n",
" \n",
" # Send response status code\n",
" self.send_response(HTTPStatus.OK)\n",
"\n",
" # Send headers\n",
" self.send_header('Content-type','text/plain; charset=utf-8')\n",
" self.send_header('Content-length', str(len(message)))\n",
" self.end_headers()\n",
"\n",
" # Write content as utf-8 data\n",
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
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
" return\n",
" # End of function do_GET\n",
"\n",
" def do_POST(self):\n",
" global got_notification\n",
"\n",
" logger.info('>>> do_POST: ' + self.path)\n",
"\n",
" ctype = self.headers.get('content-type')\n",
" logger.info('do_POST: ' + ctype)\n",
"\n",
" content_len = int(self.headers.get('Content-Length'))\n",
" if content_len != 0:\n",
" body = self.rfile.read(content_len).decode('utf8')\n",
" logger.info('do_POST: body:' + str(type(body)))\n",
" logger.info('do_POST: body:' + str(body))\n",
" data = json.loads(str(body))\n",
" logger.info('do_POST: data: %s', str(data))\n",
"\n",
" self.send_response(HTTPStatus.NOT_IMPLEMENTED)\n",
" self.end_headers()\n",
" got_notification = True\n",
" return\n",
" # End of function do_POST\n",
"\n",
" def do_PUT(self):\n",
" logger.info('>>> do_PUT: ' + self.path)\n",
"\n",
" ctype = self.headers.get('content-type')\n",
" logger.info('do_PUT: ' + ctype)\n",
"\n",
" self.send_response(HTTPStatus.NOT_IMPLEMENTED)\n",
" self.end_headers()\n",
" return\n",
" # End of function do_PUT\n",
"\n",
" def do_PATCH(self):\n",
" logger.info('>>> do_PATCH: ' + self.path)\n",
"\n",
" ctype = self.headers.get('content-type')\n",
" logger.info('do_PATCH: ' + ctype)\n",
" \n",
" self.send_response(HTTPStatus.NOT_IMPLEMENTED)\n",
" self.end_headers()\n",
" return\n",
" # End of function do_PATCH\n",
" # End of class HTTPRequestHandler\n",
"\n",
" def do_DELETE(self):\n",
" logger.info('>>> do_DELETE: ' + self.path)\n",
"\n",
" ctype = self.headers.get('content-type')\n",
" logger.info('do_DELETE: ' + ctype)\n",
" \n",
" self.send_response(HTTPStatus.NOT_IMPLEMENTED)\n",
" self.end_headers()\n",
" return\n",
" # End of function do_DELETE\n",
" # End of class HTTPRequestHandler\n",
"\n",
"def start_notification_server() -> HTTPServer:\n",
" \"\"\"\n",
" Start the notification server\n",
" :return The instance of the HTTP server\n",
" \"\"\"\n",
" global LISTENER_PORT, got_notification, logger\n",
" logger.debug('>>> start_notification_server on port ' + str(LISTENER_PORT))\n",
" got_notification = False\n",
" server_address = ('', LISTENER_PORT)\n",
" httpd = HTTPServer(server_address, HTTPServer_RequestHandler)\n",
" # Start notification server in a daemonized thread\n",
" notification_server = threading.Thread(target = httpd.serve_forever, name='notification_server')\n",
" notification_server.daemon = True\n",
" notification_server.start()\n",
" return httpd\n",
" # End of function HTTPRequestHandler\n",
"\n",
"def stop_notification_server(httpd: HTTPServer):\n",
" \"\"\"\n",
" Stop the notification server\n",
" :param The instance of the HTTP server\n",
" \"\"\"\n",
" global logger\n",
"\n",
" logger.debug('>>> stop_notification_server')\n",
" httpd.server_close()\n",
" httpd=None\n",
" # End of function HTTPRequestHandler\n"
]
},
{
"cell_type": "code",
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
"metadata": {},
"outputs": [],
"source": [
"def subscribe_for_user_events(sandbox_name: str) -> object:\n",
" \"\"\"\n",
" Subscriptions for notifications related to location.\n",
" :param sandbox_name: The MEC Sandbox instance to use\n",
" :return: The HTTP respone, the subscription ID and the resource URL on success, None otherwise\n",
" :see ETSI GS MEC 013 V3.1.1 Clause 7.5 Resource: user_subscriptions\n",
" \"\"\"\n",
" global MEC_PLTF, logger, service_api\n",
"\n",
" logger.debug('>>> subscribe_for_user_events: ' + sandbox_name)\n",
" try:\n",
" url = '/{sandbox_name}/{mec_pltf}//location/v3/subscriptions/users'\n",
" logger.debug('subscribe_for_user_events: 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",
" # Body\n",
" dict_body = {}\n",
" dict_body['subscriptionType'] = 'UserLocationEventSubscription'\n",
" dict_body['callbackReference'] = CALLBACK_URI + '/mec013/v3/location' # FIXME To be parameterized\n",
" dict_body['address'] = '10.100.0.1' # FIXME To be parameterized\n",
" dict_body['clientCorrelator'] = \"12345\"\n",
" dict_body['locationEventCriteria'] = [ \"ENTERING_AREA_EVENT\", \"LEAVING_AREA_EVENT\"]\n",
" m = {}\n",
" m[\"userLocationEventSubscription\"] = dict_body\n",
" (result, status, headers) = service_api.call_api(url, 'POST', header_params=header_params, path_params = path_params, body=m, async_req=False)\n",
" return (result, extract_sub_id(headers['Location']), headers['Location'])\n",
" except ApiException as e:\n",
" logger.error('Exception when calling call_api: %s\\n' % e)\n",
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
" # End of function subscribe_for_user_are_event"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Putting everything together\n",
"\n",
"It is time now to create the our third iteration of our MEC application.\n",
"\n",
"The sequence is the following:\n",
"- Login\n",
"- Activate a network scenario\n",
"- Create subscription\n",
"- Wait for notification\n",
"- Delete our application instance identifier\n",
"- Deactivate a network scenario\n",
"- Logout"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true
},
"# Uncomment the ;line above to skip execution of this cell\n",
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
"def process_main():\n",
" \"\"\"\n",
" This is the second sprint of our skeleton of our MEC application:\n",
" - Login\n",
" - Activate a network scenario\n",
" - Create subscription\n",
" - Wait for notification\n",
" - Delete our application instance identifier\n",
" - Deactivate a network scenario\n",
" - Logout\n",
" \"\"\" \n",
" global logger, nw_scenarios, got_notification\n",
"\n",
" logger.debug('Starting at ' + time.strftime('%Y%m%d-%H%M%S'))\n",
" logger.debug('\\t pwd= ' + os.getcwd())\n",
"\n",
" # Start notification server in a daemonized thread\n",
" httpd = start_notification_server()\n",
"\n",
" # Setup the MEC application\n",
" (sandbox_name, app_inst_id, sub_id) = mec_app_setup()\n",
"\n",
" # Create subscription\n",
" result, status, headers = subscribe_for_user_events(sandbox_name)\n",
" logger.info('UE location information: status: %s', str(status))\n",
" if status != 200:\n",
" logger.error('Failed to get UE location information')\n",
" else:\n",
" logger.info('UE location information: ' + str(result.data))\n",
" \n",
" # Getting UE location lookup\n",
" result, status = get_ue_location(sandbox_name, '10.100.0.1')\n",
" logger.info('UE location information: status: ' + str(status))\n",
" if status != 200:\n",
" logger.error('Failed to get UE location information')\n",
" else:\n",
" logger.info('UE location information: ' + str(result.data))\n",
"\n",
" # Wait for the notification\n",
" counter = 0\n",
" while not got_notification and counter < 30:\n",
" logger.info('Waiting for subscription...')\n",
" time.sleep(STABLE_TIME_OUT)\n",
" counter += 1\n",
" # End of 'while' statement\n",
"\n",
" # Stop notification server\n",
" stop_notification_server(httpd)\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 third MEC application: how to use V2X MEC Services\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",