cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2153
Views
5
Helpful
11
Replies
mhemmatp
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,

1 ACCEPTED SOLUTION

Accepted Solutions

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

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

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,

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

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.

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?

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.

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

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

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

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

Thanks. It worked for me!