Skip to content

Commit

Permalink
refactor device.Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
babos77 committed Mar 5, 2021
1 parent 141edfe commit b624cb5
Show file tree
Hide file tree
Showing 12 changed files with 2,936 additions and 2,851 deletions.
33 changes: 27 additions & 6 deletions core/communicator/device_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type deviceClassInterfaceTypes map[string]deviceClassInterfaceTypeDef
type deviceClassInterfaceTypeDef struct {
Detection string
Values deviceClassOIDs
Type string
}

// deviceClassSNMP represents the snmp config part of a device class.
Expand Down Expand Up @@ -731,9 +732,13 @@ func (y *yamlComponentsInterfaces) convert() (deviceClassComponentsInterfaces, e
}

func (y *yamlComponentsInterfaceTypes) convert() (deviceClassInterfaceTypes, error) {
err := y.validate()
if err != nil {
return deviceClassInterfaceTypes{}, err
}
interfaceTypes := make(map[string]deviceClassInterfaceTypeDef)

for k, interfaceType := range *y {
for t, interfaceType := range *y {
if interfaceType.Detection == "" {
return deviceClassInterfaceTypes{}, errors.New("detection information missing for special interface type")
}
Expand All @@ -742,21 +747,37 @@ func (y *yamlComponentsInterfaceTypes) convert() (deviceClassInterfaceTypes, err
if err != nil {
return deviceClassInterfaceTypes{}, errors.Wrap(err, "failed to read yaml interfaces types values")
}
interfaceTypes[k] = deviceClassInterfaceTypeDef{
interfaceTypes[t] = deviceClassInterfaceTypeDef{
Detection: interfaceType.Detection,
Values: values,
Type: t,
}
} else {
interfaceTypes[k] = deviceClassInterfaceTypeDef{
Detection: interfaceType.Detection,
Values: nil,
}
return deviceClassInterfaceTypes{}, fmt.Errorf("values is missing for interface type '%s'", t)
}
}

return interfaceTypes, nil
}

func (y *yamlComponentsInterfaceTypes) validate() error {
for t := range *y {
err := validateInterfaceType(t)
if err != nil {
return err
}
}
return nil
}

func validateInterfaceType(t string) error {
switch t {
case "ether_like", "radio", "dwdm", "optical_transponder", "optical_amplifier", "optical_opm":
return nil
}
return fmt.Errorf("unknown interface type '%s'", t)
}

func (y *yamlComponentsOIDs) convert() (deviceClassOIDs, error) {
interfaceOIDs := make(map[string]deviceClassOID)

Expand Down
52 changes: 50 additions & 2 deletions core/communicator/device_class_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func (o *deviceClassCommunicator) GetInterfaces(ctx context.Context) ([]device.I
return nil, errors.Wrap(err, "failed to get ifTable")
}

for _, typeDef := range o.components.interfaces.Types {
for t, typeDef := range o.components.interfaces.Types {
specialInterfacesRaw, err := o.getValuesBySNMPWalk(ctx, typeDef.Values)
if err != nil {
return nil, err
}

for i, networkInterface := range networkInterfaces {
if specialValues, ok := specialInterfacesRaw[fmt.Sprint(*networkInterface.IfIndex)]; ok {
err := mapstructure.WeakDecode(specialValues, &networkInterfaces[i])
err := addSpecialInterfacesValuesToInterface(t, &networkInterfaces[i], specialValues)
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("can't parse oid values into Interface struct")
return nil, errors.Wrap(err, "can't parse oid values into Interface struct")
Expand All @@ -130,6 +130,54 @@ func (o *deviceClassCommunicator) GetInterfaces(ctx context.Context) ([]device.I
return networkInterfaces, nil
}

func addSpecialInterfacesValuesToInterface(interfaceType string, interf *device.Interface, specialValues interface{}) error {
switch interfaceType {
case "ether_like":
var specialValuesStruct device.EthernetLikeInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.EthernetLike = &specialValuesStruct
case "radio":
var specialValuesStruct device.RadioInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.Radio = &specialValuesStruct
case "dwdm":
var specialValuesStruct device.DWDMInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.DWDM = &specialValuesStruct
case "optical_transponder":
var specialValuesStruct device.OpticalTransponderInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.OpticalTransponder = &specialValuesStruct
case "optical_amplifier":
var specialValuesStruct device.OpticalAmplifierInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.OpticalAmplifier = &specialValuesStruct
case "optical_opm":
var specialValuesStruct device.OpticalOPMInterface
err := mapstructure.WeakDecode(specialValues, &specialValuesStruct)
if err != nil {
return errors.Wrap(err, "failed to decode special values")
}
interf.OpticalOPM = &specialValuesStruct
}
return nil
}

func (o *deviceClassCommunicator) GetIfTable(ctx context.Context) ([]device.Interface, error) {
if o.components.interfaces == nil || o.components.interfaces.IfTable == nil {
log.Ctx(ctx).Trace().Str("property", "ifTable").Str("device_class", o.name).Msg("no interface information available")
Expand Down
4 changes: 2 additions & 2 deletions core/communicator/ekinops_module_reader_amplifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func (m *ekinopsModuleReaderAmplifier) readModuleMetrics(ctx context.Context, in
return nil, errors.Wrap(err, "failed to get interface identifier mappings")
}

for _, opticalAmplifierInterface := range opticalAmplifierInterfaces {
for i, opticalAmplifierInterface := range opticalAmplifierInterfaces {
identifier := m.slotIdentifier + "/" + m.moduleName + "/" + *opticalAmplifierInterface.Identifier
idx, ok := mappings[identifier]
if !ok {
return nil, fmt.Errorf("interface for identifier '%s' not found", identifier)
}
interfaces[idx].OpticalAmplifierInterface = opticalAmplifierInterface
interfaces[idx].OpticalAmplifier = &opticalAmplifierInterfaces[i]
}
return interfaces, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/communicator/ekinops_module_reader_opm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (m *ekinopsModuleReaderOPM8) readModuleMetrics(ctx context.Context, interfa
return nil, errors.Wrap(err, "failed to get interface identifier mappings")
}

for _, opticalOPMInterface := range opticalOPMInterfaces {
for i, opticalOPMInterface := range opticalOPMInterfaces {
identifier := m.slotIdentifier + "/" + m.moduleName + "/" + *opticalOPMInterface.Identifier
idx, ok := mappings[identifier]
if !ok {
return nil, fmt.Errorf("interface for identifier '%s' not found", identifier)
}
interfaces[idx].OpticalOPMInterface = opticalOPMInterface
interfaces[idx].OpticalOPM = &opticalOPMInterfaces[i]
}

return interfaces, nil
Expand Down
4 changes: 2 additions & 2 deletions core/communicator/ekinops_module_reader_transponder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (m *ekinopsModuleReaderTransponder) readModuleMetrics(ctx context.Context,
return nil, errors.Wrap(err, "failed to get interface identifier mappings")
}

for _, opticalTransponderInterface := range OpticalTransponderInterfaces {
for i, opticalTransponderInterface := range OpticalTransponderInterfaces {
identifier := m.slotIdentifier + "/" + m.moduleName + "/" + *opticalTransponderInterface.Identifier
idx, ok := mappings[identifier]
if !ok {
return nil, fmt.Errorf("interface for identifier '%s' not found", identifier)
}
interfaces[idx].OpticalTransponderInterface = opticalTransponderInterface
interfaces[idx].OpticalTransponder = &OpticalTransponderInterfaces[i]
}
return interfaces, nil
}
Expand Down
34 changes: 17 additions & 17 deletions core/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ type Interface struct {
// It is used to internally specify a port type, without changing the actual ifType.
SubType *string `yaml:"-" json:"-" xml:"-"`

EthernetLikeInterface `mapstructure:",squash"`
RadioInterface `mapstructure:",squash"`
DWDMInterface `mapstructure:",squash"`
OpticalTransponderInterface `mapstructure:",squash"`
OpticalAmplifierInterface `mapstructure:",squash"`
OpticalOPMInterface `mapstructure:",squash"`
EthernetLike *EthernetLikeInterface `yaml:"ethernet_lik,omitemptye" json:"ethernet_like,omitempty" xml:"ethernet_like,omitempty"`
Radio *RadioInterface `yaml:"radio,omitempty" json:"radio,omitempty" xml:"radio,omitempty" `
DWDM *DWDMInterface `yaml:"dwdm,omitempty" json:"dwdm,omitempty" xml:"dwdm,omitempty"`
OpticalTransponder *OpticalTransponderInterface `yaml:"optical_transponder,omitempty" json:"optical_transponder,omitempty" xml:"optical_transponder,omitempty"`
OpticalAmplifier *OpticalAmplifierInterface `yaml:"optical_amplifie,omitemptyr" json:"optical_amplifier,omitempty" xml:"optical_amplifier,omitempty"`
OpticalOPM *OpticalOPMInterface `yaml:"optical_opm,omitempty" json:"optical_opm,omitempty" xml:"optical_opm,omitempty"`
}

// EthernetLikeInterface represents an ethernet like interface
Expand Down Expand Up @@ -151,26 +151,26 @@ type DWDMInterface struct {
}

type OpticalTransponderInterface struct {
Identifier *string `yaml:"identifier_transponder,omitempty" json:"identifier_transponder,omitempty" xml:"identifier_transponder,omitempty" mapstructure:"identifier_transponder"`
Label *string `yaml:"label_transponder,omitempty" json:"label_transponder,omitempty" xml:"label_transponder,omitempty" mapstructure:"label_transponder"`
RXPower *float64 `yaml:"rx_power_transponder,omitempty" json:"rx_power_transponder,omitempty" xml:"rx_power_transponder,omitempty" mapstructure:"rx_power_transponder"`
TXPower *float64 `yaml:"tx_power_transponder,omitempty" json:"tx_power_transponder,omitempty" xml:"tx_power_transponder,omitempty" mapstructure:"tx_power_transponder"`
Identifier *string `yaml:"identifier,omitempty" json:"identifier,omitempty" xml:"identifier,omitempty" mapstructure:"identifier"`
Label *string `yaml:"label,omitempty" json:"label,omitempty" xml:"label,omitempty" mapstructure:"label"`
RXPower *float64 `yaml:"rx_power,omitempty" json:"rx_power,omitempty" xml:"rx_power,omitempty" mapstructure:"rx_power"`
TXPower *float64 `yaml:"tx_power,omitempty" json:"tx_power,omitempty" xml:"tx_power,omitempty" mapstructure:"tx_power"`
CorrectedFEC *uint64 `yaml:"corrected_fec,omitempty" json:"corrected_fec,omitempty" xml:"corrected_fec,omitempty" mapstructure:"corrected_fec"`
UncorrectedFEC *uint64 `yaml:"uncorrected_fec,omitempty" json:"uncorrected_fec,omitempty" xml:"uncorrected_fec,omitempty" mapstructure:"uncorrected_fec"`
}

type OpticalAmplifierInterface struct {
Identifier *string `yaml:"identifier_amplifier,omitempty" json:"identifier_amplifier,omitempty" xml:"identifier_amplifier,omitempty" mapstructure:"identifier_amplifier"`
Label *string `yaml:"label_amplifier,omitempty" json:"label_amplifier,omitempty" xml:"label_amplifier,omitempty" mapstructure:"label_amplifier"`
RXPower *float64 `yaml:"rx_power_amplifier,omitempty" json:"rx_power_amplifier,omitempty" xml:"rx_power_amplifier,omitempty" mapstructure:"rx_power_amplifier"`
TXPower *float64 `yaml:"tx_power_amplifier,omitempty" json:"tx_power_amplifier,omitempty" xml:"tx_power_amplifier,omitempty" mapstructure:"tx_power_amplifier"`
Identifier *string `yaml:"identifier,omitempty" json:"identifier,omitempty" xml:"identifier,omitempty" mapstructure:"identifier"`
Label *string `yaml:"label,omitempty" json:"label,omitempty" xml:"label,omitempty" mapstructure:"label"`
RXPower *float64 `yaml:"rx_power,omitempty" json:"rx_power,omitempty" xml:"rx_power,omitempty" mapstructure:"rx_power"`
TXPower *float64 `yaml:"tx_power,omitempty" json:"tx_power,omitempty" xml:"tx_power,omitempty" mapstructure:"tx_power"`
Gain *float64 `yaml:"gain,omitempty" json:"gain,omitempty" xml:"gain,omitempty" mapstructure:"gain"`
}

type OpticalOPMInterface struct {
Identifier *string `yaml:"identifier_opm,omitempty" json:"identifier_opm,omitempty" xml:"identifier_opm,omitempty" mapstructure:"identifier_opm"`
Label *string `yaml:"label_opm,omitempty" json:"label_opm,omitempty" xml:"label_opm,omitempty" mapstructure:"label_opm"`
RXPower *float64 `yaml:"rx_power_opm,omitempty" json:"rx_power_opm,omitempty" xml:"rx_power_opm,omitempty" mapstructure:"rx_power_opm"`
Identifier *string `yaml:"identifier,omitempty" json:"identifier,omitempty" xml:"identifier,omitempty" mapstructure:"identifier"`
Label *string `yaml:"label,omitempty" json:"label,omitempty" xml:"label,omitempty" mapstructure:"label"`
RXPower *float64 `yaml:"rx_power,omitempty" json:"rx_power,omitempty" xml:"rx_power,omitempty" mapstructure:"rx_power"`
Channels []OpticalOPMChannel `yaml:"channels,omitempty" json:"channels,omitempty" xml:"channels,omitempty" mapstructure:"channels"`
}

Expand Down
Loading

0 comments on commit b624cb5

Please sign in to comment.