def make_table_read_request(self, table_name): req = p4runtime_pb2.ReadRequest() req.device_id = self.device_id entity = req.entities.add() table = entity.table_entry table.table_id = self.get_table_id(table_name) return req, table
def read_one(self, entity): req = p4runtime_pb2.ReadRequest() if self.role_name is not None: req.role = self.role_name req.device_id = self.device_id req.entities.extend([entity]) return self.stub.Read(req)
def ReadTableEntry(self, table, match_key): global dev_id request = p4runtime_pb2.ReadRequest() request.device_id = dev_id entity = request.entities.add() entity.table_entry.table_id = table matches = entity.table_entry.match.add() matches.field_id = 1 matches.exact.value = bytes(match_key) ServerConfig.print_debug("Sending table read request to server:") ServerConfig.print_debug(request) try: for response in self.stub.Read(request): ServerConfig.print_debug( "Table read response received from server:") ServerConfig.print_debug(response) except grpc.RpcError as error: ServerConfig.print_debug( "An error occured during a 'read' execution!") ServerConfig.print_debug("{}: {}\n".format(error.code().name, error.details())) return
def make_counter_read_request(self, counter_name, direct=False): req = p4runtime_pb2.ReadRequest() req.device_id = self.device_id entity = req.entities.add() if direct: counter = entity.direct_counter_entry counter_obj = self.get_obj("direct_counters", counter_name) counter.table_entry.table_id = counter_obj.direct_table_id else: counter = entity.counter_entry counter.counter_id = self.get_counter_id(counter_name) return req, counter
def ReadRegisters(self, register_id=None): request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() register_entry = entity.register_entry if register_id is not None: register_entry.register_id = register_id else: register_entry.register_id = 0 for response in self.client_stub.Read(request): yield response
def ReadTableEntries(self, table_id=None, dry_run=False): request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() table_entry = entity.table_entry if table_id is not None: table_entry.table_id = table_id else: table_entry.table_id = 0 if dry_run: print "P4Runtime Read:", request else: for response in self.client_stub.Read(request): yield response
def ReadDirectCounter(self, table_id=False, dry_run=False): request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() direct_counter_entry = entity.direct_counter_entry # assign if table_id is not None: direct_counter_entry.table_entry.table_id = table_id else: direct_counter_entry.table_entry.table_id = 0 if dry_run: print "P4Runtime Read Direct Counters:", request else: for response in self.client_stub.Read(request): yield response
def read_table_entries(self, table_id=None): logger.info('Reading table entry on switch [%s] with table ID - [%s]', self.name, table_id) request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() table_entry = entity.table_entry if table_id is not None: table_entry.table_id = table_id else: table_entry.table_id = 0 logger.debug('Request for reading table entries to device [%s]', self.grpc_addr) return self.client_stub.Read(request)
def ReadCounters(self, counter_id=None, index=None, dry_run=False): request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() counter_entry = entity.counter_entry if counter_id is not None: counter_entry.counter_id = counter_id else: counter_entry.counter_id = 0 if index is not None: counter_entry.index.index = index if dry_run: print "P4Runtime Read:", request else: for response in self.client_stub.Read(request): yield response
def _delete_table(self, tid): request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() table_entry = entity.table_entry table_entry.table_id = tid request2 = p4runtime_pb2.WriteRequest() request2.device_id = self.device_id request2.election_id.low = 1 for resp in self.client_stub.Read(request): for entity in resp.entities: update = request2.updates.add() update.type = p4runtime_pb2.Update.DELETE update.entity.table_entry.CopyFrom(entity.table_entry) try: self.client_stub.Write(request2) except Exception as err: print(err) self.__tmp = []
def read_counters(self, counter_id=None, index=None): logger.info( 'Read counter on device [%s] with ID - [%s] and index - [%s]', self.grpc_addr, counter_id, index) request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() counter_entry = entity.counter_entry if counter_id is not None: counter_entry.counter_id = counter_id else: counter_entry.counter_id = 0 if index is not None: counter_entry.index.index = index logger.debug('Request for reading counters to device [%s] - [%s]', self.grpc_addr, request) for response in self.client_stub.Read(request): yield response
def ReadTableEntries(self, dry_run=False, **kwargs): ''' :param table_id: If default (0), entries from all tables will be selected and no other filter can be used. Otherwise only the specified table will be considered. :param match: If default (unset), all entries from the specified table will be considered. Otherwise, results will be filtered based on the provided match key, which must be a valid match key for the table. The match will be exact, which means at most one entry will be returned. (NOT WORKING) :param action: If default (unset), all entries from the specified table will be considered. Otherwise, the client can provide an action_id (for direct tables), which will be use to filter table entries. For this P4Runtime release, this is the only kind of action-based filtering we support: the client cannot filter based on action parameter values and cannot filter indirect table entries based on action profile member id / action profile group id. (NOT WORKING) :param priority: If default (0), all entries from the specified table will be considered. Otherwise, results will be filtered based on the provided priority value. :param controller_metadata: If default (0), all entries from the specified table will be considered. Otherwise, results will be filtered based on the provided controller_metadata value. :param is_default_action: If default (false), all non-default entries from the specified table will be considered. Otherwise, only the default entry will be considered. ''' table_id = kwargs.get("table_id", 0) match = kwargs.get("match", None) action = kwargs.get("action", None) priority = kwargs.get("priority", 0) controller_metadata = kwargs.get("controller_metadata", 0) is_default_action = kwargs.get("is_default_action", False) request = p4runtime_pb2.ReadRequest() request.device_id = self.device_id entity = request.entities.add() table_entry = entity.table_entry # set params (defaults are used according to P4Runtime spec) table_entry.table_id = table_id # match = table_entry.match.add() # match.match = match # table_entry.match = match # table_entry.action = action table_entry.priority = priority table_entry.controller_metadata = controller_metadata table_entry.is_default_action = is_default_action if dry_run: print "P4 Runtime Read:", request else: for response in self.client_stub.Read(request): yield response
def table_dump_data(self, table_name): req = p4runtime_pb2.ReadRequest() req.device_id = self.device_id entity = req.entities.add() table = entity.table_entry table.table_id = self.get_table_id(table_name) if table_name is None: table_entry.table_id = 0 else: table.table_id = self.get_table_id(table_name) table_entries = [] for response in self.table_dump_helper(req): for entity in response.entities: #print('entity.WhichOneof("entity")="%s"' # '' % (entity.WhichOneof('entity'))) assert entity.WhichOneof('entity') == 'table_entry' entry = entity.table_entry table_entries.append(entry) #print(entry) #print('----') return table_entries
def read_one(self, entity): req = p4runtime_pb2.ReadRequest() req.device_id = self.device_id req.entities.extend([entity]) return self.stub.Read(req)
def get_new_read_request(self): req = p4runtime_pb2.ReadRequest() req.device_id = self.device_id return req