[AETHER-2728]: Add option to report monitoring results in-band

Change-Id: Idf44ac3283bb13ffca29b325cb5639e2e18f87da
diff --git a/VERSION b/VERSION
index 0a1ffad..8bd6ba8 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.7.4
+0.7.5
diff --git a/edge-monitoring/agent_modem/config.json b/edge-monitoring/agent_modem/config.json
index 6b50a79..60a3f86 100644
--- a/edge-monitoring/agent_modem/config.json
+++ b/edge-monitoring/agent_modem/config.json
@@ -3,7 +3,8 @@
     "modem": {
         "port": "/dev/ttyACM*",
         "baud": 115200,
-        "ip_addr": "192.168.0.1"
+        "ip_addr": "192.168.0.1",
+        "iface": "eth1"
     },
     "ips": {
       "dry_run": "1.1.1.1",
@@ -15,6 +16,8 @@
     "detach_timeout": 10,
     "report_url": "https://monitoring.aetherproject.org/edges",
     "report_interval": 180,
+    "report_in_band" : false,
+    "report_iface": "eth0",
     "log_level": "WARN",
     "log_file": "/var/log/edge_monitoring_agent.log",
     "iperf_port": 0,
diff --git a/edge-monitoring/agent_modem/edge_monitoring_agent_modem.py b/edge-monitoring/agent_modem/edge_monitoring_agent_modem.py
index 1a4f7bc..0315655 100755
--- a/edge-monitoring/agent_modem/edge_monitoring_agent_modem.py
+++ b/edge-monitoring/agent_modem/edge_monitoring_agent_modem.py
@@ -9,7 +9,7 @@
 import json
 import logging
 import enum
-import requests
+import pycurl
 import time
 import serial
 import subprocess
@@ -410,16 +410,24 @@
     global cycles
     cycles += 1
     logging.info("Number of cycles since modem restart %i",cycles)
+
     try:
-        result = requests.post(CONF.report_url, json=report)
-    except requests.exceptions.ConnectionError as e:
-        logging.error("Failed to report for %s", e)
-        pass
-    try:
-        result.raise_for_status()
-    except requests.exceptions.HTTPError as e:
-        logging.error("Failed to report for %s", e)
-        pass
+        c = pycurl.Curl()
+        c.setopt(pycurl.URL, CONF.report_url)
+        c.setopt(pycurl.POST, True)
+        c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
+        c.setopt(pycurl.TIMEOUT, 10)
+        c.setopt(pycurl.POSTFIELDS, json.dumps(report))
+        if "report_in_band" in CONF._fields and CONF.report_in_band and \
+           "iface" in CONF.modem._fields and CONF.modem.iface:
+           c.setopt(pycurl.INTERFACE, CONF.modem.iface)
+        elif "report_iface" in CONF._fields and CONF.report_iface:
+           c.setopt(pycurl.INTERFACE, CONF.report_iface)
+        c.perform()
+        c.close()
+    except Exception as e:
+        logging.error("Failed to send report: " + str(e))
+
     time.sleep(CONF.report_interval)
 
 def reset_usb():
@@ -447,13 +455,22 @@
     time.sleep(delay)
     subprocess.check_output("sudo shutdown -r now", shell=True)
 
-
-
 def main():
     global cycles
     global hour_iperf_scheduled_time_last_ran
     cycles = 0
     hour_iperf_scheduled_time_last_ran = -1
+
+    try:
+        if "report_in_band" in CONF._fields and \
+        "iface" in CONF.modem._fields and CONF.modem.iface:
+            if CONF.report_in_band: # need to add default gateway if reporting in-band
+                subprocess.check_output("sudo route add default gw " + CONF.modem.ip_addr + " " + CONF.modem.iface + " || true", shell=True)
+            else:
+                subprocess.check_output("sudo route del default gw " + CONF.modem.ip_addr + " " + CONF.modem.iface + " || true", shell=True)
+    except Exception as e:
+        logging.error("Failed to change default route for modem: " + str(e))
+
     for ip in CONF.ips:
         if not ip:
             continue
diff --git a/edge-monitoring/agent_modem/requirements.txt b/edge-monitoring/agent_modem/requirements.txt
index c357f44..d2b8396 100644
--- a/edge-monitoring/agent_modem/requirements.txt
+++ b/edge-monitoring/agent_modem/requirements.txt
@@ -4,3 +4,4 @@
 
 requests
 pyserial
+pycurl