Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for sonic-gnmi. #3

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
build/
debian/.debhelper/
debian/files
debian/sonic-telemetry.debhelper.log
debian/sonic-telemetry.substvars
debian/sonic-telemetry/
debian/sonic-gnmi.debhelper.log
debian/sonic-gnmi.substvars
debian/sonic-gnmi/
vendor
src
cvl
translib
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2016 Microsoft, Inc.
Copyright 2022 Microsoft, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
58 changes: 25 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,100 +7,92 @@ INSTALL := /usr/bin/install
DBDIR := /var/run/redis/sonic-db/
GO ?= /usr/local/go/bin/go
TOP_DIR := $(abspath ..)
MGMT_COMMON_DIR := $(TOP_DIR)/sonic-mgmt-common
BUILD_DIR := build/bin
export CVL_SCHEMA_PATH := $(MGMT_COMMON_DIR)/build/cvl/schema
export GOBIN := $(abspath $(BUILD_DIR))
export PATH := $(PATH):$(GOBIN):$(shell dirname $(GO))

SRC_FILES=$(shell find . -name '*.go' | grep -v '_test.go' | grep -v '/tests/')
TEST_FILES=$(wildcard *_test.go)
TELEMETRY_TEST_DIR = build/tests/gnmi_server
TELEMETRY_TEST_BIN = $(TELEMETRY_TEST_DIR)/server.test
ifeq ($(TELEMETRY_WRITABLE),y)
BLD_FLAGS := -tags readwrite
endif

GO_DEPS := vendor/.done
PATCHES := $(wildcard patches/*.patch)
PATCHES += $(shell find $(MGMT_COMMON_DIR)/patches -type f)
UNIT_TEST := $(shell if sudo [ -e /var/run/redis/redis.sock ]; then echo "exist"; else echo "noexist"; fi)

all: sonic-telemetry $(TELEMETRY_TEST_BIN)
all: sonic-gnmi

go.mod:
$(GO) mod init github.com/Azure/sonic-telemetry
$(GO) mod init github.com/sonic-net/sonic-gnmi

$(GO_DEPS): go.mod $(PATCHES)
$(GO) mod vendor
$(GO) mod download golang.org/x/crypto@v0.0.0-20191206172530-e9b2fee46413
$(GO) mod download github.com/jipanyang/gnxi@v0.0.0-20181221084354-f0a90cca6fd0
cp -r $(GOPATH)/pkg/mod/golang.org/x/crypto@v0.0.0-20191206172530-e9b2fee46413/* vendor/golang.org/x/crypto/
cp -r $(GOPATH)/pkg/mod/github.com/jipanyang/gnxi@v0.0.0-20181221084354-f0a90cca6fd0/* vendor/github.com/jipanyang/gnxi/
$(MGMT_COMMON_DIR)/patches/apply.sh vendor
bash patches/apply.sh vendor
ganglyu marked this conversation as resolved.
Show resolved Hide resolved
chmod -R u+w vendor
patch -d vendor -p0 < patches/gnmi_cli.all.patch
patch -d vendor -p0 < patches/gnmi_set.patch
patch -d vendor -p0 < patches/gnmi_get.patch
patch -d vendor -p0 < patches/path.patch
touch $@

go-deps: $(GO_DEPS)

go-deps-clean:
$(RM) -r vendor

sonic-telemetry: $(GO_DEPS)
sonic-gnmi: $(GO_DEPS)
ifeq ($(CROSS_BUILD_ENVIRON),y)
$(GO) build -o ${GOBIN}/telemetry -mod=vendor $(BLD_FLAGS) github.com/Azure/sonic-telemetry/telemetry
$(GO) build -o ${GOBIN}/dialout_client_cli -mod=vendor $(BLD_FLAGS) github.com/Azure/sonic-telemetry/dialout/dialout_client_cli
$(GO) build -o ${GOBIN}/gnxi -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/gnxi
$(GO) build -o ${GOBIN}/gnmi_get -mod=vendor github.com/jipanyang/gnxi/gnmi_get
$(GO) build -o ${GOBIN}/gnmi_set -mod=vendor github.com/jipanyang/gnxi/gnmi_set
$(GO) build -o ${GOBIN}/gnmi_cli -mod=vendor github.com/openconfig/gnmi/cmd/gnmi_cli
$(GO) build -o ${GOBIN}/gnoi_client -mod=vendor github.com/Azure/sonic-telemetry/gnoi_client
$(GO) build -o ${GOBIN}/gnoi_client -mod=vendor github.com/sonic-net/sonic-gnmi/gnoi_client
$(GO) build -o ${GOBIN}/gnmi_dump -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
else
$(GO) install -mod=vendor $(BLD_FLAGS) github.com/Azure/sonic-telemetry/telemetry
$(GO) install -mod=vendor $(BLD_FLAGS) github.com/Azure/sonic-telemetry/dialout/dialout_client_cli
$(GO) install -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/gnxi
$(GO) install -mod=vendor github.com/jipanyang/gnxi/gnmi_get
$(GO) install -mod=vendor github.com/jipanyang/gnxi/gnmi_set
$(GO) install -mod=vendor github.com/openconfig/gnmi/cmd/gnmi_cli
$(GO) install -mod=vendor github.com/Azure/sonic-telemetry/gnoi_client
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnoi_client
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
endif

check:
ifeq ("$(UNIT_TEST)", "exist")
ifeq ($(wildcard ${DBDIR}/database_config.json),)
sudo mkdir -p ${DBDIR}
sudo cp ./testdata/database_config.json ${DBDIR}
sudo mkdir -p /usr/models/yang || true
sudo find $(MGMT_COMMON_DIR)/models -name '*.yang' -exec cp {} /usr/models/yang/ \;
-sudo $(GO) test -coverprofile=coverage-config.txt -covermode=atomic -v github.com/Azure/sonic-telemetry/sonic_db_config
-sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/Azure/sonic-telemetry/gnmi_server
-sudo $(GO) test -coverprofile=coverage-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/Azure/sonic-telemetry/dialout/dialout_client
endif
sudo $(GO) test -coverprofile=coverage-gnmi.tmp -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
ganglyu marked this conversation as resolved.
Show resolved Hide resolved
sudo $(GO) test -coverprofile=coverage-dbus.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_service_client
$(GO) get github.com/axw/gocov/...
$(GO) get github.com/AlekSi/gocov-xml
cat coverage-gnmi.tmp | grep -v "sonic-gnmi/proto" > coverage-gnmi.txt
gocov convert coverage-*.txt | gocov-xml -source $(shell pwd) > coverage.xml
rm -rf coverage-*.txt
rm -rf coverage-*.tmp
rm -rf coverage-*.txt
endif

clean:
$(RM) -r build
$(RM) -r vendor

$(TELEMETRY_TEST_BIN): $(TEST_FILES) $(SRC_FILES)
mkdir -p $(@D)
cp -r testdata $(@D)/
$(GO) test -mod=vendor $(BLD_FLAGS) -c -cover github.com/Azure/sonic-telemetry/gnmi_server -o $@

install:
$(INSTALL) -D $(BUILD_DIR)/telemetry $(DESTDIR)/usr/sbin/telemetry
$(INSTALL) -D $(BUILD_DIR)/dialout_client_cli $(DESTDIR)/usr/sbin/dialout_client_cli
$(INSTALL) -D $(BUILD_DIR)/gnxi $(DESTDIR)/usr/sbin/gnxi
$(INSTALL) -D $(BUILD_DIR)/gnmi_get $(DESTDIR)/usr/sbin/gnmi_get
$(INSTALL) -D $(BUILD_DIR)/gnmi_set $(DESTDIR)/usr/sbin/gnmi_set
$(INSTALL) -D $(BUILD_DIR)/gnmi_cli $(DESTDIR)/usr/sbin/gnmi_cli
$(INSTALL) -D $(BUILD_DIR)/gnoi_client $(DESTDIR)/usr/sbin/gnoi_client
$(INSTALL) -D $(BUILD_DIR)/gnmi_dump $(DESTDIR)/usr/sbin/gnmi_dump


deinstall:
rm $(DESTDIR)/usr/sbin/telemetry
rm $(DESTDIR)/usr/sbin/dialout_client_cli
rm $(DESTDIR)/usr/sbin/gnxi
rm $(DESTDIR)/usr/sbin/gnmi_get
rm $(DESTDIR)/usr/sbin/gnmi_set
rm $(DESTDIR)/usr/sbin/gnoi_client
rm $(DESTDIR)/usr/sbin/gnmi_dump


84 changes: 2 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,4 @@
# SONiC-telemetry
# SONiC-gnmi

## Description
This repository contains implementation for the sonic system telemetry services:
- dial-in mode system telemetry server: `telemetry`
- dial-out mode system telemetry client `dialout_client_cli`

## Getting Started

### Prerequisites

Install __go__ in your system https://golang.org/doc/install. Requires golang1.8+.

## Installing

To install dial-in mode system telemetry server, run

go get -u github.com/Azure/sonic-telemetry/telemetry

To install dial-out mode system telemetry client, run

go get -u github.com/Azure/sonic-telemetry/dialout/dialout_client_cli

There is also a test program dialout_server_cli for collecting data from dial-out mode system telemetry client. _Note_: it is for testing purpose only.

go get -u github.com/Azure/sonic-telemetry/dialout/dialout_server_cli

The binaries will be installed under $GOPATH/bin/, they may be copied to any SONiC switch and run there.

You can also build a debian package and install it:

git clone https://github.com/Azure/sonic-telemetry.git
pushd sonic-telemetry
dpkg-buildpackage -rfakeroot -b -us -uc
popd

### Running
* See [SONiC gRPC telemetry](./doc/grpc_telemetry.md) for how to run dial-in mode system telemetry server
* See [SONiC telemetry in dial-out mode](./doc/dialout.md) for how to run dial-out mode system telemetry client
* See [gNMI Usage Examples](./doc/gNMI_usage_examples.md) for gNMI client usage examples.

## Need Help?

For general questions, setup help, or troubleshooting:
- [sonicproject on Google Groups](https://groups.google.com/d/forum/sonicproject)

For bug reports or feature requests, please open an Issue.

## Contribution guide

See the [contributors guide](https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md) for information about how to contribute.

### GitHub Workflow

We're following basic GitHub Flow. If you have no idea what we're talking about, check out [GitHub's official guide](https://guides.github.com/introduction/flow/). Note that merge is only performed by the repository maintainer.

Guide for performing commits:

* Isolate each commit to one component/bugfix/issue/feature
* Use a standard commit message format:

> [component/folder touched]: Description intent of your changes
>
> [List of changes]
>
> Signed-off-by: Your Name your@email.com

For example:

> swss-common: Stabilize the ConsumerTable
>
> * Fixing autoreconf
> * Fixing unit-tests by adding checkers and initialize the DB before start
> * Adding the ability to select from multiple channels
> * Health-Monitor - The idea of the patch is that if something went wrong with the notification channel,
> we will have the option to know about it (Query the LLEN table length).
>
> Signed-off-by: user@dev.null


* Each developer should fork this repository and [add the team as a Contributor](https://help.github.com/articles/adding-collaborators-to-a-personal-repository)
* Push your changes to your private fork and do "pull-request" to this repository
* Use a pull request to do code review
* Use issues to keep track of what is going on
This repository contains implementation for the sonic gnmi interface:
45 changes: 10 additions & 35 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ pr:
include:
- master

resources:
repositories:
- repository: sonic-mgmt-common
type: github
name: Azure/sonic-mgmt-common
endpoint: build

stages:
- stage: Build
jobs:
Expand All @@ -33,19 +26,15 @@ stages:
variables:
DIFF_COVER_CHECK_THRESHOLD: 80
DIFF_COVER_ENABLE: 'true'
DIFF_COVER_WORKING_DIRECTORY: $(System.DefaultWorkingDirectory)/sonic-telemetry
DIFF_COVER_WORKING_DIRECTORY: $(System.DefaultWorkingDirectory)

container:
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-buster:latest
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-bullseye:latest

steps:
- checkout: self
clean: true
submodules: recursive
displayName: 'Checkout code'

- checkout: sonic-mgmt-common
clean: true
path: sonic-gnmi
submodules: recursive
displayName: 'Checkout code'

Expand All @@ -59,17 +48,10 @@ stages:
runBranch: 'refs/heads/master'
displayName: "Download sonic buildimage"

- task: DownloadPipelineArtifact@2
inputs:
source: specific
project: build
pipeline: 127
artifact: sonic-mgmt-common
runVersion: 'latestFromBranch'
runBranch: 'refs/heads/master'
displayName: "Download sonic-mgmt-common"

- script: |
# PYTEST
sudo pip3 install -U pytest

# REDIS
sudo apt-get install -y redis-server
sudo sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf
Expand All @@ -85,7 +67,7 @@ stages:
set -ex
# Install .NET CORE
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/debian/10/prod
sudo apt-add-repository https://packages.microsoft.com/debian/11/prod
sudo apt-get update
sudo apt-get install -y dotnet-sdk-5.0
displayName: "Install .NET CORE"
Expand All @@ -94,23 +76,16 @@ stages:
set -ex
ls -l

pushd sonic-mgmt-common

NO_TEST_BINS=1 dpkg-buildpackage -rfakeroot -b -us -uc

popd

pushd sonic-telemetry

dpkg-buildpackage -rfakeroot -us -uc -b -j$(nproc) && cp ../*.deb $(Build.ArtifactStagingDirectory)/
displayName: "Build"

- publish: $(Build.ArtifactStagingDirectory)/
artifact: sonic-telemetry
artifact: sonic-gnmi
displayName: "Archive artifacts"

- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/sonic-telemetry/coverage.xml'
summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage.xml'
displayName: 'Publish coverage'

40 changes: 38 additions & 2 deletions common_utils/context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package common_utils

import (
"context"
"fmt"
"context"
"sync/atomic"
)

Expand Down Expand Up @@ -36,6 +36,25 @@ const requestContextKey contextkey = 0
// Request Id generator
var requestCounter uint64

var CountersName = [...]string{
ganglyu marked this conversation as resolved.
Show resolved Hide resolved
"GNMI get",
"GNMI get fail",
"GNMI set",
"GNMI set fail",
"GNOI reboot",
"DBUS",
"DBUS fail",
"DBUS apply patch db",
"DBUS apply patch yang",
"DBUS create checkpoint",
"DBUS delete checkpoint",
"DBUS config save",
"DBUS config reload",
}

var globalCounters [len(CountersName)]uint64


// GetContext function returns the RequestContext object for a
// gRPC request. RequestContext is maintained as a context value of
// the request. Creates a new RequestContext object is not already
Expand All @@ -54,9 +73,26 @@ func GetContext(ctx context.Context) (*RequestContext, context.Context) {
}

func GetUsername(ctx context.Context, username *string) {
rc, _ := GetContext(ctx)
rc, _ := GetContext(ctx)
if rc != nil {
*username = rc.Auth.User
}
}

func InitCounters() {
for i := 0; i < len(CountersName); i++ {
globalCounters[i] = 0
}
SetMemCounters(&globalCounters)
}

func IncCounter(name string) {
for i := 0; i < len(CountersName); i++ {
if CountersName[i] == name {
atomic.AddUint64(&globalCounters[i], 1)
break
}
}
SetMemCounters(&globalCounters)
}

Loading