blob: 45c823872ecee5fbeca3e75848a535aaff5d75de [file] [log] [blame]
Scott Baker87eb7402016-06-20 17:21:50 -07001from django.contrib import admin
2
3from services.vrouter.models import *
4from django import forms
5from django.utils.safestring import mark_safe
6from django.contrib.auth.admin import UserAdmin
7from django.contrib.admin.widgets import FilteredSelectMultiple
8from django.contrib.auth.forms import ReadOnlyPasswordHashField
9from django.contrib.auth.signals import user_logged_in
10from django.utils import timezone
11from django.contrib.contenttypes import generic
12from suit.widgets import LinkedSelect
13from core.models import AddressPool
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070014from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline
Scott Baker87eb7402016-06-20 17:21:50 -070015from core.middleware import get_request
16
17from functools import update_wrapper
18from django.contrib.admin.views.main import ChangeList
19from django.core.urlresolvers import reverse
20from django.contrib.admin.utils import quote
21
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070022
23class VRouterPortInline(XOSTabularInline):
24 model = VRouterPort
25 fields = ['openflow_id']
26 suit_classes = 'suit-tab suit-tab-vrouter_ports'
27 extra = 0
28
29
30class VRouterInterfaceInline(XOSTabularInline):
31 model = VRouterInterface
32 fields = ['name', 'mac', 'vlan']
33 suit_classes = 'suit-tab suit-tab-vrouter_interfaces'
34 extra = 0
35
36
37class VRouterIpInline(XOSTabularInline):
38 model = VRouterIp
39 fields = ['ip']
40 suit_classes = 'suit-tab suit-tab-vrouter_ips'
41 extra = 0
42
43
Scott Baker87eb7402016-06-20 17:21:50 -070044class VRouterServiceForm(forms.ModelForm):
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070045 def __init__(self, *args, **kwargs):
46 super(VRouterServiceForm, self).__init__(*args, **kwargs)
Scott Baker87eb7402016-06-20 17:21:50 -070047
48 def save(self, commit=True):
49 return super(VRouterServiceForm, self).save(commit=commit)
50
51 class Meta:
52 model = VRouterService
Zack Williamsbde22ab2016-06-27 12:27:01 -070053 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -070054
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070055
Scott Baker87eb7402016-06-20 17:21:50 -070056class VRouterServiceAdmin(ReadOnlyAwareAdmin):
57 model = VRouterService
58 verbose_name = "vRouter Service"
59 verbose_name_plural = "vRouter Service"
60 list_display = ("backend_status_icon", "name", "enabled")
61 list_display_links = ('backend_status_icon', 'name', )
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070062 fieldsets = [(None, {
63 'fields': [
64 'backend_status_text',
65 'name',
66 'enabled',
67 'versionNumber',
68 'description',
69 'view_url',
70 'icon_url',
71 'rest_hostname',
72 'rest_port',
73 'rest_user',
74 'rest_pass',
75 ],
76 'classes':['suit-tab suit-tab-general']})]
77
78 # NOTE make rest_* params editable
79 readonly_fields = (
80 'backend_status_text',
81 'rest_hostname',
82 'rest_port',
83 'rest_user',
84 'rest_pass'
85 )
86 inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline]
Scott Baker87eb7402016-06-20 17:21:50 -070087 form = VRouterServiceForm
88
89 extracontext_registered_admins = True
90
91 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
92
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070093 suit_form_tabs = (
94 ('general', 'vRouter Service Details'),
Scott Baker87eb7402016-06-20 17:21:50 -070095 ('administration', 'Administration'),
96 ('addresspools', 'Addresses'),
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070097 # ('tools', 'Tools'),
98 ('slices', 'Slices'),
99 ('serviceattrs', 'Additional Attributes'),
100 ('serviceprivileges', 'Privileges'),
Scott Baker87eb7402016-06-20 17:21:50 -0700101 )
102
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700103 suit_form_includes = ('vrouteradmin.html', 'top', 'administration') # ('hpctools.html', 'top', 'tools') )
Scott Baker87eb7402016-06-20 17:21:50 -0700104
Zack Williamsbde22ab2016-06-27 12:27:01 -0700105 def get_queryset(self, request):
Scott Baker87eb7402016-06-20 17:21:50 -0700106 return VRouterService.get_service_objects_by_user(request.user)
107
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700108
Scott Baker87eb7402016-06-20 17:21:50 -0700109class VRouterTenantForm(forms.ModelForm):
Scott Baker87eb7402016-06-20 17:21:50 -0700110 gateway_ip = forms.CharField(required=False)
111 gateway_mac = forms.CharField(required=False)
112 cidr = forms.CharField(required=False)
Scott Baker87eb7402016-06-20 17:21:50 -0700113
114 def __init__(self,*args,**kwargs):
115 super (VRouterTenantForm,self ).__init__(*args,**kwargs)
116 self.fields['kind'].widget.attrs['readonly'] = True
117 self.fields['provider_service'].queryset = VRouterService.get_service_objects().all()
118 if self.instance:
119 # fields for the attributes
Scott Baker87eb7402016-06-20 17:21:50 -0700120 self.fields['gateway_ip'].initial = self.instance.gateway_ip
121 self.fields['gateway_mac'].initial = self.instance.gateway_mac
122 self.fields['cidr'].initial = self.instance.cidr
123 if (not self.instance) or (not self.instance.pk):
124 # default fields for an 'add' form
125 self.fields['kind'].initial = VROUTER_KIND
Scott Bakerd94cbfc2017-03-13 11:40:46 -0700126 if VRouterService.objects.exists():
127 self.fields["provider_service"].initial = VRouterService.objects.first()
Scott Baker87eb7402016-06-20 17:21:50 -0700128
129 def save(self, commit=True):
Scott Baker87eb7402016-06-20 17:21:50 -0700130 return super(VRouterTenantForm, self).save(commit=commit)
131
132 class Meta:
133 model = VRouterTenant
Zack Williamsedbf95f2016-06-27 08:31:44 -0700134 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -0700135
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700136
Scott Baker87eb7402016-06-20 17:21:50 -0700137class VRouterTenantAdmin(ReadOnlyAwareAdmin):
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700138 list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip')
Scott Baker87eb7402016-06-20 17:21:50 -0700139 list_display_links = ('backend_status_icon', 'id')
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700140 fieldsets = [(None, {
141 'fields': [
142 'backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service',
143 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'],
144 'classes':['suit-tab suit-tab-general']
145 })]
Scott Baker87eb7402016-06-20 17:21:50 -0700146 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr')
147 form = VRouterTenantForm
148
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700149 suit_form_tabs = (('general', 'Details'),)
Scott Baker87eb7402016-06-20 17:21:50 -0700150
Zack Williamsbde22ab2016-06-27 12:27:01 -0700151 def get_queryset(self, request):
Scott Baker87eb7402016-06-20 17:21:50 -0700152 return VRouterTenant.get_tenant_objects_by_user(request.user)
153
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700154
155class VRouterDeviceAdmin(ReadOnlyAwareAdmin):
156 list_display = ('name', 'openflow_id', 'config_key', 'driver')
157 fieldsets = [(None, {
158 'fields': ['name', 'openflow_id', 'vrouter_service', 'config_key', 'driver'],
159 'classes':['suit-tab suit-tab-general']
160 })]
161 inlines = [VRouterPortInline]
162
163 suit_form_tabs = (
164 ('general', 'Device Details'),
165 ('vrouter_ports', 'Ports'),
166 )
167
168
169class VRouterPortAdmin(ReadOnlyAwareAdmin):
170 list_display = ('name', 'openflow_id', 'vrouter_device')
171 fieldsets = [(None, {
172 'fields': ['name', 'openflow_id', 'vrouter_service', 'vrouter_device'],
173 'classes':['suit-tab suit-tab-general']
174 })]
175 inlines = [VRouterInterfaceInline]
176
177 suit_form_tabs = (
178 ('general', 'Ports Details'),
179 ('vrouter_interfaces', 'Interfaces'),
180 )
181
182
183class VRouterInterfaceAdmin(ReadOnlyAwareAdmin):
184 list_display = ('name', 'mac', 'vlan')
185 fieldsets = [(None, {
186 'fields': ['name', 'vrouter_port', 'mac', 'vlan'],
187 'classes':['suit-tab suit-tab-general']
188 })]
189 inlines = [VRouterIpInline]
190
191 suit_form_tabs = (
192 ('general', 'Interfaces Details'),
193 ('vrouter_ips', 'Ips'),
194 )
195
196
197class VRouterIpAdmin(ReadOnlyAwareAdmin):
198 list_display = ('name', 'ip', 'vrouter_interface')
199 fieldsets = [(None, {'fields': ['name', 'ip', 'vrouter_interface']})]
200
201
202class VRouterAppAdmin(ReadOnlyAwareAdmin):
203 list_display = ('name', 'control_plane_connect_point', 'ospf_enabled')
204 fieldsets = [(None, {'fields': ['name', 'vrouter_service', 'control_plane_connect_point', 'ospf_enabled']})]
205
206
Scott Baker87eb7402016-06-20 17:21:50 -0700207admin.site.register(VRouterService, VRouterServiceAdmin)
208admin.site.register(VRouterTenant, VRouterTenantAdmin)
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700209admin.site.register(VRouterDevice, VRouterDeviceAdmin)
210admin.site.register(VRouterPort, VRouterPortAdmin)
211admin.site.register(VRouterInterface, VRouterInterfaceAdmin)
212admin.site.register(VRouterIp, VRouterIpAdmin)
213admin.site.register(VRouterApp, VRouterAppAdmin)
Scott Baker87eb7402016-06-20 17:21:50 -0700214