Ejemplo n.º 1
0
    def test_counter_entry(self):
        ce = sh.CounterEntry("CounterA")
        ce.index = 99
        ce.packet_count = 100
        expected_entry = """
counter_id: 302055013
index {
  index: 99
}
data {
  packet_count: 100
}
"""
        expected_req = self.make_write_request(p4runtime_pb2.Update.MODIFY,
                                               P4RuntimeEntity.counter_entry,
                                               expected_entry)
        ce.modify()
        self.servicer.Write.assert_called_with(ProtoCmp(expected_req), ANY)

        self.simple_read_check(expected_req.updates[0].entity, ce,
                               P4RuntimeEntity.counter_entry)

        ce.index = None
        expected_entry = """
counter_id: 302055013
data {
  packet_count: 100
}
"""
        expected_req = self.make_write_request(p4runtime_pb2.Update.MODIFY,
                                               P4RuntimeEntity.counter_entry,
                                               expected_entry)
        ce.modify()
        self.servicer.Write.assert_called_with(ProtoCmp(expected_req), ANY)
Ejemplo n.º 2
0
 def test_counter_entry_invalid(self):
     ce = sh.CounterEntry("CounterA")
     ce.index = 99
     with self.assertRaisesRegex(UserError,
                                 "Counter 'CounterA' is of type 'PACKETS'"):
         ce.byte_count = 1
     self.assertIsNone(ce._data)
     with self.assertRaisesRegex(UserError,
                                 "Counter 'CounterA' is of type 'PACKETS'"):
         ce.data.byte_count = 1
     self.assertIsNotNone(ce._data)
Ejemplo n.º 3
0
    def test_read_error(self):
        ce = sh.CounterEntry("CounterA")

        def _Read(request, context):
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            yield p4runtime_pb2.ReadResponse()

        self.servicer.Read.side_effect = _Read

        with self.assertRaises(P4RuntimeException):
            next(ce.read())

        with self.assertRaises(P4RuntimeException):
            ce.read(lambda _: True)
Ejemplo n.º 4
0
    def test_read_iterator(self, num_reps, num_entities_per_rep):
        ce = sh.CounterEntry("CounterA")

        def gen_entities():
            for i in itertools.count():
                x = p4runtime_pb2.Entity()
                counter_entry = x.counter_entry
                counter_entry.counter_id = 302055013
                counter_entry.index.index = i
                counter_entry.data.packet_count = 100
                yield x

        def make_read_mock(num_reps, num_entities_per_rep):
            it = gen_entities()

            def _Read(request, context):
                for i in range(num_reps):
                    rep = p4runtime_pb2.ReadResponse()
                    for j in range(num_entities_per_rep):
                        rep.entities.add().CopyFrom(next(it))
                    yield rep

            return _Read

        self.servicer.Read.side_effect = make_read_mock(
            num_reps, num_entities_per_rep)

        cnt = [0]

        def inc(x):
            cnt[0] += 1

        for x in ce.read():
            inc(x)
        self.assertEqual(cnt[0], num_reps * num_entities_per_rep)

        cnt[0] = 0
        ce.read(inc)
        self.assertEqual(cnt[0], num_reps * num_entities_per_rep)
Ejemplo n.º 5
0
def modifyPacketCounter(dev, counterName, counterIndex, valueTobeWritten):
    ce = sh.CounterEntry(dev, counterName)
    ce.index = counterIndex
    ce.packet_count = valueTobeWritten
    ce.modify()
Ejemplo n.º 6
0
def readPacketCounter(dev, counterName, counterIndex):
    ce = sh.CounterEntry(dev, counterName)
    ce.index = counterIndex
    for c in ce.read():
        return int(c.data.packet_count)