Skip to content

Commit 4922601

Browse files
Bulk and RPC API support in translib (sonic-net#16)
Bulk and RPC API support in translib
1 parent 1d730df commit 4922601

File tree

9 files changed

+433
-19
lines changed

9 files changed

+433
-19
lines changed

translib/acl_app.go

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package translib
2121

2222
import (
2323
"bytes"
24+
"errors"
2425
"fmt"
2526
"reflect"
2627
"strconv"
@@ -166,6 +167,11 @@ func (app *AclApp) translateGet(dbs [db.MaxDB]*db.DB) error {
166167
return err
167168
}
168169

170+
func (app *AclApp) translateAction(dbs [db.MaxDB]*db.DB) error {
171+
err := errors.New("Not supported")
172+
return err
173+
}
174+
169175
func (app *AclApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
170176
pathInfo := NewPathInfo(path)
171177
notifInfo := notificationInfo{dbno: db.ConfigDB}
@@ -288,6 +294,13 @@ func (app *AclApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
288294
return GetResponse{Payload: payload}, err
289295
}
290296

297+
func (app *AclApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
298+
var resp ActionResponse
299+
err := errors.New("Not implemented")
300+
301+
return resp, err
302+
}
303+
291304
func (app *AclApp) translateCRUCommon(d *db.DB, opcode int) ([]db.WatchKeys, error) {
292305
var err error
293306
var keys []db.WatchKeys

translib/app_interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ type appInterface interface {
7979
translateReplace(d *db.DB) ([]db.WatchKeys, error)
8080
translateDelete(d *db.DB) ([]db.WatchKeys, error)
8181
translateGet(dbs [db.MaxDB]*db.DB) error
82+
translateAction(dbs [db.MaxDB]*db.DB) error
8283
translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error)
8384
processCreate(d *db.DB) (SetResponse, error)
8485
processUpdate(d *db.DB) (SetResponse, error)
8586
processReplace(d *db.DB) (SetResponse, error)
8687
processDelete(d *db.DB) (SetResponse, error)
8788
processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error)
89+
processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error)
8890
}
8991

9092
//App modules will use this function to register with App interface during boot up

translib/common_app.go

+12
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ func (app *CommonApp) translateGet(dbs [db.MaxDB]*db.DB) error {
112112
return err
113113
}
114114

115+
func (app *CommonApp) translateAction(dbs [db.MaxDB]*db.DB) error {
116+
err := errors.New("Not supported")
117+
return err
118+
}
119+
115120
func (app *CommonApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
116121
err := errors.New("Not supported")
117122
notifInfo := notificationInfo{dbno: db.ConfigDB}
@@ -205,6 +210,13 @@ func (app *CommonApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
205210
return GetResponse{Payload: resPayload}, err
206211
}
207212

213+
func (app *CommonApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
214+
var resp ActionResponse
215+
err := errors.New("Not implemented")
216+
217+
return resp, err
218+
}
219+
208220
func (app *CommonApp) translateCRUDCommon(d *db.DB, opcode int) ([]db.WatchKeys, error) {
209221
var err error
210222
var keys []db.WatchKeys

translib/db/db.go

+54-19
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ func(dbNo DBNum) String() string {
147147
type Options struct {
148148
DBNo DBNum
149149
InitIndicator string
150-
TableNameSeparator string
151-
KeySeparator string
150+
TableNameSeparator string //Overriden by the DB config file's separator.
151+
KeySeparator string //Overriden by the DB config file's separator.
152+
IsWriteDisabled bool //Indicated if write is allowed
152153

153154
DisableCVLCheck bool
154155
}
@@ -580,18 +581,20 @@ func (d *DB) doWrite(ts * TableSpec, op _txOp, key Key, val interface{}) error {
580581
var e error = nil
581582
var value Value
582583

584+
if d.Opts.IsWriteDisabled {
585+
glog.Error("doWrite: Write to DB disabled")
586+
e = errors.New("Write to DB disabled during this operation")
587+
goto doWriteExit
588+
}
589+
583590
switch d.txState {
584591
case txStateNone:
585-
if glog.V(2) {
586-
glog.Info("doWrite: No Transaction.")
587-
}
588-
break
592+
glog.Info("doWrite: No Transaction.")
589593
case txStateWatch:
590594
if glog.V(2) {
591595
glog.Info("doWrite: Change to txStateSet, txState: ", d.txState)
592596
}
593597
d.txState = txStateSet
594-
break
595598
case txStateSet:
596599
if glog.V(5) {
597600
glog.Info("doWrite: Remain in txStateSet, txState: ", d.txState)
@@ -653,7 +656,7 @@ func (d *DB) doWrite(ts * TableSpec, op _txOp, key Key, val interface{}) error {
653656

654657
// Transaction case.
655658

656-
glog.Info("doWrite: op: ", op, " ", key, " : ", value)
659+
glog.Info("doWrite: op: ", op, " ", d.key2redis(ts, key), " : ", value)
657660

658661
switch op {
659662
case txOpHMSet, txOpHDel:
@@ -1099,7 +1102,6 @@ func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error {
10991102
}
11001103

11011104
var e error = nil
1102-
var args []interface{}
11031105
var ret cvl.CVLRetCode
11041106

11051107
//Start CVL session
@@ -1115,6 +1117,44 @@ func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error {
11151117
goto StartTxExit
11161118
}
11171119

1120+
e = d.performWatch(w, tss)
1121+
1122+
StartTxExit:
1123+
1124+
if glog.V(3) {
1125+
glog.Info("StartTx: End: e: ", e)
1126+
}
1127+
return e
1128+
}
1129+
1130+
func (d *DB) AppendWatchTx(w []WatchKeys, tss []*TableSpec) error {
1131+
if glog.V(3) {
1132+
glog.Info("AppendWatchTx: Begin: w: ", w, " tss: ", tss)
1133+
}
1134+
1135+
var e error = nil
1136+
1137+
// Validate State
1138+
if d.txState == txStateNone {
1139+
glog.Error("AppendWatchTx: Incorrect State, txState: ", d.txState)
1140+
e = errors.New("Transaction has not started")
1141+
goto AppendWatchTxExit
1142+
}
1143+
1144+
e = d.performWatch(w, tss)
1145+
1146+
AppendWatchTxExit:
1147+
1148+
if glog.V(3) {
1149+
glog.Info("AppendWatchTx: End: e: ", e)
1150+
}
1151+
return e
1152+
}
1153+
1154+
func (d *DB) performWatch(w []WatchKeys, tss []*TableSpec) error {
1155+
var e error
1156+
var args []interface{}
1157+
11181158
// For each watchkey
11191159
// If a pattern, Get the keys, appending results to Cmd args.
11201160
// Else append keys to the Cmd args
@@ -1133,7 +1173,7 @@ func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error {
11331173

11341174
redisKeys, e := d.client.Keys(redisKey).Result()
11351175
if e != nil {
1136-
glog.Warning("StartTx: Keys: " + e.Error())
1176+
glog.Warning("performWatch: Keys: " + e.Error())
11371177
continue
11381178
}
11391179
for j := 0; j < len(redisKeys); j++ {
@@ -1148,27 +1188,22 @@ func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error {
11481188
}
11491189

11501190
if len(args) == 1 {
1151-
glog.Warning("StartTx: Empty WatchKeys. Skipping WATCH")
1152-
goto StartTxSkipWatch
1191+
glog.Warning("performWatch: Empty WatchKeys. Skipping WATCH")
1192+
goto SkipWatch
11531193
}
11541194

11551195
// Issue the WATCH
11561196
_, e = d.client.Do(args...).Result()
11571197

11581198
if e != nil {
1159-
glog.Warning("StartTx: Do: WATCH ", args, " e: ", e.Error())
1199+
glog.Warning("performWatch: Do: WATCH ", args, " e: ", e.Error())
11601200
}
11611201

1162-
StartTxSkipWatch:
1202+
SkipWatch:
11631203

11641204
// Switch State
11651205
d.txState = txStateWatch
11661206

1167-
StartTxExit:
1168-
1169-
if glog.V(3) {
1170-
glog.Info("StartTx: End: e: ", e)
1171-
}
11721207
return e
11731208
}
11741209

translib/intf_app.go

+12
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ func (app *IntfApp) translateGet(dbs [db.MaxDB]*db.DB) error {
245245
return err
246246
}
247247

248+
func (app *IntfApp) translateAction(dbs [db.MaxDB]*db.DB) error {
249+
err := errors.New("Not supported")
250+
return err
251+
}
252+
248253
func (app *IntfApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
249254
app.appDB = dbs[db.ApplDB]
250255
pathInfo := NewPathInfo(path)
@@ -470,6 +475,13 @@ func (app *IntfApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
470475
return GetResponse{Payload: payload}, err
471476
}
472477

478+
func (app *IntfApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
479+
var resp ActionResponse
480+
err := errors.New("Not implemented")
481+
482+
return resp, err
483+
}
484+
473485
/* Checking IP adderss is v4 */
474486
func validIPv4(ipAddress string) bool {
475487
ipAddress = strings.Trim(ipAddress, " ")

translib/lldp_app.go

+12
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func (app *lldpApp) translateGet(dbs [db.MaxDB]*db.DB) error {
132132
return err
133133
}
134134

135+
func (app *lldpApp) translateAction(dbs [db.MaxDB]*db.DB) error {
136+
err := errors.New("Not supported")
137+
return err
138+
}
139+
135140
func (app *lldpApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
136141
pathInfo := NewPathInfo(path)
137142
notifInfo := notificationInfo{dbno: db.ApplDB}
@@ -254,6 +259,13 @@ func (app *lldpApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
254259
return GetResponse{Payload:payload}, err
255260
}
256261

262+
func (app *lldpApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
263+
var resp ActionResponse
264+
err := errors.New("Not implemented")
265+
266+
return resp, err
267+
}
268+
257269
/** Helper function to populate JSON response for GET request **/
258270
func (app *lldpApp) getLldpNeighInfoFromInternalMap(ifName *string, ifInfo *ocbinds.OpenconfigLldp_Lldp_Interfaces_Interface) {
259271

translib/pfm_app.go

+13
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func (app *PlatformApp) getAppRootObject() (*ocbinds.OpenconfigPlatform_Componen
7272
return deviceObj.Components
7373
}
7474

75+
func (app *PlatformApp) translateAction(dbs [db.MaxDB]*db.DB) error {
76+
err := errors.New("Not supported")
77+
return err
78+
}
79+
7580
func (app *PlatformApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
7681

7782
var err error
@@ -189,6 +194,14 @@ func (app *PlatformApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
189194
return GetResponse{Payload: payload}, err
190195
}
191196

197+
func (app *PlatformApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
198+
var resp ActionResponse
199+
err := errors.New("Not implemented")
200+
201+
return resp, err
202+
}
203+
204+
192205
///////////////////////////
193206

194207

translib/sys_app.go

+13
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (app *SysApp) getAppRootObject() *ocbinds.OpenconfigSystem_System {
7676
return deviceObj.System
7777
}
7878

79+
func (app *SysApp) translateAction(dbs [db.MaxDB]*db.DB) error {
80+
err := errors.New("Not supported")
81+
return err
82+
}
83+
7984
func (app *SysApp) translateSubscribe(dbs [db.MaxDB]*db.DB, path string) (*notificationOpts, *notificationInfo, error) {
8085
var err error
8186

@@ -339,3 +344,11 @@ func (app *SysApp) processGet(dbs [db.MaxDB]*db.DB) (GetResponse, error) {
339344
}
340345
return GetResponse{Payload: payload}, err
341346
}
347+
348+
func (app *SysApp) processAction(dbs [db.MaxDB]*db.DB) (ActionResponse, error) {
349+
var resp ActionResponse
350+
err := errors.New("Not implemented")
351+
352+
return resp, err
353+
}
354+

0 commit comments

Comments
 (0)