srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 3 | # |
| 4 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 5 | # "License"); you may not use this file except in compliance with the |
| 6 | # License. You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.eclipse.org/legal/epl-v10.html |
| 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 |
| 13 | # implied. See the License for the specific language governing |
| 14 | # permissions and limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import os |
| 18 | # this line must be before the next imports |
| 19 | import constants |
| 20 | SDN_ROOT = constants.SDN_ROOT |
| 21 | from sdncon.apploader import AppLoader |
| 22 | from sdncon.urls import urlpatterns |
| 23 | from django.conf.urls.defaults import * |
| 24 | from django.http import HttpResponse |
| 25 | import os.path |
| 26 | |
| 27 | coreui_inited = False |
| 28 | |
| 29 | ROLE_PATH = "%s/current_role" % SDN_ROOT |
| 30 | UPGRADE_PATH = "%s/upgrading" % SDN_ROOT |
| 31 | CASSANDRA_SENTINEL = "%s/run/starting" %SDN_ROOT |
| 32 | |
| 33 | SLAVE_RO_URL_WHITELIST = ["/rest/v1/system", |
| 34 | "/rest/v1/controller/storage/tables", |
| 35 | "/rest/v1/model/controller-node", |
| 36 | "/rest/v1/model/controller-alias", |
| 37 | "/rest/v1/model/syncd-config/default", |
| 38 | "/rest/v1/model/feature", |
| 39 | "/rest/v1/model/statdropd-progress-info", |
| 40 | "/rest/v1/model/statdropd-config/default", |
| 41 | "/rest/v1/model/firewall-rule", |
| 42 | "/rest/v1/model/controller-domain-name-server", |
| 43 | "/rest/v1/stats/metadata", |
| 44 | "/rest/v1/system/ha/clustername", |
| 45 | "/rest/v1/system/ha/role", |
| 46 | "/rest/v1/system/reload", |
| 47 | ] |
| 48 | |
| 49 | SLAVE_RW_URL_WHITELIST = [ |
| 50 | "/rest/v1/stats/data/default", |
| 51 | # Need to make controller-interface RW because the |
| 52 | # discover_ip daemon monitors changes to the network |
| 53 | # interfaces and updates the "discovered_ip" and "mac" |
| 54 | # fields in the controller-interface, even on the slave. |
| 55 | # FIXME: Really those fields should be in a different model so |
| 56 | # we wouldn't be mixing configuration and discovered state, |
| 57 | # but it's too late in the release cycle to make that change. |
| 58 | "/rest/v1/model/controller-interface", |
| 59 | # Needs to be RW, to allow cluster number update |
| 60 | # even if all the controllers are slaves |
| 61 | "/rest/v1/model/global-config", |
| 62 | "/rest/v1/model/firewall-rule", |
| 63 | "/rest/v1/model/controller-node", |
| 64 | "/rest/v1/system/upgrade/image-name", |
| 65 | "/rest/v1/system/upgrade/extract-image-manifest", |
| 66 | "/rest/v1/system/upgrade/execute-upgrade-step", |
| 67 | "/rest/v1/system/upgrade/abort", |
| 68 | "/rest/v1/system/rollback/config", |
| 69 | "/rest/v1/system/ha/decommission", |
| 70 | "/rest/v1/system/ha/role", |
| 71 | "/rest/v1/system/reload", |
| 72 | "/rest/v1/system/resetbsc", |
| 73 | "/rest/v1/system/upload-data", |
| 74 | ] |
| 75 | |
| 76 | class SDNConAppLoaderMiddleWare(object): |
| 77 | def process_request(self, request): |
| 78 | global coreui_inited, urlpatterns |
| 79 | if not coreui_inited: |
| 80 | try: |
| 81 | AppLoader.registerAllApps() |
| 82 | coreui_inited = True |
| 83 | except Exception, e: |
| 84 | print "**** panic! AppLoader.registerAllApps() threw this Exception:" |
| 85 | print e |
| 86 | for index, app in enumerate(AppLoader.apps): |
| 87 | # Redirect / to the first app in the list |
| 88 | if (index == 0): |
| 89 | urlpatterns += patterns( '', (r'^$', 'django.views.generic.simple.redirect_to', {'url': '/'+app.name}),) |
| 90 | urlpatterns += patterns( '', (r'^'+app.name+'/?$', 'sdncon.coreui.views.show_application_tabs', {'app':app.name}) ) |
| 91 | urlpatterns += patterns( '', (r'^'+app.name+'/', include(app.name+'.urls')), ) |
| 92 | for t in app.tabs: |
| 93 | urlpatterns += patterns(app.name+'.views', (r'^'+app.name+'/'+t["id"]+'/?$', t["view"]) ) |
| 94 | |
| 95 | class HARedirectMiddleWare(object): |
| 96 | def process_request (self, request): |
| 97 | pinfo = request.path_info |
| 98 | if pinfo[-1] == "/" : |
| 99 | pinfo = pinfo[:-1] |
| 100 | |
| 101 | if request.method == "GET": |
| 102 | for match in SLAVE_RO_URL_WHITELIST: |
| 103 | if pinfo.startswith(match): |
| 104 | return None |
| 105 | |
| 106 | for match in SLAVE_RW_URL_WHITELIST: |
| 107 | if pinfo.startswith(match): |
| 108 | return None |
| 109 | |
| 110 | role = "MASTER" |
| 111 | try: |
| 112 | with open(ROLE_PATH, "r") as f: |
| 113 | for line in f: |
| 114 | if line.startswith("sdnplatform.role"): |
| 115 | role = line.split("=")[1].strip() |
| 116 | except IOError, e: # Firstboot doesn't have the file |
| 117 | return None |
| 118 | if role == "SLAVE" and os.path.exists(UPGRADE_PATH) == False: |
| 119 | return HttpResponse(status = 303) |
| 120 | else: |
| 121 | return None |