# openconfig/gnmi architecture

> Architecture diagram of the GitHub repository openconfig/gnmi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openconfig/gnmi
- Repository: https://github.com/openconfig/gnmi

## Overview

##### Main Components
- Client Library – Located in the “client” directory. This is the core component that provides the main entry points for interacting with gNMI services. It includes support for various functionalities such as connection handling, reconnection logic, flag parsing (in “client/flags”), gNMI-specific protocols (in “client/gnmi”), and more.
- CLI Tool – Found under “cmd/gnmi_cli”. This component is built on top of the client library and exposes a command-line interface for users to interact with gNMI services.
- Caching Collector – Present in “cmd/gnmi_collector” and supported by the “collector” package. Its responsibility is to connect to multiple gNMI targets, collect and cache data, and then provide it over a single gNMI Subscribe RPC to clients.
- Protocol Definitions and gRPC Components – The “proto” directories (including proto/collector, proto/gnmi, proto/gnmi_ext, and proto/target) contain the Protocol Buffer files and the generated gRPC Go files. These files define service interfaces and data structures used across the project.
- Supporting Modules – Other directories (such as connection, manager, metadata, coalesce, subscribe, target, etc.) contain supporting functions and components that help manage connections, organize the tree structure, handle metadata, error handling (errdiff, errlist), and manage subscriptions.
- Testing & Fake Implementations – The “testing” and “client/fake” directories include fake implementations, mocks, and test data that help validate the behavior of the core functionalities.

##### Determining Relationships and Interactions
- The CLI tool (cmd/gnmi_cli) uses the client library to communicate with external gNMI targets.
- The Caching Collector uses both the client library and the collector components to consolidate data from multiple gNMI endpoints.
- All network interactions are based on gRPC and defined via the Protocol Buffer files found in the proto directories.
- Underlying modules like “connection”, “manager”, and “metadata” provide essential support to both the client and collector components.
- Testing and fake packages simulate real-world interactions, ensuring that modules such as reconnect logic and subscription management perform as expected.

##### Highlighting Architectural Patterns and Design Principles
- Modularization: The project is clearly divided into modular components (e.g., CLI, client library, collector), making it easier to maintain and extend.
- Separation of Concerns: Each directory addresses a specific concern (e.g., protocol definitions in proto, connection management in connection, flag parsing in client/flags).
- Reuse and Extensibility: The use of well-defined APIs and shared protocols allows these components to be reused across different tools (CLI vs. Collector) and potentially integrated into other systems.
- Build/CI Integration: The presence of Bazel build files (BUILD.bazel, MODULE.bazel) and GitHub workflows illustrates the integration of modern build and CI practices.

##### Including Relevant Technologies, Frameworks, and Tools
- Go Language – The entire project is built in Go.
- gRPC – For remote procedure calls and network communications as defined using Protocol Buffers.
- Bazel – Used as the build system (as shown by the several BUILD.bazel, MODULE.bazel, and related files).
- Testing Framework – The presence of “_test.go” files and directories for fake/test implementations indicates standard Go testing practices.

##### Overall Detailed Steps
- Client library to the supporting modules (manager, connection, metadata, etc.) arranged in a subordinate layer.
- Optionally, show a side-branch for testing components to indicate how fake implementations and testing configurations integrate into the development cycle.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "External Interface"
        CLI["CLI Tool (gnmi_cli)"]:::external
        Collector["Caching Collector (gnmi_collector)"]:::external
    end

    subgraph "Core Library"
        ClientLib["Client Library"]:::core
        Flags["Flags (client/flags)"]:::core
        GNMI["gNMI (client/gnmi)"]:::core
    end

    subgraph "Supporting Modules"
        Connect["Connection Management"]:::support
        Manager["Manager"]:::support
        Metadata["Metadata"]:::support
        Coalesce["Coalesce"]:::support
        Subscribe["Subscription Management"]:::support
        Tgt["Target Handling"]:::support
        CollectorPkg["Collector Package"]:::support
    end

    subgraph "Protocol Definitions"
        ProtoCollector["Proto: collector"]:::proto
        ProtoGNMI["Proto: gnmi"]:::proto
        ProtoGNMIExt["Proto: gnmi_ext"]:::proto
        ProtoTarget["Proto: target"]:::proto
    end

    subgraph "External gNMI Targets"
        GNMITargets["gNMI Targets/Devices"]:::external
    end

    subgraph "Testing Components"
        ClientFake["Client Fake Implementations"]:::testing
        TestingComp["Additional Testing Components"]:::testing
    end

    %% Connections between external interfaces and core library
    CLI -->|"calls"| ClientLib
    Collector -->|"calls"| ClientLib
    Collector -->|"uses"| CollectorPkg

    %% Core Library internal structure
    ClientLib -->|"includes"| Flags
    ClientLib -->|"includes"| GNMI

    %% Core Library using supporting modules
    ClientLib -->|"uses"| Connect
    ClientLib -->|"uses"| Manager
    ClientLib -->|"uses"| Metadata
    ClientLib -->|"uses"| Coalesce
    ClientLib -->|"uses"| Subscribe
    ClientLib -->|"uses"| Tgt

    %% Communication with protocol definitions and external targets
    ClientLib -->|"defines"| ProtoGNMI
    CollectorPkg -->|"aggregates"| ClientLib
    ClientLib -->|"communicates"| GNMITargets
    Collector -->|"communicates"| GNMITargets

    %% Supporting modules also leverage protocols
    Connect -->|"uses"| ProtoGNMI
    Manager -->|"uses"| ProtoGNMI
    Metadata -->|"uses"| ProtoGNMI
    Subscribe -->|"uses"| ProtoGNMI

    %% Testing interactions
    ClientFake -->|"tests"| ClientLib
    TestingComp -->|"validates"| ClientLib

    %% Click Events
    click ClientLib "https://github.com/openconfig/gnmi/tree/master/client"
    click Flags "https://github.com/openconfig/gnmi/tree/master/client/flags"
    click GNMI "https://github.com/openconfig/gnmi/tree/master/client/gnmi"
    click CLI "https://github.com/openconfig/gnmi/tree/master/cmd/gnmi_cli"
    click Collector "https://github.com/openconfig/gnmi/tree/master/cmd/gnmi_collector"
    click CollectorPkg "https://github.com/openconfig/gnmi/blob/master/collector/collector.go"
    click ProtoCollector "https://github.com/openconfig/gnmi/tree/master/proto/collector"
    click ProtoGNMI "https://github.com/openconfig/gnmi/tree/master/proto/gnmi"
    click ProtoGNMIExt "https://github.com/openconfig/gnmi/tree/master/proto/gnmi_ext"
    click ProtoTarget "https://github.com/openconfig/gnmi/tree/master/proto/target"
    click Connect "https://github.com/openconfig/gnmi/blob/master/connection/connection.go"
    click Manager "https://github.com/openconfig/gnmi/blob/master/manager/manager.go"
    click Metadata "https://github.com/openconfig/gnmi/blob/master/metadata/metadata.go"
    click Coalesce "https://github.com/openconfig/gnmi/blob/master/coalesce/coalesce.go"
    click Subscribe "https://github.com/openconfig/gnmi/blob/master/subscribe/subscribe.go"
    click Tgt "https://github.com/openconfig/gnmi/blob/master/target/target.go"
    click ClientFake "https://github.com/openconfig/gnmi/tree/master/client/fake"
    click TestingComp "https://github.com/openconfig/gnmi/tree/master/testing/fake"

    %% Styles
    classDef external fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef core fill:#ABEBC6,stroke:#1E8449,stroke-width:2px;
    classDef support fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef proto fill:#F5CBA7,stroke:#7D6608,stroke-width:2px;
    classDef testing fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
```
