forked from google/gnxi
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gnmi_set.go
165 lines (145 loc) · 4.69 KB
/
gnmi_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Binary gnmi_set performs a set request against a gNMI target with the specified config file.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
log "github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/google/gnxi/utils"
"github.com/google/gnxi/utils/credentials"
"github.com/google/gnxi/utils/xpath"
pb "github.com/openconfig/gnmi/proto/gnmi"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var (
deleteOpt arrayFlags
replaceOpt arrayFlags
updateOpt arrayFlags
targetAddr = flag.String("target_addr", "localhost:10161", "The target address in the format of host:port")
targetName = flag.String("target_name", "hostname.com", "The target name use to verify the hostname returned by TLS handshake")
timeOut = flag.Duration("time_out", 10*time.Second, "Timeout for the Get request, 10 seconds by default")
)
func buildPbUpdateList(pathValuePairs []string) []*pb.Update {
var pbUpdateList []*pb.Update
for _, item := range pathValuePairs {
pathValuePair := strings.SplitN(item, ":", 2)
// TODO (leguo): check if any path attribute contains ':'
if len(pathValuePair) != 2 || len(pathValuePair[1]) == 0 {
log.Exitf("invalid path-value pair: %v", item)
}
pbPath, err := xpath.ToGNMIPath(pathValuePair[0])
if err != nil {
log.Exitf("error in parsing xpath %q to gnmi path", pathValuePair[0])
}
var pbVal *pb.TypedValue
if pathValuePair[1][0] == '@' {
jsonFile := pathValuePair[1][1:]
jsonConfig, err := ioutil.ReadFile(jsonFile)
if err != nil {
log.Exitf("cannot read data from file %v", jsonFile)
}
jsonConfig = bytes.Trim(jsonConfig, " \r\n\t")
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_JsonIetfVal{
JsonIetfVal: jsonConfig,
},
}
} else {
if strVal, err := strconv.Unquote(pathValuePair[1]); err == nil {
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_StringVal{
StringVal: strVal,
},
}
} else {
if intVal, err := strconv.ParseInt(pathValuePair[1], 10, 64); err == nil {
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_IntVal{
IntVal: intVal,
},
}
} else if floatVal, err := strconv.ParseFloat(pathValuePair[1], 32); err == nil {
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_FloatVal{
FloatVal: float32(floatVal),
},
}
} else if boolVal, err := strconv.ParseBool(pathValuePair[1]); err == nil {
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_BoolVal{
BoolVal: boolVal,
},
}
} else {
pbVal = &pb.TypedValue{
Value: &pb.TypedValue_StringVal{
StringVal: pathValuePair[1],
},
}
}
}
}
pbUpdateList = append(pbUpdateList, &pb.Update{Path: pbPath, Val: pbVal})
}
return pbUpdateList
}
func main() {
flag.Var(&deleteOpt, "delete", "xpath to be deleted.")
flag.Var(&replaceOpt, "replace", "xpath:value pair to be replaced. Value can be numeric, boolean, string, or IETF JSON file (. starts with '@').")
flag.Var(&updateOpt, "update", "xpath:value pair to be updated. Value can be numeric, boolean, string, or IETF JSON file (. starts with '@').")
flag.Parse()
opts := credentials.ClientCredentials(*targetName)
conn, err := grpc.Dial(*targetAddr, opts...)
if err != nil {
log.Exitf("Dialing to %q failed: %v", *targetAddr, err)
}
defer conn.Close()
var deleteList []*pb.Path
for _, xPath := range deleteOpt {
pbPath, err := xpath.ToGNMIPath(xPath)
if err != nil {
log.Exitf("error in parsing xpath %q to gnmi path", xPath)
}
deleteList = append(deleteList, pbPath)
}
replaceList := buildPbUpdateList(replaceOpt)
updateList := buildPbUpdateList(updateOpt)
setRequest := &pb.SetRequest{
Delete: deleteList,
Replace: replaceList,
Update: updateList,
}
fmt.Println("== setRequest:")
utils.PrintProto(setRequest)
cli := pb.NewGNMIClient(conn)
setResponse, err := cli.Set(context.Background(), setRequest)
if err != nil {
log.Exitf("Set failed: %v", err)
}
fmt.Println("== getResponse:")
utils.PrintProto(setResponse)
}