+_ucidef_add_switch_port() {
+ # inherited: $num $device $need_tag $role $index $prev_role
+ # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
+
+ n_ports=$((n_ports + 1))
+
+ json_select_array ports
+ json_add_object
+ json_add_int num "$num"
+ [ -n "$device" ] && json_add_string device "$device"
+ [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
+ [ -n "$role" ] && json_add_string role "$role"
+ [ -n "$index" ] && json_add_int index "$index"
+ json_close_object
+ json_select ..
+
+ # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
+ [ -n "$device" ] && {
+ export "cpu$n_cpu=$n_ports"
+ n_cpu=$((n_cpu + 1))
+ }
+
+ # create/append object to role list
+ [ -n "$role" ] && {
+ json_select_array roles
+
+ if [ "$role" != "$prev_role" ]; then
+ json_add_object
+ json_add_string role "$role"
+ json_add_string ports "$num"
+ json_close_object
+
+ prev_role="$role"
+ n_vlan=$((n_vlan + 1))
+ else
+ json_select_object "$n_vlan"
+ json_get_var port ports
+ json_add_string ports "$port $num"
+ json_select ..
+ fi
+
+ json_select ..
+ }
+}
+
+_ucidef_finish_switch_roles() {
+ # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
+ local index role roles num device need_tag port ports
+
+ json_select switch
+ json_select "$name"
+ json_get_keys roles roles
+ json_select ..
+ json_select ..
+
+ for index in $roles; do
+ eval "port=\$cpu$(((index - 1) % n_cpu))"
+
+ json_select switch
+ json_select "$name"
+ json_select ports
+ json_select "$port"
+ json_get_vars num device need_tag
+ json_select ..
+ json_select ..
+
+ if [ $n_vlan -gt $n_cpu -o ${need_tag:-0} -eq 1 ]; then
+ num="${num}t"
+ device="${device}.${index}"
+ fi
+
+ json_select roles
+ json_select "$index"
+ json_get_vars role ports
+ json_add_string ports "$ports $num"
+ json_add_string device "$device"
+ json_select ..
+ json_select ..
+ json_select ..
+ json_select ..
+
+ json_select_object network
+ json_select_object "$role"
+ # attach previous interfaces (for multi-switch devices)
+ local prev_device; json_get_var prev_device ifname
+ if ! list_contains prev_device "$device"; then
+ device="${prev_device:+$prev_device }$device"
+ fi
+ json_add_string ifname "$device"
+ json_select ..
+ json_select ..
+ done
+}
+