|
| 1 | +package iosxr |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | +) |
| 8 | + |
| 9 | +// YANG has an empty type that is represented as a JSON array with a single null value. Whenever the key is present in YANG, it should evaluate to true. |
| 10 | +type Empty bool |
| 11 | + |
| 12 | +func (e *Empty) UnmarshalJSON(b []byte) error { |
| 13 | + var elements []any |
| 14 | + if err := json.Unmarshal(b, &elements); err != nil { |
| 15 | + return err |
| 16 | + } |
| 17 | + |
| 18 | + // Check if the data is a JSON array with a single null value |
| 19 | + if len(elements) == 1 && elements[0] == nil { |
| 20 | + *e = true |
| 21 | + return nil |
| 22 | + } |
| 23 | + *e = false |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +func (e Empty) MarshalJSON() ([]byte, error) { |
| 28 | + // If e is true, marshal as a JSON array with a single null value |
| 29 | + if e { |
| 30 | + return json.Marshal([]any{nil}) |
| 31 | + } |
| 32 | + return nil, nil |
| 33 | +} |
| 34 | + |
| 35 | +type PhisIf struct { |
| 36 | + Name string `json:"-"` |
| 37 | + Description string `json:"description,omitempty"` |
| 38 | + Active string `json:"active,omitempty"` |
| 39 | + Vrf string `json:"Cisco-IOS-XR-infra-rsi-cfg:vrf,omitempty"` |
| 40 | + Statistics Statistics `json:"Cisco-IOS-XR-infra-statsd-cfg:statistics,omitzero"` |
| 41 | + IPv4Network IPv4Network `json:"Cisco-IOS-XR-ipv4-io-cfg:ipv4-network,omitzero"` |
| 42 | + IPv6Network IPv6Network `json:"Cisco-IOS-XR-ipv6-ma-cfg:ipv6-network,omitzero"` |
| 43 | + IPv6Neighbor IPv6Neighbor `json:"Cisco-IOS-XR-ipv6-nd-cfg:ipv6-neighbor,omitzero"` |
| 44 | + MTUs MTUs `json:"mtus,omitzero"` |
| 45 | + Shutdown Empty `json:"shutdown,omitzero"` |
| 46 | +} |
| 47 | + |
| 48 | +type Statistics struct { |
| 49 | + LoadInterval uint8 `json:"load-interval,omitzero"` |
| 50 | +} |
| 51 | + |
| 52 | +type IPv4Network struct { |
| 53 | + Addresses AddressesIPv4 `json:"addresses,omitzero"` |
| 54 | + Mtu uint16 `json:"mtu,omitzero"` |
| 55 | +} |
| 56 | + |
| 57 | +type AddressesIPv4 struct { |
| 58 | + Primary Primary `json:"primary,omitzero"` |
| 59 | +} |
| 60 | + |
| 61 | +type Primary struct { |
| 62 | + Address string `json:"address,omitempty"` |
| 63 | + Netmask string `json:"netmask,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +type IPv6Network struct { |
| 67 | + Mtu uint16 `json:"mtu,omitzero"` |
| 68 | + Addresses AddressesIPv6 `json:"addresses,omitzero"` |
| 69 | +} |
| 70 | + |
| 71 | +type AddressesIPv6 struct { |
| 72 | + RegularAddresses RegularAddresses `json:"regular-addresses,omitzero"` |
| 73 | +} |
| 74 | + |
| 75 | +type RegularAddresses struct { |
| 76 | + RegularAddress []RegularAddress `json:"regular-address,omitempty"` |
| 77 | +} |
| 78 | + |
| 79 | +type RegularAddress struct { |
| 80 | + Address string `json:"address,omitempty"` |
| 81 | + PrefixLength uint8 `json:"prefix-length,omitzero"` |
| 82 | + Zone string `json:"zone,omitempty"` |
| 83 | +} |
| 84 | + |
| 85 | +type IPv6Neighbor struct { |
| 86 | + RASuppress bool `json:"ra-suppress,omitempty"` |
| 87 | +} |
| 88 | + |
| 89 | +type MTUs struct { |
| 90 | + MTU []MTU `json:"mtu,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +type MTU struct { |
| 94 | + MTU int32 `json:"mtu,omitzero"` |
| 95 | + Owner string `json:"owner,omitempty"` |
| 96 | +} |
| 97 | + |
| 98 | +func (i *PhisIf) XPath() string { |
| 99 | + return fmt.Sprintf("Cisco-IOS-XR-ifmgr-cfg:interface-configurations/interface-configuration[active=act][interface-name=%s]", i.Name) |
| 100 | +} |
| 101 | + |
| 102 | +func NewIface(name string) *PhisIf { |
| 103 | + return &PhisIf{ |
| 104 | + Name: name, |
| 105 | + Statistics: Statistics{}, |
| 106 | + IPv4Network: IPv4Network{}, |
| 107 | + IPv6Network: IPv6Network{}, |
| 108 | + MTUs: MTUs{}, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (i *PhisIf) String() string { |
| 113 | + return fmt.Sprintf("Name: %s, Description=%s, ShutDown=%t", i.Name, i.Description, i.Shutdown) |
| 114 | +} |
| 115 | + |
| 116 | +type IFaceSpeed string |
| 117 | + |
| 118 | +const ( |
| 119 | + TenGig IFaceSpeed = "TenGigE" |
| 120 | + TwentyFiveGig IFaceSpeed = "TwentyFiveGigE" |
| 121 | + FortyGig IFaceSpeed = "FortyGigE" |
| 122 | + HundredGig IFaceSpeed = "HundredGigE" |
| 123 | +) |
| 124 | + |
| 125 | +func ExractMTUOwnerFromIfaceName(ifaceName string) (IFaceSpeed, error) { |
| 126 | + re := regexp.MustCompile(`^\D*`) |
| 127 | + |
| 128 | + mtuOwner := string(re.Find([]byte(ifaceName))) |
| 129 | + |
| 130 | + if mtuOwner == "" { |
| 131 | + return "", fmt.Errorf("failed to extract MTU owner from interface name %s", ifaceName) |
| 132 | + } |
| 133 | + |
| 134 | + switch mtuOwner { |
| 135 | + case string(TenGig): |
| 136 | + return TenGig, nil |
| 137 | + case string(TwentyFiveGig): |
| 138 | + return TwentyFiveGig, nil |
| 139 | + case string(FortyGig): |
| 140 | + return FortyGig, nil |
| 141 | + case string(HundredGig): |
| 142 | + return HundredGig, nil |
| 143 | + default: |
| 144 | + return "", fmt.Errorf("unsupported interface type %s for MTU owner extraction", mtuOwner) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +type PhysIfStates int |
| 149 | + |
| 150 | +const ( |
| 151 | + StateUp PhysIfStates = iota |
| 152 | + StateDown |
| 153 | + StateNotReady |
| 154 | + StateAdminDown |
| 155 | + StateShutDown |
| 156 | +) |
| 157 | + |
| 158 | +var stateMapping = map[string]PhysIfStates{ |
| 159 | + "im-state-not-ready": StateNotReady, |
| 160 | + "im-state-down": StateDown, |
| 161 | + "im-state-up": StateUp, |
| 162 | + "im-state-shutdown": StateShutDown, |
| 163 | +} |
| 164 | + |
| 165 | +type PhysIfState struct { |
| 166 | + State string `json:"state,omitempty"` |
| 167 | + Name string `json:"-,omitempty"` |
| 168 | +} |
| 169 | + |
| 170 | +func (phys *PhysIfState) XPath() string { |
| 171 | + // (fixme): hardcoded route processor for the moment |
| 172 | + return fmt.Sprintf("Cisco-IOS-XR-ifmgr-oper:interface-properties/data-nodes/data-node[data-node-name=0/RP0/CPU0]/system-view/interfaces/interface[interface-name=%s]", phys.Name) |
| 173 | +} |
0 commit comments