-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaincode.go
More file actions
497 lines (414 loc) · 16.3 KB
/
Copy pathchaincode.go
File metadata and controls
497 lines (414 loc) · 16.3 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"os/exec"
"sync"
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
//This packages are for the code that reads the ovs-db - meaning the readLine fucntion
// "log"
"bufio"
"io"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
type flow struct {
ObjectType string `json:"docType"` //docType is used to distinguish the various types of objects in state database
InPort string `json:"in_port"` //the fieldtags are needed to keep case from bouncing around
Actions string `json:"actions"`
Priority int `json:"priority"`
NetworkSourceIP string `json:"nw_src"`
Cookie string `json:"cookie"` //normally this field requires some hex data type, because it can have only hex values
Duration int `json:"duration"`
Table int `json:"table"`
N_packets int `json:"n_packets"`
N_bytes int `json:"n_bytes"`
Idle_age int `json:"idle_age"`
}
// This function is mine
// It was added for the reason that i wanted somehow external commands to be executed outside the source code when a branching condition was met
func exe_cmd(cmd string, wg *sync.WaitGroup) {
fmt.Println(cmd)
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
fmt.Println("failed !!!!!!! with %s\n", err)
}
fmt.Printf("%s", out)
wg.Done()
}
//This function is from https://gist.github.com/jgfrancisco/6610078040b15b7e9611 and we need it in order to read ovs-db
func readLine(reader *bufio.Reader) (strLine string, err error) {
buffer := new(bytes.Buffer)
for {
var line []byte
var isPrefix bool
line, isPrefix, err = reader.ReadLine()
// log.Printf("[DEBUG] Read Len: %d, isPrefix: %t, Error: %v\n", len(line), isPrefix, err)
if err != nil && err != io.EOF {
return "", err
}
buffer.Write(line)
if !isPrefix {
// log.Println("[INFO] EOL found")
break
}
}
// log.Println("[DEBUG] End of line")
return buffer.String(), err
}
// ===================================================================================
// Main
// ===================================================================================
func main() {
fmt.Println("Chaincode is now starting@@")
err := shim.Start(new(SimpleChaincode))
fmt.Println("Chaincode has been started@@")
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
// Init initializes chaincode
// ===========================
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("Init is about to retun here")
return shim.Success(nil)
}
// Invoke - Our entry point for Invocations
// ========================================
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
fmt.Println("invoke is running " + function)
// Handle different functions
if function == "initFlow" { //create a new flow
return t.initFlow(stub, args)
} else if function == "transferFlow" { //change nw_src of a specific flow
return t.transferFlow(stub, args)
} else if function == "transferFlowsBasedOnActions" { //transfer all flows of a certain actions
return t.transferFlowsBasedOnActions(stub, args)
} else if function == "delete" { //delete a flow
return t.delete(stub, args)
} else if function == "readFlow" { //read a flow
return t.readFlow(stub, args)
} else if function == "queryFlowsByNetworkSourceIP" { //find flows for nw_src X using rich query
return t.queryFlowsByNetworkSourceIP(stub, args)
} else if function == "queryFlows" { //find flows based on an ad hoc rich query
return t.queryFlows(stub, args)
} else if function == "getFlowsByRange" { //get flows based on range query
return t.getFlowsByRange(stub, args)
}
//the fmt.Println call output is printed in the docker container of the cc logs (docker logs net-peer*)
fmt.Println("invoke did not find func: " + function) //error
//the shim.Error call output is printed in the stdout when we invoke a function from the Invokefunction packets.
return shim.Error("Received unknown function invocation")
}
// ============================================================
// initFlow - create a new flow, store into chaincode state
// ============================================================
func (t *SimpleChaincode) initFlow(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
if len(args) != 10 {
return shim.Error("Incorrect number of arguments. Expecting 10")
}
// ==== Input sanitation ====
fmt.Println("- start init flow")
if len(args[0]) <= 0 {
return shim.Error("1st argument must be a non-empty string")
}
if len(args[1]) <= 0 {
return shim.Error("2nd argument must be a non-empty string")
}
if len(args[2]) <= 0 {
return shim.Error("3rd argument must be a non-empty string")
}
if len(args[3]) <= 0 {
return shim.Error("4th argument must be a non-empty string")
}
if len(args[4]) <= 0 {
return shim.Error("5th argument must be a possitive integer")
}
if len(args[5]) < 0 {
return shim.Error("6th argument must be a possitive integer")
}
if len(args[6]) < 0 {
return shim.Error("7th argument must be a possitive integer")
}
if len(args[7]) < 0 {
return shim.Error("8th argument must be a possitive integer")
}
if len(args[8]) <= 0 {
return shim.Error("9th argument must be a possitive integer")
}
if len(args[9]) <= 0 {
return shim.Error("10th argument must be a non-empty string")
}
flowInPort := args[0]
actions := strings.ToLower(args[1])
nw_src := strings.ToLower(args[3])
priority, err := strconv.Atoi(args[2])
cookie := strings.ToLower(args[4])
duration, err := strconv.Atoi(args[5])
table, err:= strconv.Atoi(args[6])
n_packets, err:= strconv.Atoi(args[7])
n_bytes, err:= strconv.Atoi(args[8])
idle_age, err:= strconv.Atoi(args[9])
if err != nil {
return shim.Error("3rd argument must be a numeric string")
}
// ==== Check if flow already exists ====
flowAsBytes, err := stub.GetState(flowInPort)
if err != nil {
return shim.Error("Failed to get flow: " + err.Error())
} else if flowAsBytes != nil {
fmt.Println("This flow already exists: " + flowInPort)
return shim.Error("This flow already exists: " + flowInPort)
}
// ==== Create flow object and marshal to JSON ====
objectType := "flow"
// when executing ./install.sh this line causes an error, we have added some more literals that need to be added here too.
flow := &flow{objectType, flowInPort, actions, priority, nw_src, cookie, duration, table, n_packets, n_bytes, idle_age}
flowJSONasBytes, err := json.Marshal(flow)
if err != nil {
return shim.Error(err.Error())
}
// === Save flow to state ===
err = stub.PutState(flowInPort, flowJSONasBytes)
if err != nil {
return shim.Error(err.Error())
}
indexInPort := "actions~in_port"
actionsInPortIndexKey, err := stub.CreateCompositeKey(indexInPort, []string{flow.Actions, flow.InPort})
if err != nil {
return shim.Error(err.Error())
}
// Save index entry to state. Only the key in_port is needed, no need to store a duplicate copy of the flow.
// Note - passing a 'nil' value will effectively delete the key from state, therefore we pass null character as value
value := []byte{0x00}
stub.PutState(actionsInPortIndexKey, value)
// ==== Flow saved and indexed. Return success ====
fmt.Println("- end init flow")
return shim.Success(nil)
}
// ===============================================
// readFlow - read a flow from chaincode state
// ===============================================
func (t *SimpleChaincode) readFlow(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var in_port, jsonResp string
var err error
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting in_port of the flow to query")
}
in_port = args[0]
valAsbytes, err := stub.GetState(in_port) //get the flow from chaincode state
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + in_port + "\"}"
return shim.Error(jsonResp)
} else if valAsbytes == nil {
jsonResp = "{\"Error\":\"Flow does not exist: " + in_port + "\"}"
return shim.Error(jsonResp)
}
return shim.Success(valAsbytes)
}
// ==================================================
// delete - remove a flow key/value pair from state
// ==================================================
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var jsonResp string
var flowJSON flow
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
flowInPort := args[0]
// to maintain the actions~in_port index, we need to read the flow first and get its actions
valAsbytes, err := stub.GetState(flowInPort) //get the flow from chaincode state
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + flowInPort + "\"}"
return shim.Error(jsonResp)
} else if valAsbytes == nil {
jsonResp = "{\"Error\":\"Flow does not exist: " + flowInPort + "\"}"
return shim.Error(jsonResp)
}
err = json.Unmarshal([]byte(valAsbytes), &flowJSON)
if err != nil {
jsonResp = "{\"Error\":\"Failed to decode JSON of: " + flowInPort + "\"}"
return shim.Error(jsonResp)
}
err = stub.DelState(flowInPort) //remove the flow from chaincode state
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}
// maintain the index
indexInPort := "actions~in_port"
actionsInPortIndexKey, err := stub.CreateCompositeKey(indexInPort, []string{flowJSON.Actions, flowJSON.InPort})
if err != nil {
return shim.Error(err.Error())
}
// Delete index entry to state.
err = stub.DelState(actionsInPortIndexKey)
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}
return shim.Success(nil)
}
// ===========================================================
// transfer a flow by setting a new nw_src in_port on the flow
// ===========================================================
func (t *SimpleChaincode) transferFlow(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
flowInPort := args[0]
newNetworkSourceIP := strings.ToLower(args[1])
fmt.Println("- start transferFlow ", flowInPort, newNetworkSourceIP)
flowAsBytes, err := stub.GetState(flowInPort)
if err != nil {
return shim.Error("Failed to get flow:" + err.Error())
} else if flowAsBytes == nil {
return shim.Error("Flow does not exist")
}
flowToTransfer := flow{}
err = json.Unmarshal(flowAsBytes, &flowToTransfer) //unmarshal it aka JSON.parse()
if err != nil {
return shim.Error(err.Error())
}
flowToTransfer.NetworkSourceIP = newNetworkSourceIP //change the nw_src
flowJSONasBytes, _ := json.Marshal(flowToTransfer)
err = stub.PutState(flowInPort, flowJSONasBytes) //rewrite the flow
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end transferFlow (success)")
return shim.Success(nil)
}
// ===========================================================================================
// constructQueryResponseFromIterator constructs a JSON array containing query results from
// a given result iterator
// ===========================================================================================
func constructQueryResponseFromIterator(resultsIterator shim.StateQueryIteratorInterface) (*bytes.Buffer, error) {
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
return &buffer, nil
}
func (t *SimpleChaincode) getFlowsByRange(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
startKey := args[0]
endKey := args[1]
resultsIterator, err := stub.GetStateByRange(startKey, endKey)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
buffer, err := constructQueryResponseFromIterator(resultsIterator)
if err != nil {
return shim.Error(err.Error())
}
fmt.Printf("- getFlowsByRange queryResult:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
func (t *SimpleChaincode) transferFlowsBasedOnActions(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
actions := args[0]
newNetworkSourceIP := strings.ToLower(args[1])
fmt.Println("- start transferFlowsBasedOnActions ", actions, newNetworkSourceIP)
// Query the actions~in_port index by actions
// This will execute a key range query on all keys starting with 'actions'
actionsedFlowResultsIterator, err := stub.GetStateByPartialCompositeKey("actions~in_port", []string{actions})
if err != nil {
return shim.Error(err.Error())
}
defer actionsedFlowResultsIterator.Close()
// Iterate through result set and for each flow found, transfer to newNetworkSourceIP
var i int
for i = 0; actionsedFlowResultsIterator.HasNext(); i++ {
// Note that we don't get the value (2nd return variable), we'll just get the flow in_port from the composite key
responseRange, err := actionsedFlowResultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// get the actions and in_port from actions~in_port composite key
objectType, compositeKeyParts, err := stub.SplitCompositeKey(responseRange.Key)
if err != nil {
return shim.Error(err.Error())
}
returnedActions := compositeKeyParts[0]
returnedFlowInPort := compositeKeyParts[1]
fmt.Printf("- found a flow from index:%s actions:%s in_port:%s\n", objectType, returnedActions, returnedFlowInPort)
// Now call the transfer function for the found flow.
// Re-use the same function that is used to transfer individual flows
response := t.transferFlow(stub, []string{returnedFlowInPort, newNetworkSourceIP})
// if the transfer failed break out of loop and return error
if response.Status != shim.OK {
return shim.Error("Transfer failed: " + response.Message)
}
}
responsePayload := fmt.Sprintf("Transferred %d %s flows to %s", i, actions, newNetworkSourceIP)
fmt.Println("- end transferFlowsBasedOnActions: " + responsePayload)
return shim.Success([]byte(responsePayload))
}
func (t *SimpleChaincode) queryFlowsByNetworkSourceIP(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
nw_src := strings.ToLower(args[0])
queryString := fmt.Sprintf("{\"selector\":{\"docType\":\"flow\",\"nw_src\":\"%s\"}}", nw_src)
queryResults, err := getQueryResultForQueryString(stub, queryString)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(queryResults)
}
func (t *SimpleChaincode) queryFlows(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
queryString := args[0]
queryResults, err := getQueryResultForQueryString(stub, queryString)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(queryResults)
}
func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string) ([]byte, error) {
fmt.Printf("- getQueryResultForQueryString queryString:\n%s\n", queryString)
resultsIterator, err := stub.GetQueryResult(queryString)
if err != nil {
return nil, err
}
defer resultsIterator.Close()
buffer, err := constructQueryResponseFromIterator(resultsIterator)
if err != nil {
return nil, err
}
fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())
return buffer.Bytes(), nil
}