blob: a6ce2706c880ec2d5b77f48793606a581e614e02 [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):
110 public_ip = forms.CharField(required=True)
111 public_mac = forms.CharField(required=True)
112 gateway_ip = forms.CharField(required=False)
113 gateway_mac = forms.CharField(required=False)
114 cidr = forms.CharField(required=False)
115 address_pool = forms.ModelChoiceField(queryset=AddressPool.objects.all(),required=False)
116
117 def __init__(self,*args,**kwargs):
118 super (VRouterTenantForm,self ).__init__(*args,**kwargs)
119 self.fields['kind'].widget.attrs['readonly'] = True
120 self.fields['provider_service'].queryset = VRouterService.get_service_objects().all()
121 if self.instance:
122 # fields for the attributes
123 self.fields['address_pool'].initial = self.instance.address_pool
124 self.fields['public_ip'].initial = self.instance.public_ip
125 self.fields['public_mac'].initial = self.instance.public_mac
126 self.fields['gateway_ip'].initial = self.instance.gateway_ip
127 self.fields['gateway_mac'].initial = self.instance.gateway_mac
128 self.fields['cidr'].initial = self.instance.cidr
129 if (not self.instance) or (not self.instance.pk):
130 # default fields for an 'add' form
131 self.fields['kind'].initial = VROUTER_KIND
132 if VRouterService.get_service_objects().exists():
133 self.fields["provider_service"].initial = VRouterService.get_service_objects().all()[0]
134
135 def save(self, commit=True):
136 self.instance.public_ip = self.cleaned_data.get("public_ip")
137 self.instance.public_mac = self.cleaned_data.get("public_mac")
138 self.instance.address_pool = self.cleaned_data.get("address_pool")
139 return super(VRouterTenantForm, self).save(commit=commit)
140
141 class Meta:
142 model = VRouterTenant
Zack Williamsedbf95f2016-06-27 08:31:44 -0700143 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -0700144
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700145
Scott Baker87eb7402016-06-20 17:21:50 -0700146class VRouterTenantAdmin(ReadOnlyAwareAdmin):
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700147 list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip')
Scott Baker87eb7402016-06-20 17:21:50 -0700148 list_display_links = ('backend_status_icon', 'id')
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700149 fieldsets = [(None, {
150 'fields': [
151 'backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service',
152 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'],
153 'classes':['suit-tab suit-tab-general']
154 })]
Scott Baker87eb7402016-06-20 17:21:50 -0700155 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr')
156 form = VRouterTenantForm
157
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700158 suit_form_tabs = (('general', 'Details'),)
Scott Baker87eb7402016-06-20 17:21:50 -0700159
Zack Williamsbde22ab2016-06-27 12:27:01 -0700160 def get_queryset(self, request):
Scott Baker87eb7402016-06-20 17:21:50 -0700161 return VRouterTenant.get_tenant_objects_by_user(request.user)
162
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700163
164class VRouterDeviceAdmin(ReadOnlyAwareAdmin):
165 list_display = ('name', 'openflow_id', 'config_key', 'driver')
166 fieldsets = [(None, {
167 'fields': ['name', 'openflow_id', 'vrouter_service', 'config_key', 'driver'],
168 'classes':['suit-tab suit-tab-general']
169 })]
170 inlines = [VRouterPortInline]
171
172 suit_form_tabs = (
173 ('general', 'Device Details'),
174 ('vrouter_ports', 'Ports'),
175 )
176
177
178class VRouterPortAdmin(ReadOnlyAwareAdmin):
179 list_display = ('name', 'openflow_id', 'vrouter_device')
180 fieldsets = [(None, {
181 'fields': ['name', 'openflow_id', 'vrouter_service', 'vrouter_device'],
182 'classes':['suit-tab suit-tab-general']
183 })]
184 inlines = [VRouterInterfaceInline]
185
186 suit_form_tabs = (
187 ('general', 'Ports Details'),
188 ('vrouter_interfaces', 'Interfaces'),
189 )
190
191
192class VRouterInterfaceAdmin(ReadOnlyAwareAdmin):
193 list_display = ('name', 'mac', 'vlan')
194 fieldsets = [(None, {
195 'fields': ['name', 'vrouter_port', 'mac', 'vlan'],
196 'classes':['suit-tab suit-tab-general']
197 })]
198 inlines = [VRouterIpInline]
199
200 suit_form_tabs = (
201 ('general', 'Interfaces Details'),
202 ('vrouter_ips', 'Ips'),
203 )
204
205
206class VRouterIpAdmin(ReadOnlyAwareAdmin):
207 list_display = ('name', 'ip', 'vrouter_interface')
208 fieldsets = [(None, {'fields': ['name', 'ip', 'vrouter_interface']})]
209
210
211class VRouterAppAdmin(ReadOnlyAwareAdmin):
212 list_display = ('name', 'control_plane_connect_point', 'ospf_enabled')
213 fieldsets = [(None, {'fields': ['name', 'vrouter_service', 'control_plane_connect_point', 'ospf_enabled']})]
214
215
Scott Baker87eb7402016-06-20 17:21:50 -0700216admin.site.register(VRouterService, VRouterServiceAdmin)
217admin.site.register(VRouterTenant, VRouterTenantAdmin)
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700218admin.site.register(VRouterDevice, VRouterDeviceAdmin)
219admin.site.register(VRouterPort, VRouterPortAdmin)
220admin.site.register(VRouterInterface, VRouterInterfaceAdmin)
221admin.site.register(VRouterIp, VRouterIpAdmin)
222admin.site.register(VRouterApp, VRouterAppAdmin)
Scott Baker87eb7402016-06-20 17:21:50 -0700223