cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1383
Views
5
Helpful
11
Replies
Highlighted
Cisco Employee

NSO add device python API

Hello all,

 

I am trying to add NETCONF device to NSO through python API as follows:

 

import ncs;

with ncs.maapi.single_write_trans('admin', 'python', groups=['ncsadmin']) as t:

root = ncs.maagic.get_root(t)

root.devices.device.create('test')

root.devices.device['test'].address = '127.0.0.1'

 

root.devices.device['test'].port = 22

root.devices.device['test'].device_type.cli.create()

root.devices.device['test'].device_type.cli.ned_id = 'netconf'

root.devices.device['test'].device_type.cli.protocol = 'ssh'

root.devices.device['test'].authgroup = 'authtest'

root.devices.device['test'].state.admin_state = 'unlocked'

 

But I receive the following error:

 

root.devices.device['test'].device_type.cli.ned_id = 'netconf'
File "src/ncs/pyapi/ncs/maagic.py", line 471, in __setattr__
child.set_value(value)
File "src/ncs/pyapi/ncs/maagic.py", line 939, in set_value
value = _python_to_yang(value, self._cs_node)
File "src/ncs/pyapi/ncs/maagic.py", line 1927, in _python_to_yang
return _tm.Value.str2val(str(value), cs_node)
_ncs.error.Error: item has a bad/wrong type (5): Invalid string for type

 

what is the problem of my code?

 

Thanks,

Regards,

Everyone's tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Cisco Employee

Re: NSO add device python API

This python code will add a netconf device to the device tree:

In [15]: with ncs.maapi.Maapi() as m:
...:           with ncs.maapi.Session(m, 'admin', 'python'):
...:              with m.start_write_trans() as t:
...:                  root = ncs.maagic.get_root(t)
...:                  newdev = root.devices.device.create('test-dev')
...:                  newdev.address = '127.0.0.1'
...:                  newdev.port = 830
...:                  newdev.authgroup = 'default'

...:                  dev_type = newdev.device_type.netconf.create()
...:                  dev_type.ned_id = 'netconf'
...:                  print (dev_type)
...:                  newdev.state.admin_state = 'southbound-locked'
...:                  t.apply()
...:
netconf

 

admin@ncs% show devices device test-dev
address 127.0.0.1;
port 830;
authgroup default;
device-type {
      netconf {
           ned-id netconf;
      }
}
state {
      admin-state southbound-locked;
}

View solution in original post

11 REPLIES 11
Highlighted

Re: NSO add device python API

root.devices.device['test'].device_type.cli.ned_id = 'netconf'

root.devices.device['test'].device_type.cli.protocol = 'ssh'

 

protocol will = netconf or ssh

 

ned_id should be cisco-ios or nx or xr etc etc

Highlighted
Cisco Employee

Re: NSO add device python API

Still it does not work. My goal is to add a netconf device do you think my syntax is OK: 

 

root.devices.device['test'].device_type.cli.ned_id = ...

 

device_type.cli does not indicate I am adding a cli device? 

 

Thanks,

Highlighted

Re: NSO add device python API

what is the actual device ?

and do you have a NED for it

 

eg if i wanted to add an IOS router i would use the cisco-ios NED that ned can communicate with the device in several ways eg SSH or Netconf

Highlighted
Cisco Employee

Re: NSO add device python API

It is worth noting that netconf and ssh are not interchangeable here. Either you want a CLI ned that can communicate over a protocol like ssh or telnet or you want a netconf ned (that can also communicate over ssh). So when it says .cli you are in the cli-ned specific part of the config.
Highlighted
Cisco Employee

Re: NSO add device python API

So, as I understood the cli does not indicate I am adding a cli device it can be used to add a netconf device ad well. Am I right?

Highlighted
Cisco Employee

Re: NSO add device python API

No. CLI definitely indicates that you are adding a CLI device. To add a netconf device, use the netconf sub-tree. That is device_type.netconf instead of device_type.cli.
Highlighted
Cisco Employee

Re: NSO add device python API

Thanks for the reply. I set the netconf.ned_id to 'netconf' and I removed the netconf.protocol. Still I have the following ERR:

 

File "src/ncs/pyapi/ncs/maapi.py", line 1005, in apply
self.maapi.apply_trans_flags(self.th, keep_open, flags)
File "src/ncs/pyapi/ncs/maapi.py", line 254, in proxy
return real(self2.msock, *args, **kwargs)
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
File "/usr/lib/python2.7/socket.py", line 174, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

Highlighted
Cisco Employee

Re: NSO add device python API

That is an unrelated error. Might be related to reusing a socket after closing it or overwriting a handle or something.
Highlighted
Cisco Employee

Re: NSO add device python API

I have a host with NETCONF server. I created already a NED for it in the NSO.

Highlighted
Cisco Employee

Re: NSO add device python API

This python code will add a netconf device to the device tree:

In [15]: with ncs.maapi.Maapi() as m:
...:           with ncs.maapi.Session(m, 'admin', 'python'):
...:              with m.start_write_trans() as t:
...:                  root = ncs.maagic.get_root(t)
...:                  newdev = root.devices.device.create('test-dev')
...:                  newdev.address = '127.0.0.1'
...:                  newdev.port = 830
...:                  newdev.authgroup = 'default'

...:                  dev_type = newdev.device_type.netconf.create()
...:                  dev_type.ned_id = 'netconf'
...:                  print (dev_type)
...:                  newdev.state.admin_state = 'southbound-locked'
...:                  t.apply()
...:
netconf

 

admin@ncs% show devices device test-dev
address 127.0.0.1;
port 830;
authgroup default;
device-type {
      netconf {
           ned-id netconf;
      }
}
state {
      admin-state southbound-locked;
}

View solution in original post

Highlighted
Cisco Employee

Re: NSO add device python API

Thanks. It worked for me!