fix REST API calls to VTN

Change-Id: Ie3c610fd8401ecb3d1193febeca019d3b9342503
diff --git a/xos/synchronizer/steps/sync_vtn_service.py b/xos/synchronizer/steps/sync_vtn_service.py
index bccfe0c..000f303 100644
--- a/xos/synchronizer/steps/sync_vtn_service.py
+++ b/xos/synchronizer/steps/sync_vtn_service.py
@@ -98,33 +98,52 @@
         glo_saved_vtn_maps = vtn_maps
         # TODO: save this
 
+    def get_method(self, url, id):
+        url_with_id = "%s/%s" % (url, id)
+        r = requests.get(url_with_id, auth=self.get_vtn_auth())
+        if (r.status_code==200):
+            method="PUT"
+            url = url_with_id
+            req_func = requests.put
+            exists=True
+        else:
+            method="POST"
+            req_func = requests.post
+            exists=False
+        return (exists, url, method, req_func)
+
     def sync_service_networks(self):
         valid_ids = []
         for network in Network.objects.all():
             network = VTNNetwork(network)
             valid_ids.append(network.id)
             if (glo_saved_networks.get(network.id, None) != network.to_dict()):
-                logger.info("POSTing VTN API for network %s" % network.id)
+                (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/serviceNetworks", network.id)
 
-                url = "http://" + self.get_vtn_addr() + ":8181/onos/serviceNetworks/%s" % network.id
+                logger.info("%sing VTN API for network %s" % (method, network.id))
+
                 logger.info("URL: %s" % url)
 
-                data = {"id": network.id,
+                # clean the providerNetworks list
+                providerNetworks = [{"id": x["id"], "bidirectional": x["bidirectional"]} for x in network.providerNetworks]
+
+                data = {"ServiceNetwork": {"id": network.id,
                         "type": network.type,
-                        "providerNetworks": network.providerNetworks}
+                        "providerNetworks": providerNetworks} }
                 logger.info("DATA: %s" % str(data))
 
-                r=requests.post(url, data=data, auth=self.get_vtn_auth() )
-                if (r.status_code in [200]):
+                r=req_func(url, json=data, auth=self.get_vtn_auth() )
+                if (r.status_code in [200,201]):
                     glo_saved_networks[network.id] = network.to_dict()
                 else:
                     logger.error("Received error from vtn service (%d)" % r.status_code)
 
+
         for network_id in glo_saved_networks.keys():
             if network_id not in valid_ids:
                 logger.info("DELETEing VTN API for network %s" % network_id)
 
-                url = "http://" + self.get_vtn_addr() + ":8181/onos/serviceNetworks/%s" % network_id
+                url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/serviceNetworks/%s" % network_id
                 logger.info("URL: %s" % url)
 
                 r = requests.delete(url, auth=self.get_vtn_auth() )
@@ -139,18 +158,19 @@
             port = VTNPort(port)
             valid_ids.append(port.id)
             if (glo_saved_ports.get(port.id, None) != port.to_dict()):
-                logger.info("POSTing VTN API for port %s" % port.id)
+                (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/servicePorts", port.id)
 
-                url = "http://" + self.get_vtn_addr() + ":8181/onos/servicePorts/%s" % port.id
+                logger.info("%sing VTN API for port %s" % (method, port.id))
+
                 logger.info("URL: %s" % url)
 
-                data = {"id": port.id,
+                data = {"ServicePort": {"id": port.id,
                         "vlan_id": port.vlan_id,
-                        "floating_address_pairs": port.floating_address_pairs}
+                        "floating_address_pairs": port.floating_address_pairs} }
                 logger.info("DATA: %s" % str(data))
 
-                r=requests.post(url, data=data, auth=self.get_vtn_auth() )
-                if (r.status_code in [200]):
+                r=req_func(url, json=data, auth=self.get_vtn_auth() )
+                if (r.status_code not in [200,201]):
                     glo_saved_ports[port.id] = port.to_dict()
                 else:
                     logger.error("Received error from vtn service (%d)" % r.status_code)
@@ -159,7 +179,7 @@
             if port_id not in valid_ids:
                 logger.info("DELETEing VTN API for port %s" % port_id)
 
-                url = "http://" + self.get_vtn_addr() + ":8181/onos/servicePorts/%s" % port_id
+                url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/servicePorts/%s" % port_id
                 logger.info("URL: %s" % url)
 
                 r = requests.delete(url, auth=self.get_vtn_auth() )