def save_all_topologies(request): """ Generate topology files for all ASes or specific ASes in a ISD. :param HttpRequest request: Django HTTP request passed on through urls.py :returns: Django HTTP Response object. :rtype: HttpResponse. """ current_page = request.META.get('HTTP_REFERER') topology_params = request.POST.copy() isd_list = topology_params.getlist('ISD') for isd in isd_list: for ad_obj in AD.objects.filter(isd_id=isd): isd_as_obj = ISD_AS.from_values(ad_obj.isd_id, ad_obj.as_id) isd_as = str(isd_as_obj) topo_dict = ad_obj.original_topology # write the topology file create_local_gen(isd_as, topo_dict) addr_list = [] cloud_engine_list = [] host_name_list = [] for cloud in CloudMachine.objects.filter(ad_id=ad_obj): addr_list.append(cloud.addr) cloud_engine_list.append(cloud.cloud_provider) host_name_list.append(cloud.host_name) topology_params.setlist('inputCloudAddress', addr_list) topology_params.setlist('inputCloudEngine', cloud_engine_list) topology_params.setlist('inputHostname', host_name_list) commit_hash = ad_obj.commit_hash # sanitize commit hash from comments, take first part up to |, strip spaces commit_hash = (commit_hash.split('|'))[0].strip() generate_ansible_hostfile(topology_params, topo_dict, isd_as, commit_hash) return redirect(current_page)
def add_to_topology(request): """ Adds the router information which comes with a connection reply into the topology of the AS. :param HttpRequest request: Django HTTP request. """ con_reply = json.loads(request.body.decode('utf-8')) # find the corresponding connection request from DB try: con_req = ConnectionRequest.objects.get(id=con_reply['RequestId']) except ConnectionRequest.DoesNotExist: logger.error("Connection request for reply with ID %s not found", con_reply['RequestId']) return HttpResponseNotFound( "Connection request for reply %s not found" % con_reply['RequestId']) # find the corresponding router ip = con_req.router_public_ip port = con_req.router_public_port try: router_intf = BorderRouterInterface.objects.get(addr=ip, l4port=port) router_addr = router_intf.router_addr router = router_addr.router except BorderRouterAddress.DoesNotExist: logger.error("Router for connection reply with ID %s not found.", con_reply['RequestId']) return HttpResponseNotFound("Router for connection reply with ID %s " "not found." % con_reply['RequestId']) isd_id, as_id = ISD_AS(con_reply['RequestIA']) try: req_ia = AD.objects.get(isd_id=isd_id, as_id=as_id) except AD.DoesNotExist: logger.error("AS %s was not found." % con_reply['RequestIA']) return HttpResponseNotFound("AS %s was not found" % con_reply['RequestIA']) topo = req_ia.original_topology interface = topo['BorderRouters'][router.name]['Interfaces'][str( router_intf.interface_id)] interface['Remote']['Addr'] = con_reply['IP'] if "UDP" in con_reply['OverlayType']: interface['Remote']['L4Port'] = con_reply['Port'] # TODO(ercanucan): verify the other parameters of the request as well? req_ia.save() # write the updated topology file create_local_gen(con_reply['RequestIA'], topo) # save the data into DB req_ia.fill_from_topology(topo, clear=True) return HttpResponse("Successfully added to topology of %s" % router.name)
def add_to_topology(request): """ Adds the router information which comes with a connection reply into the topology of the AS. :param HttpRequest request: Django HTTP request. """ con_reply = json.loads(request.body.decode('utf-8')) # find the corresponding connection request from DB try: con_req = ConnectionRequest.objects.get(id=con_reply['RequestId']) except ConnectionRequest.DoesNotExist: logger.error("Connection request for reply with ID %s not found", con_reply['RequestId']) return HttpResponseNotFound("Connection request for reply %s not found" % con_reply['RequestId']) # find the corresponding router ip = con_req.router_public_ip port = con_req.router_public_port try: router_intf = BorderRouterInterface.objects.get(addr=ip, l4port=port) router_addr = router_intf.router_addr router = router_addr.router except BorderRouterAddress.DoesNotExist: logger.error("Router for connection reply with ID %s not found.", con_reply['RequestId']) return HttpResponseNotFound("Router for connection reply with ID %s " "not found." % con_reply['RequestId']) isd_id, as_id = TopoID(con_reply['RequestIA']) try: req_ia = AD.objects.get(isd_id=isd_id, as_id=as_id) except AD.DoesNotExist: logger.error("AS %s was not found." % con_reply['RequestIA']) return HttpResponseNotFound("AS %s was not found" % con_reply['RequestIA']) topo = req_ia.original_topology interface = topo['BorderRouters'][router.name]['Interfaces'][str(router_intf.interface_id)] interface['Remote']['Addr'] = con_reply['IP'] if "UDP" in con_reply['OverlayType']: interface['Remote']['L4Port'] = con_reply['Port'] # TODO(ercanucan): verify the other parameters of the request as well? req_ia.save() # write the updated topology file create_local_gen(con_reply['RequestIA'], topo) # save the data into DB req_ia.fill_from_topology(topo, clear=True) return HttpResponse("Successfully added to topology of %s" % router.name)
def save_all_topologies(request): """ Generate topology files for all ASes or specific ASes in a ISD. :param HttpRequest request: Django HTTP request passed on through urls.py :returns: Django HTTP Response object. :rtype: HttpResponse. """ current_page = request.META.get('HTTP_REFERER') topology_params = request.POST.copy() isd_list = topology_params.getlist('ISD') for isd in isd_list: for ad_obj in AD.objects.filter(isd_id=isd): isd_as = TopoID.from_values(ad_obj.isd_id, ad_obj.as_id) topo_dict = ad_obj.original_topology # TODO: in the DB there is at least one entry (ffaa:0:1306) with {} if len(topo_dict) == 0: continue # write the topology file create_local_gen(isd_as, topo_dict) addr_list = [] cloud_engine_list = [] host_name_list = [] for cloud in CloudMachine.objects.filter(ad_id=ad_obj): addr_list.append(cloud.addr) cloud_engine_list.append(cloud.cloud_provider) host_name_list.append(cloud.host_name) topology_params.setlist('inputCloudAddress', addr_list) topology_params.setlist('inputCloudEngine', cloud_engine_list) topology_params.setlist('inputHostname', host_name_list) commit_hash = ad_obj.commit_hash # sanitize commit hash from comments, take first part up to |, strip spaces commit_hash = (commit_hash.split('|'))[0].strip() generate_ansible_hostfile(topology_params, topo_dict, isd_as, commit_hash) return redirect(current_page)
def generate_topology(request): # TODO(ercanucan): This function should be refactored into smaller pieces. topology_params = request.POST.copy() topology_params.pop('csrfmiddlewaretoken', None) # remove csrf entry, as we don't need it here topo_dict = {} tp = topology_params isd_as = TopoID(tp['inputISD_AS']) topo_dict['Core'] = True if (tp['inputIsCore'] == 'on') else False service_types = ['BeaconService', 'CertificateService', 'PathService'] for s_type in service_types: topo_dict[s_type] = \ name_entry_dict(tp.getlist('input{}Name'.format(s_type)), tp.getlist('input{}Address'.format(s_type)), tp.getlist('input{}Port'.format(s_type)), tp.getlist('input{}InternalAddress'.format(s_type)), tp.getlist('input{}InternalPort'.format(s_type)), ) topo_dict['BorderRouters'] = name_entry_dict_router(tp) topo_dict['ISD_AS'] = tp['inputISD_AS'] topo_dict['MTU'] = st_int(tp['inputMTU'], DEFAULT_MTU) # TODO(jonghoonkwon): We currently assume that the overlay network is 'UDP/IPv4' topo_dict['Overlay'] = 'UDP/IPv4' # Zookeeper special case s_type = 'ZookeeperServer' zk_dict = name_entry_dict_zk(tp.getlist('input{}Name'.format(s_type)), tp.getlist('input{}Address'.format(s_type)), tp.getlist('input{}Port'.format(s_type)), tp.getlist( 'input{}InternalAddress'.format(s_type)), tp.getlist('input{}InternalPort'.format(s_type)), ) named_keys = list(zk_dict.keys()) # copy 'named' keys int_key = 1 # dict keys get replaced with numeric keys, 1 based for key in named_keys: zk_dict[int_key] = zk_dict.pop(key) int_key += 1 topo_dict['ZookeeperService'] = zk_dict # IP:port uniqueness in AS check all_ip_port_pairs = get_all_ip_port_pairs(topo_dict, service_types) if len(all_ip_port_pairs) != len(set(all_ip_port_pairs)): return JsonResponse( {'data': 'IP:port combinations not unique within AS'}) create_local_gen(isd_as, topo_dict) commit_hash = tp['commitHash'] # sanitize commit hash from comments, take first part up to |, strip spaces commit_hash = (commit_hash.split('|'))[0].strip() generate_ansible_hostfile(topology_params, topo_dict, isd_as, commit_hash) curr_as = get_object_or_404(AD, as_id=isd_as[1], isd=isd_as[0]) # load as usual model (for persistance and display in overview) # TODO : hash displayed queryset and curr_as query set and compare # allow the user to write back the new configuration only if it hasn't # changed in the meantime curr_as.fill_from_topology(topo_dict, clear=True) curr_as.fill_cloud_info(topology_params) current_page = request.META.get('HTTP_REFERER') return redirect(current_page)
def generate_topology(request): # TODO(ercanucan): This function should be refactored into smaller pieces. topology_params = request.POST.copy() topology_params.pop('csrfmiddlewaretoken', None) # remove csrf entry, as we don't need it here topo_dict = {} tp = topology_params isd_as = tp['inputISD_AS'] isd_id, as_id = isd_as.split('-') topo_dict['Core'] = True if (tp['inputIsCore'] == 'on') else False service_types = [ 'BeaconServer', 'CertificateServer', 'PathServer', 'SibraServer' ] for s_type in service_types: section_name = s_type + 's' topo_dict[section_name] = \ name_entry_dict(tp.getlist('input{}Name'.format(s_type)), tp.getlist('input{}Address'.format(s_type)), tp.getlist('input{}Port'.format(s_type)), tp.getlist('input{}InternalAddress'.format(s_type)), tp.getlist('input{}InternalPort'.format(s_type)), ) topo_dict['BorderRouters'] = name_entry_dict_router(tp) topo_dict['ISD_AS'] = tp['inputISD_AS'] topo_dict['MTU'] = st_int(tp['inputMTU'], DEFAULT_MTU) # Zookeeper special case s_type = 'ZookeeperServer' zk_dict = name_entry_dict( tp.getlist('input{}Name'.format(s_type)), tp.getlist('input{}Address'.format(s_type)), tp.getlist('input{}Port'.format(s_type)), tp.getlist('input{}InternalAddress'.format(s_type)), tp.getlist('input{}InternalPort'.format(s_type)), ) named_keys = list(zk_dict.keys()) # copy 'named' keys int_key = 1 # dict keys get replaced with numeric keys, 1 based for key in named_keys: zk_dict[int_key] = zk_dict.pop(key) int_key += 1 topo_dict['Zookeepers'] = zk_dict # IP:port uniqueness in AS check all_ip_port_pairs = [] for r in [ 'BeaconServers', 'CertificateServers', 'PathServers', 'SibraServers', 'Zookeepers' ]: servers_of_type_r = topo_dict[r] for server in servers_of_type_r: curr_pair = servers_of_type_r[server]['Addr'] + ':' + str( servers_of_type_r[server]['Port']) all_ip_port_pairs.append(curr_pair) if len(all_ip_port_pairs) != len(set(all_ip_port_pairs)): return JsonResponse( {'data': 'IP:port combinations not unique within AS'}) os.makedirs(static_tmp_path, exist_ok=True) with open(yaml_topo_path, 'w') as file: yaml.dump(topo_dict, file, default_flow_style=False) create_local_gen(isd_as, topo_dict) commit_hash = tp['commitHash'] # sanitize commit hash from comments, take first part up to |, strip spaces commit_hash = (commit_hash.split('|'))[0].strip() generate_ansible_hostfile(topology_params, topo_dict, isd_as, commit_hash) curr_as = get_object_or_404(AD, as_id=as_id, isd=isd_id) # load as usual model (for persistance and display in overview) # TODO : hash displayed queryset and curr_as query set and compare # allow the user to write back the new configuration only if it hasn't # changed in the meantime curr_as.fill_from_topology(topo_dict, clear=True) current_page = request.META.get('HTTP_REFERER') return redirect(current_page)