-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
932 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package common_utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config" | ||
"github.com/go-redis/redis" | ||
log "github.com/golang/glog" | ||
) | ||
|
||
const ( | ||
dbName = "STATE_DB" | ||
) | ||
|
||
func getRedisDBClient() (*redis.Client, error) { | ||
rclient := redis.NewClient(&redis.Options{ | ||
Network: "tcp", | ||
Addr: sdcfg.GetDbTcpAddr(dbName), | ||
Password: "", // no password set | ||
DB: sdcfg.GetDbId(dbName), | ||
DialTimeout: 0, | ||
}) | ||
if rclient == nil { | ||
return nil, fmt.Errorf("Cannot create redis client.") | ||
} | ||
if _, err := rclient.Ping().Result(); err != nil { | ||
return nil, err | ||
} | ||
return rclient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package common_utils | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/go-redis/redis" | ||
log "github.com/golang/glog" | ||
) | ||
|
||
// NotificationProducer provides utilities for sending messages using notification channel. | ||
// NewNotificationProducer must be called for a new producer. | ||
// Close must be called when finished. | ||
type NotificationProducer struct { | ||
ch string | ||
rc *redis.Client | ||
} | ||
|
||
// NewNotificationProducer returns a new NotificationProducer. | ||
func NewNotificationProducer(ch string) (*NotificationProducer, error) { | ||
n := new(NotificationProducer) | ||
n.ch = ch | ||
|
||
// Create redis client. | ||
var err error | ||
n.rc, err = getRedisDBClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
// Close performs cleanup works. | ||
// Close must be called when finished. | ||
func (n *NotificationProducer) Close() { | ||
if n.rc != nil { | ||
n.rc.Close() | ||
} | ||
} | ||
|
||
func (n *NotificationProducer) Send(op, data string, kvs map[string]string) error { | ||
fvs := []string{op, data} | ||
for k, v := range kvs { | ||
fvs = append(fvs, k) | ||
fvs = append(fvs, v) | ||
} | ||
|
||
val, err := json.Marshal(fvs) | ||
if err != nil { | ||
log.Error(err.Error()) | ||
return err | ||
} | ||
log.Infof("Publishing to channel %s: %v.", n.ch, string(val)) | ||
return n.rc.Publish(n.ch, val).Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package common_utils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const ( | ||
channel string = "channel" | ||
) | ||
|
||
func TestNotificationProducerSucceedsWithEmptyOp(t *testing.T) { | ||
n, _ := NewNotificationProducer(channel) | ||
defer n.Close() | ||
if err := n.Send("", "somedata", map[string]string{}); err != nil { | ||
t.Fatalf("Expected no error!") | ||
} | ||
} | ||
|
||
func TestNotificationProducerSucceedsWithEmptyData(t *testing.T) { | ||
n, _ := NewNotificationProducer(channel) | ||
defer n.Close() | ||
if err := n.Send("someop", "", map[string]string{}); err != nil { | ||
t.Fatalf("Expected no error!") | ||
} | ||
} | ||
|
||
func TestNotificationProducerSucceedsWithEmptyOpAndData(t *testing.T) { | ||
n, _ := NewNotificationProducer(channel) | ||
defer n.Close() | ||
if err := n.Send("", "", map[string]string{}); err != nil { | ||
t.Fatalf("Expected no error!") | ||
} | ||
} | ||
|
||
func TestNotificationProducerSucceedsWithEmptyKeyValues(t *testing.T) { | ||
n, _ := NewNotificationProducer(channel) | ||
defer n.Close() | ||
if err := n.Send("someop", "somedata", map[string]string{}); err != nil { | ||
t.Fatalf("Expected no error!") | ||
} | ||
} | ||
|
||
func TestNotificationProducerSucceeds(t *testing.T) { | ||
n, _ := NewNotificationProducer(channel) | ||
defer n.Close() | ||
if err := n.Send("someop", "somedata", map[string]string{"somekey": "somevalue"}); err != nil { | ||
t.Fatalf("Expected no error!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.