Avoid changing value of a leaf, if active leafref is present referencing the key in the list

how to avoid changing a leaf, if the key of the list the leaf is present has an active leafref elsewhere. If there is no active leafref then the value must be allowed to change. How can i achieve this.

For example:

a.yang:

module moda {
    grouping a_gone {
        list a_lone {
            key a_lone;
            leaf a_lone {
                type string;
            }
            leaf a_ltwo {
                type uint32;
            }
        }
    }
    container a_cone {
        uses a_gone;
    }
}

b.yang:

module modb {
    import moda {
        prefix moda;
    }
    grouping b_gone {
        leaf b_lone {
            type uint32;
        }
        leaf b_ltwo {
            type leafref {
                path "moda:a_cone/moda:a_lone"
            }
        }
    }
    container b_cone {
        uses b_gone;
    }
}

Now, when a configuration $ b_lone b_ltwo <a_lone> is present, i want to block any changes to a_ltwo.

anyone has ideas on this ?

Hello,

i am not sure whether it can be easily addressed in the yang model only.
Maybe someone else can add their opinion.

One way to resolve is to add a validation callback.
In the validation, you can iterate list of changes that are attempted in the transaction - maapi_diff_iterate().
If you detect changes in “a_ltwo”, you can check the existing configuration for the leafref existence for the record being validated, and respond as needed to ConfD whether the changes are OK to be executed or not…

you can check the existing configuration for the leafref existence for the record being validated

how can i check this ? is there any available api for this ?
Is there any cdb api that i can use to check if the leafref is actually referenced in the config ?

In the yang model, you can define “tailf:validate” callpoint for specific section of model (in your case, that would be “a_ltwo”).

Inside the “struct confd_valpoint_cb” that you register to service the defined validation callpoint, you implement the “validate” callback - where you can call “maapi_diff_iterate()” to browse the list of changes.

For maapi_diff_itera() - you need to write iteration function with predefined input arguments.

Now, when a configuration $ b_lone b_ltwo is present, i want to block any changes to a_ltwo.

pseudo-code of your iteration function:

if (path-being-changed.matches("a_ltwo")) {
   use maapi_()... read procedures to check the existing config;
   if ("b_lone b_ltwo is present in config") {
       reject config - "fail" the validation process;
   }
}

When you hit the specific “rejection” criteria, you can “fail” the validation callback - to see how this mechanism works on a simple example in “examples.confd/validate” ConfD example package.

Actually, i want to know if there is any simple way of doing this following statement in your pesudocode.

if (“b_lone b_ltwo is present in config”)

Though, i simplified with just one config pointing at the leaf, in my case there can be multiple such configs.
So, i’m looking for simple way to check if the leafref is active for instance

if (leafref_is_active(“a_lone”))

i’m not aware of such “reverse” mapping - imho you may need to explicitly verify parts of the model where such a leafref’s might exist using some xpath reduction in C code.
Maybe someone else (@cohult / @per) has alternate ideas?

Well, I could elaborate a bit on the general issue, starting with saying that a leafref is not a “pointer” but a constraint on valid data. But perhaps a more relevant question is “what is the use case?” IMO the only interesting one is when a node that is a leafref target is changed such that the constraint is violated for one or more leafref nodes in the resulting config - and this is (of course) already handled by ConfD’s validation, which will reject the new config - no need for application code to deal with that.

Hi Per,

Thanks for your reply.

I think my requirement is lost in translation. I will explain again. In the following yang model, a_lone
can be referenced as leafref elsewhere. when there is an active leafref to a_lone, i want to block
modifications to a_ltwo. This is what i’m trying to achieve.

module moda {
grouping a_gone {
list a_lone {
key a_lone;
leaf a_lone {
type string;
}
leaf a_ltwo {
type uint32;
}
}
}
container a_cone {
uses a_gone;
}
}

by the way i’m surprised there is no api to achieve this req. This seems to be an easy one as the confd already has information that tells that there is an active reference to the leaf since it will not allow the
deletion of leaf if there is such reference. No one every had such use case ?