Skip to content

Commit

Permalink
group property reader indices mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
babos77 committed May 9, 2021
1 parent b640095 commit a025b95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
9 changes: 8 additions & 1 deletion config/device-classes/generic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,11 @@ components:
dot3HCStatsFCSErrors:
oid: 1.3.6.1.2.1.10.7.11.1.2
etherStatsCRCAlignErrors:
oid: 1.3.6.1.2.1.16.1.1.1.8
oid: 1.3.6.1.2.1.16.1.1.1.8
indices_mapping:
oid: 1.3.6.1.2.1.16.1.1.1.2
operators:
- type: modify
modify_method: regexSubmatch
regex: '\.([0-9]+)$'
format: "$1"
18 changes: 15 additions & 3 deletions core/communicator/device_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ type yamlComponentsInterfaces struct {

type yamlComponentsOID struct {
network.SNMPGetConfiguration `yaml:",inline" mapstructure:",squash"`
Operators []interface{} `yaml:"operators"`
Operators []interface{} `yaml:"operators"`
IndicesMapping *yamlComponentsOID `yaml:"indices_mapping" mapstructure:"indices_mapping"`
}

var genericDeviceClass struct {
Expand Down Expand Up @@ -720,6 +721,15 @@ func (y *yamlComponentsInterfaces) convert(parentComponentsInterfaces *deviceCla
}

func (y *yamlComponentsOID) convert() (deviceClassOID, error) {
var idxMappings *deviceClassOID
if y.IndicesMapping != nil {
mappings, err := y.IndicesMapping.convert()
if err != nil {
return deviceClassOID{}, errors.New("failed to convert indices mappings")
}
idxMappings = &mappings
}

if y.Operators != nil {
operators, err := interfaceSlice2propertyOperators(y.Operators, propertyDefault)
if err != nil {
Expand All @@ -730,7 +740,8 @@ func (y *yamlComponentsOID) convert() (deviceClassOID, error) {
OID: y.OID,
UseRawResult: y.UseRawResult,
},
operators: operators,
operators: operators,
indicesMapping: idxMappings,
}, nil
}

Expand All @@ -739,7 +750,8 @@ func (y *yamlComponentsOID) convert() (deviceClassOID, error) {
OID: y.OID,
UseRawResult: y.UseRawResult,
},
operators: nil,
operators: nil,
indicesMapping: idxMappings,
}, nil
}

Expand Down
31 changes: 30 additions & 1 deletion core/communicator/device_class_group_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ func (d *deviceClassOIDs) merge(overwrite deviceClassOIDs) deviceClassOIDs {

type deviceClassOID struct {
network.SNMPGetConfiguration
operators propertyOperators
operators propertyOperators
indicesMapping *deviceClassOID
}

func (d *deviceClassOID) readOID(ctx context.Context) (map[int]interface{}, error) {
Expand Down Expand Up @@ -179,5 +180,33 @@ func (d *deviceClassOID) readOID(ctx context.Context) (map[int]interface{}, erro
result[index] = resNormalized
}
}

//change indices if necessary
if d.indicesMapping != nil {
indices, err := d.indicesMapping.readOID(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to read indices")
}
mappedResult := make(map[int]interface{})

for k, v := range result {
var idx int
if _, ok := indices[k]; ok {
idx, err = indices[k].(value.Value).Int()
if err != nil {
return nil, errors.Wrap(err, "failed to convert Value to int")
}
} else {
idx = k
}

if _, ok := mappedResult[idx]; ok {
return nil, fmt.Errorf("index mappings resulted in duplicated index '%d'", idx)
}

mappedResult[idx] = v
}
result = mappedResult
}
return result, nil
}

0 comments on commit a025b95

Please sign in to comment.