
/*
 * We've appropriated a TielinePath in the middle. We need to reset the SourcePort and then so we need to release the upstream portion.
 */
IF OBJECT_ID('NV_ReleaseUpstreamTieline2Path') IS NOT NULL
  DROP PROCEDURE NV_ReleaseUpstreamTieline2Path
GO

CREATE PROCEDURE NV_ReleaseUpstreamTieline2Path
	@intTieline2In integer,
	@intNvUserIn integer,
	@intSrcPortIn integer,
	@intDestPhysLevelIn integer,
	@intUpPortIn integer,
	@intDestPortIn integer


AS

SET NOCOUNT ON;

declare @vchValue varchar(200)
--PRINT 'Entering NV_ReleaseExistingTieline2Path. Input parameters ...'

/*
exec NV_ConvertIntToString @intTieline2In, @vchValue = @vchValue OUTPUT
--PRINT '		@intTieline2In: ' + @vchValue
exec NV_ConvertIntToString @intNvUserIn, @vchValue = @vchValue OUTPUT
--PRINT '		@intNvUserIn: ' + @vchValue
exec NV_ConvertIntToString @intSrcPortIn, @vchValue = @vchValue OUTPUT
--PRINT 	'	@intSrcPortIn: ' + @vchValue
exec NV_ConvertIntToString @intDestPhysLevelIn, @vchValue = @vchValue OUTPUT
--PRINT '		@intDestPhysLevelIn: ' + @vchValue
exec NV_ConvertIntToString @intDestPortIn, @vchValue = @vchValue OUTPUT
--PRINT '		@intDestPortIn: ' + @vchValue
*/


declare @intSrcPhysLevel integer
declare @intSrcSignalType integer
declare @intSrcPort integer
declare @intDestPhysLevel integer
declare @intDestPort integer
declare @intUpPort integer
declare @intDownPort integer
declare @intStatus integer


-- Get the details we need from the new source in order to find the next
-- upstream segment using the unique key
SELECT @intSrcPhysLevel = -1
SELECT
	@intSrcPhysLevel = Tieline2Reservation_SourcePhysLevel,
	@intSrcSignalType =	Tieline2Reservation_SourceSignalType,
	@intSrcPort = Tieline2Reservation_SourcePort,
	@intUpPort = Tieline2Reservation_UpstreamPort,
	@intDownPort = Tieline2Reservation_DownstreamPort 
FROM Tieline2Reservation
WHERE 
	Tieline2Reservation_Tieline2 = @intTieline2In and 
	Tieline2Reservation_User = @intNvUserIn and 
	Tieline2Reservation_DestinationPhysLevel = @intDestPhysLevelIn and
    Tieline2Reservation_UpstreamPort = @intUpPortIn and
	Tieline2Reservation_DestinationPort = @intDestPortIn

IF (   @intSrcPhysLevel < 0  )
BEGIN
	--PRINT '		Big problem can not find match based on procedure input parameters'
	--PRINT 'Exiting NV_ReleaseExistingTieline2Path.'
	RETURN 1
END

SET @intStatus = 0
--- Did the soure port change? If so, then update it.
IF (   @intSrcPort != @intSrcPortIn )
BEGIN
	--PRINT 'Source port in reservation system does not match source port passed in as a parameter. Updating reservation entry.'
	UPDATE Tieline2Reservation SET Tieline2Reservation_SourcePort = @intSrcPortIn
	WHERE
		Tieline2Reservation_Tieline2 = @intTieline2In and
		Tieline2Reservation_User = @intNvUserIn and
		Tieline2Reservation_DestinationPhysLevel = @intDestPhysLevelIn and
		Tieline2Reservation_UpstreamPort = @intUpPortIn and
		Tieline2Reservation_DestinationPort = @intDestPortIn	
	SET @intStatus = @@ERROR
END

IF ( @intStatus = 0 )
BEGIN
	-- We've already handled the current segment, start releasing the upstream values ( if any )
	
	SET @intDestPhysLevel = @intSrcPhysLevel
	SET @intDestPort = @intUpPort
	SET @intUpPort = @intSrcPort
	
	IF ( SELECT count(*) 
			FROM Tieline2Reservation
		 WHERE 
			Tieline2Reservation_User = @intNvUserIn and 
			Tieline2Reservation_DestinationPhysLevel = @intDestPhysLevel and
			Tieline2Reservation_UpstreamPort = @intUpPort and 
			Tieline2Reservation_DestinationPort = @intDestPort ) > 0
	BEGIN
		--PRINT '   Start releasing upstream segments'
		EXEC @intStatus = NV_ReleaseTieline2Path @intNvUserIn, -1, -1, -1, @intDestPhysLevel, @intUpPort, @intDestPort
	END 
	--ELSE
	--BEGIN
		--PRINT 'No upstream segments to release'
	--END 
	
END
--ELSE
--BEGIN
	--PRINT 'Update failed.'
--END

--PRINT 'Exiting NV_ReleaseExistingTieline2Path.'

RETURN @intStatus

