1
0
Files
build.ems/linux/usr/local/etc/omc/script/init_ne.sh

214 lines
6.5 KiB
Bash

#!/bin/bash
set -e
declare -A NE_LIST
# Default network element number ne_id = 001
# Defining a list of network elements
# Format: "ne_type, ne_ip, ne_port"
# -----------------------------------------
# ne_id 001 Network Elements
# -----------------------------------------
NE_LIST["001"]="\
OMC,172.16.5.100,33030
IMS,172.16.5.110,33030
AMF,172.16.5.120,33030
AUSF,172.16.5.130,33030
UDM,172.16.5.140,33030
SMF,172.16.5.150,33030
PCF,172.16.5.160,33030
UPF,172.16.5.190,33030
"
# -----------------------------------------
# ne_id 002 Network Elements
# -----------------------------------------
# NE_LIST["002"]="\
# IMS,172.16.15.110,33030
# AMF,172.16.15.120,33030
# AUSF,172.16.15.130,33030
# UDM,172.16.15.140,33030
# SMF,172.16.15.150,33030
# PCF,172.16.15.160,33030
# UPF,172.16.15.190,33030
# "
# Default SSH info
DEFAULT_SSH_USER="manager"
DEFAULT_SSH_PORT=22
# If you already have a password-free authorization, please set the password to ""
#DEFAULT_SSH_PWD=""
DEFAULT_SSH_PWD="bSQ21KY7cjn58JNkZ0UZM64EbSIPMQwfCTmzk3MTBvQ=" # admin123
# 5gc+omc use local 127.0.0.1
DEFAULT_SSH_LOCAL=false
# Default ne_host info
DEFAULT_TELNET_PORT=4100
DEFAULT_TELNET_PORT2=5002
DEFAULT_REDIS_PORT=6379
# Path to the output file
output_file="output.sql"
# Empty the contents of the file (if the file already exists)
> "$output_file"
# ======================= generate sql
# ne_info
# generate_ne_info 1 OMC 001 127.0.0.1 33030 1,2
generate_ne_info() {
local row_id=$1
local ne_type=$2
local ne_id=$3
local ip=$4
local port=$5
local ne_host=$6
local ne_name="${ne_type}_${ne_id}"
local ne_rmuid="4400HX${ne_type}${ne_id}"
echo "INSERT INTO ne_info VALUES (${row_id}, '${ne_type}', '${ne_id}', '${ne_rmuid}', '${ne_name}', '${ip}', ${port}, 'PNF', '-', '-', '-', '-', '${ne_host}', 1, '', '', 0, 'script', $(date +%s%3N));"
}
# ne_host
# generate_ne_host 1 OMC 001 127.0.0.1
generate_ne_host() {
local row_id=$1
local ne_type=$2
local ne_id=$3
local ip=$4
local ne_name="${ne_type}_${ne_id}"
local start_id=$row_id
local ssh_ip=$4
if [ "$DEFAULT_SSH_LOCAL" == true ]; then
ssh_ip="127.0.0.1"
fi
local ssh_type="2"
local ssh_password=""
if [ -n "$DEFAULT_SSH_PWD" ]; then
ssh_type="0"
ssh_password="$DEFAULT_SSH_PWD"
fi
# SSH
echo "INSERT INTO ne_host VALUES (${start_id}, 'ssh', '1', '${ne_name}_${DEFAULT_SSH_PORT}_${start_id}', '${ssh_ip}', ${DEFAULT_SSH_PORT}, '${DEFAULT_SSH_USER}', '${ssh_type}', '${ssh_password}', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
((start_id++))
# Telnet
echo "INSERT INTO ne_host VALUES (${start_id}, 'telnet', '1', '${ne_name}_${DEFAULT_TELNET_PORT}_${start_id}', '${ip}', ${DEFAULT_TELNET_PORT}, 'admin', '0', 'okzArqkFYlnVVdQYkNiqmiW+CGQrxg1AFmroR1bw1Kg=', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
((start_id++))
# Special network element type handling
case "$ne_type" in
"UDM")
echo "INSERT INTO ne_host VALUES (${start_id}, 'redis', '1', '${ne_name}_${DEFAULT_REDIS_PORT}_${start_id}', '${ip}', ${DEFAULT_REDIS_PORT}, 'udmdb', '0', 'jW965gWZK0mw6EtYfihMV9QqHjqa03vJ8VlVii9non8=', '', '', '0', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
;;
"UPF")
echo "INSERT INTO ne_host VALUES (${start_id}, 'telnet', '1', '${ne_name}_${DEFAULT_TELNET_PORT2}_${start_id}', '${ip}', ${DEFAULT_TELNET_PORT2}, 'admin', '0', '', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
;;
esac
}
# generate_ne_host_ids OMC 1
generate_ne_host_ids() {
local ne_type=$1
local start_id=$2
local ids="${start_id},$((start_id+1))"
case "$ne_type" in
"UDM")
ids="${ids},$((start_id+2))"
;;
"UPF")
ids="${ids},$((start_id+2))"
;;
esac
echo "$ids"
}
# ne_version
# generate_ne_version 1 OMC 001
generate_ne_version() {
local row_id=$1
local ne_type=$2
local ne_id=$3
echo "INSERT INTO ne_version VALUES (${row_id}, '${ne_type}', '${ne_id}', '-', '-', '-', '', '', '', '', '', '', '0', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
}
# ne_license
# generate_ne_license 1 OMC 001
generate_ne_license() {
local row_id=$1
local ne_type=$2
local ne_id=$3
echo "INSERT INTO ne_license VALUES (${row_id}, '${ne_type}', '${ne_id}', '', '', 0, '', '', '1', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));"
}
# =======================
# Execute output sql to file
execute_output() {
local row_id=1
local host_id=1
# empty the table
echo "DELETE FROM ne_host;" >> "$output_file"
echo "DELETE FROM ne_info;" >> "$output_file"
echo "DELETE FROM ne_version;" >> "$output_file"
echo "DELETE FROM ne_license;" >> "$output_file"
echo "" >> "$output_file"
# -----------------------------------------
# Example of accessing and parsing the elements
# -----------------------------------------
for ne_id in "${!NE_LIST[@]}"; do
echo "NEID: $ne_id:"
# Splitting the values by newline to get each device entry
IFS=$'\n' read -r -d '' -a devices <<< "${NE_LIST[$ne_id]}"
# Iterating over each device entry
for device in "${devices[@]}"; do
# Split device entry into name, IP, and port
IFS=',' read -r ne_type ne_ip ne_port <<< "$device"
echo "===> ${ne_type} ${ne_ip} ${ne_port}"
generate_ne_host "$host_id" "$ne_type" "$ne_id" "$ne_ip" >> "$output_file"
local host_ids=$(generate_ne_host_ids $ne_type $host_id)
generate_ne_info "$row_id" "$ne_type" "$ne_id" "$ne_ip" "$ne_port" "$host_ids" >> "$output_file"
generate_ne_version "$row_id" "$ne_type" "$ne_id" >> "$output_file"
generate_ne_license "$row_id" "$ne_type" "$ne_id" >> "$output_file"
echo "" >> "$output_file"
((row_id++))
case "$ne_type" in
"UDM"|"UPF")
host_id=$((host_id+3))
;;
*)
host_id=$((host_id+2))
;;
esac
done
done
}
execute_output
# Execute import sql to omc
execute_import() {
local OMCBinFile=/usr/local/bin/omc
local OMCEtcDir=/usr/local/etc/omc
local confFile=$OMCEtcDir/omc.yaml
source $OMCEtcDir/omc.conf
$OMCBinFile -c $confFile --sqlPath $output_file --sqlSource $MODE
}
execute_import
# Remove the output file
rm -rf $output_file