Skip to content
MEC application.ipynb 62.8 KiB
Newer Older
      "2024-06-19 08:33:49,262 - __main__ - DEBUG - >>> process_logout: sandbox=sbx0rphiwx\n",
      "2024-06-19 08:33:49,265 DEBUG Resetting dropped connection: mec-platform.etsi.org\n",
      "/usr/lib/python3/dist-packages/urllib3/connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'mec-platform.etsi.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n",
      "2024-06-19 08:33:49,416 DEBUG https://mec-platform.etsi.org:443 \"POST /sandbox-api/v1/logout?sandbox_name=sbx0rphiwx HTTP/1.1\" 204 0\n",
      "2024-06-19 08:33:49,418 DEBUG response body: b''\n",
      "2024-06-19 08:33:49,420 - __main__ - DEBUG - To check that logout is effective, verify on the MEC Sandbox server that the MEC Sandbox is removed (kubectl get pods -A\n",
      "2024-06-19 08:33:49,421 - __main__ - DEBUG - Stopped at 20240619-083349\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "send: b'POST /sandbox-api/v1/logout?sandbox_name=sbx0rphiwx HTTP/1.1\\r\\nHost: mec-platform.etsi.org\\r\\nAccept-Encoding: identity\\r\\nContent-Length: 2\\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 204 No Content\\r\\n'\n",
      "header: Date: Wed, 19 Jun 2024 06:33:48 GMT\n",
      "header: Content-Type: application/json; charset=UTF-8\n",
      "header: Connection: keep-alive\n",
      "header: Strict-Transport-Security: max-age=15724800; includeSubDomains\n"
     ]
    }
   ],
Yann Garcia's avatar
Yann Garcia committed
   "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",
    "        - Check that the network scenario is activated and the MEC services are running\n",
    "        - Deactivate a network scenario\n",
    "        - Logout\n",
    "        - Check that logout is effective\n",
    "    \"\"\" \n",
Yann Garcia's avatar
Yann Garcia committed
    "    global PROVIDER, MEC_SANDBOX_API_URL, logger, nw_scenarios\n",
Yann Garcia's avatar
Yann Garcia committed
    "\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",
Yann Garcia's avatar
Yann Garcia committed
    "    time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
Yann Garcia's avatar
Yann Garcia committed
    "\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",
Yann Garcia's avatar
Yann Garcia committed
    "        time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
Yann Garcia's avatar
Yann Garcia committed
    "    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",
Yann Garcia's avatar
Yann Garcia committed
    "        time.sleep(STABLE_TIME_OUT) # Wait for k8s pods up and running\n",
Yann Garcia's avatar
Yann Garcia committed
    "\n",
    "    # Request for a new application instance identifer\n",
    "    app_inst_id = request_application_instance_id(sandbox)\n",
    "    if app_inst_id == None:\n",
    "        logger.error(\"Failed to request an application instance identifer\")\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",
    "    # Check that the network scenario is activated and the MEC services are running \n",
    "    logger.info(\"To check that the app_inst_id is created, verify on the MEC Sandbox server that the MEC services are running (kubectl get pods -A\")\n",
    "    time.sleep(3) # Sleep for 3 seconds\n",
    "\n",
Yann Garcia's avatar
Yann Garcia committed
    "    # Delete the application instance identifer\n",
    "    if delete_application_instance_id(sandbox, app_inst_id.id) == -1:\n",
Yann Garcia's avatar
Yann Garcia committed
    "        logger.error(\"Failed to delete the application instance identifer\")\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": "markdown",
   "metadata": {},
   "source": [
Yann Garcia's avatar
Yann Garcia committed
    "## MEC Registration and the READY confirmation\n",
    "\n",
    "Having an application instance identifier allows us to register to the MEC Sandbox and interacts with it (e.g. to send service queries, to subscribe to events and to recieve notifications...).\n",
    "\n",
    "The standard MEC 011 Clause 5.2.2 MEC application start-up describes the start up process. Basically, our MEC application has to:\n",
    "1. Indicates that it is running by sending a Confirm Ready message\n",
    "2. Retrieve the list of MEC services \n",
    "\n",
    "To do so, a MEC application need to be able to send request but also to recieve notifications (POST requests) and to reply to them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fifth step: Send the READY confirmation\n",
    "\n",
    "Sending READY confirmation is described by MEC 011 Clause 5.2.2 MEC application start-up.\n",
    "\n",
Yann Garcia's avatar
Yann Garcia committed
    "\n"
   ]
  },
Yann Garcia's avatar
Yann Garcia committed
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def send_ready_confirmation(app_inst_id: str) -> int:\n",
    "    global MEC_SANDBOX_URL, MEC_PLTF, logger\n",
    "\n",
    "    try:\n",
    "        url = MEC_SANDBOX_URL + '/' + MEC_PLTF + '/mec_app_support/v2/applications/' + app_inst_id + '/confirm_ready'\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",
    "        body = '{\\\"indication\\\":\\\"READY\\\"}'\n",
    "        result = api.call_api(url, 'POST', header_params=header_params, body=body, async_req=False)\n",
    "        return 0\n",
    "    except ApiException as e:\n",
    "        logger.error(\"Exception when calling AuthorizationApi->logout: %s\\n\" % e)\n",
    "    return -1\n",
    "    # End of function send_ready_confirmation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def send_termination() -> int:\n",
    "    pass"
   ]
  },
  {
   "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 identifer\n",
    "- Send READY confirmation\n",
    "- Get MEC services\n",
    "- Check list of services \n",
    "- Delete our application instance identifer\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 identifer\n",
    "        - Send READY confirmation\n",
    "        - Get MEC services\n",
    "        - Send Termination\n",
    "        - Delete our application instance identifer\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",
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 identifer\n",
    "    app_inst_id = request_application_instance_id(sandbox)\n",
    "    if app_inst_id == None:\n",
    "        logger.error(\"Failed to request an application instance identifer\")\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",
    "    # Send READY confirmation\n",
    "    send_ready_confirmation(app_inst_id)\n",
    "\n",
    "    # Check list of services\n",
    "\n",
    "    # Delete the application instance identifer\n",
    "    if delete_application_instance_id(sandbox) == -1:\n",
    "        logger.error(\"Failed to delete the application instance identifer\")\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": "markdown",
   "metadata": {},
   "source": [
    "### Notification support\n",
    "\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",
    "\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 (V2.2.1) (05-2022): \"Multi-access Edge Computing (MEC); V2X Information Service API\".\n",
    "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
}