Ejemplo n.º 1
0
    def default_enabled(self, want=None, have=None, action=""):
        # 'enabled' default state depends on the interface type and L2 state.
        # Note that the current default could change when changing L2/L3 modes.
        if self.state == "rendered":
            # For "rendered", we always assume that
            # the default enabled state is False
            return False
        if want is None:
            want = {}
        if have is None:
            have = {}
        name = have.get("name")
        if name is None:
            return None

        sysdefs = self.intf_defs["sysdefs"]
        sysdef_mode = sysdefs["mode"]

        # Get the default enabled state for this interface. This was collected
        # during Facts gathering.
        intf_def_enabled = self.intf_defs.get(name)

        have_mode = have.get("mode", sysdef_mode)
        if action == "delete" and not want:
            want_mode = sysdef_mode
        else:
            want_mode = want.get("mode", have_mode)
        if (
            (want_mode and have_mode) is None
            or (want_mode != have_mode)
            or intf_def_enabled is None
        ):
            # L2-L3 is changing or this is a new virtual intf. Get new default.
            intf_def_enabled = default_intf_enabled(name=name, sysdefs=sysdefs, mode=want_mode)
        return intf_def_enabled
Ejemplo n.º 2
0
    def default_enabled(self, want=None, have=None, action=''):
        # 'enabled' default state depends on the interface type and L2 state.
        # Note that the current default could change when changing L2/L3 modes.
        if want is None:
            want = {}
        if have is None:
            have = {}
        name = have.get('name')
        if name is None:
            return None

        sysdefs = self.intf_defs['sysdefs']
        sysdef_mode = sysdefs['mode']

        # Get the default enabled state for this interface. This was collected
        # during Facts gathering.
        intf_def_enabled = self.intf_defs.get(name)

        have_mode = have.get('mode', sysdef_mode)
        if action == 'delete' and not want:
            want_mode = sysdef_mode
        else:
            want_mode = want.get('mode', have_mode)
        if (want_mode and have_mode) is None or (want_mode != have_mode) or intf_def_enabled is None:
            # L2-L3 is changing or this is a new virtual intf. Get new default.
            intf_def_enabled = default_intf_enabled(name=name, sysdefs=sysdefs, mode=want_mode)

        return intf_def_enabled
Ejemplo n.º 3
0
    def render_interface_defaults(self, config, intfs):
        """Collect user-defined-default states for 'system default switchport'
        configurations. These configurations determine default L2/L3 modes
        and enabled/shutdown states. The default values for user-defined-default
        configurations may be different for legacy platforms.
        Notes:
        - L3 enabled default state is False on N9K,N7K but True for N3K,N6K
        - Changing L2-L3 modes may change the default enabled value.
        - '(no) system default switchport shutdown' only applies to L2 interfaces.
        Run through the gathered interfaces and tag their default enabled state.
        """
        intf_defs = {}
        L3_enabled = (
            True if re.search("N[356]K", self.get_platform()) else False
        )
        intf_defs = {
            "sysdefs": {
                "mode": None,
                "L2_enabled": None,
                "L3_enabled": L3_enabled,
            }
        }
        pat = "(no )*system default switchport$"
        m = re.search(pat, config, re.MULTILINE)
        if m:
            intf_defs["sysdefs"]["mode"] = (
                "layer3" if "no " in m.groups() else "layer2"
            )

        pat = "(no )*system default switchport shutdown$"
        m = re.search(pat, config, re.MULTILINE)
        if m:
            intf_defs["sysdefs"]["L2_enabled"] = (
                True if "no " in m.groups() else False
            )

        for item in intfs:
            intf_defs[item["name"]] = default_intf_enabled(
                name=item["name"],
                sysdefs=intf_defs["sysdefs"],
                mode=item.get("mode"),
            )

        return intf_defs
Ejemplo n.º 4
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys
          from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)

        match = re.search(r"^(\S+)", conf)
        intf = match.group(1)
        if get_interface_type(intf) == "unknown":
            return {}
        config["name"] = intf
        config["description"] = utils.parse_conf_arg(conf, "description")
        config["speed"] = utils.parse_conf_arg(conf, "speed")
        config["mtu"] = utils.parse_conf_arg(conf, "mtu")
        config["duplex"] = utils.parse_conf_arg(conf, "duplex")
        config["mode"] = utils.parse_conf_cmd_arg(
            conf, "switchport", "layer2", "layer3"
        )

        config["enabled"] = utils.parse_conf_cmd_arg(
            conf, "shutdown", False, True
        )

        # Capture the default 'enabled' state, which may be interface-specific
        config["enabled_def"] = default_intf_enabled(
            name=intf, sysdefs=self.sysdefs, mode=config["mode"]
        )

        config["fabric_forwarding_anycast_gateway"] = utils.parse_conf_cmd_arg(
            conf, "fabric forwarding mode anycast-gateway", True
        )
        config["ip_forward"] = utils.parse_conf_cmd_arg(
            conf, "ip forward", True
        )

        interfaces_cfg = utils.remove_empties(config)
        return interfaces_cfg
Ejemplo n.º 5
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys
          from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)

        match = re.search(r'^(\S+)', conf)
        intf = match.group(1)
        if get_interface_type(intf) == 'unknown':
            return {}
        config['name'] = intf
        config['description'] = utils.parse_conf_arg(conf, 'description')
        config['speed'] = utils.parse_conf_arg(conf, 'speed')
        config['mtu'] = utils.parse_conf_arg(conf, 'mtu')
        config['duplex'] = utils.parse_conf_arg(conf, 'duplex')
        config['mode'] = utils.parse_conf_cmd_arg(conf, 'switchport', 'layer2',
                                                  'layer3')

        config['enabled'] = utils.parse_conf_cmd_arg(conf, 'shutdown', False,
                                                     True)

        # Capture the default 'enabled' state, which may be interface-specific
        config['enabled_def'] = default_intf_enabled(name=intf,
                                                     sysdefs=self.sysdefs,
                                                     mode=config['mode'])

        config['fabric_forwarding_anycast_gateway'] = utils.parse_conf_cmd_arg(
            conf, 'fabric forwarding mode anycast-gateway', True)
        config['ip_forward'] = utils.parse_conf_cmd_arg(
            conf, 'ip forward', True)

        interfaces_cfg = utils.remove_empties(config)
        return interfaces_cfg