Code to replace and delete single element in the list

  1. how to delete a single element in the list?
  2. how can we replace the element in the list if is already present in the list based on the order?
  1. config (config true) or operational state (config false) data?
  2. Application interfaces (e.g. MAAPI or CDB) or northbound interfaces, (e.g. NETCONF, RESTCONF, CLI, etc.)?

how config true will delete a element in the list, can you please share an example?

You didn’t answer the second question, but you did say “code” in the header for this topic. "code* and config true data would mean “MAAPI” to me, so based on the example in the RFC 7950:

  • 7.8.7. Usage Example (lists NETCONF)

Given the following list:

module example-config {
  yang-version 1.1;
  namespace "urn:example:config";
  prefix "co";

  revision 2020-06-14;
  
  list user {
    key "name";
    ordered-by user;
    description
      "This is a list of users in the system.";

    leaf name {
      type string;
    }
    leaf type {
      type string;
    }
    leaf full-name {
      type string;
    }
  }
}

With the following config:

  <config xmlns="http://tail-f.com/ns/config/1.0">
     <user xmlns="urn:example:config">
       <name>wilma</name>
       <type>admin</type>
       <full-name>Wilma Flintstone</full-name>
     </user>
     <user xmlns="urn:example:config">
       <name>barney</name>
       <type>admin</type>
       <full-name>Barney Rubble</full-name>
     </user>
     <user xmlns="urn:example:config">
       <name>fred</name>
       <type>admin</type>
       <full-name>Fred Flintstone</full-name>
     </user>
  </config>

To delete user "barney* using the MAAPI C-API:
maapi_delete(sock, tid, “/user{barney}”);

Using the confd_cmd tool that in turn use maapi_delete():

$ confd_cmd -dd -c 'mdel "/co:user{barney}"'
mdel "/co:user{barney}"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_DELETE /co:user{barney} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

See $CONFD_DIIR/src/confd/tools/confd_cmd.c for source code

To insert a list entry you first create it using the MAAPI C-API maapi_create(), then set the “full-name” using maapi_set(), and lastly move the new entry after “wilma” using maapi_move_ordered()

Example using confd_cmd that use the above maapi functions:

$ confd_cmd -dd -c 'mcreate "/co:user{betty}"; mset /co:user{betty}/full-name "Betty Rubble"; mset /co:user{betty}/type admin; mmove "/co:user" "{betty}" after "wilma"'
mcreate "/co:user{betty}" ; mset "/co:user{betty}/full-name" "Betty Rubble" ; mset "/co:user{betty}/type" "admin" ; mmove "/co:user" "{betty}" "after" "wilma"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_CREATE /co:user{betty} --> CONFD_OK
TRACE MAAPI_SET_ELEM2 /co:user{betty}/full-name --> CONFD_OK
TRACE MAAPI_SET_ELEM2 /co:user{betty}/type --> CONFD_OK
TRACE Connected (maapi) to ConfD
TRACE MAAPI_LOAD_ALL_NS
TRACE MAAPI_LOAD_MNS_MAPS
TRACE MAAPI_LOAD_HASH_DB
TRACE MAAPI_MOVE_ORDERED /co:user{betty} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK
$ confd_load -dd -Fp -p /co:user 
TRACE Connected (maapi) to ConfD
starting user session ctxt=system user=system groups=[system]
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_SAVE_CONFIG  --> CONFD_OK
TRACE Connected (stream) to ConfD
<config xmlns="http://tail-f.com/ns/config/1.0">
  <user xmlns="urn:example:config">
    <name>wilma</name>
    <type>admin</type>
    <full-name>Wilma Flintstone</full-name>
  </user>
  <user xmlns="urn:example:config">
    <name>betty</name>
    <type>admin</type>
    <full-name>Betty Rubble</full-name>
  </user>
  <user xmlns="urn:example:config">
    <name>fred</name>
    <type>admin</type>
    <full-name>Fred Flintstone</full-name>
  </user>
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK