Exemplo n.º 1
0
    def ApplyConnectivityChanges(self, context: ResourceCommandContext,
                                 request: str) -> str:
        """
        Create vlan and add or remove it to/from network interface.

        :param context: an object with all Resource Attributes inside
        :param str request: request json
        :return:
        """
        logger = LoggingSessionContext.get_logger_with_thread_id(context)
        api = CloudShellSessionContext(context).get_api()

        resource_config = NetworkingResourceConfig.from_context(
            shell_name=self.SHELL_NAME,
            supported_os=self.SUPPORTED_OS,
            context=context,
            api=api,
        )

        cli_handler = self._cli.get_cli_handler(resource_config, logger)
        connectivity_operations = ConnectivityFlow(
            logger=logger,
            cli_handler=cli_handler,
            support_vlan_range_str=False,
            support_multi_vlan_str=False,
        )
        logger.info("Start applying connectivity changes.")
        result = connectivity_operations.apply_connectivity_changes(
            request=request)
        logger.info("Apply Connectivity changes completed")
        return result
class TestCiscoRemoveVlanFlow(TestCase):
    def setUp(self):
        self._handler = CiscoConnectivityFlow(MagicMock(), MagicMock())

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions"
    )
    def test_execute_flow(self, iface_mock, vlan_actions_mock):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        vlan_id = "45"
        iface_mock.return_value.get_port_name.return_value = converted_port_name
        vlan_actions_mock.return_value.verify_interface_configured.return_value = False

        self._handler._remove_vlan_flow(vlan_id, port_name, port_mode)

        iface_obj_mock = iface_mock.return_value

        iface_obj_mock.get_port_name.assert_called_once_with(port_name)
        iface_obj_mock.get_current_interface_config.assert_called_with(
            converted_port_name)
        if_curconf_mock = iface_obj_mock.get_current_interface_config
        self.assertTrue(if_curconf_mock.call_count == 2)
        iface_obj_mock.enter_iface_config_mode.assert_called_once_with(
            converted_port_name)
        iface_obj_mock.clean_interface_switchport_config.assert_called_once()
        vlan_actions_mock.return_value.verify_interface_configured.assert_called_once_with(  # noqa: E501
            vlan_id, if_curconf_mock.return_value)
    def ApplyConnectivityChanges(self, context, request):
        """
        Create vlan and add or remove it to/from network interface

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param str request: request json
        :return:
        """

        logger = LoggingSessionContext.get_logger_with_thread_id(context)
        api = CloudShellSessionContext(context).get_api()

        resource_config = NetworkingResourceConfig.from_context(shell_name=self.SHELL_NAME,
                                                                supported_os=self.SUPPORTED_OS,
                                                                context=context, api=api)

        cli_handler = self._cli.get_cli_handler(resource_config, logger)
        connectivity_operations = ConnectivityFlow(logger=logger, cli_handler=cli_handler)
        logger.info('Start applying connectivity changes, request is: {0}'.format(str(request)))
        result = connectivity_operations.apply_connectivity_changes(request=request)
        logger.info('Finished applying connectivity changes, response is: {0}'.format(str(result)))
        logger.info('Apply Connectivity changes completed')
        return result
Exemplo n.º 4
0
class TestCiscoAddVlanFlow(TestCase):
    def setUp(self):
        self._handler = CiscoConnectivityFlow(MagicMock(), MagicMock())

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions"
    )
    def test_execute_flow(self, iface_mock, vlan_actions_mock):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        vlan_id = "45"
        qnq = False
        c_tag = ""
        iface_mock.return_value.get_port_name.return_value = converted_port_name

        self._handler._add_vlan_flow(vlan_id, port_mode, port_name, qnq, c_tag)
        iface_mock.return_value.get_port_name.assert_called_once_with(
            port_name)
        vlan_actions_mock.return_value.create_vlan.assert_called_once_with(
            vlan_id)
        iface_mock.return_value.get_current_interface_config.assert_called_with(
            converted_port_name)
        self.assertTrue(iface_mock.return_value.get_current_interface_config.
                        call_count == 2)
        iface_mock.return_value.enter_iface_config_mode.assert_called_once_with(
            converted_port_name)
        iface_mock.return_value.clean_interface_switchport_config.assert_called_once(
        )
        vlan_actions_mock.return_value.verify_interface_configured.assert_called_once(
        )
        vlan_actions_mock.return_value.set_vlan_to_interface.assert_called_once_with(
            vlan_id, port_mode, converted_port_name, qnq, c_tag)
Exemplo n.º 5
0
 def setUp(self):
     self._handler = CiscoConnectivityFlow(MagicMock(), MagicMock())
Exemplo n.º 6
0
class TestCiscoAddVlanFlow(TestCase):
    def setUp(self):
        self._handler = CiscoConnectivityFlow(MagicMock(), MagicMock())

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow."
        "CiscoConnectivityFlow._add_sub_interface_vlan"
    )
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow."
        "CiscoConnectivityFlow._add_switchport_vlan"
    )
    def test_add_vlan_router_flow(
        self, add_vlan_mock, add_sub_iface_mock, iface_mock, vlan_actions_mock
    ):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        vlan_id = "45"
        qnq = False
        c_tag = ""
        iface_mock.return_value.get_port_name.return_value = converted_port_name
        add_vlan_mock.side_effect = [CommandExecutionException("failed")]
        add_sub_iface_mock.return_value = "interface {}.{}".format(
            converted_port_name, vlan_id
        )

        self._handler._add_vlan_flow(vlan_id, port_mode, port_name, qnq, c_tag)

        iface_mock.return_value.get_port_name.assert_called_once_with(port_name)

        add_sub_iface_mock.assert_called_once_with(
            vlan_actions_mock.return_value,
            iface_mock.return_value,
            vlan_id,
            converted_port_name,
            port_mode,
            qnq,
            c_tag,
        )

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow."
        "CiscoConnectivityFlow._add_switchport_vlan"
    )
    def test_add_vlan_switch_flow(self, add_vlan_mock, iface_mock, vlan_actions_mock):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        response = MagicMock()
        converted_port_name = "Ethernet4/5"
        vlan_id = "45"
        qnq = False
        c_tag = ""
        iface_mock.return_value.get_port_name.return_value = converted_port_name
        add_vlan_mock.return_value = response

        self._handler._add_vlan_flow(vlan_id, port_mode, port_name, qnq, c_tag)
        iface_mock.return_value.get_port_name.assert_called_once_with(port_name)
        add_vlan_mock.assert_called_once_with(
            vlan_actions_mock.return_value,
            iface_mock.return_value,
            vlan_id,
            converted_port_name,
            port_mode,
            qnq,
            c_tag,
        )
        vlan_mock = vlan_actions_mock.return_value
        vlan_mock.verify_interface_has_vlan_assigned.assert_called_once_with(
            vlan_id, response
        )

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow."
        "CiscoConnectivityFlow._remove_sub_interface"
    )
    def test_remove_flow_router(self, rm_sub_iface_mock, iface_mock, vlan_actions_mock):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        converted_sub_port_name = "Ethernet4/5.45"
        vlan_id = "45"
        output = "ip address 10.0.0.0/24"
        iface_obj_mock = iface_mock.return_value
        iface_obj_mock.get_port_name.return_value = converted_port_name
        iface_obj_mock.check_sub_interface_has_vlan.return_value = False
        iface_obj_mock.get_current_interface_config.return_value = output
        iface_obj_mock.get_sub_interfaces_config.return_value = [
            converted_sub_port_name
        ]

        self._handler._remove_vlan_flow(vlan_id, port_name, port_mode)

        iface_obj_mock = iface_mock.return_value

        iface_obj_mock.get_port_name.assert_called_once_with(port_name)
        iface_obj_mock.get_current_interface_config.assert_called_with(
            converted_sub_port_name
        )

        rm_sub_iface_mock.assert_called_once_with(
            converted_sub_port_name, iface_obj_mock
        )

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    def test_remove_flow_switch(self, iface_mock, vlan_actions_mock):
        port_mode = "trunk"
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        vlan_id = "45"
        output = "switchport"
        result = "switchport allow vlan 45"
        iface_mock.return_value.get_port_name.return_value = converted_port_name
        vlan_mock = vlan_actions_mock.return_value
        vlan_mock.verify_interface_has_vlan_assigned.return_value = False
        iface_obj_mock = iface_mock.return_value
        iface_obj_mock.get_current_interface_config.side_effect = [output, result]

        self._handler._remove_vlan_flow(vlan_id, port_name, port_mode)

        iface_obj_mock.get_port_name.assert_called_once_with(port_name)
        iface_obj_mock.get_current_interface_config.assert_called_with(
            converted_port_name
        )
        iface_mock.return_value.enter_iface_config_mode.assert_called_once_with(
            converted_port_name
        )
        iface_obj_mock.clean_interface_switchport_config.assert_called_once()
        self.assertTrue(iface_obj_mock.get_current_interface_config.call_count == 2)

        iface_obj_mock.enter_iface_config_mode.assert_called_once_with(
            converted_port_name
        )
        iface_obj_mock.clean_interface_switchport_config.assert_called_once_with(output)
        vlan_mock = vlan_actions_mock.return_value
        vlan_mock.verify_interface_has_vlan_assigned.assert_called_once_with(
            vlan_id, result
        )

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow."
        "CiscoConnectivityFlow._remove_vlan_from_sub_interface"
    )
    def test_remove_all_flow_router(
        self, rm_sub_iface_mock, iface_mock, vlan_actions_mock
    ):
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        output = "ip address 10.0.0.0/24"

        iface_mock.return_value.get_port_name.return_value = converted_port_name
        vlan_actions_mock.return_value.verify_interface_configured.return_value = False
        iface_obj_mock = iface_mock.return_value
        iface_obj_mock.get_current_interface_config.return_value = output
        iface_obj_mock.get_sub_interfaces_config.return_value = [
            converted_port_name + ".45"
        ]

        self._handler._remove_all_vlan_flow(port_name)

        iface_obj_mock.get_port_name.assert_called_once_with(port_name)
        iface_obj_mock.get_current_interface_config.assert_called_once_with(
            converted_port_name
        )
        iface_obj_mock.get_sub_interfaces_config.assert_called_once()
        iface_obj_mock.check_sub_interface_has_vlan.assert_called_once()

        rm_sub_iface_mock.assert_called_once_with(converted_port_name, iface_obj_mock)

    @patch(
        "cloudshell.networking.cisco.flows.cisco_connectivity_flow.AddRemoveVlanActions"
    )
    @patch("cloudshell.networking.cisco.flows.cisco_connectivity_flow.IFaceActions")
    def test_remove_all_flow_switch(self, iface_mock, vlan_actions_mock):
        port_name = "Ethernet4-5"
        converted_port_name = "Ethernet4/5"
        output = "switchport"
        iface_mock.return_value.get_port_name.return_value = converted_port_name
        vlan_actions_mock.return_value.verify_interface_configured.return_value = False
        iface_obj_mock = iface_mock.return_value
        iface_obj_mock.get_current_interface_config.return_value = output

        self._handler._remove_all_vlan_flow(port_name)

        iface_obj_mock.get_port_name.assert_called_once_with(port_name)
        iface_obj_mock.get_current_interface_config.assert_called_with(
            converted_port_name
        )
        iface_mock.return_value.enter_iface_config_mode.assert_called_once_with(
            converted_port_name
        )
        iface_obj_mock.clean_interface_switchport_config.assert_called_once()
        self.assertTrue(iface_obj_mock.get_current_interface_config.call_count == 2)

        iface_obj_mock.enter_iface_config_mode.assert_called_once_with(
            converted_port_name
        )
        iface_obj_mock.clean_interface_switchport_config.assert_called_once_with(output)