Exemple #1
0
def main():
    channel = grpc.insecure_channel(args.grpc_addr)
    stub = gnmi_pb2.gNMIStub(channel)

    def req_iterator():
        while True:
            req = gnmi_pb2.SubscribeRequest()
            subList = req.subscribe
            subList.mode = gnmi_pb2.SubscriptionList.ONCE
            sub = subList.subscription.add()
            path = sub.path
            for name in ["interfaces", "interface", "..."]:
                e = path.elem.add()
                e.name = name
            print("***************************")
            print("REQUEST")
            print(req)
            print("***************************")
            yield req
            return

    for response in stub.Subscribe(req_iterator()):
        print("***************************")
        print("RESPONSE")
        print(response)
        print("***************************")
Exemple #2
0
def main():
    channel = grpc.insecure_channel(args.grpc_addr)
    stub = gnmi_pb2.gNMIStub(channel)
    req = None
    path = None

    if args.cmd == 'get':
        req = build_gnmi_get_req()
        print_msg(req, "REQUEST")
        resp = stub.Get(req)
        print_msg(resp, "RESPONSE")
    elif args.cmd == 'set':
        req = build_gnmi_set_req()
        print_msg(req, "REQUEST")
        resp = stub.Set(req)
        print_msg(resp, "RESPONSE")
    elif args.cmd == 'sub':
        req = build_gnmi_sub()
        stream_out_q.put(req)
        stream = stub.Subscribe(req_iterator())
        stream_recv_thread = threading.Thread(target=stream_recv,
                                              args=(stream, ))
        stream_recv_thread.start()

        try:
            while True:
                sleep(1)
        except KeyboardInterrupt:
            stream_out_q.put(None)
            stream_recv_thread.join()
    else:
        print 'Unknown command %s', args.cmd
        return
def main():
    channel = grpc.insecure_channel(args.grpc_addr)
    stub = gnmi_pb2.gNMIStub(channel)

    stream_out_q = queue.Queue()
    stream_in_q = queue.Queue()

    def req_iterator():
        while True:
            req = stream_out_q.get()
            if req is None:
                break
            print("***************************")
            print("REQUEST")
            print(req)
            print("***************************")
            yield req

    def stream_recv(stream):
        for response in stream:
            print("***************************")
            print("RESPONSE")
            print(response)
            print("***************************")
            stream_in_q.put(response)

    stream = stub.Subscribe(req_iterator())
    stream_recv_thread = threading.Thread(
        target=stream_recv, args=(stream,))
    stream_recv_thread.start()

    req = gnmi_pb2.SubscribeRequest()
    subList = req.subscribe
    subList.mode = gnmi_pb2.SubscriptionList.STREAM
    subList.updates_only = True
    sub = subList.subscription.add()
    sub.mode = gnmi_pb2.ON_CHANGE
    path = sub.path
    for name in ["interfaces", "interface", "..."]:
        e = path.elem.add()
        e.name = name

    stream_out_q.put(req)

    try:
        while True:
            sleep(1)
    except KeyboardInterrupt:
        stream_out_q.put(None)
        stream_recv_thread.join()
    def Subscribe(self, request_iterator, context):
        #create a channel connecting to the southbound device
        channel = grpc.insecure_channel(device_ip + ":" + str(device_port))
        stub = gnmi_pb2.gNMIStub(channel)
        counter = 0
        global interval
        eventQ = Queue.Queue()
        ptime = time.time()
        #start streaming
        for response in stub.Subscribe(request_iterator):
            #logger.debug(response)
            path =""
            for update in response.update.update:
                event = p4_pb2.P4_int()
                update.val.any_val.Unpack(event)
                eventQ.put(event)
                path = update.path
            else:
                pass
            ctime = time.time()
            if(ctime - ptime >= interval):
                eventlist = []
                while not eventQ.empty():
                    eventlist.append(eventQ.get())
                if len(eventlist) > 0: 
                    for e in eventlist:
                        jsonObj = MessageToJson(e)
                        #logger.debug(jsonObj)
                    aggregated = self.processQ(eventlist)
                    logger.debug("aggregatedTo: %s" %(MessageToJson(aggregated)))
                    any_msg = any_pb2.Any()
                    any_msg.Pack(aggregated)
                    typedValue = gnmi_pb2.TypedValue(any_val=any_msg)
                    a_update = gnmi_pb2.Update(path=path, val=typedValue)
                    a_updates = []
                    a_updates.append(a_update)
                    a_noti = gnmi_pb2.Notification(timestamp=response.update.timestamp, update=a_updates)
                    yield gnmi_pb2.SubscribeResponse(update=a_noti) 
                ptime = time.time()

        print "Streaming done!"