cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2478
Views
2
Helpful
2
Replies

Does anybody have an example of an action developed in python that calls upon a service?

gfreire
Level 1
Level 1

I am trying to code an action in python that grabs certain variables from devices and then creates a service based on those variables. I already have the service's yang and xml template produced and I can create the service manually. However, I'm having some trouble calling it via nso action. My action returns the output but doesn't seem to actually push anything into NSO.

I was wondering if someone has a project developed where this kind of scenario happens. I'd be much obliged if you could share it with me.

1 Accepted Solution

Accepted Solutions

Hiro Takahashi
Cisco Employee
Cisco Employee

Hi Gabriel,

Following is a sample code to create a new service instance from NSO action.

In this model, a new instance for a service model called "aclservice" is created with four variables (device, aclname, aclnum and aclentry).

class EnableAction(Action):

    @Action.action

    def cb_action(self, uinfo, name, kp, input, output):

        self.log.info('action name: ', name)

        # get instance name

        self.instance_name = str(kp).split("{")[1].split("}")[0]

        self.log.info('action key path: ', self.instance_name)

        self.uinfo=uinfo

        self.kp=kp

        with ncs.maapi.single_write_trans(self.uinfo.username, self.uinfo.context) as t:

            root = ncs.maagic.get_root(t)

            # get service path

            service = ncs.maagic.cd(root, self.kp)

            # create a new instance of aclservice

            aclservice = root.aclservice.create(self.instance_name)

            aclservice.device = service.device

            aclservice.aclname = service.aclname

            aclservice.aclnum = service.aclnum

            aclservice.aclentry = service.aclentry

            t.apply()

Hope this helps.

Best regards,

Hiro

View solution in original post

2 Replies 2

Hiro Takahashi
Cisco Employee
Cisco Employee

Hi Gabriel,

Following is a sample code to create a new service instance from NSO action.

In this model, a new instance for a service model called "aclservice" is created with four variables (device, aclname, aclnum and aclentry).

class EnableAction(Action):

    @Action.action

    def cb_action(self, uinfo, name, kp, input, output):

        self.log.info('action name: ', name)

        # get instance name

        self.instance_name = str(kp).split("{")[1].split("}")[0]

        self.log.info('action key path: ', self.instance_name)

        self.uinfo=uinfo

        self.kp=kp

        with ncs.maapi.single_write_trans(self.uinfo.username, self.uinfo.context) as t:

            root = ncs.maagic.get_root(t)

            # get service path

            service = ncs.maagic.cd(root, self.kp)

            # create a new instance of aclservice

            aclservice = root.aclservice.create(self.instance_name)

            aclservice.device = service.device

            aclservice.aclname = service.aclname

            aclservice.aclnum = service.aclnum

            aclservice.aclentry = service.aclentry

            t.apply()

Hope this helps.

Best regards,

Hiro

Thank you so much for the help, that was exactly what I was looking for! I adapted it a bit to my specific context and it's working perfectly.