Maapi.load_file equivalent in the latest confd version to load the operational data into operational databse

Looks like the latest versions of confd has no function maapi.load_file to load the operational data from an xml file.
Could you please share the equivalent function to load the operational xml file into operational database.
We are using the below code earlier

rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        cdb.connect(rsock, cdb.DATA_SOCKET, '127.0.0.1', _confd.CONFD_PORT, '/')
        cdb.start_session2(rsock, cdb.OPERATIONAL, 0)
        cdb.set_namespace(rsock, test_ns.ns.hash)
load_oper_data = cdb.load_file(rsock, file_path, 0)

Try something like:

import confd
import _confd
with confd.maapi.single_write_trans('admin', 'python_maapi', db=confd.OPERATIONAL) as t:
    try:
        root = confd.maagic.get_root(t)
        maapi = confd.maagic.get_maapi(root)
        _confd.maapi.load_config(
                               maapi.msock, t.th,
                               _confd.maapi.CONFIG_XML | _confd.maapi.CONFIG_MERGE,
                              file_path)
    except (_confd.error.Error) as exception:
        print(f'Error: {exception}')
    t.apply()

Thanks @cohult. I tried earlier without specifying _confd.maapi.CONFIG_MERGE and got internal error. If we use CONFIG_MERGE, the load works. But in the earlier versions with load_file, we were not specifying this flag. If I want to just load the xml file without merging the content, what is the best way?

From the confd_lib_maapi(3) man page under the maapi_load_config() C-function (the Python API uses the C API):

By default, the complete configuration (as allowed by the user of the current transaction) is deleted before the file is loaded. To merge the contents of the file, use the MAAPI_CONFIG_MERGE flag. To replace only the part of the configuration in the file, use the MAAPI_CONFIG_REPLACE flag.

So to replace, use the _confd.maapi.CONFIG_REPLACE flag. See the $CONFD_DIR/src/confd/pyapi/confd/__init__.py file to find out what the C-API flags are called with the Python API. Usually, the C-API “MAAPI_” part is removed for the Python API.

Thanks for the details. It helps

CONFIG_REPLACE flag is not present in the API documentation and also when I try with this flag, getting internal error.
This flag is showing only in this $CONFD_DIR/src/confd/pyapi/confd/init.py

Also while loading operational data, if the xml file contains config leaf, the transaction fails with object is not writable: error.
I didn’t find flags to exclude config data.

We were using the following command earlier to load the operational data.
confd_load -d -d -C file
But in the latest version, -C option is not supported.
Hence I tried an alternate way of loading operational with maapi.load_config with operational database, but getting the above exception.

Can you suggest the alternate approach for this.

CONFIG_REPLACE flag is not present in the API documentation and also when I try with this flag, getting internal error.

You must use the CONFIG_MERGE flag with operational data. That’s why you get the “internal error”. I should have informed you of that in my earlier reply. Use maapi.delete() to remove data before loading data.
As I wrote earlier in this thread/topic, see the confd_lib_maapi(3) man page under the maapi_load_config() C-function. The Python API uses the C-API.

As the C-API documentation instructs, use the CONFIG_OPER_ONLY flag that the confd_load -O option uses.

The CDB API load_file() function, together with that confd_load option, was removed in ConfD 7.3, released in 2019. Try:

confd_load -dd -m -O -p /path/to/list{I}want/to/replace -l file

ConfD ships with the source code for the confd_load tool. See the src/confd/tools/confd_load.c file if you are interested in, for example, what MAAPI flags are used with the different options.

Example:

$ cat dummy.yang
module dummy {
  namespace "http://com/example/dummy";
  prefix d;
  list dummy-cfg {
    key cfg-name;
    leaf cfg-name {
      type string;
    }
    list dummy-oper {
      config false;
      key oper-name;
      leaf oper-name {
        type string;
      }
    }
  }
}

$ confd_load -dd -o -Fp -p /dummy-cfg
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">
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ cat dummy.xml
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
    <dummy-oper>
      <oper-name>Owen</oper-name>
    </dummy-oper>
  </dummy-cfg>
</config>

$ confd_load -dd -o -m -l dummy.xml
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_LOAD_CONFIG_FILE  --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_load -dd -o -Fp -p /dummy-cfg
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
  </dummy-cfg>
</config>

$ confd_load -dd -O -m -l dummy.xml
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_LOAD_CONFIG_FILE  --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_load -dd -o -Fp -p /dummy-cfg
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
    <dummy-oper>
      <oper-name>Owen</oper-name>
    </dummy-oper>
  </dummy-cfg>
</config>

$ cat dummy2.xml
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
    <dummy-oper>
      <oper-name>Johan</oper-name>
    </dummy-oper>
  </dummy-cfg>
</config>

$ confd_load -dd -m -O -l dummy2.xml
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_LOAD_CONFIG_FILE  --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_load -dd -o -Fp -p /dummy-cfg
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
    <dummy-oper>
      <oper-name>Johan</oper-name>
    </dummy-oper>
    <dummy-oper>
      <oper-name>Owen</oper-name>
    </dummy-oper>
  </dummy-cfg>
</config>

$ confd_load -dd -m -O -p /dummy-cfg{Greta}/dummy-oper -l dummy2.xml
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_DELETE /dummy-cfg{Greta}/dummy-oper --> CONFD_OK
TRACE MAAPI_LOAD_CONFIG_FILE  --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_load -o -Fp -p /dummy-cfg
<config xmlns="http://tail-f.com/ns/config/1.0">
  <dummy-cfg xmlns="http://com/example/dummy">
    <cfg-name>Greta</cfg-name>
    <dummy-oper>
      <oper-name>Johan</oper-name>
    </dummy-oper>
  </dummy-cfg>
</config>