CORD-1244 cleanup use of get_tenant/get_service

Change-Id: Ib7e0efc2c084d891c831451db4bdae3dbd3c9814
diff --git a/xos/attic/vsgtenant_model.py b/xos/attic/vsgtenant_model.py
index 68b9adc..3b58df4 100644
--- a/xos/attic/vsgtenant_model.py
+++ b/xos/attic/vsgtenant_model.py
@@ -73,7 +73,7 @@
 
 @property
 def wan_vm_ip(self):
-    tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
+    tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
     if tags:
         tenant = VRouterTenant.objects.get(id=tags[0].value)
         return tenant.public_ip
@@ -82,7 +82,7 @@
 
 @property
 def wan_vm_mac(self):
-    tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
+    tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
     if tags:
         tenant = VRouterTenant.objects.get(id=tags[0].value)
         return tenant.public_mac
@@ -98,7 +98,7 @@
     pass
 
 def get_vrouter_service(self):
-    vrouterServices = VRouterService.get_service_objects().all()
+    vrouterServices = VRouterService.objects.all()
     if not vrouterServices:
         raise XOSConfigurationError("No VROUTER Services available")
     return vrouterServices[0]
@@ -141,7 +141,7 @@
     return slice
 
 def get_vsg_service(self):
-    return VSGService.get_service_objects().get(id=self.provider_service.id)
+    return VSGService.objects.get(id=self.provider_service.id)
 
 def find_instance_for_s_tag(self, s_tag):
     tags = Tag.objects.filter(name="s_tag", value=s_tag)
@@ -229,6 +229,18 @@
         raise XOSProgrammingError("No lan_network")
     return lan_networks[0]
 
+def port_set_parameter(self, port, name, value):
+    instance_type = port.get_content_type_key()
+    existing_params = NetworkParameter.objects.filter(parameter__name=name, content_type=instance_type, object_id=port.id)
+    if existing_params:
+        p=existing_params[0]
+        p.value = value
+        p.save()
+    else:
+        pt = NetworkParameterType.objects.get(name=name)
+        p = NetworkParameter(parameter=pt, content_type=instance_type, object_id=port.id, value=value)
+        p.save()
+
 def save_instance(self, instance):
     with transaction.atomic():
         instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
@@ -237,24 +249,24 @@
         if instance.isolation in ["container", "container_vm"]:
             lan_network = self.get_lan_network(instance)
             port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
-            port.set_parameter("c_tag", self.volt.c_tag)
-            port.set_parameter("s_tag", self.volt.s_tag)
-            port.set_parameter("device", "eth1")
-            port.set_parameter("bridge", "br-lan")
+            self.port_set_parameter(port, "c_tag", self.volt.c_tag)
+            self.port_set_parameter(port, "s_tag", self.volt.s_tag)
+            self.port_set_parameter(port, "device", "eth1")
+            self.port_set_parameter(port, "bridge", "br-lan")
 
             wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
             if not wan_networks:
                 raise XOSProgrammingError("No wan_network")
             port = self.find_or_make_port(instance, wan_networks[0])
-            port.set_parameter("next_hop", value="10.0.1.253")   # FIX ME
-            port.set_parameter("device", "eth0")
+            self.port_set_parameter(port, "next_hop", value="10.0.1.253")   # FIX ME
+            self.port_set_parameter(port, "device", "eth0")
 
         if instance.isolation in ["vm"]:
             lan_network = self.get_lan_network(instance)
             port = self.find_or_make_port(instance, lan_network)
-            port.set_parameter("c_tag", self.volt.c_tag)
-            port.set_parameter("s_tag", self.volt.s_tag)
-            port.set_parameter("neutron_port_name", "stag-%s" % self.volt.s_tag)
+            self.port_set_parameter(port, "c_tag", self.volt.c_tag)
+            self.port_set_parameter(port, "s_tag", self.volt.s_tag)
+            self.port_set_parameter(port, "neutron_port_name", "stag-%s" % self.volt.s_tag)
             port.save()
 
         # tag the instance with the s-tag, so we can easily find the
@@ -267,7 +279,7 @@
 
         # VTN-CORD needs a WAN address for the VM, so that the VM can
         # be configured.
-        tags = Tag.select_by_content_object(instance).filter(name="vm_vrouter_tenant")
+        tags = Tag.objects.filter(content_type=instance.get_content_type_key(), object_id=instance.id, name="vm_vrouter_tenant")
         if not tags:
             vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
             vrouter.set_attribute("tenant_for_instance_id", instance.id)