Skip to content

Commit

Permalink
Merge pull request #54 from inexio/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TheFireMike authored Jun 18, 2021
2 parents 8e6300b + c1c77de commit b248fe3
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 82 deletions.
2 changes: 2 additions & 0 deletions config/codecommunicator/code_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func GetCodeCommunicator(deviceClass communicator.Communicator, parentNetworkDev
return &timosSASCommunicator{base}, nil
case "timos":
return &timosCommunicator{base}, nil
case "junos":
return &junosCommunicator{base}, nil
}
return nil, tholaerr.NewNotFoundError(fmt.Sprintf("no code communicator found for device class identifier '%s'", classIdentifier))
}
Expand Down
111 changes: 111 additions & 0 deletions config/codecommunicator/junos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package codecommunicator

import (
"context"
"fmt"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"strings"
)

type junosCommunicator struct {
codeCommunicator
}

func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
interfaces, err := c.deviceClass.GetInterfaces(ctx)
if err != nil {
return nil, err
}

con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("snmp client is empty")
}

// dot1dBasePortIfIndex
res, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.17.1.4.1.2")
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("failed to get dot1dBasePortIfIndex, skipping VLANs")
return interfaces, nil
}

portIfIndex := make(map[string]string)
for _, response := range res {
ifIndex, err := response.GetValueString()
if err != nil {
return nil, err
}

oid := response.GetOID()
oidSplit := strings.Split(oid, ".")

portIfIndex[oidSplit[len(oidSplit)-1]] = ifIndex
}

// jnxExVlanPortStatus
res, err = con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.2636.3.40.1.5.1.7.1.3")
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("failed to get jnxExVlanPortStatus, skipping VLANs")
return interfaces, nil
}

vlanIndexVLAN := make(map[string]device.VLAN)
ifIndexVLANIndices := make(map[string][]string)
for _, response := range res {
status, err := response.GetValueString()
if err != nil {
return nil, err
}

oid := response.GetOID()
oidSplit := strings.Split(oid, ".")

ifIndex := portIfIndex[oidSplit[len(oidSplit)-1]]
ifIndexVLANIndices[ifIndex] = append(ifIndexVLANIndices[ifIndex], oidSplit[len(oidSplit)-2])
vlanIndexVLAN[oidSplit[len(oidSplit)-2]] = device.VLAN{
Status: &status,
}
}

// jnxExVlanName
res, err = con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.2636.3.40.1.5.1.5.1.2")
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("failed to get jnxExVlanName, skipping VLANs")
return interfaces, nil
}

for _, response := range res {
name, err := response.GetValueString()
if err != nil {
return nil, err
}

oid := response.GetOID()
oidSplit := strings.Split(oid, ".")

if vlan, ok := vlanIndexVLAN[oidSplit[len(oidSplit)-1]]; ok {
vlan.Name = name
vlanIndexVLAN[oidSplit[len(oidSplit)-1]] = vlan
}
}

for i, interf := range interfaces {
if interf.IfIndex != nil {
if vlanIndices, ok := ifIndexVLANIndices[fmt.Sprint(*interf.IfIndex)]; ok {
for _, vlanIndex := range vlanIndices {
if vlan, ok := vlanIndexVLAN[vlanIndex]; ok {
if interfaces[i].VLAN == nil {
interfaces[i].VLAN = &device.VLANInformation{}
}
interfaces[i].VLAN.VLANs = append(interfaces[i].VLAN.VLANs, vlan)
}
}
}
}
}

return interfaces, nil
}
Loading

0 comments on commit b248fe3

Please sign in to comment.