CORD-1008 add convenience wrappers for VSGTenant and VOLTTenant

Change-Id: I75fc21e7464753c66378296b456fffc95bc16d16
diff --git a/xos/xos_client/xosapi/convenience/vroutertenant.py b/xos/xos_client/xosapi/convenience/vroutertenant.py
new file mode 100644
index 0000000..ef8a1bf
--- /dev/null
+++ b/xos/xos_client/xosapi/convenience/vroutertenant.py
@@ -0,0 +1,63 @@
+import json
+from xosapi.orm import ORMWrapper, register_convenience_wrapper
+
+class ORMWrapperVRouterTenant(ORMWrapper):
+    # hopefully this goes away when VRouterTenant is made a real object
+
+    def get_attribute(self, name, default=None):
+        if self.service_specific_attribute:
+            attributes = json.loads(self.service_specific_attribute)
+        else:
+            attributes = {}
+        return attributes.get(name, default)
+
+    @property
+    def address_pool_id(self):
+        return self.get_attribute("address_pool_id", None)
+
+    @property
+    def public_ip(self):
+        return self.get_attribute("public_ip", None)
+
+    @property
+    def public_mac(self):
+        return self.get_attribute("public_mac", None)
+
+    @property
+    def gateway_ip(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.gateway_ip
+
+    @property
+    def gateway_mac(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.gateway_mac
+
+    @property
+    def cidr(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.cidr
+
+    @property
+    def netbits(self):
+        # return number of bits in the network portion of the cidr
+        if self.cidr:
+            parts = self.cidr.split("/")
+            if len(parts) == 2:
+                return int(parts[1].strip())
+        return None
+
+    @property
+    def address_pool(self):
+        if not self.address_pool_id:
+            return None
+        aps = self.stub.AddressPool.objects.filter(id=self.address_pool_id)
+        if not aps:
+            return None
+        ap = aps[0]
+        return ap
+
+register_convenience_wrapper("VRouterTenant", ORMWrapperVRouterTenant)
diff --git a/xos/xos_client/xosapi/convenience/vsgtenant.py b/xos/xos_client/xosapi/convenience/vsgtenant.py
new file mode 100644
index 0000000..aef9295
--- /dev/null
+++ b/xos/xos_client/xosapi/convenience/vsgtenant.py
@@ -0,0 +1,66 @@
+from xosapi.orm import ORMWrapper, register_convenience_wrapper
+
+class ORMWrapperVSGTenant(ORMWrapper):
+    @property
+    def volt(self):
+        if not self.subscriber_tenant:
+            return None
+        # make sure subscriber_tenant is properly subclassed to a volt object
+        volts = self.stub.VOLTTenant.objects.filter(id = self.subscriber_tenant.id)
+        if not volts:
+            return None
+        return volts[0]
+
+    @property
+    def vrouter(self):
+        vrouter_tenants = self.stub.VRouterTenant.objects.filter(subscriber_tenant_id = self.id)
+        if vrouter_tenants:
+            return vrouter_tenants[0]
+        return None
+
+    def get_vrouter_field(self, name, default=None):
+        if self.vrouter:
+            return getattr(self.vrouter, name, default)
+        else:
+            return default
+
+    @property
+    def wan_container_ip(self):
+        return self.get_vrouter_field("public_ip", None)
+
+    @property
+    def wan_container_mac(self):
+        return self.get_vrouter_field("public_mac", None)
+
+    @property
+    def wan_container_netbits(self):
+        return self.get_vrouter_field("netbits", None)
+
+    @property
+    def wan_container_gateway_ip(self):
+        return self.get_vrouter_field("gateway_ip", None)
+
+    @property
+    def wan_container_gateway_mac(self):
+        return self.get_vrouter_field("gateway_mac", None)
+
+    @property
+    def wan_vm_ip(self):
+        tags = self.stub.Tag.objects.filter(name="vm_vrouter_tenant", object_id=self.instance.id, content_type_id=self.instance.self_content_type_id)
+        if tags:
+            tenant = self.stub.VRouterTenant.objects.get(id=int(tags[0].value))
+            return tenant.public_ip
+        else:
+            raise Exception("no vm_vrouter_tenant tag for instance %s" % self.instance)
+
+    @property
+    def wan_vm_mac(self):
+        tags = self.stub.Tag.objects.filter(name="vm_vrouter_tenant", object_id=self.instance.id, content_type_id=self.instance.self_content_type_id)
+        if tags:
+            tenant = self.stub.VRouterTenant.objects.get(id=int(tags[0].value))
+            return tenant.public_mac
+        else:
+            raise Exception("no vm_vrouter_tenant tag for instance %s" % self.instance)
+
+
+register_convenience_wrapper("VSGTenant", ORMWrapperVSGTenant)
diff --git a/xos/xos_client/xosapi/orm.py b/xos/xos_client/xosapi/orm.py
index dc84c96..bf59222 100644
--- a/xos/xos_client/xosapi/orm.py
+++ b/xos/xos_client/xosapi/orm.py
@@ -425,4 +425,6 @@
 import convenience.instance
 import convenience.cordsubscriberroot
 import convenience.volttenant
+import convenience.vsgtenant
+import convenience.vroutertenant