Beispiel #1
0
    def http_post(self, httpurl, body):
        #_,netloc,path,_,_,_= urlparse(httpurl)

        from httpc_httplib import httpclient
        try:
            hc = httpclient()
        except:
            print 'create httpclient fail'
            return
        #print 'send to ',httpurl
        #print 'send body:\n',body

        from xmlTojson import isdk_convert_xml2json

        try:
            body = isdk_convert_xml2json(body)
            body = json.dumps(body,
                              sort_keys=True,
                              indent=4,
                              separators=(', ', ': '))
            body = json.loads(body)
            status, _ = hc._post(httpurl, body)
        except:
            print 'post to %s fail' % httpurl
            return
Beispiel #2
0
    def http_post(self,httpurl,body):        
        #_,netloc,path,_,_,_= urlparse(httpurl)
        
        from httpc_httplib import httpclient
        try:
            hc = httpclient()  
        except:
            print 'create httpclient fail'   
            return
        #print 'send to ',httpurl
        #print 'send body:\n',body        
        
        from xmlTojson import isdk_convert_xml2json

        try:
            body = isdk_convert_xml2json(body)
            body = json.dumps(body, sort_keys=True, indent=4, separators=(', ',': '))
            body=json.loads(body)
            status,_ = hc._post(httpurl,body) 
        except:
            print 'post to %s fail'%httpurl  
            return       
Beispiel #3
0
        return e.message
    
    finally:
        connectOne.free()
         
    if dataObj == None :
        response.status = HTTP_SERVER_ERROR
        return 
    elif dataObj.startswith('<rpc-error') or dataObj.strip().startswith('<rpc-error')  :
        response.status = HTTP_SERVER_ERROR
        
    ## do convert or not by judging the suffix
    if (dataObj == "" or dataObj == None):
        pass
    elif ("json" == responseContentType):
        dataObj = isdk_convert_xml2json(dataObj)
    else:
        dataObj = str(dataObj)
    ## delete the netconf session object.
    end = time.time()
    diff = end-begin
    logger.info('End Message Proc -- %s %s , process time = %fs ', strMethod, recordpath, diff)
    
    #print dataObj
    if (dataObj == None or dataObj == ""):
        response.status = HTTP_RESOURCE_EMPTY
        
    return dataObj


def read_http_body_strategy(dInputBody):
Beispiel #4
0
def general_process(device_id, path):
    
    if (device_id is not None and 
        (not str(device_id).isdigit() or int(device_id) < 0 or int(device_id) > sys.maxint )):
        response.status = HTTP_SERVER_ERROR
        return dict(error='deivce id is digital, greater than 0, and less than %s'% sys.maxint)
    
    begin = time.time()
    strMethod = request.method
    strMethod = strMethod.upper()
    ## get resource's schemapath in URI
    urlfull = request.url
    prefix,schemapath=urlfull.split('/devices/')
    recordpath =  '/devices/'+schemapath  
    logger.info('Start Message Proc -- %s %s', strMethod, recordpath)
    network_device = network_devices.getDevice(device_id)
    if network_device == None:
        response.status = HTTP_SERVER_ERROR
        errorinfo = "can not find device id : %s" % (device_id)
        return errorinfo
    
    isWrite = True
    if strMethod == 'GET':
        isWrite = False
    
    connectOne = None
    try:
        connectOne = network_device.getFreeConnect(isWrite)
    except Exception as e:
        logger.error('Exception when get a free connection : %s %s error:%s', strMethod, recordpath, e)
        response.status = HTTP_SERVER_ERROR
        return ('%s' % e.message)
   
    try:
        
        if connectOne == None:
            response.status = HTTP_SERVER_ERROR
            errorinfo = "can not find free connection on device id : %s" % (device_id)
            return errorinfo
        
        if connectOne.getStatus() == False:
            response.status = HTTP_SERVER_ERROR
            errorinfo = connectOne.errorinfo
            connectOne.free()
            return errorinfo
    except Exception as e:
        connectOne.free()
        logger.error('Exception when find free connection : %s %s error:%s', strMethod, recordpath, e)
        logger.exception(e)
        response.status = HTTP_SERVER_ERROR
        return e.message
        
    try:
        dInputBody = {
        'json_array' : None,
        'xml_input'  : None
        }

        ## get request messege's body
        read_http_body_strategy(dInputBody)
    
        ## if the xpath has suffix, you need to split it.
        lpath = schemapath.split('/')
        if (lpath[len(lpath) - 1] == 'xml' or lpath[len(lpath) - 1] == 'json'):
            lastElem = lpath.pop(-1)
        else:
            lastElem = lpath[len(lpath) - 1]
            
        ## remove the data type and compose the real schema path
        delimiter = '/'
        schemapath =  delimiter.join(lpath)
    
        #  print dInputBody, schemapath
        #  India's interfaces, return the xml data whatever it is.
        if (strMethod == 'GET'): 
            #temp3= time.time()          
            dataObj = connectOne.get_rest_api_handle(dInputBody, schemapath)
            #temp4= time.time()
            #print 'get info:',(temp4-temp3)
        elif (strMethod == 'DELETE'):
            #print dInputBody
            dataObj = connectOne.delete_rest_api_handle(dInputBody, schemapath)
        elif (strMethod == 'PUT'):
            dataObj = connectOne.put_rest_api_handle(dInputBody, schemapath)
        elif (strMethod == 'POST'):
            dataObj = connectOne.post_rest_api_handle(dInputBody, schemapath)
        else:
            ## there may be some other operators extended
            pass
    except Exception as e:
        logger.error('Exception when executing rest operation : %s %s error:%s', strMethod, recordpath, e)
        logger.exception(e)
        response.status = HTTP_SERVER_ERROR
        return e.message
    finally:
         connectOne.free()
         
    if dataObj == None :
        response.status = HTTP_SERVER_ERROR
        return 
    elif dataObj.startswith('<rpc-error') :
        response.status = HTTP_SERVER_ERROR
        
    ## do convert or not by judging the suffix
    if ("json" == lastElem):
        dataObj = isdk_convert_xml2json(dataObj)
    else:
        dataObj = str(dataObj)
    ## delete the netconf session object.
    end = time.time()
    diff = end-begin
    logger.info('End Message Proc -- %s %s , process time = %fs ', strMethod, recordpath, diff)
    
    #print dataObj
    if (dataObj == None):
        response.status = HTTP_RESOURCE_EMPTY
        
    return dataObj
Beispiel #5
0
def general_process(device_id, path):
    
    if (device_id is not None and 
        (not str(device_id).isdigit() or int(device_id) < 0 or int(device_id) > sys.maxint )):
        response.status = HTTP_SERVER_ERROR
        return dict(error='deivce id is digital, greater than 0, and less than %s'% sys.maxint)
    
    begin = time.time()
    strMethod = request.method
    strMethod = strMethod.upper()
    ## get resource's schemapath in URI
    urlfull = request.url
    prefix,schemapath=urlfull.split('/devices/')
    recordpath =  '/devices/'+schemapath  
    logger.info('Start Message Proc -- %s %s', strMethod, recordpath)
    network_device = network_devices.getDevice(device_id)
    if network_device == None:
        response.status = HTTP_SERVER_ERROR
        errorinfo = "can not find device id : %s" % (device_id)
        return errorinfo
    
    isWrite = True
    if strMethod == 'GET':
        isWrite = False
    
    connectOne = None
    try:
        connectOne = network_device.getFreeConnect(isWrite)
    except Exception as e:
        logger.error('Exception when get a free connection : %s %s error:%s', strMethod, recordpath, e)
        response.status = HTTP_SERVER_ERROR
        return ('%s' % e.message)
   
    try:
        
        if connectOne == None:
            response.status = HTTP_SERVER_ERROR
            errorinfo = "can not find free connection on device id : %s" % (device_id)
            return errorinfo
        
        if connectOne.getStatus() == False:
            response.status = HTTP_SERVER_ERROR
            errorinfo = connectOne.errorinfo
            connectOne.free()
            return errorinfo
    except Exception as e:
        connectOne.free()
        logger.error('Exception when find free connection : %s %s error:%s', strMethod, recordpath, e)
        logger.exception(e)
        response.status = HTTP_SERVER_ERROR
        return e.message
        
    try:
        dInputBody = {
        'json_array' : None,
        'xml_input'  : None
        }

        ## get request messege's body
        read_http_body_strategy(dInputBody)
    
        ## if the xpath has suffix, you need to split it.
        lpath = schemapath.split('/')
        if (lpath[len(lpath) - 1] == 'xml' or lpath[len(lpath) - 1] == 'json'):
            lastElem = lpath.pop(-1)
        else:
            lastElem = lpath[len(lpath) - 1]
            
        ## remove the data type and compose the real schema path
        delimiter = '/'
        schemapath =  delimiter.join(lpath)
    
        #  print dInputBody, schemapath
        #  India's interfaces, return the xml data whatever it is.
        if (strMethod == 'GET'): 
            #temp3= time.time()          
            dataObj = connectOne.get_rest_api_handle(dInputBody, schemapath)
            #temp4= time.time()
            #print 'get info:',(temp4-temp3)
        elif (strMethod == 'DELETE'):
            #print dInputBody
            dataObj = connectOne.delete_rest_api_handle(dInputBody, schemapath)
        elif (strMethod == 'PUT'):
            dataObj = connectOne.put_rest_api_handle(dInputBody, schemapath)
        elif (strMethod == 'POST'):
            dataObj = connectOne.post_rest_api_handle(dInputBody, schemapath)
        else:
            ## there may be some other operators extended
            pass
    except Exception as e:
        logger.error('Exception when executing rest operation : %s %s error:%s', strMethod, recordpath, e)
        logger.exception(e)
        response.status = HTTP_SERVER_ERROR
        return e.message
    finally:
         connectOne.free()
         
    if dataObj == None :
        response.status = HTTP_SERVER_ERROR
        return 
    elif dataObj.startswith('<rpc-error') :
        response.status = HTTP_SERVER_ERROR
        
    ## do convert or not by judging the suffix
    if ("json" == lastElem):
        dataObj = isdk_convert_xml2json(dataObj)
    else:
        dataObj = str(dataObj)
    ## delete the netconf session object.
    end = time.time()
    diff = end-begin
    logger.info('End Message Proc -- %s %s , process time = %fs ', strMethod, recordpath, diff)
    
    #print dataObj
    if (dataObj == None):
        response.status = HTTP_RESOURCE_EMPTY
        
    return dataObj