Search code examples
modelconstraintsopendaylightietf-netmod-yang

YANG - leafref seems not to be working


I would appreciate if a more yang experienced person could help me. I am triying to apply a constraint reference between 2 different lists, in the next way:

list company{
    key company-id;
    leaf company-id {
        type yang:uuid;
    }
}

list employee {
   key employee-id;
   leaf employee-id {
       type yang:uuid;
    }
    leaf company {
     type leafref {
         path "/company/company-id"; 
     }
  }
}

I am performing some tests with RESTCONF api, and I would like to check if it is possible to avoid a POST command, in order to create a new employee, if its related company is not created. I mean, I would like to use a yang model and include a referential constraint between the 2 lists.

I have tried using "must" statement as follows:

must "boolean(/company[company-id=current()]"

But there was no luck with that attempt.

Thanks in advance. Best regards


Solution

  • I have found the way to make it work. If you want that one list of your model has a foreign key reference with other second list, you have to include the field of type leafref in the key of the list. Using the example of my previous question:

    Having the next list:

    list company{
        key company-id;
        leaf company-id {
            type yang:uuid;
        }
    }
    

    If you want to include in the model other list, which will depend of company-id, you have to declare it with the next key:

    list employee {   
      key "employee-id company";    
    
       leaf employee-id {
           type yang:uuid;
        }
        leaf company {
         type leafref {
             path "/company/company-id"; 
            } 
       }
     }
    

    Pay attention to the key of the second list:

    key "employee-id company";

    I have tested it, and its working properly.

    Thanks anyway to the people that have read my question.

    Cheers, Julián