CORD-1250 Update to new Service/Tenancy models

Change-Id: I7532a2728e42a90c2a597e01b8f5d702221bf857
diff --git a/xos/admin.py b/xos/admin.py
index fa658db..b773883 100644
--- a/xos/admin.py
+++ b/xos/admin.py
@@ -11,7 +11,7 @@
 from django.contrib.contenttypes import generic
 from suit.widgets import LinkedSelect
 from core.models import AddressPool
-from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline
+from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline
 from core.middleware import get_request
 
 from functools import update_wrapper
@@ -83,7 +83,7 @@
         'rest_user',
         'rest_pass'
     )
-    inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline]
+    inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline, ProviderDependencyInline,SubscriberDependencyInline]
     form = VRouterServiceForm
 
     extracontext_registered_admins = True
@@ -97,6 +97,7 @@
         # ('tools', 'Tools'),
         ('slices', 'Slices'),
         ('serviceattrs', 'Additional Attributes'),
+        ('servicetenants', 'Dependencies'),
         ('serviceprivileges', 'Privileges'),
     )
 
@@ -113,8 +114,7 @@
 
     def __init__(self,*args,**kwargs):
         super (VRouterTenantForm,self ).__init__(*args,**kwargs)
-        self.fields['kind'].widget.attrs['readonly'] = True
-        self.fields['provider_service'].queryset = VRouterService.objects.all()
+        self.fields['owner'].queryset = VRouterService.objects.all()
         if self.instance:
             # fields for the attributes
             self.fields['gateway_ip'].initial = self.instance.gateway_ip
@@ -122,9 +122,8 @@
             self.fields['cidr'].initial = self.instance.cidr
         if (not self.instance) or (not self.instance.pk):
             # default fields for an 'add' form
-            self.fields['kind'].initial = VROUTER_KIND
             if VRouterService.objects.exists():
-               self.fields["provider_service"].initial = VRouterService.objects.first()
+               self.fields["owner"].initial = VRouterService.objects.first()
 
     def save(self, commit=True):
         return super(VRouterTenantForm, self).save(commit=commit)
@@ -135,18 +134,19 @@
 
 
 class VRouterTenantAdmin(ReadOnlyAwareAdmin):
-    list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip')
+    list_display = ('backend_status_icon', 'id', 'public_ip')
     list_display_links = ('backend_status_icon', 'id')
     fieldsets = [(None, {
         'fields': [
-            'backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service',
+            'backend_status_text', 'owner',
             'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'],
         'classes':['suit-tab suit-tab-general']
     })]
     readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr')
+    inlines = (ProviderLinkInline, SubscriberLinkInline)
     form = VRouterTenantForm
 
-    suit_form_tabs = (('general', 'Details'),)
+    suit_form_tabs = (('general', 'Details'), ('servicelinks','Links'),)
 
     def get_queryset(self, request):
         return VRouterTenant.select_by_user(request.user)
diff --git a/xos/api/service/vrouter.py b/xos/api/service/vrouter.py
deleted file mode 100644
index a932931..0000000
--- a/xos/api/service/vrouter.py
+++ /dev/null
@@ -1,146 +0,0 @@
-from rest_framework.decorators import api_view
-from rest_framework.response import Response
-from rest_framework.reverse import reverse
-from rest_framework import serializers
-from rest_framework import generics
-from rest_framework import status
-from core.models import *
-from django.forms import widgets
-from services.vrouter.models import *
-from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
-from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
-import json
-
-BASE_NAME = 'vrouter'
-
-
-class VRouterServiceSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    name = serializers.CharField(required=False)
-    kind = serializers.CharField(required=False)
-    service_specific_attribute = serializers.CharField(required=False)
-    humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
-
-    class Meta:
-        model = VRouterService
-        fields = ('humanReadableName', 'id', 'name', 'kind', 'service_specific_attribute')
-
-    def getHumanReadableName(self, obj):
-        return obj.__unicode__()
-
-
-class VRouterDeviceSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    name = serializers.CharField(required=False)
-    openflow_id = serializers.CharField(required=False)
-    config_key = serializers.CharField(required=False)
-    driver = serializers.CharField(required=False)
-    vrouter_service = serializers.PrimaryKeyRelatedField(read_only=True)
-
-    ports = serializers.SerializerMethodField("getDevicePorts")
-
-    def getDevicePorts(self, device):
-        ports = VRouterPort.objects.filter(vrouter_device=device.id)
-        return VRouterPortSerializer(ports, many=True).data
-
-    class Meta:
-        model = VRouterDevice
-        fields = ('id', 'name', 'openflow_id', 'config_key', 'driver', 'vrouter_service', 'ports')
-
-
-class VRouterPortSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    name = serializers.CharField(required=False)
-    openflow_id = serializers.CharField(required=False)
-    interfaces = serializers.SerializerMethodField("getPortInterfaces")
-
-    def getPortInterfaces(self, port):
-        interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
-        return VRouterInterfaceSerializer(interfaces, many=True).data
-
-    class Meta:
-        model = VRouterPort
-        fields = ('id', 'name', 'openflow_id', 'interfaces')
-
-
-class VRouterInterfaceSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    name = serializers.CharField(required=False)
-    mac = serializers.CharField(required=False)
-    vlan = serializers.CharField(required=False)
-    ips = serializers.SerializerMethodField("getInterfaceIps")
-
-    def getInterfaceIps(self, interface):
-        interfaces = VRouterIp.objects.filter(vrouter_interface=interface.id)
-        return VRouterIpSerializer(interfaces, many=True).data
-
-    class Meta:
-        model = VRouterPort
-        fields = ('id', 'name', 'mac', 'vlan', 'ips')
-
-
-class VRouterIpSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    ip = serializers.CharField(required=False)
-    name = serializers.CharField(required=False)
-
-    class Meta:
-        model = VRouterIp
-        fields = ('id', 'ip', 'name')
-
-
-class VRouterAppSerializer(PlusModelSerializer):
-    id = ReadOnlyField()
-    name = serializers.CharField(required=False)
-    control_plane_connect_point = serializers.CharField(required=False)
-    ospf_enabled = serializers.BooleanField(required=False)
-    interfaces = serializers.SerializerMethodField("dumpInterfaces")
-
-    def dumpInterfaces(self, app):
-        return json.dumps(app.interfaces)
-
-    class Meta:
-        model = VRouterApp
-        fields = ('id', 'name', 'control_plane_connect_point', 'ospf_enabled', 'interfaces')
-
-
-class VRouterServiceViewSet(XOSViewSet):
-    base_name = BASE_NAME
-    method_name = "vrouter"
-    method_kind = "viewset"
-    queryset = VRouterService.objects.filter(kind='vROUTER')
-    serializer_class = VRouterServiceSerializer
-
-    @classmethod
-    def get_urlpatterns(self, api_path="^"):
-        patterns = super(VRouterServiceViewSet, self).get_urlpatterns(api_path=api_path)
-
-        patterns.append(self.detail_url("devices/$", {
-            "get": "get_devices"
-        }, "vrouter_devices"))
-
-        patterns.append(self.detail_url("apps/$", {
-            "get": "get_apps"
-        }, "vrouter_apps"))
-
-        return patterns
-
-    def get_devices(self, request, pk=None):
-        if (not request.user.is_authenticated()):
-            raise XOSPermissionDenied("You must be authenticated in order to use this API")
-        else:
-            if(pk is not None):
-                devices = VRouterDevice.objects.filter(vrouter_service=pk)
-            else:
-                devices = VRouterDevice.objects.all()
-            return Response(VRouterDeviceSerializer(devices, many=True).data)
-
-    def get_apps(self, request, pk=None):
-        if (not request.user.is_authenticated()):
-            raise XOSPermissionDenied("You must be authenticated in order to use this API")
-        else:
-            if(pk is not None):
-                apps = VRouterApp.objects.filter(vrouter_service=pk)
-            else:
-                apps = VRouterApp.objects.all()
-            return Response(VRouterAppSerializer(apps, many=True).data)
diff --git a/xos/models.py b/xos/models.py
index 906d125..32dd4f2 100644
--- a/xos/models.py
+++ b/xos/models.py
@@ -1,5 +1,5 @@
 from django.db import models
-from core.models import Service, XOSBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
+from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
 from core.models.xosbase import StrippedCharField
 import os
 from django.db import models, transaction
@@ -22,40 +22,39 @@
         proxy = True
 
     def ip_to_mac(self, ip):
-	(a, b, c, d) = ip.split('.')
-	return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
+        (a, b, c, d) = ip.split('.')
+        return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
 
     def get_gateways(self):
-	gateways = []
+        gateways = []
+        aps = self.addresspools.all()
+        for ap in aps:
+            gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
 
-	aps = self.addresspools.all()
-	for ap in aps:
-	    gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
-
-	return gateways
+        return gateways
 
     def get_address_pool(self, name):
-	ap = AddressPool.objects.filter(name=name, service=self)
-	if not ap:
-	    raise Exception("vRouter unable to find addresspool %s" % name)
-	return ap[0]
+        ap = AddressPool.objects.filter(name=name, service=self)
+        if not ap:
+            raise Exception("vRouter unable to find addresspool %s" % name)
+        return ap[0]
 
     def get_tenant(self, **kwargs):
-	address_pool_name = kwargs.pop("address_pool_name")
+        address_pool_name = kwargs.pop("address_pool_name")
 
-	ap = self.get_address_pool(address_pool_name)
+        ap = self.get_address_pool(address_pool_name)
 
-	ip = ap.get_address()
-	if not ip:
-	    raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
+        ip = ap.get_address()
+        if not ip:
+            raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
 
-	t = VRouterTenant(provider_service=self, **kwargs)
-	t.public_ip = ip
-	t.public_mac = self.ip_to_mac(ip)
-	t.address_pool_id = ap.id
-	t.save()
+        t = VRouterTenant(owner=self, **kwargs)
+        t.public_ip = ip
+        t.public_mac = self.ip_to_mac(ip)
+        t.address_pool_id = ap.id
+        t.save()
 
-	return t
+        return t
 
 
 class VRouterDevice (VRouterDevice_decl):
diff --git a/xos/vrouter-onboard.yaml b/xos/vrouter-onboard.yaml
index 5e3c48b..c6d2562 100644
--- a/xos/vrouter-onboard.yaml
+++ b/xos/vrouter-onboard.yaml
@@ -18,6 +18,6 @@
           admin_template: templates/vrouteradmin.html
           tosca_custom_types: vrouter.yaml
           tosca_resource: tosca/resources/vrouterservice.py
-          rest_service: api/service/vrouter.py
+
 
 
diff --git a/xos/vrouter.xproto b/xos/vrouter.xproto
index 3a91d9d..47e2fe3 100644
--- a/xos/vrouter.xproto
+++ b/xos/vrouter.xproto
@@ -45,7 +45,7 @@
      required string ip = 3 [help_text = "interface ips", max_length = 19, null = False, db_index = False, blank = False];
 }
 
-message VRouterTenant (Tenant){
+message VRouterTenant (ServiceInstance){
      optional string public_ip = 1 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
      optional string public_mac = 2 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
      optional manytoone address_pool->AddressPool:vrouter_tenants = 3 [db_index = True, null = True, blank = True];