def add_device(request): if request.method == 'POST': try: payload = json.loads(request.body) if payload['name']: # Save it to NSO netconf_controller = ApiNetconf(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) netconf_controller.create('device', device_name=payload['name'], device_ip=payload['ip'], device_authgroup_name=payload['authgroup'], device_port=payload['port'], device_type=payload['device_type']) rest_controller = ApiRest(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_rest_port()) rest_controller.fetch_ssh_keys() rest_controller.sync_from_devices() # device_model = db_controller.get_first_model(id=payload['model']['id']) # Save it to local database to associate the model # db_controller.add_device(name=payload['name']) return HttpResponse('Ok') except Exception as e: return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) else: return render(request, 'web_app/devices/add_device.html')
def api_alerts(request): """ GET: List all alerts in NSO :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'GET': rest_controller = ApiRestNSO(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_rest_port()) alarms = rest_controller.get_alerts() # Fetch keys return JSONResponse(alarms) else: return JSONResponse("Bad request. " + request.method + " is not supported", status=400) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse( "Bad request. HTTP_AUTHORIZATION header is required", status=400)
def api_authgroups(request): """ GET: List all authgroups from NSO :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'GET': try: controller = APINetconfNso(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) result = controller.get_config('nso/get_authgroup_filter') response_data = [] if type(result['rpc-reply']['data']['devices']['authgroups']['group']).__name__ == "OrderedDict": response_data.append({ 'name': result['rpc-reply']['data']['devices']['authgroups']['group']['name'] }) else: for authgroup in result['rpc-reply']['data']['devices']['authgroups']['group']: response_data.append({ 'name': authgroup['name'] }) return JSONResponse(response_data) except Exception as e: return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) else: return JSONResponse("Bad request. " + request.method + " is not supported", status=400) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse("Bad request. HTTP_AUTHORIZATION header is required", status=400)
def api_delete_running_services(request): """ POST: Delete a service :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'POST': try: # Parse the json payload = json.loads(request.body) # Create a new netconf nso controller nso_controller = APINetconfNso(user=envs.get_nso_user(), password=envs.get_nso_password(), ip=envs.get_nso_ip(), port=envs.get_nso_netconf_port()) # Send service request to NSO nso_controller.delete_service(type=payload['type'], xmlns=payload['xmlns'], name=payload[payload['service_key']], key=payload['service_key']) return JSONResponse('ok') except Exception as e: print traceback.print_exc() # return the error to web client return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse("Bad request. HTTP_AUTHORIZATION header is required", status=400)
def api_delete_devices(request): """ POST: Delete device from NSO :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'POST': print "*** Delete device from NSO ***" try: payload = json.loads(request.body) controller = APINetconfNso(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) controller.delete_device(name=payload['name']) return JSONResponse('Ok') except Exception as e: print traceback.print_exc() return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) else: return JSONResponse("Bad request. " + request.method + " is not supported", status=400) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse("Bad request. HTTP_AUTHORIZATION header is required", status=400)
def list_authgroup_json(request): try: controller = ApiNetconf(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) result = controller.get_config('get_authgroup_filter') response_data = [] for authgroup in result['rpc-reply']['data']['devices']['authgroups']['group']: response_data.append({ 'name': authgroup['name'] }) return JSONResponse(response_data) except Exception as e: return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500)
def delete_device(request): if request.method == 'POST': payload = json.loads(request.body) if payload['name']: try: controller = ApiNetconf(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) controller.delete('device', device_name=payload['name']) # device_id = db_controller.get_first_device(name=payload['name']).id # db_controller.delete_device(device_id) except Exception as e: return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) return HttpResponse('Ok')
def basic_config(request): if request.method == 'POST': payload = json.loads(request.body) if payload['name']: try: controller = ApiNetconf(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) controller.edit_config('basic_config_xr', device_name=payload['name'], hostname=payload['changed_configuration']['hostname'], ntp_server_new=payload['changed_configuration']['ntp_server'], ntp_server_current=payload['current_configuration']['ntp_server'], dns_server_new=payload['changed_configuration']['dns_server'], dns_server_current=payload['current_configuration']['dns_server']) except Exception as e: return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500) return HttpResponse('ok') else: return render(request, 'web_app/device_basic_config/basic_config.html')
def api_running_services(request, ): """ GET: List all running services from NSO POST: Creates a new service :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'GET': try: # Create a new api controller nso_packages_controller = ApiPackagesNSO( envs.get_nso_server_user(), envs.get_nso_server_password(), envs.get_nso_ip()) # Get Services definition nso_services = nso_packages_controller.get_services_cache() # Create a new netconf nso controller nso_controller = ApiRestNSO( user=envs.get_nso_user(), password=envs.get_nso_password(), ip=envs.get_nso_ip(), port=envs.get_nso_rest_port()) nso_running_services = [] # Assembly response for service_definition in nso_services: running_services = nso_controller.get_running_services( service_name=service_definition['module']['name']) for running_service in running_services: running_service[ 'service_key'] = service_definition['module'][ 'augment']['list']['key']['value'] nso_running_services.append({ 'data': running_services, 'name': service_definition['module']['name'] }) return JSONResponse(nso_running_services) except Exception as e: print traceback.print_exc() # return the error to web client return JSONResponse( { 'error': e.__class__.__name__, 'message': str(e) }, status=500) elif request.method == 'POST': try: # Parse the json service payload = json.loads(request.body) # Create a new netconf nso controller nso_controller = APINetconfNso( user=envs.get_nso_user(), password=envs.get_nso_password(), ip=envs.get_nso_ip(), port=envs.get_nso_netconf_port()) # Creates the xml from the request payload service_xml = parser.json_to_nso_service_xml(payload) # Send service request to NSO nso_controller.send_service(service_xml) return JSONResponse('ok') except Exception as e: print traceback.print_exc() # return the error to web client return JSONResponse( { 'error': e.__class__.__name__, 'message': str(e) }, status=500) else: return JSONResponse("Bad request. " + request.method + " is not supported", status=400) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse( "Bad request. HTTP_AUTHORIZATION header is required", status=400)
def api_devices(request): """ POST: Add device to NSO GET: List all devices in NSO :param request: :return: """ # Check credentials if 'HTTP_AUTHORIZATION' in request.META: if is_token_valid(request.META['HTTP_AUTHORIZATION'].split(' ')[1]): if request.method == 'POST': print "*** Adding New Device to NSO ***" try: payload = json.loads(request.body) # Create NSO controller netconf_controller = APINetconfNso( envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) # Required parameters name = payload['name'] device_type = payload['device_type'] ip = payload['ip'] authgroup = payload['authgroup'] port = payload['port'] # Optionals protocol = None ned_id = None ned = models.NED(xmlns='') if payload['device_type'] == 'cli': protocol = payload['protocol'] ned_id = payload['ned_id'] ned = db_controller.get_first_ned( ned_id=payload['ned_id']) netconf_controller.add_device(name=name, device_type=device_type, ip=ip, authgroup=authgroup, protocol=protocol, ned_id=ned_id, ned_xmlns=ned.xmlns, port=port) rest_controller = ApiRestNSO(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_rest_port()) # Fetch keys rest_controller.fetch_ssh_keys() # Sync configuration rest_controller.sync_from_devices() return JSONResponse('Ok') except Exception as e: print traceback.print_exc() return JSONResponse( { 'error': e.__class__.__name__, 'message': str(e) }, status=500) elif request.method == 'GET': try: # Create a new api controller nso_controller = APINetconfNso(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) data = nso_controller.get_devices() return JSONResponse(data) except Exception as e: print traceback.print_exc() # return the error to web client return JSONResponse( { 'error': e.__class__.__name__, 'message': str(e) }, status=500) else: return JSONResponse("Bad request. " + request.method + " is not supported", status=400) else: return JSONResponse("Invalid Token", status=401) else: return JSONResponse( "Bad request. HTTP_AUTHORIZATION header is required", status=400)
def list_devices_json(request): """ Return a json list of devices registered in NSO :param request: :return: """ try: # Create a new api controller controller = ApiNetconf(envs.get_nso_user(), envs.get_nso_password(), envs.get_nso_ip(), envs.get_nso_netconf_port()) # Get the devices result = controller.get_config('get_devices_filter') response_data = [] device_model = '' nso_device_list = [] # If it is an ordered dictionary only brings up one element if type(result['rpc-reply']['data']['devices']['device']).__name__ == 'OrderedDict': # Append to the list nso_device_list.append(result['rpc-reply']['data']['devices']['device']) else: # Assign to the list nso_device_list = result['rpc-reply']['data']['devices']['device'] for nso_device in nso_device_list: # Traverse elements to get needed config ntp_server = '' hostname = '' dns_server = '' # Device type config if nso_device['device-type'].__contains__('cli'): device_model = {'id': nso_device['device-type']['cli']['ned-id']['#text'], 'name': nso_device['device-type']['cli']['ned-id']['#text'].split(':')[1]} # NTP Server Config if nso_device['config'].has_key('ntp'): # If it is an ordered dictionary only brings up one element if type(nso_device['config']['ntp']['server']['server-list']).__name__ == 'OrderedDict': ntp_server = nso_device['config']['ntp']['server']['server-list']['name'] else: # It is treated like a list ntp_server = nso_device['config']['ntp']['server']['server-list'][0]['name'] # Hostname if nso_device['config'].has_key('hostname'): hostname = nso_device['config']['hostname']['#text'] # Domain name if nso_device['config'].has_key('domain'): if nso_device['config']['domain'].has_key('name-server'): dns_server = nso_device['config']['domain']['name-server']['address'] # Append all device data as an item list response_data.append({ 'name': nso_device['name'], 'ip': nso_device['address'], 'model': device_model, 'current_configuration': { 'hostname': hostname, 'ntp_server': ntp_server, 'dns_server': dns_server }, 'changed_configuration': { # Used to request changes to configs from the web client 'hostname': '', 'ntp_server': '', 'dns_server': '' } }) # Write data and return response to web client return JSONResponse(response_data) except Exception as e: # return the error to web client return JSONResponse({'error': e.__class__.__name__, 'message': str(e)}, status=500)