def post_query_json(self, json_str, sync=True):
     '''
     this module is to support raw query given in json format
     '''
     res = None
     try:
         flows_url = OpServerUtils.opserver_query_url(self._ip, str(self._port))
         print flows_url
         print "query is: ", json_str
         res = []
         resp = OpServerUtils.post_url_http(flows_url, json_str, sync)
         if sync:
             if resp is not None:
                 res = json.loads(resp)
                 res = res['value']
         else: 
             if resp is not None:
                 resp = json.loads(resp)
                 qid = resp['href'].rsplit('/', 1)[1]
                 result = OpServerUtils.get_query_result(self._ip, str(self._port), qid, 30)
                 for item in result:
                     res.append(item)
     except Exception as e:
         print str(e) 
     finally:
         return res        
    def post_query(self, table, start_time=None, end_time=None,
                   select_fields=None, where_clause=None,
                   sort_fields=None, sort=None, limit=None,
                   filter=None, sync=True,dir=None):
        res = None
        try:
            flows_url = OpServerUtils.opserver_query_url(
                self._ip, str(self._port))
            print flows_url
            query_dict = OpServerUtils.get_query_dict(
                table, start_time, end_time,
                select_fields,
                where_clause,
                sort_fields, sort, limit, filter, dir)

            print json.dumps(query_dict)
            res = []
            resp = OpServerUtils.post_url_http(
                flows_url, json.dumps(query_dict), sync)
            if sync:
                if resp is not None:
                    res = json.loads(resp)
                    res = res['value']
            else:
                if resp is not None:
                    resp = json.loads(resp)
                    qid = resp['href'].rsplit('/', 1)[1]
                    result = OpServerUtils.get_query_result(
                        self._ip, str(self._port), qid, 30)
                    for item in result:
                        res.append(item)
        except Exception as e:
            print str(e)
        finally:
            return res
    def post_query(self, table, start_time=None, end_time=None,
                   select_fields=None, where_clause=None,
                   sort_fields=None, sort=None, limit=None,
                   filter=None, sync=True,dir=None):
        res = None
        try:
            flows_url = OpServerUtils.opserver_query_url(
                self._ip, str(self._port))
            print flows_url
            query_dict = OpServerUtils.get_query_dict(
                table, start_time, end_time,
                select_fields,
                where_clause,
                sort_fields, sort, limit, filter, dir)

            print json.dumps(query_dict)
            res = []
            resp = OpServerUtils.post_url_http(
                flows_url, json.dumps(query_dict), sync)
            if sync:
                if resp is not None:
                    res = json.loads(resp)
                    res = res['value']
            else:
                if resp is not None:
                    resp = json.loads(resp)
                    qid = resp['href'].rsplit('/', 1)[1]
                    result = OpServerUtils.get_query_result(
                        self._ip, str(self._port), qid, 30)
                    for item in result:
                        res.append(item)
        except Exception as e:
            print str(e)
        finally:
            return res
 def post_query_json(self, json_str, sync=True):
     '''
     this module is to support raw query given in json format
     '''
     res = None
     try:
         flows_url = OpServerUtils.opserver_query_url(self._ip,
             str(self._port))
         print(flows_url)
         print("query is: ", json_str)
         res = []
         resp = OpServerUtils.post_url_http(flows_url, json_str,
             self._user, self._password, sync, headers=self._headers)
         if sync:
             if resp is not None:
                 res = json.loads(resp)
                 res = res['value']
         else: 
             if resp is not None:
                 resp = json.loads(resp)
                 qid = resp['href'].rsplit('/', 1)[1]
                 result = OpServerUtils.get_query_result(self._ip,
                     str(self._port), qid, self._user, self._password, 30,
                      headers = self._headers)
                 for item in result:
                     res.append(item)
     except Exception as e:
         print(str(e)) 
     finally:
         return res        
 def post_uve_request(self, table, json_body):
     url = 'http://%s:%s/analytics/uves/%s' % (self._ip, str(self._port), table)
     try:
         res = OpServerUtils.post_url_http(url, json_body, sync=True)
         res = json.loads(res)
     except Exception as e:
         print 'Error: POST uve request: %s' % str(e)
         return None
     else:
         return res
 def post_uve_request(self, table, json_body):
     url = 'http://%s:%s/analytics/uves/%s' % (self._ip, str(self._port), table)
     try:
         res = OpServerUtils.post_url_http(url, json_body, user=self._user,
             password=self._password, sync=True, headers=self._headers)
         res = json.loads(res)
     except Exception as e:
         print('Error: POST uve request: %s' % str(e))
         return None
     else:
         return res
 def post_alarm_request(self, table, json_body):
     url = 'http://%s:%s/analytics/alarms/%s' % (self._ip, str(
         self._port), table)
     try:
         res = OpServerUtils.post_url_http(url, json_body, sync=True)
         res = json.loads(res)
     except Exception as e:
         print 'Error: POST alarm request: %s' % str(e)
         return None
     else:
         return res
 def post_purge_query_json(self, json_str, sync=True):
     '''
     this module is to support raw purge query given in json format
     '''
     res = None
     try:
         purge_request_url = \
             OpServerUtils.opserver_database_purge_query_url(
                 self._ip, str(self._port))
         print purge_request_url
         print "query is: ", json_str
         resp = OpServerUtils.post_url_http(
                    purge_request_url, json_str, sync)
         if resp is not None:
             res = json.loads(resp)
     except Exception as e:
         print str(e)
     finally:
         return res
 def post_purge_query_json(self, json_str, sync=True):
     '''
     this module is to support raw purge query given in json format
     '''
     res = None
     try:
         purge_request_url = \
             OpServerUtils.opserver_database_purge_query_url(
                 self._ip, str(self._port))
         print purge_request_url
         print "query is: ", json_str
         resp = OpServerUtils.post_url_http(purge_request_url, json_str,
                                            self._user, self._password,
                                            sync)
         if resp is not None:
             res = json.loads(resp)
     except Exception as e:
         print str(e)
     finally:
         return res
    def post_query(self, table, start_time=None, end_time=None,
                   select_fields=None, where_clause='',
                   sort_fields=None, sort=None, limit=None,
                   filter=None, sync=True,dir=None, session_type=None,
                   is_service_instance=None):
        res = None
        try:
            flows_url = OpServerUtils.opserver_query_url(
                self._ip, str(self._port))
            print(flows_url)
            query_dict = OpServerUtils.get_query_dict(
                table, start_time=start_time, end_time=end_time,
                select_fields=select_fields,
                where_clause=where_clause,
                sort_fields=sort_fields, sort=sort, limit=limit, filter=filter,
                dir=dir,
                is_service_instance=is_service_instance,
                session_type=session_type)

            print(json.dumps(query_dict))
            res = []
            resp = OpServerUtils.post_url_http(
                flows_url, json.dumps(query_dict), self._user, self._password,
                sync, headers=self._headers)
            if sync:
                if resp is not None:
                    res = json.loads(resp)
                    res = res['value']
            else:
                if resp is not None:
                    resp = json.loads(resp)
                    qid = resp['href'].rsplit('/', 1)[1]
                    result = OpServerUtils.get_query_result(
                        self._ip, str(self._port), qid, self._user,
                        self._password, 30, headers=self._headers)
                    for item in result:
                        res.append(item)
        except Exception as e:
            print(str(e))
        finally:
            return res
Esempio n. 11
0
    def query(self,schema):
        query_url = OpServerUtils.opserver_query_url(
            self._args.opserver_ip,
            self._args.opserver_port)
        
        query_dict = OpServerUtils.get_query_dict(
                "StatTable." + self._args.table, str(self._start_time), str(self._end_time),
                select_fields = self._args.select,
                where_clause = "AND".join(self._args.where),
                sort_fields = self._args.sort)
        
        print json.dumps(query_dict)
        resp = OpServerUtils.post_url_http(
            query_url, json.dumps(query_dict), sync = True)

        res = None
        if resp is not None:
            res = json.loads(resp)
            res = res['value']

        return res
Esempio n. 12
0
    def query(self, schema):
        query_url = OpServerUtils.opserver_query_url(self._args.opserver_ip,
                                                     self._args.opserver_port)

        query_dict = OpServerUtils.get_query_dict(
            "StatTable." + self._args.table,
            str(self._start_time),
            str(self._end_time),
            select_fields=self._args.select,
            where_clause="AND".join(self._args.where),
            sort_fields=self._args.sort)

        print json.dumps(query_dict)
        resp = OpServerUtils.post_url_http(query_url,
                                           json.dumps(query_dict),
                                           sync=True)

        res = None
        if resp is not None:
            res = json.loads(resp)
            res = res['value']

        return res
Esempio n. 13
0
    def query(self):
        start_time, end_time = OpServerUtils.get_start_end_time(
            self._start_time,
            self._end_time)
        flow_url = OpServerUtils.opserver_query_url(
            self._args.opserver_ip,
            self._args.opserver_port)
        where = []
        filter = []
        if self._args.vrouter is not None:
            vrouter_match = OpServerUtils.Match(
                name=self._VROUTER,
                value=self._args.vrouter,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(vrouter_match.__dict__)

        if self._args.source_vn is not None:
            source_vn_match = OpServerUtils.Match(
                name=self._SOURCE_VN,
                value=self._args.source_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_vn_match.__dict__)

        if self._args.destination_vn is not None:
            dest_vn_match = OpServerUtils.Match(
                name=self._DESTINATION_VN,
                value=self._args.destination_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_vn_match.__dict__)

        if self._args.source_ip is not None:
            source_ip_match = OpServerUtils.Match(
                name=self._SOURCE_IP,
                value=self._args.source_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_ip_match.__dict__)

        if self._args.destination_ip is not None:
            dest_ip_match = OpServerUtils.Match(
                name=self._DESTINATION_IP,
                value=self._args.destination_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_ip_match.__dict__)

        if self._args.protocol is not None:
            protocol_match = OpServerUtils.Match(
                name=self._PROTOCOL,
                value=self._args.protocol,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(protocol_match.__dict__)

        if self._args.source_port is not None:
            source_port_match = OpServerUtils.Match(
                name=self._SOURCE_PORT,
                value=self._args.source_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_port_match.__dict__)

        if self._args.destination_port is not None:
            dest_port_match = OpServerUtils.Match(
                name=self._DESTINATION_PORT,
                value=self._args.destination_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_port_match.__dict__)

        if self._args.action is not None:
            action_match = OpServerUtils.Match(
                name=self._ACTION,
                value=self._args.action,
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(action_match.__dict__)

        # Flow Record Table Query
        table = VizConstants.FLOW_TABLE
        if len(where) == 0:
            where = None
        else:
            where = [where]

        select_list = [
            VizConstants.FLOW_TABLE_UUID,
            self._VROUTER,
            self._SETUP_TIME,
            self._TEARDOWN_TIME,
            self._SOURCE_VN,
            self._DESTINATION_VN,
            self._SOURCE_IP,
            self._DESTINATION_IP,
            self._PROTOCOL,
            self._SOURCE_PORT,
            self._DESTINATION_PORT,
            self._ACTION,
            self._DIRECTION,
            VizConstants.FLOW_TABLE_AGG_BYTES,
            VizConstants.FLOW_TABLE_AGG_PKTS,
        ]

        if len(filter) == 0:
            filter = None

        flow_query = OpServerUtils.Query(table,
                                         start_time=start_time,
                                         end_time=end_time,
                                         select_fields=select_list,
                                         where=where,
                                         filter=filter,
                                         dir=self._args.direction)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(flow_query.__dict__))
        print ''
        resp = OpServerUtils.post_url_http(
            flow_url, json.dumps(flow_query.__dict__))
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(
                self._args.opserver_ip, self._args.opserver_port, qid)
        return result
Esempio n. 14
0
    def query(self):
        start_time, end_time = OpServerUtils.get_start_end_time(
            self._start_time,
            self._end_time)
        messages_url = OpServerUtils.opserver_query_url(
            self._args.opserver_ip,
            self._args.opserver_port)
        where_msg = []
        where_obj = []
        filter = []
        if self._args.source is not None:
            source_match = OpServerUtils.Match(name=VizConstants.SOURCE,
                                               value=self._args.source,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(source_match.__dict__)

        if self._args.module is not None:
            module_match = OpServerUtils.Match(name=VizConstants.MODULE,
                                               value=self._args.module,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(module_match.__dict__)

        if self._args.category is not None:
            category_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.category,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(category_match.__dict__)

        if self._args.message_type is not None:
            message_type_match = OpServerUtils.Match(
                name=VizConstants.MESSAGE_TYPE,
                value=self._args.message_type,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(message_type_match.__dict__)

        if self._args.level is not None:
            level_match = OpServerUtils.Match(
                name=VizConstants.LEVEL,
                value=SandeshLevel._NAMES_TO_VALUES[self._args.level],
                op=OpServerUtils.MatchOp.GEQ)
            filter.append(level_match.__dict__)

        if (self._args.object is not None or
            self._args.object_id is not None or
                self._args.object_select_field is not None):
            # Object Table Query
            where_obj = list(where_msg)

            if self._args.object is not None:
                if self._args.object in OBJECT_TABLE_LIST:
                    table = self._args.object
                else:
                    print 'Unknown object table [%s]' % (self._args.object)
                    return None
            else:
                print 'Object required for query'
                return None

            if self._args.object_id is not None:
                id_match = OpServerUtils.Match(name=OpServerUtils.OBJECT_ID,
                                               value=self._args.object_id,
                                               op=OpServerUtils.MatchOp.EQUAL)
                where_obj.append(id_match.__dict__)
            else:
                print 'Object id required for table [%s]' % (self._args.object)
                return None

            if self._args.object_select_field is not None:
                if ((self._args.object_select_field !=
                     VizConstants.OBJECT_LOG) and
                    (self._args.object_select_field !=
                     VizConstants.SYSTEM_LOG)):
                    print 'Invalid object-select-field. '\
                        'Valid values are "%s" or "%s"' \
                        % (VizConstants.OBJECT_LOG,
                           VizConstants.SYSTEM_LOG)
                    return None
                obj_sel_field = [self._args.object_select_field]
                self._args.object_select_field = obj_sel_field
            else:
                self._args.object_select_field = obj_sel_field = [
                    VizConstants.OBJECT_LOG, VizConstants.SYSTEM_LOG]

            where = [where_obj]

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.MESSAGE_TYPE,
            ] + obj_sel_field
        elif self._args.trace is not None:
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            if self._args.source is None:
                print 'Source is required for trace buffer dump'
                return None
            if self._args.module is None:
                print 'Module is required for trace buffer dump'
                return None
            trace_buf_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.trace,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(trace_buf_match.__dict__)
            where = [where_msg]
            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM,
                VizConstants.DATA
            ]
            sandesh_type_filter = OpServerUtils.Match(
                name=VizConstants.SANDESH_TYPE,
                value=str(
                    SandeshType.TRACE),
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(sandesh_type_filter.__dict__)
        else:
            # Message Table Query
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            # Message Table contains both systemlog and objectlog.
            # Add a filter to return only systemlogs
            if not self._args.all:
                sandesh_type_filter = OpServerUtils.Match(
                    name=VizConstants.SANDESH_TYPE,
                    value=str(
                        SandeshType.SYSTEM),
                    op=OpServerUtils.MatchOp.EQUAL)
                filter.append(sandesh_type_filter.__dict__)

            if len(where_msg):
                where = [where_msg]
            else:
                where = None

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.CATEGORY,
                VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM,
                VizConstants.DATA,
                VizConstants.SANDESH_TYPE,
                VizConstants.LEVEL,
            ]

        if len(filter) == 0:
            filter = None

        # Add sort by timestamp
        if self._args.reverse:
            sort_op = OpServerUtils.SortOp.DESCENDING
        else:
            sort_op = OpServerUtils.SortOp.ASCENDING
        sort_fields = [VizConstants.TIMESTAMP]

        if self._args.limit:
            limit = int(self._args.limit)
        else:
            limit = None

        messages_query = OpServerUtils.Query(table,
                                             start_time=start_time,
                                             end_time=end_time,
                                             select_fields=select_list,
                                             where=where,
                                             filter=filter,
                                             sort=sort_op,
                                             sort_fields=sort_fields,
                                             limit=limit)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(messages_query.__dict__))
        print ''
        resp = OpServerUtils.post_url_http(
            messages_url, json.dumps(messages_query.__dict__))
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(
                self._args.opserver_ip, self._args.opserver_port, qid)
        return result
Esempio n. 15
0
    def query(self):
        start_time, end_time = OpServerUtils.get_start_end_time(
            self._start_time, self._end_time)
        messages_url = OpServerUtils.opserver_query_url(
            self._args.opserver_ip, self._args.opserver_port)
        where_msg = []
        where_obj = []
        filter = []
        if self._args.source is not None:
            source_match = OpServerUtils.Match(name=VizConstants.SOURCE,
                                               value=self._args.source,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(source_match.__dict__)

        if self._args.module is not None:
            module_match = OpServerUtils.Match(name=VizConstants.MODULE,
                                               value=self._args.module,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(module_match.__dict__)

        if self._args.category is not None:
            category_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.category,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(category_match.__dict__)

        if self._args.message_type is not None:
            message_type_match = OpServerUtils.Match(
                name=VizConstants.MESSAGE_TYPE,
                value=self._args.message_type,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(message_type_match.__dict__)

        if self._args.level is not None:
            level_match = OpServerUtils.Match(
                name=VizConstants.LEVEL,
                value=SandeshLevel._NAMES_TO_VALUES[self._args.level],
                op=OpServerUtils.MatchOp.GEQ)
            filter.append(level_match.__dict__)

        if self._args.node_type is not None:
            node_type_match = OpServerUtils.Match(
                name=VizConstants.NODE_TYPE,
                value=self._args.node_type,
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(node_type_match.__dict__)

        if self._args.instance_id is not None:
            instance_id_match = OpServerUtils.Match(
                name=VizConstants.INSTANCE_ID,
                value=self._args.instance_id,
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(instance_id_match.__dict__)

        if (self._args.object is not None or self._args.object_id is not None
                or self._args.object_select_field is not None):
            # Object Table Query
            where_obj = list(where_msg)

            if self._args.object is not None:
                if self._args.object in OBJECT_TABLE_LIST:
                    table = self._args.object
                else:
                    print 'Unknown object table [%s]' % (self._args.object)
                    return None
            else:
                print 'Object required for query'
                return None

            if self._args.object_id is not None:
                id_match = OpServerUtils.Match(name=OpServerUtils.OBJECT_ID,
                                               value=self._args.object_id,
                                               op=OpServerUtils.MatchOp.EQUAL)
                where_obj.append(id_match.__dict__)
            else:
                print 'Object id required for table [%s]' % (self._args.object)
                return None

            if self._args.object_select_field is not None:
                if ((self._args.object_select_field != VizConstants.OBJECT_LOG)
                        and (self._args.object_select_field !=
                             VizConstants.SYSTEM_LOG)):
                    print 'Invalid object-select-field. '\
                        'Valid values are "%s" or "%s"' \
                        % (VizConstants.OBJECT_LOG,
                           VizConstants.SYSTEM_LOG)
                    return None
                obj_sel_field = [self._args.object_select_field]
                self._args.object_select_field = obj_sel_field
            else:
                self._args.object_select_field = obj_sel_field = [
                    VizConstants.OBJECT_LOG, VizConstants.SYSTEM_LOG
                ]

            where = [where_obj]

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.MESSAGE_TYPE,
            ] + obj_sel_field
        elif self._args.trace is not None:
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            if self._args.source is None:
                print 'Source is required for trace buffer dump'
                return None
            if self._args.module is None:
                print 'Module is required for trace buffer dump'
                return None
            trace_buf_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.trace,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(trace_buf_match.__dict__)
            where = [where_msg]
            select_list = [
                VizConstants.TIMESTAMP, VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM, VizConstants.DATA
            ]
            sandesh_type_filter = OpServerUtils.Match(
                name=VizConstants.SANDESH_TYPE,
                value=str(SandeshType.TRACE),
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(sandesh_type_filter.__dict__)
        else:
            # Message Table Query
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            # Message Table contains both systemlog and objectlog.
            # Add a filter to return only systemlogs
            if not self._args.all:
                sandesh_type_filter = OpServerUtils.Match(
                    name=VizConstants.SANDESH_TYPE,
                    value=str(SandeshType.SYSTEM),
                    op=OpServerUtils.MatchOp.EQUAL)
                filter.append(sandesh_type_filter.__dict__)

            if len(where_msg):
                where = [where_msg]
            else:
                where = None

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.CATEGORY,
                VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM,
                VizConstants.DATA,
                VizConstants.SANDESH_TYPE,
                VizConstants.LEVEL,
                VizConstants.NODE_TYPE,
                VizConstants.INSTANCE_ID,
            ]

        if len(filter) == 0:
            filter = None

        # Add sort by timestamp
        if self._args.reverse:
            sort_op = OpServerUtils.SortOp.DESCENDING
        else:
            sort_op = OpServerUtils.SortOp.ASCENDING
        sort_fields = [VizConstants.TIMESTAMP]

        if self._args.limit:
            limit = int(self._args.limit)
        else:
            limit = None

        messages_query = OpServerUtils.Query(table,
                                             start_time=start_time,
                                             end_time=end_time,
                                             select_fields=select_list,
                                             where=where,
                                             filter=filter,
                                             sort=sort_op,
                                             sort_fields=sort_fields,
                                             limit=limit)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(messages_query.__dict__))
        print ''
        resp = OpServerUtils.post_url_http(messages_url,
                                           json.dumps(messages_query.__dict__))
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(self._args.opserver_ip,
                                                    self._args.opserver_port,
                                                    qid)
        return result
Esempio n. 16
0
    def query(self):
        start_time, end_time = OpServerUtils.get_start_end_time(
            self._start_time, self._end_time)
        flow_url = OpServerUtils.opserver_query_url(self._args.opserver_ip,
                                                    self._args.opserver_port)
        where = []
        filter = []
        if self._args.vrouter is not None:
            vrouter_match = OpServerUtils.Match(name=self._VROUTER,
                                                value=self._args.vrouter,
                                                op=OpServerUtils.MatchOp.EQUAL)
            where.append(vrouter_match.__dict__)

        if self._args.source_vn is not None:
            source_vn_match = OpServerUtils.Match(
                name=self._SOURCE_VN,
                value=self._args.source_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_vn_match.__dict__)

        if self._args.destination_vn is not None:
            dest_vn_match = OpServerUtils.Match(
                name=self._DESTINATION_VN,
                value=self._args.destination_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_vn_match.__dict__)

        if self._args.source_ip is not None:
            source_ip_match = OpServerUtils.Match(
                name=self._SOURCE_IP,
                value=self._args.source_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_ip_match.__dict__)

        if self._args.destination_ip is not None:
            dest_ip_match = OpServerUtils.Match(
                name=self._DESTINATION_IP,
                value=self._args.destination_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_ip_match.__dict__)

        if self._args.protocol is not None:
            protocol_match = OpServerUtils.Match(
                name=self._PROTOCOL,
                value=self._args.protocol,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(protocol_match.__dict__)

        if self._args.source_port is not None:
            source_port_match = OpServerUtils.Match(
                name=self._SOURCE_PORT,
                value=self._args.source_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_port_match.__dict__)

        if self._args.destination_port is not None:
            dest_port_match = OpServerUtils.Match(
                name=self._DESTINATION_PORT,
                value=self._args.destination_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_port_match.__dict__)

        if self._args.action is not None:
            action_match = OpServerUtils.Match(name=self._ACTION,
                                               value=self._args.action,
                                               op=OpServerUtils.MatchOp.EQUAL)
            filter.append(action_match.__dict__)

        # Flow Record Table Query
        table = VizConstants.FLOW_TABLE
        if len(where) == 0:
            where = None
        else:
            where = [where]

        select_list = [
            VizConstants.FLOW_TABLE_UUID,
            self._VROUTER,
            self._SETUP_TIME,
            self._TEARDOWN_TIME,
            self._SOURCE_VN,
            self._DESTINATION_VN,
            self._SOURCE_IP,
            self._DESTINATION_IP,
            self._PROTOCOL,
            self._SOURCE_PORT,
            self._DESTINATION_PORT,
            self._ACTION,
            self._DIRECTION,
            VizConstants.FLOW_TABLE_AGG_BYTES,
            VizConstants.FLOW_TABLE_AGG_PKTS,
        ]

        if len(filter) == 0:
            filter = None

        flow_query = OpServerUtils.Query(table,
                                         start_time=start_time,
                                         end_time=end_time,
                                         select_fields=select_list,
                                         where=where,
                                         filter=filter,
                                         dir=self._args.direction)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(flow_query.__dict__))
        print ''
        resp = OpServerUtils.post_url_http(flow_url,
                                           json.dumps(flow_query.__dict__))
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(self._args.opserver_ip,
                                                    self._args.opserver_port,
                                                    qid)
        return result