Example #1
0
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)
Example #2
0
def api_settings(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':

                settings = {}
                sync_state = 'in_sync'
                # Check if is syncing
                for thread in threading.enumerate():
                    if thread.name == 'NSO_sync':
                        if thread.isAlive():
                            sync_state = 'performing_sync'

                settings['nso'] = {
                    'address': envs.get_nso_ip(),
                    'http_port': envs.get_nso_rest_port(),
                    'netconf_port': envs.get_nso_netconf_port(),
                    'package_directory': envs.get_nso_packages_dir(),
                    'sync_state': sync_state
                }
                # Fetch keys
                return JSONResponse(settings)
            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)
Example #3
0
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')
Example #4
0
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)
Example #5
0
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)