Matteo Scandolo | d02b73b | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 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 Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 17 | from django.contrib import admin |
| 18 | |
| 19 | from services.vrouter.models import * |
| 20 | from django import forms |
| 21 | from django.utils.safestring import mark_safe |
| 22 | from django.contrib.auth.admin import UserAdmin |
| 23 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 24 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 25 | from django.contrib.auth.signals import user_logged_in |
| 26 | from django.utils import timezone |
| 27 | from django.contrib.contenttypes import generic |
| 28 | from suit.widgets import LinkedSelect |
| 29 | from core.models import AddressPool |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 30 | from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 31 | from core.middleware import get_request |
| 32 | |
| 33 | from functools import update_wrapper |
| 34 | from django.contrib.admin.views.main import ChangeList |
| 35 | from django.core.urlresolvers import reverse |
| 36 | from django.contrib.admin.utils import quote |
| 37 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 38 | |
| 39 | class VRouterPortInline(XOSTabularInline): |
| 40 | model = VRouterPort |
| 41 | fields = ['openflow_id'] |
| 42 | suit_classes = 'suit-tab suit-tab-vrouter_ports' |
| 43 | extra = 0 |
| 44 | |
| 45 | |
| 46 | class VRouterInterfaceInline(XOSTabularInline): |
| 47 | model = VRouterInterface |
| 48 | fields = ['name', 'mac', 'vlan'] |
| 49 | suit_classes = 'suit-tab suit-tab-vrouter_interfaces' |
| 50 | extra = 0 |
| 51 | |
| 52 | |
| 53 | class VRouterIpInline(XOSTabularInline): |
| 54 | model = VRouterIp |
| 55 | fields = ['ip'] |
| 56 | suit_classes = 'suit-tab suit-tab-vrouter_ips' |
| 57 | extra = 0 |
| 58 | |
| 59 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 60 | class VRouterServiceForm(forms.ModelForm): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 61 | def __init__(self, *args, **kwargs): |
| 62 | super(VRouterServiceForm, self).__init__(*args, **kwargs) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 63 | |
| 64 | def save(self, commit=True): |
| 65 | return super(VRouterServiceForm, self).save(commit=commit) |
| 66 | |
| 67 | class Meta: |
| 68 | model = VRouterService |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 69 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 70 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 71 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 72 | class 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 Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 78 | 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 Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 102 | inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline, ProviderDependencyInline,SubscriberDependencyInline] |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 103 | form = VRouterServiceForm |
| 104 | |
| 105 | extracontext_registered_admins = True |
| 106 | |
| 107 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 108 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 109 | suit_form_tabs = ( |
| 110 | ('general', 'vRouter Service Details'), |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 111 | ('administration', 'Administration'), |
| 112 | ('addresspools', 'Addresses'), |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 113 | # ('tools', 'Tools'), |
| 114 | ('slices', 'Slices'), |
| 115 | ('serviceattrs', 'Additional Attributes'), |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 116 | ('servicetenants', 'Dependencies'), |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 117 | ('serviceprivileges', 'Privileges'), |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 118 | ) |
| 119 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 120 | suit_form_includes = ('vrouteradmin.html', 'top', 'administration') # ('hpctools.html', 'top', 'tools') ) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 121 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 122 | def get_queryset(self, request): |
Scott Baker | b03e0a7 | 2017-05-25 09:38:12 -0700 | [diff] [blame] | 123 | return VRouterService.select_by_user(request.user) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 125 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 126 | class VRouterTenantForm(forms.ModelForm): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 127 | gateway_ip = forms.CharField(required=False) |
| 128 | gateway_mac = forms.CharField(required=False) |
| 129 | cidr = forms.CharField(required=False) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 130 | |
| 131 | def __init__(self,*args,**kwargs): |
| 132 | super (VRouterTenantForm,self ).__init__(*args,**kwargs) |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 133 | self.fields['owner'].queryset = VRouterService.objects.all() |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 134 | if self.instance: |
| 135 | # fields for the attributes |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 136 | 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 Baker | d94cbfc | 2017-03-13 11:40:46 -0700 | [diff] [blame] | 141 | if VRouterService.objects.exists(): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 142 | self.fields["owner"].initial = VRouterService.objects.first() |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 143 | |
| 144 | def save(self, commit=True): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 145 | return super(VRouterTenantForm, self).save(commit=commit) |
| 146 | |
| 147 | class Meta: |
| 148 | model = VRouterTenant |
Zack Williams | edbf95f | 2016-06-27 08:31:44 -0700 | [diff] [blame] | 149 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 150 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 151 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 152 | class VRouterTenantAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 153 | list_display = ('backend_status_icon', 'id', 'public_ip') |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 154 | list_display_links = ('backend_status_icon', 'id') |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 155 | fieldsets = [(None, { |
| 156 | 'fields': [ |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 157 | 'backend_status_text', 'owner', |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 158 | 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'], |
| 159 | 'classes':['suit-tab suit-tab-general'] |
| 160 | })] |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 161 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr') |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 162 | inlines = (ProviderLinkInline, SubscriberLinkInline) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 163 | form = VRouterTenantForm |
| 164 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 165 | suit_form_tabs = (('general', 'Details'), ('servicelinks','Links'),) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 166 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 167 | def get_queryset(self, request): |
Scott Baker | b03e0a7 | 2017-05-25 09:38:12 -0700 | [diff] [blame] | 168 | return VRouterTenant.select_by_user(request.user) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 169 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 170 | |
| 171 | class 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 | |
| 185 | class 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 | |
| 199 | class 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 | |
| 213 | class VRouterIpAdmin(ReadOnlyAwareAdmin): |
| 214 | list_display = ('name', 'ip', 'vrouter_interface') |
| 215 | fieldsets = [(None, {'fields': ['name', 'ip', 'vrouter_interface']})] |
| 216 | |
| 217 | |
| 218 | class 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 Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 223 | admin.site.register(VRouterService, VRouterServiceAdmin) |
| 224 | admin.site.register(VRouterTenant, VRouterTenantAdmin) |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 225 | admin.site.register(VRouterDevice, VRouterDeviceAdmin) |
| 226 | admin.site.register(VRouterPort, VRouterPortAdmin) |
| 227 | admin.site.register(VRouterInterface, VRouterInterfaceAdmin) |
| 228 | admin.site.register(VRouterIp, VRouterIpAdmin) |
| 229 | admin.site.register(VRouterApp, VRouterAppAdmin) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 230 | |