def _get(stub, paths, username, password): if username: # User/pass supplied for Authentication. return stub.Get(gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'), metadata=[('username', username), ('password', password)]) return stub.Get(gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'))
def main(): args = vars(_create_parser().parse_args()) kwargs = { 'root_cert': args['root_cert'], 'cert_chain': args['cert_chain'], 'private_key': args['private_key'] } certs = _open_certs(**kwargs) stub = _create_stub(certs, args['endpoint']) paths = _parse_path(args['target'], _path_names(args['xpath'])) mode = args['mode'] if mode == 'get': response = stub.Get( gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF')) print(response) elif mode == 'set': response = stub.Set( gnmi_pb2.SetRequest(update=[ gnmi_pb2.Update( path=paths, val=_get_val(args['value']), ) ])) print(response) elif mode == 'delete': response = stub.Set(gnmi_pb2.SetRequest(delete=[paths])) print(response)
def GetRequest(stub, path_list): """Issue a gNMI GetRequest for the given path.""" paths = gnmi_pb2.Path(elem=path_list) # Metadata is User/pass for our example gNMI Target response = stub.Get(gnmi_pb2.GetRequest(path=[paths]), metadata=[('username', 'foo'), ('password', 'bar')]) return response
def _get(stub, paths, username, password): """Create a gNMI GetRequest. Args: stub: (class) gNMI Stub used to build the secure channel. paths: gNMI Path username: (str) Username used when building the channel. password: (str) Password used when building the channel. Returns: a gnmi_pb2.GetResponse object representing a gNMI GetResponse. """ if username: # User/pass supplied for Authentication. return stub.Get( gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'), metadata=[('username', username), ('password', password)]) return stub.Get(gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'))
def gen_request(opt, log): import gnmi_pb2 mypaths = [] for path in opt.xpaths: mypath = grpc_support.path_from_string(path) mypaths.append(mypath) 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 return gnmi_pb2.GetRequest(path=mypaths)
def __getGetRequestObj(self, path): # Check the validity of the Path if len(path) == 0 or path[0] != '/': return None # Create getReq object getReq = gnmi_pb2.GetRequest() getReq.type = gnmi_pb2.GetRequest.CONFIG getReq.encoding = gnmi_pb2.JSON # Parse the Path and fill the getReq object tokens = path[1:].split('/') if gNMIUtils.fillPrefix(tokens, getReq.prefix) is False \ or \ gNMIUtils.fillPath(tokens[len(tokens)-1], getReq.path.add()) is False: return None return getReq
if args.paths: d_print("Creating getRequest...") prefix=gnmi_pb2.Path(origin=args.origin) # "repeated" PB message fields like "elem" and "path" must be Python iterables paths = [] for path in args.paths: pathElems = [] for elem in path.strip("/").split("/"): # TODO: add support for key-value pairs pathElems.append(gnmi_pb2.PathElem(name=elem)) d_print("pathElems=", pathElems) paths.append(gnmi_pb2.Path(elem=pathElems)) d_print("constructed paths=", paths) getRequest = gnmi_pb2.GetRequest(prefix=prefix, path=paths, type='ALL', encoding='JSON_IETF') d_print("getRequest=", getRequest) d_print("Executing gNMI Get()...") getResponse = stub.Get(getRequest, metadata=metadata, timeout=args.timeout) #d_print("getResponse: ", getResponse.notification) print("Response content: ") for n in getResponse.notification: # response can have multiple notifications print("timestamp: ", n.timestamp) print("alias: ", n.alias) for u in n.update: # a notification can have multiple updates print("update path: ", u.path) print("update val: ", json.dumps(json.loads(u.val.json_ietf_val), indent=4))