github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/elasticsearch/json.go (about)

     1  // Copyright (c) 2017 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package elasticsearch
     6  
     7  import (
     8  	"github.com/aristanetworks/goarista/gnmi"
     9  	pb "github.com/openconfig/gnmi/proto/gnmi"
    10  )
    11  
    12  // NotificationToMaps converts a gNMI Notification into a map[string][interface] that adheres
    13  // to the Data schema defined in schema.go
    14  func NotificationToMaps(datasetID string,
    15  	notification *pb.Notification) ([]map[string]interface{}, error) {
    16  	var requests []map[string]interface{}
    17  	var trueVar = true
    18  
    19  	timeStampNano := uint64(notification.Timestamp)
    20  
    21  	prefix := notification.Prefix
    22  	if prefix == nil {
    23  		prefix = &pb.Path{}
    24  	}
    25  
    26  	for _, delete := range notification.Delete {
    27  		path := gnmi.JoinPaths(prefix, delete)
    28  		doc := map[string]interface{}{
    29  			"Timestamp": timeStampNano,
    30  			"DatasetID": datasetID,
    31  			"Path":      gnmi.StrPath(path),
    32  			"Del":       &trueVar,
    33  		}
    34  
    35  		keyStr := gnmi.StrPath(delete)
    36  		doc["Key"] = []byte(keyStr) // use strigified delete.Path for key
    37  		if err := SetKey(doc, keyStr); err != nil {
    38  			return nil, err
    39  		}
    40  
    41  		requests = append(requests, doc)
    42  	}
    43  	for _, update := range notification.Update {
    44  		key := update.Path
    45  		path := gnmi.JoinPaths(prefix, key)
    46  		doc := map[string]interface{}{
    47  			"Timestamp": timeStampNano,
    48  			"DatasetID": datasetID,
    49  			"Path":      gnmi.StrPath(path),
    50  		}
    51  		keyStr := gnmi.StrPath(key)
    52  		doc["Key"] = []byte(keyStr) // use strigified update.Path for key
    53  		if err := SetKey(doc, keyStr); err != nil {
    54  			return nil, err
    55  		}
    56  		if err := SetValue(doc, update.Val.Value); err != nil {
    57  			return nil, err
    58  		}
    59  		requests = append(requests, doc)
    60  	}
    61  
    62  	return requests, nil
    63  }