[nmstate] [RFC] Reorgnize code from dict based to object based

Hi Guys,

PR https://github.com/nmstate/nmstate/pull/354 has express a initial layout.
I want to expand that to discuss of how we reorganize our code:

 * `InterfaecBasic` contains IPv4, IPv6 and other stuff shared between

 * Specific interface type will have their own class like: Ethernet,
   OVS, VLAN, LinuxBridge,

 * All interface class will contains:

    * Constants for default values, key names.

    * canonicalize(iface_state, full_state)

      Do pre-apply check(raise exception) and tidy up(fill missing
      Passing full_state allows checking and fixing slaves.

    * self.verify(desire_iface_state, cur_iface_state)

      Do validation.

    * self.to_dict()

      Return the state of interface as dictionary

    * self.slaves()

      Return a list of slaves interface names.

    * IfaceClass.get(iface_name)

      Retrieve current status of this interface after canonicalize().

    * self.merge(current_iface_state)

      Merge settings from current state. If nothing special, use common
      merge function.

 * Each interface will have their own python file where it invoke
   backend calls.

 * The `netinfo.show()` should return a `NmstateState` object rather
   than a dictionary. This allows us to change the API without breaking
   backward compatibility. It contains:

    * self.to_dict()
        Just call interface.to_dict() and merge them.

    * self.to_json()
        Just dump dict as jon.

    * self.to_yaml()
        Just dump dict as yaml.

    * libnmstate.State.from_dict()
        * Merge with current state
        * Run canonicalize() on each interface objects.

    * libnmstate.State.from_json()
    * libnmstate.State.from_yaml()

    * self.get_iface_state(iface_name)
      Return iface state for certain interface. This is wildly use in
      integration test, so adding to public interface might helps.

    * libnmstate.State.get(iface_name=None, config_only=False)
        * Return a State object.
        * Internal steps:
            * Get a list of interface names and their type.
            * for iface_name, iface_type in ifaces_name_type:
                base_iface_state = InterfaecBasic.get(iface_name)
                specific_iface_state = iface_class_map[iface_type].get(
                iface_state = _merge_dict(base_iface_state,
                states.ifaces[iface_name] = iface_state
            * states.route = Route.get()
            * states.dns = DNS.get()

    * self.apply(timeout=30, manual_commit=True)
        * Apply and verify if required.

    * self._verify(current_state)
        * Invoke interface.verify() for each interface object.
        * Invoke route.verify()
        * Invoke dns.verify()

Gris Ge

Attachment: signature.asc
Description: PGP signature

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]