def _parse_path(p_names): """Parses a list of path names for path keys. Args: p_names: (list) of path elements, which may include keys. Returns: a gnmi_pb2.Path object representing gNMI path elements. Raises: XpathError: Unabled to parse the xpath provided. """ gnmi_elems = [] for word in p_names: word_search = _RE_PATH_COMPONENT.search(word) if not word_search: # Invalid path specified. raise XpathError('xpath component parse error: %s' % word) if word_search.group('key') is not None: # A path key was provided. gnmi_elems.append( gnmi_pb2.PathElem( name=word_search.group('pname'), key={word_search.group('key'): word_search.group('value')})) else: gnmi_elems.append(gnmi_pb2.PathElem(name=word, key={})) return gnmi_pb2.Path(elem=gnmi_elems)
def _parse_path(p_names): """Parses a list of path names for path keys. Args: p_names: (list) of path elements, which may include keys. Returns: a gnmi_pb2.Path object representing gNMI path elements. Raises: XpathError: Unabled to parse the xpath provided. p_names will get passed in a list for examples ['interfaces', 'interface[name=Ethernet1]', 'state', 'counters'] I think this is where it actually calls everything and returns back paths from the function of what is actually available. """ gnmi_elems = [] for word in p_names: word_search = _RE_PATH_COMPONENT.search(word) if not word_search: # Invalid path specified. raise XpathError('xpath component parse error: %s' % word) if word_search.group('key') is not None: # A path key was provided. tmp_key = {} for x in re.findall(r'\[([^]]*)\]', word): tmp_key[x.split("=")[0]] = x.split("=")[-1] gnmi_elems.append(gnmi_pb2.PathElem(name=word_search.group( 'pname'), key=tmp_key)) else: gnmi_elems.append(gnmi_pb2.PathElem(name=word, key={})) return gnmi_pb2.Path(elem=gnmi_elems)
def ParsePath(p_names: Iterable[Text]) -> gnmi_pb2.Path: """Parses a list of path names for path keys. Args: p_names: (list) of path elements, which may include keys. Returns: a gnmi_pb2.Path object representing gNMI path elements. Raises: XpathError: Unabled to parse the xpath provided. """ gnmi_elems = [] for word in p_names: word_search = _RE_PATH_COMPONENT.search(word) if not word_search: # Invalid path specified. raise XpathError('xpath component parse error: %s' % word) if word_search.group('key') is not None: # A path key was provided. tmp_key = {} for x in re.findall(r'\[([^]]*)\]', word): tmp_key[x.split('=')[0]] = x.split('=')[-1] gnmi_elems.append( gnmi_pb2.PathElem(name=word_search.group('pname'), key=tmp_key)) else: gnmi_elems.append(gnmi_pb2.PathElem(name=word, key={})) return gnmi_pb2.Path(elem=gnmi_elems)
def _parse_path(p_names): gnmi_elems = [] for word in p_names: word_search = _RE_PATH_COMPONENT.search(word) if not word_search: # Invalid path specified. raise XpathError('xpath component parse error: %s' % word) if word_search.group('key') is not None: # A path key was provided. tmp_key = {} for x in re.findall(r'\[([^]]*)\]', word): tmp_key[x.split("=")[0]] = x.split("=")[-1] gnmi_elems.append( gnmi_pb2.PathElem(name=word_search.group('pname'), key=tmp_key)) else: gnmi_elems.append(gnmi_pb2.PathElem(name=word, key={})) return gnmi_pb2.Path(elem=gnmi_elems)
def GetRequest(stub, path): """Issue a gNMI GetRequest for the given path.""" path_list = [gnmi_pb2.PathElem(name=path, key={})] 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 path_from_string(path='/'): mypath = [] for e in list_from_path(path): eName = e.split("[", 1)[0] eKeys = re.findall('\[(.*?)\]', e) dKeys = dict(x.split('=', 1) for x in eKeys) mypath.append(gnmi_pb2.PathElem(name=eName, key=dKeys)) return gnmi_pb2.Path(elem=mypath)
print("Supported Encodings:") for e in capabilityResponse.supported_encodings: print(e) print("gNMI Version: ", capabilityResponse.gNMI_version) 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)
# Assgin the gRPC channel to our gNMI Target. channel = grpc.secure_channel('www.example.com:10161', creds) stub = gnmi_pb2_grpc.gNMIStub(channel) return stub 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 if __name__ == '__main__': all_configs = CreateConfigs() stub = SetUpChannel() path_list = [ gnmi_pb2.PathElem(name='interfaces', key={}), gnmi_pb2.PathElem(name='config', key={}) ] gnmi_response = GetRequest(stub, path_list) js_response = json.loads( gnmi_response.notification[0].update[0].val.json_val) pybindJSONDecoder.load_json(js_response, None, None, obj=configs.interfaces.config) print(pybindJSON.dumps(configs, indent=2))