#!/usr/bin/env python3

#curl --insecure --user 'root:NokiaNsp1!' -X POST https://10.20.0.10/rest-gateway/rest/api/v1/auth/token --data '{ "grant_type": "client_credentials" }'

user = 'admin'
passw = 'NokiaNsp1!'

import requests
from requests.auth import HTTPBasicAuth

payload = """{
    "ibn:intent":{
        "target": "113",
        "intent-type": "L3VPN",
        "intent-type-version": 1,
        "required-network-state": "active",
        "intent-specific-data": {
                        "L3VPN:L3VPN": {
                            "nodes": [
                                {
                                    "RD": "65001:10",
                                    "InterfaceParameters": {
                                        "interfaces": [
                                            {
                                                "port": "GigabitEthernet0/0/0/2",
                                                "interface-name": "darkBlue",
                                                "ipAddress": "40.2.1.1",
                                                "port-mtu": 2000,
                                                "mask": 30
                                            }
                                        ]
                                    },
                                    "ProtocolParameters": {
                                        "peer-address": "40.2.1.2",
                                        "remote-AS": 123,
                                        "local-AS": 65001
                                    },
                                    "ne-id": "10.20.0.101"
                                },
                                {
                                    "RD": "65001:10",
                                    "InterfaceParameters": {
                                        "interfaces": [
                                            {
                                                "port": "GigabitEthernet0/0/0/2",
                                                "admin-enabled": true,
                                                "interface-name": "lightBlue",
                                                "ipAddress": "30.2.1.1",
                                                "port-mtu": 2000,
                                                "mask": 30
                                            }
                                        ]
                                    },
                                    "ProtocolParameters": {
                                        "remote-AS": 123,
                                        "peer-address": "30.2.1.2",
                                        "local-AS": 65001
                                    },
                                    "ne-id": "10.20.0.100"
                                }
                            ]
                        }
                    }
        }
    }
}
"""

reply = requests.post('https://10.20.0.10/rest-gateway/rest/api/v1/auth/token', verify=False, auth=HTTPBasicAuth(user, passw), data = {"grant_type": "client_credentials"})

try:
  access_token = reply.json()['access_token']
  print(access_token)

  headers = {
    'Authorization': 'Bearer ' + access_token,
    'Accept': 'application/yang-data+json',
    'Content-Type': 'application/yang-data+json',
  }

  if 1:
    #reply = requests.get('https://10.20.0.10/restconf/data/ibn:ibn', 
    reply = requests.get('https://127.0.0.1:8545/restconf/data/ibn:ibn/ibn:intent=113,L3VPN', #https://10.20.0.10:8545/restconf/data/ibn:ibn/intent=<intent-target>,L3VPN
    #reply = requests.get('https://127.0.0.1:8545/restconf/data?depth=1', 
      verify=False, 
      headers = headers,
    ) 
    print(reply.json())

  if 0:
    reply = requests.post('https://127.0.0.1:8545/restconf/data/ibn:ibn', 
      verify=False, 
      headers = headers,
      data=payload,
    ) 
    print(reply.json())

except Exception as ex:
  print(f"ERROR {ex}:")
  print(reply.content)
  print(reply.json())
