#######################################################################################
#######################################################################################
#NIDD_VER: xL17_1610_006_00, SBTS17_1610_003_00
#FL17: FL17xxx
#Migration from FL17 to SBTS17 - xL17
#24.02.2017/ v0.1 /first draft /L.Preuss
#
#######################################################################################

#######################################################################################
#######################################################################################

if ( objects_count "MRBTS" ) == 0 abort_file
if ( objects_count "MRBTS/LNBTS" ) == 0 abort_file
if ( objects_count "MRBTS/LNBTS/FTM" ) == 0 abort_file

define "version" "none"
for_objects "MRBTS" define "mrbtsRoot" $current_dist_name
for_objects "MRBTS/LNBTS" define "lnbtsRoot" $current_dist_name
for_objects "MRBTS/LNBTS/FTM" define "ftmRoot" $current_dist_name

define "tempVersion" get_version lnbtsRoot
define "tempVersion2" replace tempVersion "(.*(FL17).*)|^(?!FL17).*" "$2" flags "g"

if tempVersion2 == "FL17" define "version" "xL17_1610_006"
if ( tempVersion2 == "" )
    begin
        define "tempVersion2" replace tempVersion "(.*(FL18).*)|^(?!FL18).*" "$2" flags "g"
        if ( tempVersion2 ) == "FL18"
            define "version" "xL17_1610_006"
    end

if version == "none" abort_file
#######################################################################################

#######################################################################################
#######################################################################################
#Add TRSNW MO to LNBTS tree

define "trnwid" "0"
for_objects "MRBTS/LNBTS/LNMME-0"
	begin
		define "trnwid" get_param $current_dist_name "transportNwId"
	end

define "trsnwDN"  lnbtsRoot JOIN "/TRSNW-0""
add_object trsnwDN

add_param trsnwDN "transportNwId" 0
add_param trsnwDN "transportNwInUse" "true"

define "addTransportNwIpAddrListCongigured" "false"

define "cplaneIp" "0.0.0.0"
define "cplaneIp2" "0.0.0.0"
define "uplaneIp" "0.0.0.0"
define "uplaneIp2" "0.0.0.0"

define "addcplaneIp" "0.0.0.0"
define "addcplaneIp2" "0.0.0.0"
define "adduplaneIp" "0.0.0.0"

for_objects "MRBTS/LNBTS/FTM/IPNO"
	begin
		define "ipnoDN" $current_dist_name
		define "cplaneIp" get_param ipnoDN "cPlaneIpAddress"
		define "cplaneIp2" get_param ipnoDN "cPlaneIpAddressSec"
		define "cplaneIpV6" get_param ipnoDN "cPlaneIpv6Address"
		define "cplaneIpV62" get_param ipnoDN "cPlaneIpv6AddressSec"
		define "uplaneIp" get_param ipnoDN "uPlaneIpAddress"
		define "uplaneIp2" get_param ipnoDN "uPlane2IpAddress"
		define "uplaneIpV6" get_param ipnoDN "uPlaneIpv6Address"
		define "uplaneIpV62" get_param ipnoDN "uPlane2Ipv6Address"
		

		


add_list trsnwDN "cPlane"
add_complex_param trsnwDN "cPlane.0"

add_list trsnwDN "uPlane"
add_complex_param trsnwDN "uPlane.0"

		if ( param_exist ipnoDN "addTransportNwIpAddrList.0" )
			begin
				define "addTransportNwIpAddrListCongigured" "true"

				define "trsnwDN1"  lnbtsRoot JOIN "/TRSNW-1" 
				add_object trsnwDN1
				add_param trsnwDN1 "transportNwId" get_param $current_dist_name "addTransportNwIpAddrList.0.transportNwId"
				add_param trsnwDN1 "transportNwInUse" "true"
				define "addcplaneIp" get_param ipnoDN  "addTransportNwIpAddrList.0.addCPlaneIpv4Address"
				define "addcplaneIp2" get_param ipnoDN "addTransportNwIpAddrList.0.addCPlaneIpAddressSec"
				define "addcplaneIpV6" get_param ipnoDN  "addTransportNwIpAddrList.0.addCPlaneIpv6Address"
				define "addcplaneIpV62" get_param ipnoDN "addTransportNwIpAddrList.0.addCPlaneIpv6AddressSec"
				define "adduplaneIp" get_param ipnoDN "addTransportNwIpAddrList.0.addUPlaneIpv4Address"
				define "adduplaneIpV6" get_param ipnoDN "addTransportNwIpAddrList.0.addUPlaneIpv6Address"

				add_list trsnwDN1 "cPlane"
				add_complex_param trsnwDN1 "cPlane.0"

				add_list trsnwDN1 "uPlane"
				add_complex_param trsnwDN1 "uPlane.0"
			end
	end


for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
	begin
		if ( get_param $current_dist_name "localIpAddr" ) == cplaneIp add_param trsnwDN "cPlane.0.ipV4AddressDN1" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == cplaneIp2 add_param trsnwDN "cPlane.0.ipV4AddressDN2" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == cplaneIpV6 add_param trsnwDN "cPlane.0.ipV6AddressDN1" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == cplaneIpV62 add_param trsnwDN "cPlane.0.ipV6AddressDN2" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == uplaneIp add_param trsnwDN "uPlane.0.ipV4AddressDN1" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == uplaneIp2 add_param trsnwDN "uPlane.0.ipV4AddressDN2" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == uplaneIpV6 add_param trsnwDN "uPlane.0.ipV6AddressDN1" $current_dist_name
		if ( get_param $current_dist_name "localIpAddr" ) == uplaneIpV62 add_param trsnwDN "uPlane.0.ipV6AddressDN2" $current_dist_name

		if ( addTransportNwIpAddrListCongigured  == "true" )
			begin
				if ( get_param $current_dist_name "localIpAddr" ) == addcplaneIp add_param trsnwDN1 "cPlane.0.ipV4AddressDN1" $current_dist_name
				if ( get_param $current_dist_name "localIpAddr" ) == addcplaneIp2 add_param trsnwDN1 "cPlane.0.ipV4AddressDN2" $current_dist_name
				if ( get_param $current_dist_name "localIpAddr" ) == addcplaneIpV6 add_param trsnwDN1 "cPlane.0.ipV6AddressDN1" $current_dist_name
				if ( get_param $current_dist_name "localIpAddr" ) == addcplaneIpV62 add_param trsnwDN1 "cPlane.0.ipV6AddressDN2" $current_dist_name
				if ( get_param $current_dist_name "localIpAddr" ) == adduplaneIp add_param trsnwDN1 "uPlane.0.ipV4AddressDN1" $current_dist_name
				if ( get_param $current_dist_name "localIpAddr" ) == adduplaneIpV6 add_param trsnwDN1 "uPlane.0.ipV6AddressDN1" $current_dist_name		
				
			end
		
	end
#######################################################################################

#######################################################################################
#######################################################################################
#set MRBTS mandatory value

# if param_exist lnbtsRoot "enbName" add_param mrbtsRoot "btsName" get_param lnbtsRoot "enbName"
# else add_param mrbtsRoot "btsName" mrbtsRoot
if not param_exist mrbtsRoot "btsName" add_param mrbtsRoot "btsName" get_param lnbtsRoot "enbName"

#######################################################################################
#######################################################################################
#add MPLANENW MO

for_objects "MRBTS/MNL/MNLENT"
	begin
		define "mplanenwDN" $current_dist_name JOIN "/MPLANENW-1"
		add_object mplanenwDN 
		set_operation mplanenwDN "create"
		set_version mplanenwDN get_version mrbtsRoot JOIN "/MNL-1"
		set_adaptation mplanenwDN "com.nokia.srbts.mnl"
		
		define "oamPeerIpAddress" "0.0.0.0"
		for_objects "FTM/IPNO/IDNS"
			if ( param_exist $current_dist_name "serverIpAddress2" )
				if ( replace get_param $current_dist_name "serverIpAddress2" "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" "" flags "g" ) == ""
					define "oamPeerIpAddress" get_param $current_dist_name "serverIpAddress2"
		add_param mplanenwDN "oamPeerIpAddress" oamPeerIpAddress
		add_param mplanenwDN "tlsRenegotiationInterval" "86400"
		
		define "ipnoDN" ftmRoot JOIN "/IPNO-1"
		
		if ( param_exist ipnoDN "mPlaneIpAddress" )
			begin
				define "tempIp" get_param ipnoDN "mPlaneIpAddress"
				for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
					begin
						if ( get_param $current_dist_name "localIpAddr" ) == tempIp add_param mplanenwDN "mPlaneIpAddressDN" $current_dist_name
					end
				for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV6"
					begin
						define "tempIp" replace tempIp "a" "A" flags "g"
						define "tempIp" replace tempIp "b" "B" flags "g"
						define "tempIp" replace tempIp "c" "C" flags "g"
						define "tempIp" replace tempIp "d" "D" flags "g"
						define "tempIp" replace tempIp "e" "E" flags "g"
						define "tempIp" replace tempIp "f" "F" flags "g"
						define "localIpAddr1" get_param $current_dist_name "localIpAddr"
						define "localIpAddr1" replace localIpAddr1 "a" "A" flags "g"
						define "localIpAddr1" replace localIpAddr1 "b" "B" flags "g"
						define "localIpAddr1" replace localIpAddr1 "c" "C" flags "g"
						define "localIpAddr1" replace localIpAddr1 "d" "D" flags "g"
						define "localIpAddr1" replace localIpAddr1 "e" "E" flags "g"
						define "localIpAddr1" replace localIpAddr1 "f" "F" flags "g"
						if localIpAddr1 == tempIp add_param mplanenwDN "mPlaneIpAddressDN" $current_dist_name
					end
			end
			
        if ( param_exist ipnoDN "omsTls" )
            begin
                define "omsTls" get_param ipnoDN "omsTls"

                if ( omsTls ) == "off"
                    add_param mplanenwDN "oamTls" "off"

                if ( omsTls ) == "forced" || ( omsTls ) == "probing"
                    add_param mplanenwDN "oamTls" "forced"
            end

		if ( param_exist ipnoDN "tlsRenegotiationInterval" )
			begin
				if ( get_param ipnoDN "tlsRenegotiationInterval" ) < 300 add_param mplanenwDN "tlsRenegotiationInterval" "300"
				if ( get_param ipnoDN "tlsRenegotiationInterval" ) > 299 add_param mplanenwDN "tlsRenegotiationInterval" get_param ipnoDN "tlsRenegotiationInterval"
				if ( get_param ipnoDN "tlsRenegotiationInterval" ) == 0 add_param mplanenwDN "tlsRenegotiationInterval" get_param ipnoDN "tlsRenegotiationInterval"

			end
			
		for_objects "MRBTS/LNBTS/FTM/IPNO/QOS"
			begin
				if ( param_exist $current_dist_name "trafficTypesMap" )
					begin
						for_list $current_dist_name "trafficTypesMap"
							begin
								if ( get_param $current_dist_name "trafficTypesMap." JOIN $current_index JOIN ".trafficType" ) == "MPLANE"
									begin
										add_param mplanenwDN "mPlaneDscp" get_param $current_dist_name "trafficTypesMap." JOIN $current_index JOIN ".dscp"
									end
							end
					end
			end
		
	end
	
#######################################################################################
#######################################################################################
#fix ip address DNs

for_objects "MRBTS/MNL/MNLENT/SYNC/CLOCK/TOP"
	begin
		define "topDN" $current_dist_name
		for_objects "MRBTS/LNBTS/FTM/IPNO" 
			begin
				define "ipnoDN" $current_dist_name
				if ( param_exist ipnoDN "sPlaneIpAddress" )
					begin
						for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
							begin
								if ( get_param $current_dist_name "localIpAddr" ) == ( get_param ipnoDN "sPlaneIpAddress" )
									add_param topDN "sPlaneIpAddressDN" $current_dist_name"
							end
						for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV6"
							begin
								if ( get_param $current_dist_name "localIpAddr" ) == ( get_param ipnoDN "sPlaneIpAddress" )
									add_param topDN "sPlaneIpAddressDN" $current_dist_name"
							end
					end
			end
	end


#######################################################################################
#######################################################################################
#FL17A conversions

for_objects "MRBTS/MNL/AMGR/LUAC"
	begin
		if ( param_exist $current_dist_name "appAdmLocalPasswdLastChangedTime" ) del_param $current_dist_name "appAdmLocalPasswdLastChangedTime"
		if ( param_exist $current_dist_name "readOnlyLocalPasswdLastChangedTime" ) del_param $current_dist_name "readOnlyLocalPasswdLastChangedTime"
		if ( param_exist $current_dist_name "secAdmLocalPasswdLastChangedTime" ) del_param $current_dist_name "secAdmLocalPasswdLastChangedTime"
	end

#######################################################################################
#######################################################################################
#del FTM tree and set proper versions to LNBTS tree


for_branch lnbtsRoot
	begin
		set_version $current_dist_name version
		set_adaptation $current_dist_name "NOKLTE"
		set_operation $current_dist_name "create"
	end
	
for_objects "MRBTS"
	begin
		set_version $current_dist_name "SBTS17_1610_003"
		set_adaptation $current_dist_name "com.nokia.srbts"
	end
	


