cleanup rest API issues
authorstevenvanrossem <steven.vanrossem@intec.ugent.be>
Sat, 16 Jul 2016 01:23:13 +0000 (03:23 +0200)
committerstevenvanrossem <steven.vanrossem@intec.ugent.be>
Sat, 16 Jul 2016 01:23:13 +0000 (03:23 +0200)
src/emuvim/api/rest/compute.py
src/emuvim/api/rest/monitor.py
src/emuvim/api/rest/rest_api_endpoint.py
src/emuvim/cli/rest/monitor.py
src/emuvim/cli/rest/network.py
src/emuvim/dcemulator/monitoring.py

index e412133..9f0516b 100755 (executable)
@@ -78,7 +78,8 @@ class ComputeStart(Resource):
         '''
         nw_list = list()
 
-        if network_str is None or '),(' not in network_str :
+        # TODO make this more robust with regex check
+        if network_str is None :
             return nw_list
 
         networks = network_str[1:-1].split('),(')
index 15ec014..45d9541 100755 (executable)
@@ -53,7 +53,7 @@ class MonitorInterfaceAction(Resource):
     """
     global net
 
-    def put(self, vnf_name, vnf_interface, metric):
+    def put(self, vnf_name, vnf_interface=None, metric='tx_packets'):
         logging.debug("REST CALL: start monitor VNF interface")
         try:
             c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
@@ -63,7 +63,7 @@ class MonitorInterfaceAction(Resource):
             logging.exception("API error.")
             return ex.message, 500
 
-    def delete(self, vnf_name, vnf_interface, metric):
+    def delete(self, vnf_name, vnf_interface=None, metric='tx_packets'):
         logging.debug("REST CALL: stop monitor VNF interface")
         try:
             c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric)
@@ -85,7 +85,7 @@ class MonitorFlowAction(Resource):
     """
     global net
 
-    def put(self, vnf_name, vnf_interface, metric, cookie):
+    def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
         logging.debug("REST CALL: start monitor VNF interface")
         try:
             c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
@@ -95,7 +95,7 @@ class MonitorFlowAction(Resource):
             logging.exception("API error.")
             return ex.message, 500
 
-    def delete(self, vnf_name, vnf_interface, metric, cookie):
+    def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
         logging.debug("REST CALL: stop monitor VNF interface")
         try:
             c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
index d6c1185..536ed7a 100755 (executable)
@@ -69,10 +69,14 @@ class RestApiEndpoint(object):
         self.api.add_resource(DatacenterList, "/restapi/datacenter")
         self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
 
-        self.api.add_resource(NetworkAction, "/restapi/network/<vnf_src_name>/<vnf_dst_name>")
-
-        self.api.add_resource(MonitorInterfaceAction, "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>")
-        self.api.add_resource(MonitorFlowAction, "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>/<cookie>")
+        self.api.add_resource(NetworkAction, "/restapi/network/<vnf_src_name>/<vnf_dst_name>",)
+
+        self.api.add_resource(MonitorInterfaceAction,
+                              "/restapi/monitor/<vnf_name>/<metric>",
+                              "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>")
+        self.api.add_resource(MonitorFlowAction,
+                              "/restapi/flowmon/<vnf_name>/<metric>/<cookie>",
+                              "/restapi/flowmon/<vnf_name>/<vnf_interface>/<metric>/<cookie>")
 
         logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
 
index 7e9b839..858aad7 100755 (executable)
@@ -25,11 +25,10 @@ the Horizon 2020 and 5G-PPP programmes. The authors would like to
 acknowledge the contributions of their colleagues of the SONATA
 partner consortium (www.sonata-nfv.eu).
 """
+
 from requests import get, put, delete
-from tabulate import tabulate
 import pprint
 import argparse
-import json
 from emuvim.cli import prometheus
 
 pp = pprint.PrettyPrinter(indent=4)
index c6e1dcc..86ed92b 100755 (executable)
@@ -26,10 +26,8 @@ acknowledge the contributions of their colleagues of the SONATA
 partner consortium (www.sonata-nfv.eu).
 """
 from requests import get,put, delete
-from tabulate import tabulate
 import pprint
 import argparse
-import json
 
 pp = pprint.PrettyPrinter(indent=4)
 
@@ -114,13 +112,12 @@ parser.add_argument(
     help="vnf name of the destination of the chain")
 parser.add_argument(
     "--weight", "-w", dest="weight",
-    help="weight metric to calculate the path")
+    help="weight edge attribute to calculate the path")
 parser.add_argument(
     "--match", "-m", dest="match",
     help="string holding extra matches for the flow entries")
 parser.add_argument(
     "--bidirectional", "-b", dest="bidirectional",
-    action='store_true',
     help="add/remove the flow entries from src to dst and back")
 parser.add_argument(
     "--cookie", "-c", dest="cookie",
index 89d82ba..78d6ebb 100755 (executable)
@@ -107,7 +107,7 @@ class DCNetworkMonitor():
         # Prometheus pushgateway and DB are started as external contianer, outside of son-emu\r
         #self.pushgateway_process = self.start_PushGateway()\r
         #self.prometheus_process = self.start_Prometheus()\r
-        self.cadvisor_process = self.start_cadvisor()\r
+        #self.cadvisor_process = self.start_cadvisor()\r
 \r
     # first set some parameters, before measurement can start\r
     def setup_flow(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):\r
@@ -167,7 +167,15 @@ class DCNetworkMonitor():
             logging.exception("setup_metric error.")\r
             return ex.message\r
 \r
-    def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=0):\r
+    def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=0,):\r
+\r
+        # check if port is specified (vnf:port)\r
+        if vnf_interface is None and metric is not None:\r
+            # take first interface by default\r
+            connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]\r
+            link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
+            vnf_interface = link_dict[0]['src_port_id']\r
+\r
         for flow_dict in self.flow_metrics:\r
             if flow_dict['vnf_name'] == vnf_name and flow_dict['vnf_interface'] == vnf_interface \\r
                     and flow_dict['metric_key'] == metric and flow_dict['cookie'] == cookie:\r
@@ -187,6 +195,8 @@ class DCNetworkMonitor():
                 logging.info('Stopped monitoring flow {3}: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric, cookie))\r
                 return 'Stopped monitoring flow {3}: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric, cookie)\r
 \r
+        return 'Error stopping monitoring flow: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface)\r
+\r
 \r
     # first set some parameters, before measurement can start\r
     def setup_metric(self, vnf_name, vnf_interface=None, metric='tx_packets'):\r
@@ -258,6 +268,13 @@ class DCNetworkMonitor():
 \r
     def stop_metric(self, vnf_name, vnf_interface=None, metric=None):\r
 \r
+        # check if port is specified (vnf:port)\r
+        if vnf_interface is None and metric is not None:\r
+            # take first interface by default\r
+            connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]\r
+            link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
+            vnf_interface = link_dict[0]['src_port_id']\r
+\r
         for metric_dict in self.network_metrics:\r
             if metric_dict['vnf_name'] == vnf_name and metric_dict['vnf_interface'] == vnf_interface \\r
                     and metric_dict['metric_key'] == metric:\r
@@ -319,8 +336,13 @@ class DCNetworkMonitor():
                 logging.info('Stopped monitoring vnf: {0}'.format(vnf_name))\r
                 return 'Stopped monitoring: {0}'.format(vnf_name)\r
 \r
+        return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface)\r
+\r
+\r
+\r
 \r
-    # get all metrics defined in the list and export it to Prometheus\r
+\r
+# get all metrics defined in the list and export it to Prometheus\r
     def get_flow_metrics(self):\r
         while self.start_monitoring:\r
 \r
@@ -429,12 +451,20 @@ class DCNetworkMonitor():
         cookie = metric_dict['cookie']\r
 \r
         # TODO aggregate all found flow stats\r
-        flow_stat = flow_stat_dict[str(switch_dpid)][0]\r
-        if 'bytes' in  metric_key:\r
-            counter = flow_stat['byte_count']\r
-        elif 'packet' in metric_key:\r
-            counter = flow_stat['packet_count']\r
+        #flow_stat = flow_stat_dict[str(switch_dpid)][0]\r
+        #if 'bytes' in metric_key:\r
+        #    counter = flow_stat['byte_count']\r
+        #elif 'packet' in metric_key:\r
+        #    counter = flow_stat['packet_count']\r
+\r
+        counter = 0\r
+        for flow_stat in flow_stat_dict[str(switch_dpid)]:\r
+            if 'bytes' in metric_key:\r
+                counter += flow_stat['byte_count']\r
+            elif 'packet' in metric_key:\r
+                counter += flow_stat['packet_count']\r
 \r
+        flow_stat = flow_stat_dict[str(switch_dpid)][0]\r
         flow_uptime = flow_stat['duration_sec'] + flow_stat['duration_nsec'] * 10 ** (-9)\r
 \r
         self.prom_metrics[metric_dict['metric_key']]. \\r
@@ -490,6 +520,7 @@ class DCNetworkMonitor():
         self.monitor_thread.join()\r
         self.monitor_flow_thread.join()\r
 \r
+        # these containers are used for monitoring but are started now outside of son-emu\r
         '''\r
         if self.prometheus_process is not None:\r
             logging.info('stopping prometheus container')\r
@@ -502,13 +533,13 @@ class DCNetworkMonitor():
             self.pushgateway_process.terminate()\r
             self.pushgateway_process.kill()\r
             self._stop_container('pushgateway')\r
-        '''\r
 \r
         if self.cadvisor_process is not None:\r
             logging.info('stopping cadvisor container')\r
             self.cadvisor_process.terminate()\r
             self.cadvisor_process.kill()\r
             self._stop_container('cadvisor')\r
+        '''\r
 \r
     def switch_tx_rx(self,metric=''):\r
         # when monitoring vnfs, the tx of the datacenter switch is actually the rx of the vnf\r