def rest_simple_request(self, url, use_cache = None, timeout = None):
        # include a trivial retry mechanism ... other specific
        #  urllib2 exception types may need to be included
        retry_count = 0
        if use_cache == None or use_cache:
            result = url_cache.get_cached_url(url)
            if result != None:
                return result
        while retry_count > 0:
            try:
                request = urllib2.Request(url, headers = self.json_headers(url))
                return urllib2.urlopen(request, timeout = timeout).read()
            except urllib2.URLError:
                retry_count -= 1
                time.sleep(1)
        # try again without the try...
        if self.display_rest:
            print "REST-SIMPLE:", 'GET', url

        request = urllib2.Request(url, headers = self.json_headers(url))
        result = urllib2.urlopen(request, timeout = timeout).read()

        if self.display_rest_reply:
            print 'REST-SIMPLE: %s reply "%s"' % (url, result)
        url_cache.save_url(url, result)
        return result
Example #2
0
    def rest_json_request(self, url):
        entries = url_cache.get_cached_url(url)
        if entries != None:
            return entries

        result = self.rest_simple_request(url)
        # XXX check result
        entries = json.loads(result)
        url_cache.save_url(url, entries)

        return entries
    def rest_json_request(self, url):
        entries =  url_cache.get_cached_url(url)
        if entries != None:
            return entries

        result = self.rest_simple_request(url)
        # XXX check result
        entries = json.loads(result)
        url_cache.save_url(url, entries)

        return entries
 def find_object_from_store(self, obj_type, key, val):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, obj_type)
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request("%s?%s__exact=%s" % (url, key, urllib.quote_plus(val)))
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
def get_model_from_url(obj_type, data):
    """
    Intended to be used as a conversion tool, to provide a way
    to move model requests to specific url requests
    """

    onos = 1
    startswith = '__startswith'
    data = dict(data)

    if mi.obj_type_has_model(obj_type):
        print "MODEL_URL: %s not supported" % obj_type
        return []
    #
    # save sort field
    sort = data.get('orderby', None)
    if sort:
        del data['orderby']

    obj_type_url = mi.obj_type_has_url(obj_type)
    url = "http://%s/rest/v1/" % sdnsh.controller + obj_type_url

    # for the devices query, be sensitive to the types of queries
    # since the devices query doesn't understand the relationship
    # between the vlan=='' for address_space == 'default'.
    if obj_type_url == 'device':
        if 'vlan' in data and data['vlan'] == '':
            del data['vlan']

    # data items need to be included into the query.
    if data:
        url += '?'
        url += urllib.urlencode(data)
    if sdnsh.description:   # description debugging
        print "get_model_from_url: request ", obj_type, data, url
    #
    # cache small, short time results
    entries = url_cache.get_cached_url(url)
    if entries != None:
        if sdnsh.description:   # description debugging
            print 'get_model_from_url: using cached result for ', url
    else:
        try:
            result = sdnsh.store.rest_simple_request(url)
            check_rest_result(result)
            entries = json.loads(result)
        except Exception, e:
            if sdnsh.description:   # description debugging
                print "get_model_from_url: failed request %s: '%s'" % (url, e)
            entries = []
        
        url_cache.save_url(url, entries)
Example #6
0
 def find_object_from_store(self, obj_type, key, val):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, obj_type)
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request("%s?%s__exact=%s" %
                                     (url, key, urllib.quote_plus(val)))
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
Example #7
0
def get_model_from_url(obj_type, data):
    """
    Intended to be used as a conversion tool, to provide a way
    to move model requests to specific url requests
    """

    startswith = '__startswith'
    data = dict(data)

    if mi.obj_type_has_model(obj_type):
        print "MODEL_URL: %s not supported" % obj_type
        return []
    #
    # save sort field
    sort = data.get('orderby', None)
    if sort:
        del data['orderby']

    obj_type_url = mi.obj_type_has_url(obj_type)
    url = "http://%s/rest/v1/" % sdnsh.controller + obj_type_url

    # for the devices query, be sensitive to the types of queries
    # since the devices query doesn't understand the relationship
    # between the vlan=='' for address_space == 'default'.
    if obj_type_url == 'device':
        if 'vlan' in data and data['vlan'] == '':
            del data['vlan']

    # data items need to be included into the query.
    if data:
        url += '?'
        url += urllib.urlencode(data)
    if sdnsh.description:   # description debugging
        print "get_model_from_url: request ", obj_type, data, url
    #
    # cache small, short time results
    entries = url_cache.get_cached_url(url)
    if entries != None:
        if sdnsh.description:   # description debugging
            print 'get_model_from_url: using cached result for ', url
    else:
        try:
            result = sdnsh.store.rest_simple_request(url)
            check_rest_result(result)
            entries = json.loads(result)
        except Exception, e:
            if sdnsh.description:   # description debugging
                print "get_model_from_url: failed request %s: '%s'" % (url, e)
            entries = []
        
        url_cache.save_url(url, entries)
Example #8
0
 def get_table_from_store(self, table_name, key=None, val=None, match=None):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, table_name)
     if not match:
         match = "startswith"
     if key and val:
         url = "%s?%s__%s=%s" % (url, key, match, urllib.quote_plus(val))
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request(url)
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
 def get_table_from_store(self, table_name, key=None, val=None, match=None):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, table_name)
     if not match:
         match = "startswith"
     if key and val:
         url = "%s?%s__%s=%s" % (url, key, match, urllib.quote_plus(val))
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request(url)
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
 def rest_query_objects(self, obj_type, query_params=None):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, obj_type)
     if query_params:
         url += '?'
         # Convert any data:None fields to <id>__isnull=True
         non_null_query_params = dict([[n,v] if v != None else [n + '__isnull', True]
                                       for (n,v) in query_params.items()])
         url += urllib.urlencode(non_null_query_params)
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request(url)
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
Example #11
0
 def rest_query_objects(self, obj_type, query_params=None):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, obj_type)
     if query_params:
         url += '?'
         # Convert any data:None fields to <id>__isnull=True
         non_null_query_params = dict(
             [[n, v] if v != None else [n + '__isnull', True]
              for (n, v) in query_params.items()])
         url += urllib.urlencode(non_null_query_params)
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     data = self.rest_simple_request(url)
     entries = json.loads(data)
     url_cache.save_url(url, entries)
     return entries
Example #12
0
 def get_object_from_store(self, table_name, pk_value):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, table_name)
     url += (pk_value + '/')
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     if self.display_rest:
         print "REST-MODEL:", url
     response = urllib2.urlopen(url)
     if response.code != 200:
         # LOOK! Should probably raise exception here instead.
         # In general we need to rethink the store interface and how
         # we should use exceptions.
         return None
     data = response.read()
     result = json.loads(data)
     if self.display_rest_reply:
         print 'REST-MODEL: %s reply: "%s"' % (url, result)
     url_cache.save_url(url, result)
     return result
 def get_object_from_store(self, table_name, pk_value):
     if not self.controller:
         print "No controller specified. Set using 'controller <server:port>'."
         return
     url = self.table_read_url % (self.controller, table_name)
     url += (pk_value + '/')
     result = url_cache.get_cached_url(url)
     if result != None:
         return result
     if self.display_rest:
         print "REST-MODEL:", url
     response = urllib2.urlopen(url)
     if response.code != 200:
         # LOOK! Should probably raise exception here instead.
         # In general we need to rethink the store interface and how
         # we should use exceptions.
         return None
     data = response.read()
     result = json.loads(data)
     if self.display_rest_reply:
         print 'REST-MODEL: %s reply: "%s"' % (url, result)
     url_cache.save_url(url, result)
     return result