Merge branch 'master' of https://github.com/open-cloud/xos
diff --git a/xos/core/admin.py b/xos/core/admin.py
index b3dc67d..5d2996a 100644
--- a/xos/core/admin.py
+++ b/xos/core/admin.py
@@ -787,6 +787,23 @@
 
         return tabs
 
+class TenantAttributeAdmin(XOSBaseAdmin):
+    model = TenantAttribute
+    list_display = ('backend_status_icon', 'tenant', 'name', 'value')
+    list_display_links = ('backend_status_icon', 'name')
+    fieldList = ('backend_status_text', 'tenant', 'name', 'value', )
+    fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})]
+    readonly_fields = ('backend_status_text', )
+
+    suit_form_tabs =(('general', 'Tenant Root Details'),
+    )
+
+class TenantAttrAsTabInline(XOSTabularInline):
+    model = TenantAttribute
+    fields = ['name','value']
+    extra = 0
+    suit_classes = 'suit-tab suit-tab-tenantattrs'
+
 class TenantRootRoleAdmin(XOSBaseAdmin):
     model = TenantRootRole
     fields = ('role',)
@@ -2006,4 +2023,5 @@
     admin.site.register(Flavor, FlavorAdmin)
     admin.site.register(TenantRoot, TenantRootAdmin)
     admin.site.register(TenantRootRole, TenantRootRoleAdmin)
+    admin.site.register(TenantAttribute, TenantAttributeAdmin)
 
diff --git a/xos/core/models/__init__.py b/xos/core/models/__init__.py
index ad271a4..c380e9c 100644
--- a/xos/core/models/__init__.py
+++ b/xos/core/models/__init__.py
@@ -2,7 +2,7 @@
 from .project import Project
 from .singletonmodel import SingletonModel
 from .service import Service, Tenant, TenantWithContainer, CoarseTenant, ServicePrivilege, TenantRoot, TenantRootPrivilege, TenantRootRole, Subscriber, Provider
-from .service import ServiceAttribute
+from .service import ServiceAttribute, TenantAttribute
 from .tag import Tag
 from .role import Role
 from .site import Site, Deployment, DeploymentRole, DeploymentPrivilege, Controller, ControllerRole, ControllerSite, SiteDeployment
diff --git a/xos/core/models/service.py b/xos/core/models/service.py
index 9530c72..8d2180c 100644
--- a/xos/core/models/service.py
+++ b/xos/core/models/service.py
@@ -488,6 +488,11 @@
 
     KIND = "Provider"
 
+class TenantAttribute(PlCoreBase):
+    name = models.SlugField(help_text="Attribute Name", max_length=128)
+    value = models.TextField(help_text="Attribute Value")
+    tenant = models.ForeignKey(Tenant, related_name='tenantattributes', help_text="The Tenant this attribute is associated with")
+
 class TenantRootRole(PlCoreBase):
     ROLE_CHOICES = (('admin','Admin'), ('access','Access'))
 
diff --git a/xos/services/__init__.py b/xos/services/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/xos/services/__init__.py
@@ -0,0 +1 @@
+
diff --git a/xos/services/onos/__init__.py b/xos/services/onos/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/xos/services/onos/__init__.py
@@ -0,0 +1 @@
+
diff --git a/xos/services/onos/admin.py b/xos/services/onos/admin.py
new file mode 100644
index 0000000..b2e5349
--- /dev/null
+++ b/xos/services/onos/admin.py
@@ -0,0 +1,93 @@
+from django.contrib import admin
+
+from services.onos.models import *
+from django import forms
+from django.utils.safestring import mark_safe
+from django.contrib.auth.admin import UserAdmin
+from django.contrib.admin.widgets import FilteredSelectMultiple
+from django.contrib.auth.forms import ReadOnlyPasswordHashField
+from django.contrib.auth.signals import user_logged_in
+from django.utils import timezone
+from django.contrib.contenttypes import generic
+from suit.widgets import LinkedSelect
+from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline, TenantAttrAsTabInline
+from core.middleware import get_request
+
+from functools import update_wrapper
+from django.contrib.admin.views.main import ChangeList
+from django.core.urlresolvers import reverse
+from django.contrib.admin.utils import quote
+
+class ONOSServiceAdmin(ReadOnlyAwareAdmin):
+    model = ONOSService
+    verbose_name = "ONOS Service"
+    verbose_name_plural = "ONOS Services"
+    list_display = ("backend_status_icon", "name", "enabled")
+    list_display_links = ('backend_status_icon', 'name', )
+    fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})]
+    readonly_fields = ('backend_status_text', )
+    inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
+
+    extracontext_registered_admins = True
+
+    user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
+
+    suit_form_tabs =(('general', 'ONOS Service Details'),
+        ('administration', 'Administration'),
+        ('slices','Slices'),
+        ('serviceattrs','Additional Attributes'),
+        ('serviceprivileges','Privileges'),
+    )
+
+    suit_form_includes = (('onosadmin.html', 'top', 'administration'),
+                           )
+
+    def queryset(self, request):
+        return ONOSService.get_service_objects_by_user(request.user)
+
+class ONOSAppForm(forms.ModelForm):
+    creator = forms.ModelChoiceField(queryset=User.objects.all())
+    name = forms.CharField()
+
+    def __init__(self,*args,**kwargs):

+        super (ONOSAppForm,self ).__init__(*args,**kwargs)

+        self.fields['kind'].widget.attrs['readonly'] = True

+        self.fields['provider_service'].queryset = ONOSService.get_service_objects().all()

+        if self.instance:

+            # fields for the attributes

+            self.fields['creator'].initial = self.instance.creator

+            self.fields['name'].initial = self.instance.name

+        if (not self.instance) or (not self.instance.pk):

+            # default fields for an 'add' form

+            self.fields['kind'].initial = ONOS_KIND

+            self.fields['creator'].initial = get_request().user

+            if ONOSService.get_service_objects().exists():

+               self.fields["provider_service"].initial = ONOSService.get_service_objects().all()[0]

+

+

+    def save(self, commit=True):

+        self.instance.creator = self.cleaned_data.get("creator")

+        self.instance.name = self.cleaned_data.get("name")

+        return super(ONOSAppForm, self).save(commit=commit)

+

+    class Meta:

+        model = ONOSApp
+
+class ONOSAppAdmin(ReadOnlyAwareAdmin):
+    list_display = ('backend_status_icon', 'name', )
+    list_display_links = ('backend_status_icon', 'name')
+    fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'provider_service', 'service_specific_attribute',
+                                     'creator'],
+                          'classes':['suit-tab suit-tab-general']})]
+    readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute')
+    inlines = [TenantAttrAsTabInline]
+    form = ONOSAppForm
+
+    suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes'))
+
+    def queryset(self, request):
+        return ONOSApp.get_tenant_objects_by_user(request.user)
+
+admin.site.register(ONOSService, ONOSServiceAdmin)
+admin.site.register(ONOSApp, ONOSAppAdmin)
+
diff --git a/xos/services/onos/models.py b/xos/services/onos/models.py
new file mode 100644
index 0000000..018bd25
--- /dev/null
+++ b/xos/services/onos/models.py
@@ -0,0 +1,90 @@
+from django.db import models
+from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber
+from core.models.plcorebase import StrippedCharField
+import os
+from django.db import models, transaction
+from django.forms.models import model_to_dict
+from django.db.models import Q
+from operator import itemgetter, attrgetter, methodcaller
+import traceback
+from xos.exceptions import *
+from core.models import SlicePrivilege, SitePrivilege
+from sets import Set
+
+ONOS_KIND = "onos"
+
+class ONOSService(Service):
+    KIND = ONOS_KIND
+
+    class Meta:
+        app_label = "onos"
+        verbose_name = "ONOS Service"
+        proxy = True
+
+class ONOSApp(Tenant):   # aka 'ONOSTenant'
+    class Meta:
+        proxy = True
+
+    KIND = ONOS_KIND
+
+    default_attributes = {"name": ""}
+    def __init__(self, *args, **kwargs):
+        onos_services = ONOSService.get_service_objects().all()
+        if onos_services:
+            self._meta.get_field("provider_service").default = onos_services[0].id
+        super(ONOSApp, self).__init__(*args, **kwargs)
+
+    @property
+    def creator(self):
+        from core.models import User
+        if getattr(self, "cached_creator", None):
+            return self.cached_creator
+        creator_id=self.get_attribute("creator_id")
+        if not creator_id:
+            return None
+        users=User.objects.filter(id=creator_id)
+        if not users:
+            return None
+        user=users[0]
+        self.cached_creator = users[0]
+        return user
+
+    @creator.setter
+    def creator(self, value):
+        if value:
+            value = value.id
+        if (value != self.get_attribute("creator_id", None)):
+            self.cached_creator=None
+        self.set_attribute("creator_id", value)
+
+    @property
+    def name(self):
+        return self.get_attribute("name", self.default_attributes["name"])
+
+    @name.setter
+    def name(self, value):
+        self.set_attribute("name", value)
+
+    def save(self, *args, **kwargs):
+        if not self.creator:
+            if not getattr(self, "caller", None):
+                # caller must be set when creating a vCPE since it creates a slice
+                raise XOSProgrammingError("ONOSApp's self.caller was not set")
+            self.creator = self.caller
+            if not self.creator:
+                raise XOSProgrammingError("ONOSApp's self.creator was not set")
+
+        super(ONOSApp, self).save(*args, **kwargs)
+        model_policy_onos_app(self.pk)
+
+# TODO: Probably don't need this...
+def model_policy_onos_app(pk):
+    # TODO: this should be made in to a real model_policy
+    with transaction.atomic():
+        oa = ONOSApp.objects.select_for_update().filter(pk=pk)
+        if not oa:
+            return
+        oa = oa[0]
+        #oa.manage_container()
+
+
diff --git a/xos/xos/settings.py b/xos/xos/settings.py
index ba5ff1d..e660352 100644
--- a/xos/xos/settings.py
+++ b/xos/xos/settings.py
@@ -148,6 +148,7 @@
     'core',
     'hpc',
     'cord',
+    'services.onos',
     'ceilometer',
     'requestrouter',
 #    'urlfilter',