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

How to access transaction parameters in @Service.pre_modification

ben_piret
Level 1
Level 1

Hello,

I would like to do a check in pre-modification.

For this check I would need to access some config parameters which are in the current transaction.

The problem is that the service and root object don't hold (yet) my parameters as we are before the apply() of the transaction.

I can only extract info from the keypath but I would need to obtain some parameters inside...

 

Coud you explain me how to do that in pre_modification ?

 

Kind regards.

 

Benoit

 ############## 

 

    def cb_pre_modification(selftctxopkprootproplist
        #self.log.info('Service premod(service=', kp, '), OP = ',op,'PROPLIST = ',proplist)
        if 'SWITCHAGGR' in str(kp) :
            if op == 0 : # check only on create/modify (0=create, 1=modify, 2=delete)
                with ncs.maapi.single_read_trans("admin""system"as t:
                    root = ncs.maagic.get_root(t)
                    info_from_kp = re.findall(r'{[A-Z0-9]+\.*[0-9]*}',str(kp))
                    keys_in_kp = [x.replace('{','').replace('}',''for x in info_from_kp]
                    service = ncs.maagic.cd(root, kp)
 
===>
<ERROR> 03-Feb-2021::09:43:25.209 l3vpn ncs-dp-21274-l3vpn:main-1-th-1084440: - Traceback (most recent call last):
File "/opt/ncs/ncs-5.4.1/src/ncs/pyapi/ncs/application.py", line 447, in wrapper
pl = fn(self, tctx, op, kp, root, proplist)
File "/var/opt/ncs/state/packages-in-use/1/l3vpn/python/l3vpn/main.py", line 1774, in cb_pre_modification
service = ncs.maagic.cd(root, kp)
File "/opt/ncs/ncs-5.4.1/src/ncs/pyapi/ncs/maagic.py", line 1808, in cd
node = node[child]
File "/opt/ncs/ncs-5.4.1/src/ncs/pyapi/ncs/maagic.py", line 1094, in __getitem__
raise KeyError('%s not in %s' % (keystr, self._path))
KeyError: '{FE9S4530MUT2.25980003} not in /l3vpn:l3vpn{DUMMYVRF}/link_type{SWITCHAGGR}/pelinks'
1 Accepted Solution

Accepted Solutions

Hi,

 

just to let know that this code s what I wanted  :

 

    @service.pre_modification
    def cb_pre_modification(selftctxopkprootproplist
        if 'SWITCHAGGR' in str(kp) :  ### pre_modification is only needed for 'SWITCHAGGR' in the keypath
            info_from_kp = re.findall(r'{[A-Z0-9]+\.*[0-9]*}',str(kp))
            keys_in_kp = [x.replace('{','').replace('}',''for x in info_from_kp]
            if op == 0 : # check only on create/modify (0=create, 1=modify, 2=delete)
                with ncs.maagic.get_trans(root) as t :   ### get_trans to have parameters in the current transaction
                    service = ncs.maagic.get_node(t, kp)
                    if len(keys_in_kp) == 3 :  
                        LINK_NAME = keys_in_kp[2]
                        vlan_from_service = service.wan_config.svlan  ### this depends on the yang model
                        try :
                            vlan_from_kp = int(keys_in_kp[2].split('.')[-1][:-4])
                        except :
                            raise ValueError("Wrong format for LINK_NAME {}".format(LINK_NAME))
 
                        if vlan_from_service != vlan_from_kp :
                            raise ValueError("your_error message_1"))
 
                        proceed_further = ### call
                        if proceed_further :
                            return proplist
                        else :
                            raise ValueError('your_error message_2'))

View solution in original post

2 Replies 2

ben_piret
Level 1
Level 1

I found a get_trans in ncs.maagic - NSO API Documentation - Document - Cisco DevNet

 

should it look like this ?

 

    @service.pre_modification
    def cb_pre_modification(selftctxopkprootproplist
        if 'SWITCHAGGR' in str(kp) :
            if op == 0 : # check only on create/modify (0=create, 1=modify, 2=delete)
                with ncs.maagic.get_trans(root) as t :
                    info_from_kp = re.findall(r'{[A-Z0-9]+\.*[0-9]*}',str(kp))
                    keys_in_kp = [x.replace('{','').replace('}',''for x in info_from_kp]
                    service = ncs.maagic.get_node(t, kp)

 

Hi,

 

just to let know that this code s what I wanted  :

 

    @service.pre_modification
    def cb_pre_modification(selftctxopkprootproplist
        if 'SWITCHAGGR' in str(kp) :  ### pre_modification is only needed for 'SWITCHAGGR' in the keypath
            info_from_kp = re.findall(r'{[A-Z0-9]+\.*[0-9]*}',str(kp))
            keys_in_kp = [x.replace('{','').replace('}',''for x in info_from_kp]
            if op == 0 : # check only on create/modify (0=create, 1=modify, 2=delete)
                with ncs.maagic.get_trans(root) as t :   ### get_trans to have parameters in the current transaction
                    service = ncs.maagic.get_node(t, kp)
                    if len(keys_in_kp) == 3 :  
                        LINK_NAME = keys_in_kp[2]
                        vlan_from_service = service.wan_config.svlan  ### this depends on the yang model
                        try :
                            vlan_from_kp = int(keys_in_kp[2].split('.')[-1][:-4])
                        except :
                            raise ValueError("Wrong format for LINK_NAME {}".format(LINK_NAME))
 
                        if vlan_from_service != vlan_from_kp :
                            raise ValueError("your_error message_1"))
 
                        proceed_further = ### call
                        if proceed_further :
                            return proplist
                        else :
                            raise ValueError('your_error message_2'))