blob: 853cd00389adfb510f1ae67c2f9b5ceb8541dd9e [file] [log] [blame]
Matteo Scandolod02b73b2017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker87eb7402016-06-20 17:21:50 -070017from django.contrib import admin
18
19from services.vrouter.models import *
20from django import forms
21from django.utils.safestring import mark_safe
22from django.contrib.auth.admin import UserAdmin
23from django.contrib.admin.widgets import FilteredSelectMultiple
24from django.contrib.auth.forms import ReadOnlyPasswordHashField
25from django.contrib.auth.signals import user_logged_in
26from django.utils import timezone
27from django.contrib.contenttypes import generic
28from suit.widgets import LinkedSelect
29from core.models import AddressPool
Scott Baker7c241cd2017-07-18 10:54:14 -070030from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline
Scott Baker87eb7402016-06-20 17:21:50 -070031from core.middleware import get_request
32
33from functools import update_wrapper
34from django.contrib.admin.views.main import ChangeList
35from django.core.urlresolvers import reverse
36from django.contrib.admin.utils import quote
37
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070038
39class VRouterPortInline(XOSTabularInline):
40 model = VRouterPort
41 fields = ['openflow_id']
42 suit_classes = 'suit-tab suit-tab-vrouter_ports'
43 extra = 0
44
45
46class VRouterInterfaceInline(XOSTabularInline):
47 model = VRouterInterface
48 fields = ['name', 'mac', 'vlan']
49 suit_classes = 'suit-tab suit-tab-vrouter_interfaces'
50 extra = 0
51
52
53class VRouterIpInline(XOSTabularInline):
54 model = VRouterIp
55 fields = ['ip']
56 suit_classes = 'suit-tab suit-tab-vrouter_ips'
57 extra = 0
58
59
Scott Baker87eb7402016-06-20 17:21:50 -070060class VRouterServiceForm(forms.ModelForm):
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070061 def __init__(self, *args, **kwargs):
62 super(VRouterServiceForm, self).__init__(*args, **kwargs)
Scott Baker87eb7402016-06-20 17:21:50 -070063
64 def save(self, commit=True):
65 return super(VRouterServiceForm, self).save(commit=commit)
66
67 class Meta:
68 model = VRouterService
Zack Williamsbde22ab2016-06-27 12:27:01 -070069 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -070070
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070071
Scott Baker87eb7402016-06-20 17:21:50 -070072class VRouterServiceAdmin(ReadOnlyAwareAdmin):
73 model = VRouterService
74 verbose_name = "vRouter Service"
75 verbose_name_plural = "vRouter Service"
76 list_display = ("backend_status_icon", "name", "enabled")
77 list_display_links = ('backend_status_icon', 'name', )
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070078 fieldsets = [(None, {
79 'fields': [
80 'backend_status_text',
81 'name',
82 'enabled',
83 'versionNumber',
84 'description',
85 'view_url',
86 'icon_url',
87 'rest_hostname',
88 'rest_port',
89 'rest_user',
90 'rest_pass',
91 ],
92 'classes':['suit-tab suit-tab-general']})]
93
94 # NOTE make rest_* params editable
95 readonly_fields = (
96 'backend_status_text',
97 'rest_hostname',
98 'rest_port',
99 'rest_user',
100 'rest_pass'
101 )
Scott Baker7c241cd2017-07-18 10:54:14 -0700102 inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline, ProviderDependencyInline,SubscriberDependencyInline]
Scott Baker87eb7402016-06-20 17:21:50 -0700103 form = VRouterServiceForm
104
105 extracontext_registered_admins = True
106
107 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
108
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700109 suit_form_tabs = (
110 ('general', 'vRouter Service Details'),
Scott Baker87eb7402016-06-20 17:21:50 -0700111 ('administration', 'Administration'),
112 ('addresspools', 'Addresses'),
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700113 # ('tools', 'Tools'),
114 ('slices', 'Slices'),
115 ('serviceattrs', 'Additional Attributes'),
Scott Baker7c241cd2017-07-18 10:54:14 -0700116 ('servicetenants', 'Dependencies'),
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700117 ('serviceprivileges', 'Privileges'),
Scott Baker87eb7402016-06-20 17:21:50 -0700118 )
119
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700120 suit_form_includes = ('vrouteradmin.html', 'top', 'administration') # ('hpctools.html', 'top', 'tools') )
Scott Baker87eb7402016-06-20 17:21:50 -0700121
Zack Williamsbde22ab2016-06-27 12:27:01 -0700122 def get_queryset(self, request):
Scott Bakerb03e0a72017-05-25 09:38:12 -0700123 return VRouterService.select_by_user(request.user)
Scott Baker87eb7402016-06-20 17:21:50 -0700124
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700125
Scott Baker87eb7402016-06-20 17:21:50 -0700126class VRouterTenantForm(forms.ModelForm):
Scott Baker87eb7402016-06-20 17:21:50 -0700127 gateway_ip = forms.CharField(required=False)
128 gateway_mac = forms.CharField(required=False)
129 cidr = forms.CharField(required=False)
Scott Baker87eb7402016-06-20 17:21:50 -0700130
131 def __init__(self,*args,**kwargs):
132 super (VRouterTenantForm,self ).__init__(*args,**kwargs)
Scott Baker7c241cd2017-07-18 10:54:14 -0700133 self.fields['owner'].queryset = VRouterService.objects.all()
Scott Baker87eb7402016-06-20 17:21:50 -0700134 if self.instance:
135 # fields for the attributes
Scott Baker87eb7402016-06-20 17:21:50 -0700136 self.fields['gateway_ip'].initial = self.instance.gateway_ip
137 self.fields['gateway_mac'].initial = self.instance.gateway_mac
138 self.fields['cidr'].initial = self.instance.cidr
139 if (not self.instance) or (not self.instance.pk):
140 # default fields for an 'add' form
Scott Bakerd94cbfc2017-03-13 11:40:46 -0700141 if VRouterService.objects.exists():
Scott Baker7c241cd2017-07-18 10:54:14 -0700142 self.fields["owner"].initial = VRouterService.objects.first()
Scott Baker87eb7402016-06-20 17:21:50 -0700143
144 def save(self, commit=True):
Scott Baker87eb7402016-06-20 17:21:50 -0700145 return super(VRouterTenantForm, self).save(commit=commit)
146
147 class Meta:
148 model = VRouterTenant
Zack Williamsedbf95f2016-06-27 08:31:44 -0700149 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -0700150
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700151
Scott Baker87eb7402016-06-20 17:21:50 -0700152class VRouterTenantAdmin(ReadOnlyAwareAdmin):
Scott Baker7c241cd2017-07-18 10:54:14 -0700153 list_display = ('backend_status_icon', 'id', 'public_ip')
Scott Baker87eb7402016-06-20 17:21:50 -0700154 list_display_links = ('backend_status_icon', 'id')
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700155 fieldsets = [(None, {
156 'fields': [
Scott Baker7c241cd2017-07-18 10:54:14 -0700157 'backend_status_text', 'owner',
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700158 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'],
159 'classes':['suit-tab suit-tab-general']
160 })]
Scott Baker87eb7402016-06-20 17:21:50 -0700161 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr')
Scott Baker7c241cd2017-07-18 10:54:14 -0700162 inlines = (ProviderLinkInline, SubscriberLinkInline)
Scott Baker87eb7402016-06-20 17:21:50 -0700163 form = VRouterTenantForm
164
Scott Baker7c241cd2017-07-18 10:54:14 -0700165 suit_form_tabs = (('general', 'Details'), ('servicelinks','Links'),)
Scott Baker87eb7402016-06-20 17:21:50 -0700166
Zack Williamsbde22ab2016-06-27 12:27:01 -0700167 def get_queryset(self, request):
Scott Bakerb03e0a72017-05-25 09:38:12 -0700168 return VRouterTenant.select_by_user(request.user)
Scott Baker87eb7402016-06-20 17:21:50 -0700169
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700170
171class VRouterDeviceAdmin(ReadOnlyAwareAdmin):
172 list_display = ('name', 'openflow_id', 'config_key', 'driver')
173 fieldsets = [(None, {
174 'fields': ['name', 'openflow_id', 'vrouter_service', 'config_key', 'driver'],
175 'classes':['suit-tab suit-tab-general']
176 })]
177 inlines = [VRouterPortInline]
178
179 suit_form_tabs = (
180 ('general', 'Device Details'),
181 ('vrouter_ports', 'Ports'),
182 )
183
184
185class VRouterPortAdmin(ReadOnlyAwareAdmin):
186 list_display = ('name', 'openflow_id', 'vrouter_device')
187 fieldsets = [(None, {
188 'fields': ['name', 'openflow_id', 'vrouter_service', 'vrouter_device'],
189 'classes':['suit-tab suit-tab-general']
190 })]
191 inlines = [VRouterInterfaceInline]
192
193 suit_form_tabs = (
194 ('general', 'Ports Details'),
195 ('vrouter_interfaces', 'Interfaces'),
196 )
197
198
199class VRouterInterfaceAdmin(ReadOnlyAwareAdmin):
200 list_display = ('name', 'mac', 'vlan')
201 fieldsets = [(None, {
202 'fields': ['name', 'vrouter_port', 'mac', 'vlan'],
203 'classes':['suit-tab suit-tab-general']
204 })]
205 inlines = [VRouterIpInline]
206
207 suit_form_tabs = (
208 ('general', 'Interfaces Details'),
209 ('vrouter_ips', 'Ips'),
210 )
211
212
213class VRouterIpAdmin(ReadOnlyAwareAdmin):
214 list_display = ('name', 'ip', 'vrouter_interface')
215 fieldsets = [(None, {'fields': ['name', 'ip', 'vrouter_interface']})]
216
217
218class VRouterAppAdmin(ReadOnlyAwareAdmin):
219 list_display = ('name', 'control_plane_connect_point', 'ospf_enabled')
220 fieldsets = [(None, {'fields': ['name', 'vrouter_service', 'control_plane_connect_point', 'ospf_enabled']})]
221
222
Scott Baker87eb7402016-06-20 17:21:50 -0700223admin.site.register(VRouterService, VRouterServiceAdmin)
224admin.site.register(VRouterTenant, VRouterTenantAdmin)
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -0700225admin.site.register(VRouterDevice, VRouterDeviceAdmin)
226admin.site.register(VRouterPort, VRouterPortAdmin)
227admin.site.register(VRouterInterface, VRouterInterfaceAdmin)
228admin.site.register(VRouterIp, VRouterIpAdmin)
229admin.site.register(VRouterApp, VRouterAppAdmin)
Scott Baker87eb7402016-06-20 17:21:50 -0700230