def gen_request(xpath): mysubs = [] for path in xpath: mypath = path_from_string(path) #mysub = gnmi_pb2.Subscription(path=mypath, mode=opt.submode, suppress_redundant=opt.suppress, sample_interval=opt.interval*1000000000, heartbeat_interval=opt.heartbeat) mysub = gnmi_pb2.Subscription(path=mypath, mode=0, suppress_redundant=False, sample_interval=10 * 1000000000) mysubs.append(mysub) myprefix = path_from_string(None) mysblist = gnmi_pb2.SubscriptionList(prefix=myprefix, mode=0, allow_aggregation=True, encoding=4, subscription=mysubs) mysubreq = gnmi_pb2.SubscribeRequest(subscribe=mysblist) #log.info('Sending SubscribeRequest\n'+str(mysubreq)) #yield mysubreq #return mysblist,mysubreq #return mysubreq #return mysubreq return mypath
def gen_request(paths, opt, prefix): """Create subscribe request for passed xpath. Args: paths: (str) gNMI path. opt: (dict) Command line argument passed for subscribe reqeust. Returns: gNMI SubscribeRequest object. """ mysubs = [] mysub = gnmi_pb2.Subscription(path=paths, mode=opt["submode"], sample_interval=opt["interval"]*1000000, heartbeat_interval=opt['heartbeat']*1000000, suppress_redundant=opt['suppress']) mysubs.append(mysub) if prefix: myprefix = prefix elif opt["prefix"]: myprefix = _parse_path(_path_names(opt["prefix"])) else: myprefix = None if opt["qos"]: myqos = gnmi_pb2.QOSMarking(marking=opt["qos"]) else: myqos = None mysblist = gnmi_pb2.SubscriptionList(prefix=myprefix, mode=opt['subscribe_mode'], allow_aggregation=opt['aggregate'], encoding=opt['encoding'], subscription=mysubs, use_aliases=opt['use_alias'], qos=myqos) mysubreq = gnmi_pb2.SubscribeRequest(subscribe=mysblist) print('Sending SubscribeRequest\n'+str(mysubreq)) yield mysubreq
def _sub(stub, paths, username, password): if username: # User/pass supplied for Authentication. mysblist = gnmi_pb2.SubscriptionList(prefix=[paths], mode='STREAM', allow_aggregation=False, encoding='JSON_IETF', subscription=[paths]) return stub.Subscribe(gnmi_pb2.SubscribeRequest(path=[paths], encoding='JSON_IETF'), metadata=[('username', username), ('password', password)])
def __init__(self, channel): self.channel = channel self.stub = gnmi_pb2_grpc.gNMIStub(self.channel) # ask for the capabilites #cap_req = gnmi_pb2.CapabilityRequest() #cap_res = self.stub.Capabilities(cap_req) self.encapsulation = gnmi_pb2.PROTO encoding_path = "/interfaces" path = gnmi_utils.simple_gnmi_string_parser(encoding_path) mysub = gnmi_pb2.Subscription(path=path, sample_interval=60*1000000000) mysubs = [mysub] mysblist = gnmi_pb2.SubscriptionList(prefix=None, encoding=self.encapsulation, subscription=mysubs) mysubreq = gnmi_pb2.SubscribeRequest( subscribe=mysblist ) def x(): yield mysubreq y = x() base_grpc = {"grpcPeer": self.channel._channel.target().decode(), "ne_vendor": "gnmi"} msgs = self.stub.Subscribe(y, None) for msg in msgs: if msg.HasField('update'): grpc = dict(base_grpc) data = {"node_id_str": "r33.labxtx01.us.bb"} notification = msg.update timestamp = notification.timestamp # in nanoseconds since epoch prefix = notification.prefix sensor_path, keys = gnmi_utils.gnmi_to_string_and_keys(prefix) data["encoding_path"] = sensor_path data["collection_timestamp"] = timestamp / 1000 data["keys"] = keys gnmi = [] header_info = None for upd in notification.update: upd_name, extra_keys = gnmi_utils.gnmi_to_string_and_keys(upd.path) try: value = getattr(upd.val, upd.val.WhichOneof("value")) except: breakpoint() if upd.val.WhichOneof("value") in ("leaflist_val", "any_val", "decimal_val"): value = str(value) if upd_name == "__juniper_telemetry_header__": header_bytes = value continue if extra_keys: breakpoint() gnmi.append({"keys": extra_keys, "name": upd_name, "value": value}) data["gnmi"] = gnmi message_dict = {"collector": {"grpc": grpc, "data": data}} try: returned = FinalizeTelemetryData(message_dict) except Exception as e: PMGRPCDLOG.error("Error finalazing message: %s", e)
def gen_request( opt, log ): import gnmi_pb2 mysubs = [] for path in opt.xpaths: mypath = grpc_support.path_from_string(path) mysub = gnmi_pb2.Subscription(path=mypath, mode=opt.submode, suppress_redundant=opt.suppress, sample_interval=opt.interval*1000000000, heartbeat_interval=opt.heartbeat) mysubs.append(mysub) if opt.prefix: myprefix = path_from_string(opt.prefix) else: myprefix = None if opt.qos: myqos = gnmi_pb2.QOSMarking(marking=opt.qos) else: myqos = None mysblist = gnmi_pb2.SubscriptionList(prefix=myprefix, mode=opt.mode, allow_aggregation=opt.aggregate, encoding=opt.encoding, subscription=mysubs, use_aliases=opt.use_alias, qos=myqos) mysubreq = gnmi_pb2.SubscribeRequest( subscribe=mysblist ) log.info('Sending SubscribeRequest\n'+str(mysubreq)) yield mysubreq
def __getSubscribeRequestObj(self, path): if len(path) == 0 or path[0] != '/': yield None tokens = path[1:].split('/') # SubscribeList sublist = gnmi_pb2.SubscriptionList() gNMIUtils.fillPrefix(tokens, sublist.prefix) # Prefix # Add subscription mysub = sublist.subscription.add() gNMIUtils.fillPath(tokens[len(tokens)-1], mysub.path) # Path mysub.mode = 0 # Mode = TARGET_DEFINED mysub.sample_interval = 0 mysub.suppress_redundant = 0 mysub.heartbeat_interval = 0 # Model data modelData = sublist.use_models.add() modelData.name = "my_model" modelData.organization = "My Company Inc" modelData.version = "1.0" sublist.mode = 0 # Stream sublist.allow_aggregation = False sublist.encoding = 0 # JSON sublist.updates_only = False # Create a subscribe request subReq = gnmi_pb2.SubscribeRequest(subscribe = sublist) #print subReq yield subReq