#!/usr/bin/env bash

cat << EOF > all_ifaces.yml
routes:
  config:
    - destination: 0.0.0.0/0
      next-hop-address: 100.1.1.254
      next-hop-interface: g0bond0
      table-id: 100
    - destination: ::/0
      next-hop-address: 100::254
      next-hop-interface: g0bond0
      table-id: 100
route-rules:
  config:
    - ip-from: 8.8.8.8/24
      ip-to: 200.1.1.1/24
      priority: 3000
      route-table: 100
    - ip-to: 111::/64
      priority: 4000
      route-table: 100
interfaces:
  - name: g0bond0
    type: bond
    state: up
    ipv4:
      enabled: true
      address:
        - ip: 100.1.1.1
          prefix-length: 24
    ipv6:
      enabled: true
      address:
        - ip: 100::1
          prefix-length: 64
    link-aggregation:
      mode: balance-rr
      port:
        - g0veth0
        - g0veth1
    lldp:
      enabled: true
  - name: g0bond1
    type: bond
    state: up
    ipv4:
      enabled: true
      dhcp: true
    ipv6:
      enabled: true
      dhcp: true
    link-aggregation:
      mode: active-backup
      port:
        - g0veth2
        - g0veth3
    lldp:
      enabled: true
  - name: g0bond2
    type: bond
    state: up
    link-aggregation:
      mode: balance-xor
      port:
        - g0veth4
        - g0veth5
    lldp:
      enabled: true
  - name: g0bond3
    type: bond
    state: up
    link-aggregation:
      mode: broadcast
      port:
        - g0veth6
        - g0veth7
    lldp:
      enabled: true
  - name: g0bond4
    type: bond
    state: up
    link-aggregation:
      mode: 802.3ad
      port:
        - g0veth8
        - g0veth9
    lldp:
      enabled: true
  - name: g0bond5
    type: bond
    state: up
    link-aggregation:
      mode: balance-tlb
      port:
        - g0veth10
        - g0veth11
    lldp:
      enabled: true
  - name: g0bond6
    type: bond
    state: up
    link-aggregation:
      mode: balance-alb
      port:
        - g0veth12
        - g0veth13
    lldp:
      enabled: true
  # veth
  - name: g0vethm0
    type: ethernet
    state: up
    lldp:
      enabled: true
  # vlan
  - name: g0vethm0.1
    type: vlan
    state: up
    vlan:
      base-iface: g0vethm0
      id: 1
    lldp:
      enabled: true
  - name: g0bond0.1
    type: vlan
    state: up
    vlan:
      base-iface: g0bond0
      id: 1
    lldp:
      enabled: true
  - name: g0bond1.1
    type: vlan
    state: up
    vlan:
      base-iface: g0bond1
      id: 1
    lldp:
      enabled: true
  # bridge:
  - name: g0br0
    type: linux-bridge
    state: up
    bridge:
      port:
        - name: g0vethm0.1
        - name: g0bond0.1
        - name: g0bond1.1
        - name: g0veth26
    lldp:
      enabled: true
  # ovs
  - name: g0ovsbr0
    type: ovs-bridge
    state: up
    bridge:
      port:
        - name: g0ovs0
        - name: g0veth27
        - name: g0ovsbond0
          vlan:
            mode: access
            tag: 0
          link-aggregation:
            mode: balance-slb
            port:
              - name: g0veth28
              - name: g0ovs1
        - name: g0ovsbond1
          link-aggregation:
            mode: active-backup
            port:
              - name: g0veth29
              - name: g0patch0
          vlan:
            mode: trunk
            tag: 1
            enable-native: true
            trunk-tags:
              - id: 101
        - name: g0ovsbond2
          link-aggregation:
            mode: balance-tcp
            port:
              - name: g0veth30
              - name: g0veth31
    lldp:
      enabled: true
  - name: g0ovs0
    type: ovs-interface
    state: up
    lldp:
      enabled: true
  - name: g0ovs1
    type: ovs-interface
    state: up
    lldp:
      enabled: true
  - name: g0patch1
    type: ovs-interface
    state: up
    patch:
      peer: g0patch0
    lldp:
      enabled: true
  - name: g0ovsbr1
    type: ovs-bridge
    state: up
    bridge:
      options:
        stp: true
      port:
        - name: g0patch1
    lldp:
      enabled: true
  - name: g0patch0
    type: ovs-interface
    state: up
    patch:
      peer: g0patch1
    lldp:
      enabled: true
  # base veth
  - {name: g0veth0, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth1, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth2, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth3, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth4, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth5, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth6, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth7, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth8, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth9, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth10, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth11, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth12, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth13, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth14, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth15, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth16, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth17, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth18, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth19, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth20, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth21, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth22, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth23, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth24, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth25, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth26, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth27, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth28, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth29, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth30, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth31, type: ethernet, state: up, lldp: {enabled: true}}
  # vrf
  - name: g0vrf0
    type: vrf
    state: up
    vrf:
      port:
        - g0veth32
        - g0veth33
      route-table-id: 1000
    lldp:
      enabled: true
  - {name: g0veth32, type: ethernet, state: up, lldp: {enabled: true}}
  - {name: g0veth33, type: ethernet, state: up, lldp: {enabled: true}}
EOF

cat << EOF > all_ifaces_peer.yml
interfaces:
  - name: p_g0bond0
    type: bond
    state: up
    link-aggregation:
      mode: balance-rr
      port:
        - p_g0veth0
        - p_g0veth1
  - name: p_g0bond1
    type: bond
    state: up
    ipv4:
      enabled: true
      dhcp: true
    ipv6:
      enabled: true
      dhcp: true
    link-aggregation:
      mode: active-backup
      port:
        - p_g0veth2
        - p_g0veth3
  - name: p_g0bond2
    type: bond
    state: up
    link-aggregation:
      mode: balance-xor
      port:
        - p_g0veth4
        - p_g0veth5
  - name: p_g0bond3
    type: bond
    state: up
    link-aggregation:
      mode: broadcast
      port:
        - p_g0veth6
        - p_g0veth7
  - name: p_g0bond4
    type: bond
    state: up
    link-aggregation:
      mode: 802.3ad
      port:
        - p_g0veth8
        - p_g0veth9
  - name: p_g0bond5
    type: bond
    state: up
    link-aggregation:
      mode: balance-tlb
      port:
        - p_g0veth10
        - p_g0veth11
  - name: p_g0bond6
    type: bond
    state: up
    link-aggregation:
      mode: balance-alb
      port:
        - p_g0veth12
        - p_g0veth13
  # veth
  - name: p_g0vethm0
    type: ethernet
    state: up
  # vlan
  - name: p_g0vethm0.1
    type: vlan
    state: up
    vlan:
      base-iface: p_g0vethm0
      id: 1
  - name: p_g0bond0.1
    type: vlan
    state: up
    vlan:
      base-iface: p_g0bond0
      id: 1
  - name: p_g0bond1.1
    type: vlan
    state: up
    vlan:
      base-iface: p_g0bond1
      id: 1
  # bridge:
  - name: p_g0br0
    type: linux-bridge
    state: up
    bridge:
      port:
        - name: p_g0vethm0.1
        - name: p_g0bond0.1
        - name: p_g0bond1.1
        - name: p_g0veth26
  # ovs
  - name: p_g0ovsbr0
    type: ovs-bridge
    state: up
    bridge:
      port:
        - name: p_g0ovs0
        - name: p_g0veth27
        - name: p_g0ovsbond0
          vlan:
            mode: access
            tag: 0
          link-aggregation:
            mode: balance-slb
            port:
              - name: p_g0veth28
              - name: p_g0ovs1
        - name: p_g0ovsbond1
          link-aggregation:
            mode: active-backup
            port:
              - name: p_g0veth29
              - name: p_g0patch0
          vlan:
            mode: trunk
            tag: 1
            enable-native: true
            trunk-tags:
              - id: 101
        - name: p_g0ovsbond2
          link-aggregation:
            mode: balance-tcp
            port:
              - name: p_g0veth30
              - name: p_g0veth31
  - name: p_g0ovs0
    type: ovs-interface
    state: up
  - name: p_g0ovs1
    type: ovs-interface
    state: up
  - name: p_g0patch1
    type: ovs-interface
    state: up
    patch:
      peer: p_g0patch0
  - name: p_g0ovsbr1
    type: ovs-bridge
    state: up
    bridge:
      options:
        stp: true
      port:
        - name: p_g0patch1
  - name: p_g0patch0
    type: ovs-interface
    state: up
    patch:
      peer: p_g0patch1
  # base veth
  - {name: p_g0veth0, type: ethernet, state: up}
  - {name: p_g0veth1, type: ethernet, state: up}
  - {name: p_g0veth2, type: ethernet, state: up}
  - {name: p_g0veth3, type: ethernet, state: up}
  - {name: p_g0veth4, type: ethernet, state: up}
  - {name: p_g0veth5, type: ethernet, state: up}
  - {name: p_g0veth6, type: ethernet, state: up}
  - {name: p_g0veth7, type: ethernet, state: up}
  - {name: p_g0veth8, type: ethernet, state: up}
  - {name: p_g0veth9, type: ethernet, state: up}
  - {name: p_g0veth10, type: ethernet, state: up}
  - {name: p_g0veth11, type: ethernet, state: up}
  - {name: p_g0veth12, type: ethernet, state: up}
  - {name: p_g0veth13, type: ethernet, state: up}
  - {name: p_g0veth14, type: ethernet, state: up}
  - {name: p_g0veth15, type: ethernet, state: up}
  - {name: p_g0veth16, type: ethernet, state: up}
  - {name: p_g0veth17, type: ethernet, state: up}
  - {name: p_g0veth18, type: ethernet, state: up}
  - {name: p_g0veth19, type: ethernet, state: up}
  - {name: p_g0veth20, type: ethernet, state: up}
  - {name: p_g0veth21, type: ethernet, state: up}
  - {name: p_g0veth22, type: ethernet, state: up}
  - {name: p_g0veth23, type: ethernet, state: up}
  - {name: p_g0veth24, type: ethernet, state: up}
  - {name: p_g0veth25, type: ethernet, state: up}
  - {name: p_g0veth26, type: ethernet, state: up}
  - {name: p_g0veth27, type: ethernet, state: up}
  - {name: p_g0veth28, type: ethernet, state: up}
  - {name: p_g0veth29, type: ethernet, state: up}
  - {name: p_g0veth30, type: ethernet, state: up}
  - {name: p_g0veth31, type: ethernet, state: up}
  # vrf
  - name: p_g0vrf0
    type: vrf
    state: up
    vrf:
      port:
        - p_g0veth32
        - p_g0veth33
      route-table-id: 1000
  - {name: p_g0veth32, type: ethernet, state: up}
  - {name: p_g0veth33, type: ethernet, state: up}
EOF

# to create veth pairs
gveth() {
    local if_up=${if_up:-yes};
    local nm_managed=${nm_managed:-yes};
    local groupid=g${2:-0};
    ip link add ${groupid}vethm0 type veth peer p_${groupid}vethm0;
    for i in $(seq 0 ${1});
    do
        ip link add ${groupid}veth${i} type veth peer name p_${groupid}veth${i};
        if [ ${if_up} == 'yes' ]; then
            ip link set ${groupid}veth${i} up;
            ip link set p_${groupid}veth${i} up;
        fi;
        if [ ${nm_managed} == 'yes' ]; then
            nmcli device set ${groupid}veth${i} managed yes;
            nmcli device set p_${groupid}veth${i} managed yes;
        fi;
    done
}

dnf install -y lldpd
systemctl start lldpd
gveth 33
ip link set g0vethm0 up
ip link set p_g0vethm0 up
nmcli dev set g0vethm0 managed yes
nmcli dev set p_g0vethm0 managed yes
nmstatectl set all_ifaces.yml
nmstatectl set all_ifaces_peer.yml
set -x
sleep 10
nmstatectl show > 1.yaml
nmstatectl show | grep abcdef
nmstatectl show -r | grep abcdef
systemctl stop lldpd
sed 's/state: up/state: absent/g' all_ifaces.yml | nmstatectl apply
sed 's/state: up/state: absent/g' all_ifaces_peer.yml | nmstatectl apply
set +x

ip link del g0vethm0
for i in $(seq 0 33); do
    ip link del g0veth$i
done

