cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1594
Views
16
Helpful
6
Replies
Highlighted
Cisco Employee

Python: service notifications

Hi, I see there are examples to create service notifications in java but not in python, does someone have any experience or example in python to share?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Beginner

Hi Chenxi,

This is how you can do it:

csocket = socket.socket()
         try:
               ctx = dp.init_daemon('send-notif')
              # making a control socket
               dp.connect(dx=ctx, sock=csocket, type=dp.CONTROL_SOCKET, ip='127.0.0.1', port=4571)
              # getting all the required hashes
               ns_hash = _ncs.str2hash("http://cisco.barclays.com/rfs-notification")
               notif_name_hash = _ncs.str2hash('name')
               notif_id_hash = _ncs.str2hash('notif-id')
               mynotif_hash = _ncs.str2hash('my-notif')
               # making the notification
               message = []
               message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, mynotif_hash), _ncs.Value((mynotif_hash, ns_hash), _ncs.C_XMLBEGIN))]
               message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_name_hash), _ncs.Value('notif-name'))]
               message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_id_hash), _ncs.Value('notif-id'))]
               message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, mynotif_hash),
               _ncs.Value((mynotif_hash, ns_hash), _ncs.C_XMLEND))]

               # registering the stream
               livectx = dp.register_notification_stream(ctx, None, csocket, "mystream1")
              # time
               now = datetime.now(tzlocal())
               time = _ncs.DateTime(now.year, now.month,now.day, now.hour, now.minute,
               now.second, now.microsecond, now.timetz().hour,now.timetz().minute)

               # sending the notification

               dp.notification_send(livectx, time1, message)               
         except Exception as e:
               self.log.info("Exception : " + e.message)
         finally:
               csocket.close()

View solution in original post

6 REPLIES 6
Highlighted
Cisco Employee

Hi,

Could you please clarify what do you mean by "service notifications"? Maybe pointing to the Java examples?

Roque

Highlighted

About the service notification, for example, i have a service using rpc actions and reactive FASTMAP which may take a few minutes to deploy, i would like the service to send a notification in the end.

I saw the example below in java api documents, but no details are given in python api documents.

This class implements the Notification streams. The purpose of this class to provide a mechanism for sending notifications.

Example: Consider the following yang model of a notification:

module mynotif {

namespace "http://tail-f.com/test/mynotif/1.0";

prefix myn;

import ietf-yang-types { prefix yang; }

notification my_notif {

leaf arg1 { type string; }

leaf arg2 { type int64; }

}

}

For a netconf notification stream to be valid it must be defined in the ConfD/NCS config file. For NCS this example needs the following definitions in the config (there are small differences in the tagnames for ConfD)

<notifications>

<event-streams>

<stream>

<name>mystream</name>

<description>my test stream</description>

<replay-support>false</replay-support>

</stream>

</event-streams>

</notifications>

If we want to sent a NETCONF notification based on the above model we can do the following:

// create new control socket

Socket ctrlSocket = new Socket("127.0.0.1", Conf.PORT);

// This is the main Data Provider instance. "dp"

Dp dp = new Dp("hosts_daemon", ctrlSocket);

mynotif myn = new mynotif();

// create Dp Notification stream (you may create many)

DpNotifStream stream = dp.createNotifStream("mystream");

// send a notification

ConfXMLParam[] vals = new ConfXMLParam[] {

new ConfXMLParamStart(myn.hash(), mynotif.myn_my_notif),

new ConfXMLParamValue(myn.hash(), mynotif.myn_arg1, new ConfBuf("Hello")),

new ConfXMLParamValue(myn.hash(), mynotif.myn_arg2, new ConfInt64(32)),

new ConfXMLParamStop(myn.hash(), mynotif.myn_my_notif)

};

stream.send(ConfDatetime.getConfDatetime(), vals);

Highlighted

If I understand correctly you want to listen to Netconf notifications and act upon them? You can listen do device notifications, but first you have to configure the subscription which you can do via CLI.

"devices device myDevice netconf-notifications subscription status" - then you also have to specify the event you want to listen to and a local user. What you can do then is implementing a subscriber in Python which is listening to that subscription status and the logic how to act on changes. I believe it could be done this way.

BR, Simon

Highlighted

Hi Simon,

Thanks for your response. However, i am not trying to listen netconf notification from devices but create netconf notifications in nso services. Those notifications may inform service status and can be consumed by applications above NSO.

Chenxi

Highlighted
Beginner

Hi Chenxi,

This is how you can do it:

csocket = socket.socket()
         try:
               ctx = dp.init_daemon('send-notif')
              # making a control socket
               dp.connect(dx=ctx, sock=csocket, type=dp.CONTROL_SOCKET, ip='127.0.0.1', port=4571)
              # getting all the required hashes
               ns_hash = _ncs.str2hash("http://cisco.barclays.com/rfs-notification")
               notif_name_hash = _ncs.str2hash('name')
               notif_id_hash = _ncs.str2hash('notif-id')
               mynotif_hash = _ncs.str2hash('my-notif')
               # making the notification
               message = []
               message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, mynotif_hash), _ncs.Value((mynotif_hash, ns_hash), _ncs.C_XMLBEGIN))]
               message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_name_hash), _ncs.Value('notif-name'))]
               message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_id_hash), _ncs.Value('notif-id'))]
               message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, mynotif_hash),
               _ncs.Value((mynotif_hash, ns_hash), _ncs.C_XMLEND))]

               # registering the stream
               livectx = dp.register_notification_stream(ctx, None, csocket, "mystream1")
              # time
               now = datetime.now(tzlocal())
               time = _ncs.DateTime(now.year, now.month,now.day, now.hour, now.minute,
               now.second, now.microsecond, now.timetz().hour,now.timetz().minute)

               # sending the notification

               dp.notification_send(livectx, time1, message)               
         except Exception as e:
               self.log.info("Exception : " + e.message)
         finally:
               csocket.close()

View solution in original post

Highlighted

It works! Thanks a lot