Skip to content
MEC application.ipynb 57 KiB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is time now to create the our third 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",
Yann Garcia's avatar
Yann Garcia committed
    "- Send READY confirmation\n",
    "- Subscribe to AppTerminationNotificationSubscription\n",
Yann Garcia's avatar
Yann Garcia committed
    "- Get MEC services\n",
    "- Check list of services \n",
    "- Delete our application instance identifier\n",
Yann Garcia's avatar
Yann Garcia committed
    "- 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",
Yann Garcia's avatar
Yann Garcia committed
    "        - Send READY confirmation\n",
    "        - Get MEC services\n",
    "        - Send Termination\n",
    "        - Delete our application instance identifier\n",
Yann Garcia's avatar
Yann Garcia committed
    "        - Deactivate a network scenario\n",
    "        - Logout\n",
    "        - Check that logout is effective\n",
    "    \"\"\" \n",
    "    global PROVIDER, MEC_SANDBOX_API_URL, 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",
    "    # 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",
Yann Garcia's avatar
Yann Garcia committed
    "    # 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",
    "        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",
    "    else:\n",
    "        logger.info(\"Network scenario activated: \" + nw_scenarios[nw_scenario_idx].id)\n",
    "        # Wait for the MEC services are running\n",
    "        time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
    "\n",
    "    # Request for a new application instance identifier\n",
Yann Garcia's avatar
Yann Garcia committed
    "    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",
Yann Garcia's avatar
Yann Garcia committed
    "    else:\n",
    "        logger.info(\"app_inst_id: %s\", str(type(app_inst_id)))\n",
    "        logger.info(\"app_inst_id: %s\", str(app_inst_id))\n",
    "\n",
    "    # Send READY confirmation\n",
    "    send_ready_confirmation(app_inst_id)\n",
    "\n",
    "    # Subscribe to AppTerminationNotificationSubscription\n",
    "    send_subscribe_termination(sandbox, app_inst_id)\n",
    "\n",
Yann Garcia's avatar
Yann Garcia committed
    "    # Check list of services\n",
    "\n",
    "    # Delete the application instance identifier\n",
Yann Garcia's avatar
Yann Garcia committed
    "    if delete_application_instance_id(sandbox) == -1:\n",
    "        logger.error(\"Failed to delete the application instance identifier\")\n",
Yann Garcia's avatar
Yann Garcia committed
    "    else:\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",
    "    else:\n",
    "        logger.info(\"Network scenario deactivated: \" + nw_scenarios[nw_scenario_idx].id)\n",
    "        # Wait for the MEC services are terminated\n",
    "        time.sleep(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",
    "  \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()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## How to use MEC Services\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 V2X message distribution server (ETSI GS MEC 030 Clause 5.5.7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Getting UU unicast provisioning information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def send_uu_unicast_provisioning_info(sandbox_name str, ecgi: str) -> int:\n",
    "    global MEC_SANDBOX_URL, MEC_PLTF, logger\n",
    "\n",
    "    try:\n",
    "        url = MEC_SANDBOX_URL + '/' + sandbox_name + '/' + MEC_PLTF + '/vis/v2/queries/uu_unicast_provisioning_info?location_info=ecgi,\" + ecgi\n",
    "        header_params = {}\n",
    "        # HTTP header `Accept`\n",
    "        header_params['Accept'] = self.api_client.select_header_accept(['application/json'])  # noqa: E501\n",
    "        # HTTP header `Content-Type`\n",
    "        header_params['Content-Type'] = self.api_client.select_header_accept(['application/json'])  # noqa: E501\n",
    "        result = api.call_api(url, 'POST', header_params=header_params, async_req=False)\n",
    "        return result\n",
    "    except ApiException as e:\n",
    "        logger.error(\"Exception when calling call_api: %s\\n\" % e)\n",
    "    return Node\n",
    "    # End of function send_uu_unicast_provisioning_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "let's create the our new 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",
    "- Send READY confirmation\n",
    "- Get UU unicast provisioning information (ETSI GS MEC 030 Clause 5.5.1)\n",
    "- Delete our application instance identifier\n",
    "- Deactivate a network scenario\n",
    "- Logout\n",
    "- Check that logout is effective"
   ]
  },
  {
   "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",
    "        - Send READY confirmation\n",
    "        - Get MEC services\n",
    "        - Send Termination\n",
    "        - Delete our application instance identifier\n",
    "        - Deactivate a network scenario\n",
    "        - Logout\n",
    "        - Check that logout is effective\n",
    "    \"\"\" \n",
    "    global PROVIDER, MEC_SANDBOX_API_URL, 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",
    "    # 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",
    "    elif len(nw_scenarios) != 0:\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",
    "    else:\n",
    "        logger.info(\"Network scenario activated: \" + nw_scenarios[nw_scenario_idx].id)\n",
    "        # Wait for the MEC services are running\n",
    "        time.sleep(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",
    "    else:\n",
    "        logger.info(\"app_inst_id: %s\", str(type(app_inst_id)))\n",
    "        logger.info(\"app_inst_id: %s\", str(app_inst_id))\n",
    "\n",
    "    ecgi = \"C33139970001614,33139971112725\" # 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: \", result)\n",
    "\n",
    "    # Delete the application instance identifier\n",
    "    if delete_application_instance_id(sandbox) == -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",
    "\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",
    "        logger.info(\"Network scenario deactivated: \" + nw_scenarios[nw_scenario_idx].id)\n",
    "        # Wait for the MEC services are terminated\n",
    "        time.sleep(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",
    "  \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()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Subscribing to V2X message distribution server\n",
    "\n",
    "\n"
   ]
  },
Yann Garcia's avatar
Yann Garcia committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notification support\n",
Yann Garcia's avatar
Yann Garcia committed
    "\n",
    "To recieve notifcation, our MEC application is required to support an HTTP listenener to recieve POST request from the MEC Sandbox and replto repry to them: this is the notification mechanism.\n",
    "\n",
    "The class HTTPRequestHandler (see cell below) provides the suport of such mechanism.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class HTTPRequestHandler(BaseHTTPRequestHandler):\n",
    "\n",
    "    def do_POST(self):\n",
    "        if re.search(CALLBACK_URI, self.path):\n",
    "            ctype, pdict = _parse_header(self.headers.get(\"content-type\"))\n",
    "            if ctype == \"application/json\":\n",
    "                length = int(self.headers.get(\"content-length\"))\n",
    "                rfile_str = self.rfile.read(length).decode(\"utf8\")\n",
    "                data = parse.parse_qs(rfile_str, keep_blank_values=True)\n",
    "                record_id = self.path.split(\"/\")[-1]\n",
    "                LocalData.records[record_id] = data\n",
    "                print(\"addrecord %s: %s\" % (record_id, data))\n",
    "                self.send_response(HTTPStatus.OK)\n",
    "            else:\n",
    "                self.send_response(HTTPStatus.BAD_REQUEST, 'Only application/json is supported')\n",
    "        else:\n",
    "            self.send_response(HTTPStatus.BAD_REQUEST, 'Unsupported URI')\n",
    "        self.end_headers()\n",
    "\n",
    "    def do_GET(self):\n",
    "        self.send_response(HTTPStatus.BAD_REQUEST)\n",
    "        self.end_headers()\n",
    "    # End of class HTTPRequestHandler\n",
    "\n",
    "class LocalData(object):\n",
    "    records = {}\n",
    "    # End of class LocalData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process():\n",
    "    # Start notification server in a daemonized thread\n",
    "    notification_server = threading.Thread(name='notification_server', target=start_server, args=(LISTENER_IP, LISTENER_PORT))\n",
    "    notification_server.setDaemon(True) # Set as a daemon so it will be killed once the main thread is dead.\n",
    "    notification_server.start()\n",
    "    # Continue\n"
   ]
  },
Yann Garcia's avatar
Yann Garcia committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Annexes\n",
    "\n",
    "## Annex A: How to use an existing MEC sandbox instance\n",
    "\n",
    "TODO\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bibliography\n",
Yann Garcia's avatar
Yann Garcia committed
    "\n",
    "1. ETSI GS MEC 002 (V2.2.1) (01-2022): \"Multi-access Edge Computing (MEC); Phase 2: Use Cases and Requirements\".\n",
    "2. ETSI GS MEC 010-1 (V1.1.1) (10-2017): \"Mobile Edge Computing (MEC); Mobile Edge Management; Part 1: System, host and platform management\".\n",
    "3. ETSI GS MEC 010-2 (V2.2.1) (02-2022): \"Multi-access Edge Computing (MEC); MEC Management; Part 2: Application lifecycle, rules and requirements management\".\n",
    "4. ETSI GS MEC 011 (V3.1.1) (09-2022): \"Multi-access Edge Computing (MEC); Edge Platform Application Enablement\".\n",
    "5. ETSI GS MEC 012 (V2.2.1) (02-2022): \"Multi-access Edge Computing (MEC); Radio Network Information API\".\n",
    "6. ETSI GS MEC 013 (V2.2.1) (01-2022): \"Multi-access Edge Computing (MEC); Location API\".\n",
    "7. ETSI GS MEC 014 (V2.1.1) (03-2021): \"Multi-access Edge Computing (MEC); UE Identity API\".\n",
    "8. ETSI GS MEC 015 (V2.1.1) (06-2020): \"Multi-Access Edge Computing (MEC); Traffic Management APIs\".\n",
    "9. ETSI GS MEC 016 (V2.2.1) (04-2020): \"Multi-access Edge Computing (MEC); Device application interface\".\n",
    "10. ETSI GS MEC 021 (V2.2.1) (02-2022): \"Multi-access Edge Computing (MEC); Application Mobility Service API\".\n",
    "11. ETSI GS MEC 028 (V2.3.1) (07-2022): \"Multi-access Edge Computing (MEC); WLAN Access Information API\".\n",
    "12. ETSI GS MEC 029 (V2.2.1) (01-2022): \"Multi-access Edge Computing (MEC); Fixed Access Information API\".\n",
    "13. ETSI GS MEC 030 (V3.2.1) (05-2022): \"Multi-access Edge Computing (MEC); V2X Information Service API\".\n",
Yann Garcia's avatar
Yann Garcia committed
    "14. ETSI GR MEC-DEC 025 (V2.1.1) (06-2019): \"Multi-access Edge Computing (MEC); MEC Testing Framework\".\n",
    "15. ETSI GR MEC 001 (V3.1.1) (01-2022): \"Multi-access Edge Computing (MEC); Terminology\".\n",
    "16. [The Wiki MEC web site](https://www.etsi.org/technologies/multi-access-edge-computing)\n",
    "17. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}