| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 1 | """ |
| 2 | Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION] |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | """ |
| stevenvanrossem | e131bf5 | 2016-07-14 11:42:09 +0200 | [diff] [blame] | 28 | |
| 29 | """ |
| 30 | Distributed Cloud Emulator (dcemulator) |
| 31 | Networking and monitoring functions |
| 32 | (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be> |
| 33 | """ |
| 34 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 35 | import logging |
| 36 | from flask_restful import Resource |
| 37 | from flask import request |
| 38 | import json |
| 39 | |
| 40 | logging.basicConfig(level=logging.INFO) |
| 41 | |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 42 | CORS_HEADER = {'Access-Control-Allow-Origin': '*'} |
| 43 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 44 | net = None |
| 45 | |
| 46 | |
| 47 | |
| 48 | class MonitorInterfaceAction(Resource): |
| 49 | """ |
| 50 | Monitor the counters of a VNF interface |
| 51 | :param vnf_name: name of the VNF to be monitored |
| 52 | :param vnf_interface: name of the VNF interface to be monitored |
| 53 | :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets |
| 54 | :return: message string indicating if the monitor action is succesful or not |
| 55 | """ |
| 56 | global net |
| 57 | |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 58 | def put(self, vnf_name, vnf_interface=None, metric='tx_packets'): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 59 | logging.debug("REST CALL: start monitor VNF interface") |
| 60 | try: |
| 61 | c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric) |
| 62 | # return monitor message response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 63 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 64 | except Exception as ex: |
| 65 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 66 | return ex.message, 500, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 67 | |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 68 | def delete(self, vnf_name, vnf_interface=None, metric='tx_packets'): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 69 | logging.debug("REST CALL: stop monitor VNF interface") |
| 70 | try: |
| 71 | c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric) |
| 72 | # return monitor message response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 73 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 74 | except Exception as ex: |
| 75 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 76 | return ex.message, 500, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class MonitorFlowAction(Resource): |
| 80 | """ |
| 81 | Monitor the counters of a specific flow |
| 82 | :param vnf_name: name of the VNF to be monitored |
| 83 | :param vnf_interface: name of the VNF interface to be monitored |
| 84 | :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets |
| 85 | :param cookie: specific identifier of flows to monitor |
| 86 | :return: message string indicating if the monitor action is succesful or not |
| 87 | """ |
| 88 | global net |
| 89 | |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 90 | def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 91 | logging.debug("REST CALL: start monitor VNF interface") |
| 92 | try: |
| 93 | c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie) |
| 94 | # return monitor message response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 95 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 96 | except Exception as ex: |
| 97 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 98 | return ex.message, 500, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 99 | |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 100 | def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 101 | logging.debug("REST CALL: stop monitor VNF interface") |
| 102 | try: |
| 103 | c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie) |
| 104 | # return monitor message response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 105 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 106 | except Exception as ex: |
| 107 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 108 | return ex.message, 500, CORS_HEADER |