Andy Bavier | 03df22b | 2017-08-30 14:46:02 -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 | |
| 17 | from django.contrib import admin |
| 18 | |
| 19 | from services.volt.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.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline |
| 30 | from core.middleware import get_request |
| 31 | |
| 32 | from functools import update_wrapper |
| 33 | from django.contrib.admin.views.main import ChangeList |
| 34 | from django.core.urlresolvers import reverse |
| 35 | from django.contrib.admin.utils import quote |
| 36 | |
| 37 | #----------------------------------------------------------------------------- |
| 38 | # vOLT |
| 39 | #----------------------------------------------------------------------------- |
| 40 | |
| 41 | class VOLTServiceAdmin(ReadOnlyAwareAdmin): |
| 42 | model = VOLTService |
| 43 | verbose_name = "vOLT Service" |
| 44 | verbose_name_plural = "vOLT Service" |
| 45 | list_display = ("backend_status_icon", "name", "enabled") |
| 46 | list_display_links = ('backend_status_icon', 'name', ) |
| 47 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})] |
| 48 | readonly_fields = ('backend_status_text', ) |
| 49 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline,ProviderDependencyInline,SubscriberDependencyInline] |
| 50 | |
| 51 | extracontext_registered_admins = True |
| 52 | |
| 53 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 54 | |
| 55 | suit_form_tabs =(('general', 'vOLT Service Details'), |
| 56 | ('administration', 'Administration'), |
| 57 | #('tools', 'Tools'), |
| 58 | ('slices','Slices'), |
| 59 | ('serviceattrs','Additional Attributes'), |
| 60 | ('servicetenants', 'Dependencies'), |
| 61 | ('serviceprivileges','Privileges'), |
| 62 | ) |
| 63 | |
| 64 | suit_form_includes = (('voltadmin.html', 'top', 'administration'), |
| 65 | ) #('hpctools.html', 'top', 'tools') ) |
| 66 | |
| 67 | class VOLTTenantForm(forms.ModelForm): |
| 68 | s_tag = forms.CharField() |
| 69 | c_tag = forms.CharField() |
| 70 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 71 | |
| 72 | def __init__(self,*args,**kwargs): |
| 73 | super (VOLTTenantForm,self ).__init__(*args,**kwargs) |
| 74 | self.fields['owner'].queryset = VOLTService.objects.all() |
| 75 | if self.instance: |
| 76 | # fields for the attributes |
| 77 | self.fields['c_tag'].initial = self.instance.c_tag |
| 78 | self.fields['s_tag'].initial = self.instance.s_tag |
| 79 | self.fields['creator'].initial = self.instance.creator |
| 80 | if (not self.instance) or (not self.instance.pk): |
| 81 | # default fields for an 'add' form |
| 82 | self.fields['creator'].initial = get_request().user |
| 83 | if VOLTService.objects.exists(): |
| 84 | self.fields["owner"].initial = VOLTService.objects.all()[0] |
| 85 | |
| 86 | def save(self, commit=True): |
| 87 | self.instance.s_tag = self.cleaned_data.get("s_tag") |
| 88 | self.instance.c_tag = self.cleaned_data.get("c_tag") |
| 89 | self.instance.creator = self.cleaned_data.get("creator") |
| 90 | return super(VOLTTenantForm, self).save(commit=commit) |
| 91 | |
| 92 | class Meta: |
| 93 | model = VOLTTenant |
| 94 | fields = '__all__' |
| 95 | |
| 96 | |
| 97 | class VOLTTenantAdmin(ReadOnlyAwareAdmin): |
| 98 | list_display = ('backend_status_icon', 'id', 'service_specific_id', 's_tag', 'c_tag', ) |
| 99 | list_display_links = ('backend_status_icon', 'id') |
| 100 | fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', 'service_specific_id', # 'service_specific_attribute', |
| 101 | 's_tag', 'c_tag', 'creator'], |
| 102 | 'classes':['suit-tab suit-tab-general']})] |
| 103 | readonly_fields = ('backend_status_text', 'service_specific_attribute') |
| 104 | inlines = (ProviderLinkInline, SubscriberLinkInline) |
| 105 | form = VOLTTenantForm |
| 106 | |
| 107 | suit_form_tabs = (('general','Details'), ('servicelinks','Links'),) |
| 108 | |
| 109 | def get_queryset(self, request): |
| 110 | return VOLTTenant.select_by_user(request.user) |
| 111 | |
| 112 | class AccessDeviceInline(XOSTabularInline): |
| 113 | model = AccessDevice |
| 114 | fields = ['volt_device','uplink','vlan'] |
| 115 | readonly_fields = [] |
| 116 | extra = 0 |
| 117 | # max_num = 0 |
| 118 | suit_classes = 'suit-tab suit-tab-accessdevices' |
| 119 | |
| 120 | # @property |
| 121 | # def selflink_reverse_path(self): |
| 122 | # return "admin:cord_volttenant_change" |
| 123 | |
| 124 | class VOLTDeviceAdmin(ReadOnlyAwareAdmin): |
| 125 | list_display = ('backend_status_icon', 'name', 'openflow_id', 'driver' ) |
| 126 | list_display_links = ('backend_status_icon', 'name', 'openflow_id') |
| 127 | fieldsets = [ (None, {'fields': ['backend_status_text','name','volt_service','openflow_id','driver','access_agent'], |
| 128 | 'classes':['suit-tab suit-tab-general']})] |
| 129 | readonly_fields = ('backend_status_text',) |
| 130 | inlines = [AccessDeviceInline] |
| 131 | |
| 132 | suit_form_tabs = (('general','Details'), ('accessdevices','Access Devices')) |
| 133 | |
| 134 | class AccessDeviceAdmin(ReadOnlyAwareAdmin): |
| 135 | list_display = ('backend_status_icon', 'id', 'volt_device', 'uplink', 'vlan' ) |
| 136 | list_display_links = ('backend_status_icon', 'id') |
| 137 | fieldsets = [ (None, {'fields': ['backend_status_text','volt_device','uplink','vlan'], |
| 138 | 'classes':['suit-tab suit-tab-general']})] |
| 139 | readonly_fields = ('backend_status_text',) |
| 140 | |
| 141 | suit_form_tabs = (('general','Details'),) |
| 142 | |
| 143 | class AgentPortMappingInline(XOSTabularInline): |
| 144 | model = AgentPortMapping |
| 145 | fields = ['access_agent', 'mac', 'port'] |
| 146 | readonly_fields = [] |
| 147 | extra = 0 |
| 148 | # max_num = 0 |
| 149 | suit_classes = 'suit-tab suit-tab-accessportmaps' |
| 150 | |
| 151 | # @property |
| 152 | # def selflink_reverse_path(self): |
| 153 | # return "admin:cord_volttenant_change" |
| 154 | |
| 155 | class AccessAgentAdmin(ReadOnlyAwareAdmin): |
| 156 | list_display = ('backend_status_icon', 'name', 'mac' ) |
| 157 | list_display_links = ('backend_status_icon', 'name') |
| 158 | fieldsets = [ (None, {'fields': ['backend_status_text','name','volt_service','mac'], |
| 159 | 'classes':['suit-tab suit-tab-general']})] |
| 160 | readonly_fields = ('backend_status_text',) |
| 161 | inlines= [AgentPortMappingInline] |
| 162 | |
| 163 | suit_form_tabs = (('general','Details'), ('accessportmaps', 'Port Mappings')) |
| 164 | |
| 165 | admin.site.register(VOLTService, VOLTServiceAdmin) |
| 166 | admin.site.register(VOLTTenant, VOLTTenantAdmin) |
| 167 | admin.site.register(VOLTDevice, VOLTDeviceAdmin) |
| 168 | admin.site.register(AccessDevice, AccessDeviceAdmin) |
| 169 | admin.site.register(AccessAgent, AccessAgentAdmin) |
| 170 | |
| 171 | |