cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
475
Views
0
Helpful
13
Replies
Highlighted
Beginner

Exception/Error Handling

Hello,

I'm trying to build some error checking into a onePK Python application and was wondering if there are built-in exceptions or some other way to help with what I'm trying to accomplish.  I understand a singleton application can only establish a single connection to a network element, but in my scenario, that is the case, but there may be an application using the same that is already connected to the element, which is what I'm trying to simulate.

I run this program fine the first time (as expected) and then it fails when running it subsequent times (as expected).  But rather have it fail, I want to make some change to have it function and not display any errors.

As soon as the line 'session_handle = ne.connect(username, password, session_config)' is hit, this error is displayed:

ERROR:onep.element.NetworkElement:IDL Exception: ExceptionIDL(text='Connect: provide unique application name', code=11, context=725)

Just after, the OnepDuplicateElementException is hit in the except statement as desired. 

The two main questions are:

1. Why is the IDL error shown above being displayed since it is in the try statement?  Is there a way to not display this?

2. What is the recommended way to connect to 'existing' in the OnepDuplicateElementException except statement?

Thanks,
Jason

Code:

address = "10.1.1.110"

myapp = NetworkApplication.get_instance()

myapp.name = 'jason1'

username = 'ciscosdn'

password = 'cisco'

session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS)

session_config.ca_certs = "/home/cisco/ca.pem"

ne = myapp.get_network_element(address)

if not ne.is_connected():

  try:

       print 'Trying to connect...'

       session_handle = ne.connect(username, password, session_config)

       print 'Connection made to ' + address + ' successfully.'

  except  OnepDuplicateElementException, e:

       print e

       existing = e.get_original_network_element()

       print existing

  except  Exception, e:

       print e

else:

  print 'Connection to ' + address + ' already exists.'

Everyone's tags (3)
13 REPLIES 13
Highlighted
Cisco Employee

Re: Exception/Error Handling

Hi, Jason.

The message is considered to be an ERROR and it is generally not a good idea to turn off logging errors.  We could possibly modify that to make this an INFO message, but that might not be a good idea either.

Probably a better idea here to avoid the problem altogether.  When there is a possibility that the application might connect twice, save the ne in a location.  When the application tries to find it, it first checks if ne.is_connected.

ne = get_ne()

if not ne:

    <instantiate ne>

    ne.connect(…)

    save_ne(ne)

In function get_ne() you can check if existing ne.is_connected and if not, remove it.

However...now that I'm re-reading your code, it looks like you're running two separate instances of the same application.  Which would explain why you pass the "if not ne._is_connected()" check when you run the second instance.  Hmmm.  If that's the case, then what you're trying won't work.  The second application can't take over the first application's connection.  Can you explain a little more the use case that would require this?

Thanks,

Shelly

Highlighted
Beginner

Re: Exception/Error Handling

Hi Shelly,

Thanks for the response.  Are you or is someone able to jump on a call for a few mins to talk about this?

Thanks,
Jason

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

Shelly is away at the moment.  Is this an urgent matter?

Beginner

Re: Exception/Error Handling

I wouldn't call it urgent by any means, but I'm trying to develop something.  It's hard to wait days for a response and make progress.  My question still stands.  Figured I could give a demo of what I'm working on and why I'm asking.

If you think I should wait for Shelly, that's what I'll do.

-Jason

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

In looking at your code, I see that you call "print e" in your except block, so that would explain why you're seeing the IDL error printed.  Removing the "print" should remove that.

For question 2, what I do is when I have a successful connection to an NE, I iterate through the list of IP addresses on it and create a dictionary that points each IP address to the related NE object.  This way, when my method is called with a new IP address (maybe found through CDP), I think look up the IP in the dictionary, and if found, I return the existing NE object.  Else I connect to the new IP, learn all the IPs, and return the new NE object.

Something else to note, your app name MUST be unique across instances.  Meaning that if I connect from app jason1, then launch a new instance of jason1, that new instance will see this same error.  The NE will key off of the app name to make sure the same app doesn't connect twice.

If your app doesn't disconnect properly, you may have to run "onep stop session APP" to kill it.

Does this help address your questions?

Highlighted
Beginner

Re: Exception/Error Handling

Few things:

(1) The error isn't coming from me printing out 'e' in the except statements.  For example, this error...

"ERROR:onep.element.NetworkElement:IDL Exception: ExceptionIDL(text='Connect: provide unique application name', code=11, context=8472)

Could not connect to 10.1.1.110.  Please validate reachability."

...is still displayed even with the code below.

(2) Fully understand and makes sense, but semi-not relevant for my application, IMHO.

(3) This is the issue at hand that I'm trying to find away around it.  It is also not a feasible workaround to login and issue the 'onep stop...' command.

To give more context, I am building a framework that uses the Python interpreter as a network CLI.  You enter the Python interpreter, connect to a device, gather some info, and may disconnect from the interpreter (ctrl+d) or just close the terminal.  A few mins later, you need to re-connect and do the same thing.  The device still sees that app as being connected and the error is thrown as shown above.  Even while this isn't ideal, I'd still like to know how to suppress that error.

As a possible workaround, I've even tried creating a new class per NE that uses a dynamic application name, but this doesn't work either.

So at the root of it all, how can 'myapp = NetworkApplication.get_instance()' return a different instance?  I'm still not sure why a different instance isn't reated if I'm creating a new class object for each new device.  How is an instance defined? 

As you can see, my application is more of a framework and not a "thick" app per se, so the singleton application instance is limiting the usability of what I'm trying to accomplish.  Does that make sense?

Code:

if not ne.is_connected():

  try:

  #print 'Trying to connect...'

  session_handle = ne.connect(self.username, self.password, session_config)

  #print 'Connection made to ' + self.address + ' successfully.'

  except  OnepDuplicateElementException, e:

     #print e

     existing = e.get_original_network_element()

     #print existing

     print 'Unable to connect to device.'

  except  Exception, e:

     #print e

     print 'Could not connect to ' + self.address + '.  Please validate reachability.'

     return 'DNE'

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

This does help, thanks.  But NetworkApplication is a singleton, and there is no way around that.  But I may be missing something.  If I do this:

shell$ python

Python 2.7.3 (default, Sep 26 2013, 20:03:06)

[GCC 4.6.3] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> onep_connect(dev, user, pass)

[dev : ok] getCPU

[dev : ok] CPU == 20%

Then I exit Python, the process running the "app" dies.  The app goes away.  If I restart the Python interpreter, I get a new process space, and thus I can create a new instance of the application.  Are you not doing this?  Do you have a long-running background app that one connects to?

Highlighted
Beginner

Re: Exception/Error Handling

That's right.  Were you able to re-connect the second time or did you get the error I get?  The router still thinks it has the first app connected and sees a new one with the *same* name connecting and the error (from above) is triggered.

ERROR:onep.element.NetworkElement:IDL Exception: ExceptionIDL(text='Connect: provide unique application name', code=11, context=4540) 

As a workaround to this problem, I've generated dynamic application names and it actually fixes this.  It would work fine if there was only one device in the network...

But then when I go to connect to a second device, I get a different error 'NameError: global name 'OnepInvalidSettingsException' is not defined' --- I believe this is because I'm trying to assign a different name to the same singleton (even though it's a new python class for the new device).

So, two things:

1. Any idea still how to suppress that error?

2. Any thoughts on how to accomplish what I'm looking for?

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

The "app" I showed was nothing more than a mock-up to help me get an understanding of what you were doing.  It sounds like now that you are getting a new instance of the app on each execution of the interpreter, but what might be happening is that the previous session wasn't properly terminated, so the NE thinks the app is still connected.  That's why dynamic app names work.  This sound right?

If so, have you looked at 27.9. atexit — Exit handlers — Python v2.7.6 documentation ?  Cleaning up on exit might be what you need here.  You might also try adjusting the onePK Session timers to make sure an app goes away after 60 seconds of idle time.

Highlighted
Beginner

Re: Exception/Error Handling

Understood it was a mock up.  It represented what I was asking nicely. 

I can check on the Exit handlers and timers, but please also take back the feedback on the notion of an application.  As I work with something like the eAPI form Arista, this is a non-issue.  I'm going to guess the NX-API is a non-issue because they are REST based.  So, ideally there is that kind of flexibility with onePK not worrying about application instances and duplicate names.

-Jason

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

Thanks for the feedback, Jason.  I will make sure our product marketing team sees this.  This is not the first report of issues with dup application names/sessions.  The workaround I mentioned previously came up because of this, and we have been looking to make things easier.

Highlighted
Beginner

Re: Exception/Error Handling

Can you please confirm which timer would ensure the application disconnects the from the device after a certain amount of time?  I am looking at the session_config timers and see KeepAliveIdleTime and a few others, but they don't seem to be what I'm looking for.  Can you confirm?

Thanks.

Highlighted
Hall of Fame Cisco Employee

Re: Exception/Error Handling

The reconnect timer is what you're looking for.  I imagine you have this at the default of zero already, which means that as soon as the device detects a loss of session, it will free the app resources.  But what's happening is that the NE is waiting for its own internal timers to give up.  That can be about four minutes (though we're working to improve that).  If your reconnect timer is already 0, then look at the atexit workaround to call ne.disconnect().

I have taken your feedback to our product team and developers.  There's a discussion going on now concerning a long term solution to this.

This widget could not be displayed.