Ejemplo n.º 1
0
def _create_stub(certs, endpoint):
    creds = gnmi_pb2_grpc.grpc.ssl_channel_credentials(
        root_certificates=certs['root_cert'],
        private_key=certs['private_key'],
        certificate_chain=certs['cert_chain'])
    channel = gnmi_pb2_grpc.grpc.secure_channel(endpoint, creds)
    return gnmi_pb2_grpc.gNMIStub(channel)
Ejemplo n.º 2
0
def _create_stub(creds, target, port, host_override):
    """Creates a gNMI Stub.

  Args:
    creds: (object) of gNMI Credentials class used to build the secure channel.
    target: (str) gNMI Target.
    port: (str) gNMI Target IP port.
    host_override: (str) Hostname being overridden for Cert check.

  Returns:
    a gnmi_pb2_grpc object representing a gNMI Stub.
  """
    if creds:
        if host_override:
            channel = gnmi_pb2_grpc.grpc.secure_channel(
                target + ':' + port, creds, ((
                    'grpc.ssl_target_name_override',
                    host_override,
                ), ))
        else:
            channel = gnmi_pb2_grpc.grpc.secure_channel(
                target + ':' + port, creds)
    else:
        channel = gnmi_pb2_grpc.grpc.insecure_channel(target + ':' + port)
    return gnmi_pb2_grpc.gNMIStub(channel)
Ejemplo n.º 3
0
 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)
     breakpoint()
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
def _create_stub(target, port):
    """Creates a gNMI Stub.

  Args:
    target: (str) gNMI Target.
    port: (str) gNMI Target IP port.

  Returns:
    a gnmi_pb2_grpc object representing a gNMI Stub.
  """
    channel = gnmi_pb2_grpc.grpc.insecure_channel(target + ':' + port)
    return gnmi_pb2_grpc.gNMIStub(channel)
Ejemplo n.º 6
0
def SetUpChannel():
    """Set up the gNMI RPC."""
    wdir = '/home/user/'
    # Specify the credentials in use.
    creds = grpc.ssl_channel_credentials(
        root_certificates=open(wdir + 'client-ca.crt').read(),
        private_key=open(wdir + 'client.key').read(),
        certificate_chain=open(wdir + 'client.crt').read())
    # 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
Ejemplo n.º 7
0
    def __init__(self, targetUrl, tlsEnabled, caCertPath, clientCertPath, privKeyPath):
        self.targetUrl = targetUrl
        if tlsEnabled == True:
            # secure grpc connection
            with open(caCertPath) as f:
                trustedCerts = f.read()
            credentials = grpc.ssl_channel_credentials(trustedCerts, None, None)
            self.__grpcChannel = grpc.secure_channel(targetUrl, credentials)
        else:
            # insecure grpc connection
            self.__grpcChannel = grpc.insecure_channel(targetUrl)

        self.__grpcStub = gnmi_pb2_grpc.gNMIStub(self.__grpcChannel)
Ejemplo n.º 8
0
def CreateStub(creds: grpc.ssl_channel_credentials,
               target: Text,
               port: Text,
               host_override: Optional[Text] = None) -> gnmi_pb2_grpc.gNMIStub:
    """Creates a gNMI GetRequest.

  Args:
    creds: (object) of gNMI Credentials class used to build the secure channel.
    target: (str) gNMI Target.
    port: (str) gNMI Target IP port.
    host_override: (str) Hostname being overridden for Cert check.

  Returns:
    a gnmi_pb2_grpc object representing a gNMI Stub.
  """
    if host_override:
        channel = grpc.secure_channel(target + ':' + port, creds, ((
            'grpc.ssl_target_name_override',
            host_override,
        ), ))
    else:
        channel = grpc.secure_channel(target + ':' + port, creds)
    return gnmi_pb2_grpc.gNMIStub(channel)
Ejemplo n.º 9
0
def _create_stub(target, port):
    channel = gnmi_pb2_grpc.grpc.insecure_channel(target + ':' + port)
    return gnmi_pb2_grpc.gNMIStub(channel)
Ejemplo n.º 10
0
    with open(args.cert, 'rb') as cert:
        creds=grpc.ssl_channel_credentials(cert.read())
    d_print("Credentials supplied: ", creds, ". Opening secure channel to " +
            args.target + "...")
    channel = grpc.secure_channel(target=args.target, credentials=creds,
                                  options=(('grpc.ssl_target_name_override',
                                            args.cert_hostname_override,),))
    d_print("Secure channel connected.")
else:
    d_print("Credentials not supplied. Opening _INSECURE_ channel to " +
            args.target + "...")
    channel = grpc.insecure_channel(target=args.target)
    d_print("Insecure channel connected.")

d_print("Creating gNMI stub...")
stub = gnmi_pb2_grpc.gNMIStub(channel)
d_print("Done.")


# metadata for gNMI authentication:
# https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-authentication.md
metadata = None
if args.username is not None and args.password is not None:
    metadata = (('username', args.username),('password', args.password))

# get & show capabilities if requested
if args.capabilities:
    d_print("Requesting capabilities...")
    capabilityResponse = stub.Capabilities(gnmi_pb2.CapabilityRequest(),
                                           metadata=metadata,
                                           timeout=args.timeout)