# Copyright 2017 Juniper Networks, Inc. All rights reserved.
# Licensed under the Juniper Networks Script Software License (the "License").
# You may not use this script file except in compliance with the License, which is located at
# http://www.juniper.net/support/legal/scriptlicense/
# Unless required by applicable law or otherwise agreed to in writing by the parties, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#

*** Settings ***
Documentation   A resource file with reusable keywords and variables
Variables 	${EXECDIR}/variables_file/northstar_variables.py
Variables 	${input_directory}/test.yaml
Library		OperatingSystem
Library		String
Library 	XML
Library		Collections
Library 	${EXECDIR}/libraries/pybot_jnorthstar.py     	grant_type=${northstar_grant_type} 	username=${northstar_username}    password=${northstar_password}    api_server=${northstar_server}  api_port=${northstar_api_port} 		igp=${igp} 		WITH NAME 	northstar

*** Variables ***

${output_path}=	${output_directory}
${input_path}=	${input_directory}

*** Keywords ***

######################### GENERAL KEYWORDS #############################

Get Northstar token
	northstar.get_token

Authenticate Northstar
	Get Northstar token

Get Topology Index from Northstar
	northstar.get_topology_index

Get Node using Hostname from Northstar
	[Arguments] 	${name}
	northstar.get_node_using_hostname 	hostname=${name}

Get Nodes using Hostnames from Northstar
	:FOR 	${router} 	IN 	@{check_hostname}
	\ 	Get Node using Hostname from Northstar 	${router['hostname']}

Search Node using RouterId from Northstar
	[Arguments] 	${router_id}
	${node_name} = 	northstar.get_node_hostname_using_router_id 	router_id=${router_id}
	[Return] 	${node_name}

Search Nodes using RouterIds from Northstar
	:FOR 	${router} 	IN 	@{check_hostname}
	\ 	Search Node using RouterId from Northstar 	${router['router_id']}

Get Node using Name from Northstar
	[Arguments] 	${name}
	${node} = 	northstar.get_node_name_using_hostname 	hostname=${name}
	[Return] 	${node}

Get Nodes using Names from Northstar
	:FOR 	${router} 	IN 	@{check_name}
	\ 	Get Node using Name from Northstar 	${router['hostname']}

Get Links using Hostnames
	[Arguments] 	${node1} 	${node2}
	${links} = 		northstar.get_links_using_node_names 	node1=${node1} 	node2=${node2}
	[Return] 	${links}

Get LSP with Source and Name
	[Arguments] 	${source} 	${lspname}
	${lsp} = 	northstar.get_lsp_using_name_and_source 	source=${source} 	lspname=${lspname}
	[Return] 	${lsp}

Get LSP using Source and Name in Northstar
	:FOR 	${item} 	IN 	@{get_lsp}
	\ 	Get LSP with Source and Name 	source=${item['source']} 	lspname=${item['name']}

Delete LSP using Source and Name in Northstar
	:FOR 	${item} 	IN 	@{delete_lsp}
	\ 	${lsp} = 	Get LSP with Source and Name 	source=${item['source']} 	lspname=${item['name']}
	\ 	northstar.delete_lsp 	${lsp['lspIndex']}

Search Links using Hostnames from Northstar
	:FOR 	${item} 	IN 	@{check_link}
	\	${endpoint1} = 	Get Node using Name from Northstar 	${item['hostname1']}
	\	${endpoint2} = 	Get Node using Name from Northstar 	${item['hostname2']}
	\ 	${links} 	Get Links using Hostnames 	node1=${endpoint1} 	node2=${endpoint2}

Update Link attributes
	[Arguments] 	${locallinks} 	&{attributes}
	:FOR 	${link} 	IN 	@{locallinks}
	\	northstar.update_link_attributes 	${link} 	&{attributes}

Update Links using Hostnames from Northstar
	:FOR 	${item} 	IN 	@{update_link}
	\	${endpoint1} = 	Get Node using Name from Northstar 	${item['hostname1']}
	\	${endpoint2} = 	Get Node using Name from Northstar 	${item['hostname2']}
	\ 	${links} 	Get Links using Hostnames	node1=${endpoint1} 	node2=${endpoint2}
	\ 	Convert to Dictionary 	${item}
	\ 	Update Link attributes 	${links} 	&{item}

Update Nodes using RouterIds from Northstar
	:FOR 	${item} 	IN 	@{update_node}
	\ 	Authenticate Northstar
	\ 	${node} = 	northstar.get_node_using_router_id 	router_id=${item['router_id']}
	\ 	Convert to Dictionary 	${item}
	\	northstar.update_node_attributes 	${node} 	&{item}

Update Nodes using Hostnames from Northstar
	:FOR 	${item} 	IN 	@{update_node}
	\ 	Authenticate Northstar
	\ 	${node} = 	northstar.get_node_using_hostname 	hostname=${item['hostname']}
	\ 	Convert to Dictionary 	${item}
	\	northstar.update_node_attributes 	${node} 	&{item}

Update LSP using Source and Name in Northstar
	:FOR 	${item} 	IN 	@{update_lsp}
	\	${lsp} = 	Get LSP with Source and Name 	${item['source']} 	${item['name']}
	\ 	Convert to Dictionary 	${item}
	\	northstar.update_lsp 	${lsp} 	&{item}

Create LSP Name with Endpoints and Prefix
	[Arguments] 	${source} 	${destination} 	${regexp}
	${prefix} = 	Fetch From Left 	${regexp} 	*
	${lsp_members} = 	Catenate 	SEPARATOR= 	${prefix} 	${source} 	- 	${destination}
	${lsp_name} = 	Convert to String 	${lsp_members}
	[Return] 	${lsp_name}

Create LSPs using Endpoints in Northstar
	:FOR 	${item} 	IN 	@{create_lsp}
	\ 	${source} = 	Search Node using RouterId from Northstar 	router_id=${item['source']}
	\ 	${destination} = 	Search Node using RouterId from Northstar 	router_id=${item['destination']}
	\ 	${lsp_name} = 	Create LSP Name with Endpoints and Prefix 	${source[0]} 	${destination[0]} 	${item['name_regexp']}
	\ 	Convert to Dictionary 	${item}
	\	northstar.create_lsp 	${lsp_name} 	&{item}

Create Single Automesh using Endpoints in Northstar
	[Arguments] 	${variables}
	:FOR 	${router} 	IN 	@{variables['routers']}
	\ 	${destinations} = 	Copy List 	${variables['routers']}
	\ 	Remove Values From List 	${destinations} 	${router}
	\ 	Convert to Dictionary 	${variables}
	\ 	${args} = 	Copy Dictionary 	${variables}
	\ 	Pop from Dictionary 	${args} 	routers
	\ 	Convert to Dictionary 	${args}
	\ 	northstar.create_lsp_bulk 	${router} 	${destinations} 	&{args}

Create Automesh using Endpoints in Northstar
	:FOR 	${item} 	IN 	@{create_automesh_static}
	\ 	Create Single Automesh using Endpoints in Northstar 	${item}

Create Single Automesh using PCEP routers from Northstar
	[Arguments] 	${variables}
	${routers} = 	northstar.get_pcep_active_nodes
	:FOR 	${router} 	IN 	@{routers}
	\ 	${destinations} = 	Copy List 	${routers}
	\ 	Remove Values From List 	${destinations} 	${router}
	\ 	Convert to Dictionary 	${variables}
	\ 	${args} = 	Copy Dictionary 	${variables}
	\ 	Convert to Dictionary 	${args}
	\ 	northstar.create_lsp_bulk 	${router} 	${destinations} 	&{args}

Create Automesh using PCEP routers from Northstar
	:FOR 	${item} 	IN 	@{create_automesh_pcep}
	\ 	Create Single Automesh using PCEP routers from Northstar 	${item}

Create Nodes using Hostnames in Northstar
	:FOR 	${item} 	IN 	@{create_node}
	\ 	Authenticate Northstar
	\ 	northstar.create_node 	${item['hostname']}

Reset Topology in Northstar
	northstar.reset_topology

Delete All LSPs from Northstar
	${lsps} = 	northstar.get_pce_initiated_lsps
	northstar.delete_bulk_lsps 	${lsps}

