How activate OSPF in a router Cisco-IOS-XE using RESTCONF?

685 views Asked by At

I'm trying to activate OSPF routing protocol in a router using a python script with restconf. I send the request to this url https://(router ip)/restconf/data/Cisco-IOS-XE-ospf-native-router:10 but i get this return message: { "errors": { "error": [ { "error-message": "uri keypath not found", "error-tag": "invalid-value", "error-type": "application" } ] } }

I tried with differents urls but no one was the correct and i don't know what is wrong, the url or the content of the body , can someone help me?

This is the content of my script:

import json
import requests

requests.packages.urllib3.disable_warnings()

api_url = "https://(ip)/restconf/data/Cisco-IOS-XE-ospf-native-router:10"
headers = {"Accept": "application/yang-data+json",
           "Content-type": "application/yang-data+json"}
basicauth = ("user", "password")

body = '{"router":{"ospf":[{"id":10, "router-id":"1.1.1.1", "network":[{"ip": "172.16.199.1", "mask": "0.0.0.0", "area": 0},{"ip":"192.168.56.101", "mask": "0.0.0.0", "area":0}]}]}}'

requests.put(api_url, auth=basicauth, headers=headers, data=body, verify=False)

resp = requests.get(api_url, auth=basicauth, headers=headers, verify=False)

response_json = resp.json()

print(json.dumps(response_json, indent=5))
2

There are 2 answers

0
Jackal.C On

OSPF config with RESTCONF can just be modify, not create.

ospf_data = '''
{
    "Cisco-IOS-XE-native:router": {
        "Cisco-IOS-XE-ospf:router-ospf": {
            "ospf": {
                "process-id": [
                    {
                        "id": 10
                    }
                ]
            }
        }
    }
}
'''

def modify_ospf(data):
    ospf_api = "/restconf/data/Cisco-IOS-XE-native:native/router"
    ospf_uri = "https://" + host + ospf_api
    headers = {'Content-Type': 'application/yang-data+json','Accept': 'application/yang-data+json'}

    ospf_config_result = requests.request("PUT",
                                         url=ospf_uri, 
                                         auth=(username,password), 
                                         data=data, 
                                         headers=headers, 
                                         verify=False)
    
modify_ospf(ospf_data)

0
Eiji Tanioka On

Your body seems not vaild YANG format.

body = '{"router":{"ospf":[{"id":10, "router-id":"1.1.1.1", "network":[{"ip": "172.16.199.1", "mask": "0.0.0.0", "area": 0},{"ip":"192.168.56.101", "mask": "0.0.0.0", "area":0}]}]}}'

You have to follow YANG format like:

$ curl -k -s -u user:password -X GET -H 'Accept: application/yang-data+json' https://172.31.5.1/restconf/data/Cisco-IOS-XE-native:native/Cisco-IOS-XE-native:router/ospf

{
  "Cisco-IOS-XE-ospf:ospf": [
    {
      "id": 10,
      "router-id": "1.1.1.1",
      "network": [
        {
          "ip": "172.16.199.1",
          "mask": "0.0.0.0",
          "area": 0
        },
        {
          "ip": "192.168.56.101",
          "mask": "0.0.0.0",
          "area": 0
        }
      ]
    }
  ]
}