Skip to content
MEC application.ipynb 51.4 KiB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
    "    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
}