Ejemplo n.º 1
0
    def shutdown(self, context: ResourceCommandContext):
        """Shutdown device.

        :param context: an object with all Resource Attributes inside
        :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)
        state_operations = StateFlow(
            logger=logger,
            api=api,
            resource_config=resource_config,
            cli_configurator=cli_handler,
        )

        return state_operations.shutdown()
    def get_inventory(self, context):
        """Return device structure with all standard attributes

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :return: response
        :rtype: str
        """

        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)
        snmp_handler = SNMPHandler(resource_config, logger, cli_handler)

        autoload_operations = AutoloadFlow(logger=logger,
                                           snmp_handler=snmp_handler)
        logger.info('Autoload started')
        resource_model = NetworkingResourceModel(resource_config.name,
                                                 resource_config.shell_name,
                                                 resource_config.family_name)

        response = autoload_operations.discover(resource_config.supported_os, resource_model)
        logger.info('Autoload completed')
        return response
Ejemplo n.º 3
0
    def run_custom_command(
        self, context: ResourceCommandContext, custom_command: str
    ) -> str:
        """Send custom command."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Run Custom Command' command ...")
            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)

            send_command_operations = CiscoRunCommandFlow(
                logger=logger, cli_configurator=cli_handler
            )

            response = send_command_operations.run_custom_command(
                custom_command=custom_command
            )

            logger.info("'Run Custom Command' command completed")
            return response
Ejemplo n.º 4
0
    def initialize(self, context: InitCommandContext):
        resource_config = NetworkingResourceConfig.from_context(
            shell_name=self.SHELL_NAME, supported_os=self.SUPPORTED_OS, context=context
        )

        self._cli = CiscoNXOSCli(resource_config)
        return "Finished initializing"
Ejemplo n.º 5
0
    def run_custom_command(self, context: ResourceCommandContext,
                           custom_command: str) -> str:
        """Send custom command.

        :param custom_command: Command user wants to send to the device.
        :param context: an object with all Resource Attributes inside
        :return: result
        """
        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)
        send_command_operations = CommandFlow(logger=logger,
                                              cli_configurator=cli_handler)

        response = send_command_operations.run_custom_command(
            custom_command=custom_command)

        return response
Ejemplo n.º 6
0
    def ApplyConnectivityChanges(
        self, context: ResourceCommandContext, request: str
    ) -> str:
        """Create VLAN and add/remove it to/from network interface."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Apply Connectivity Changes' command ...")
            logger.info(f"Request: {request}")

            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 = CiscoNXOSConnectivityFlow(
                logger=logger, cli_handler=cli_handler
            )
            result = connectivity_operations.apply_connectivity_changes(request=request)

            logger.info(
                f"'Apply Connectivity Changes' command completed with result '{result}'"
            )
            return result
Ejemplo n.º 7
0
    def orchestration_save(
        self, context: ResourceCommandContext, mode: str, custom_params: str
    ) -> str:
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Orchestration Save' command ...")
            api = CloudShellSessionContext(context).get_api()

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

            mode = mode or "shallow"

            cli_handler = self._cli.get_cli_handler(resource_config, logger)
            configuration_flow = CiscoNXOSConfigurationFlow(
                cli_handler=cli_handler, logger=logger, resource_config=resource_config
            )

            response = configuration_flow.orchestration_save(
                mode=mode, custom_params=custom_params
            )

            response_json = OrchestrationSaveRestore(
                logger, resource_config.name
            ).prepare_orchestration_save_result(response)

            logger.info("'Orchestration Save' command completed")
            return response_json
Ejemplo n.º 8
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
    def load_firmware(self, context, path, vrf_management_name):
        """Upload and updates firmware on the resource

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param path: full path to firmware file, i.e. tftp://10.10.10.1/firmware.tar
        :param vrf_management_name: VRF management Name
        """

        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)

        if not vrf_management_name:
            vrf_management_name = resource_config.vrf_management_name

        cli_handler = self._cli.get_cli_handler(resource_config, logger)

        logger.info('Start Load Firmware')
        firmware_operations = FirmwareFlow(cli_handler=cli_handler,
                                           logger=logger)
        response = firmware_operations.load_firmware(path=path,
                                                     vrf_management_name=vrf_management_name)
        logger.info('Finish Load Firmware: {}'.format(response))
Ejemplo n.º 10
0
    def load_firmware(
        self, context: ResourceCommandContext, path: str, vrf_management_name: str
    ):
        """Upload and updates firmware on the resource."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Load Firmware' command ...")
            api = CloudShellSessionContext(context).get_api()

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

            vrf_management_name = (
                vrf_management_name or resource_config.vrf_management_name
            )
            cli_handler = self._cli.get_cli_handler(resource_config, logger)

            firmware_operations = CiscoLoadFirmwareFlow(
                cli_handler=cli_handler, logger=logger
            )
            firmware_operations.load_firmware(
                path=path, vrf_management_name=vrf_management_name
            )
            logger.info("'Load Firmware' command completed")
    def orchestration_save(self, context, mode, custom_params):
        """

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param mode: mode
        :param custom_params: json with custom save parameters
        :return str response: response json
        """

        if not mode:
            mode = 'shallow'

        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)
        configuration_flow = ConfigurationFlow(cli_handler=cli_handler,
                                               logger=logger,
                                               resource_config=resource_config)

        logger.info('Orchestration save started')
        response = configuration_flow.orchestration_save(mode=mode,
                                                         custom_params=custom_params)
        response_json = OrchestrationSaveRestore(logger, resource_config.name).prepare_orchestration_save_result(
            response)
        logger.info('Orchestration save completed')
        return response_json
    def orchestration_restore(self, context, saved_artifact_info, custom_params):
        """

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param saved_artifact_info: OrchestrationSavedArtifactInfo json
        :param custom_params: json with custom restore parameters
        """

        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)
        configuration_flow = ConfigurationFlow(cli_handler=cli_handler,
                                               logger=logger,
                                               resource_config=resource_config)

        logger.info('Orchestration restore started')
        restore_params = OrchestrationSaveRestore(logger, resource_config.name).parse_orchestration_save_result(
            saved_artifact_info)
        configuration_flow.restore(**restore_params)

        logger.info('Orchestration restore completed')
    def restore(self, context, path, configuration_type, restore_method, vrf_management_name):
        """Restore selected file to the provided destination

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param path: source config file
        :param configuration_type: running or startup configs
        :param restore_method: append or override methods
        :param vrf_management_name: VRF management Name
        """

        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)

        if not configuration_type:
            configuration_type = 'running'

        if not restore_method:
            restore_method = 'override'

        if not vrf_management_name:
            vrf_management_name = resource_config.vrf_management_name

        cli_handler = self._cli.get_cli_handler(resource_config, logger)
        configuration_flow = ConfigurationFlow(cli_handler=cli_handler,
                                               logger=logger,
                                               resource_config=resource_config)
        logger.info('Restore started')
        configuration_flow.restore(path=path, restore_method=restore_method,
                                   configuration_type=configuration_type,
                                   vrf_management_name=vrf_management_name)
        logger.info('Restore completed')
    def save(self, context, folder_path, configuration_type, vrf_management_name):
        """Save selected file to the provided destination

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :param configuration_type: source file, which will be saved
        :param folder_path: destination path where file will be saved
        :param vrf_management_name: VRF management Name
        :return str saved configuration file name:
        """

        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)

        if not configuration_type:
            configuration_type = 'running'

        if not vrf_management_name:
            vrf_management_name = resource_config.vrf_management_name

        cli_handler = self._cli.get_cli_handler(resource_config, logger)
        configuration_flow = ConfigurationFlow(cli_handler=cli_handler,
                                               logger=logger,
                                               resource_config=resource_config)
        logger.info('Save started')
        response = configuration_flow.save(folder_path=folder_path,
                                           configuration_type=configuration_type,
                                           vrf_management_name=vrf_management_name)
        logger.info('Save completed')
        return response
Ejemplo n.º 15
0
    def get_inventory(self, context: AutoLoadCommandContext) -> AutoLoadDetails:
        """Return device structure with all standard attributes."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Autoload' command ...")
            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)
            snmp_handler = CiscoSnmpHandler(resource_config, logger, cli_handler)
            autoload_operations = CiscoSnmpAutoloadFlow(
                logger=logger, snmp_handler=snmp_handler
            )

            resource_model = NetworkingResourceModel(
                resource_config.name,
                resource_config.shell_name,
                resource_config.family_name,
            )

            response = autoload_operations.discover(
                resource_config.supported_os, resource_model
            )
            logger.info("'Autoload' command completed")

            return response
Ejemplo n.º 16
0
    def health_check(self, context: ResourceCommandContext) -> str:
        """Performs device health check."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Health Check' command ...")

            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)

            state_operations = CiscoStateFlow(
                logger=logger,
                api=api,
                resource_config=resource_config,
                cli_configurator=cli_handler,
            )

            result = state_operations.health_check()
            logger.info(f"'Health Check' command completed with result '{result}'")

            return result
Ejemplo n.º 17
0
    def orchestration_restore(
        self,
        context: ResourceCommandContext,
        saved_artifact_info: str,
        custom_params: str,
    ):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Orchestration Restore' command ...")
            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)
            configuration_flow = CiscoNXOSConfigurationFlow(
                cli_handler=cli_handler, logger=logger, resource_config=resource_config
            )

            restore_params = OrchestrationSaveRestore(
                logger, resource_config.name
            ).parse_orchestration_save_result(saved_artifact_info)

            configuration_flow.restore(**restore_params)
            logger.info("'Orchestration Restore' command completed")
    def initialize(self, context):
        """Initialize method

        :type context: cloudshell.shell.core.context.driver_context.InitCommandContext
        """

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

        self._cli = CiscoCli(resource_config)
        return 'Finished initializing'
Ejemplo n.º 19
0
    def initialize(self, context: InitCommandContext) -> str:
        """Initialize method.

        :param context: an object with all Resource Attributes inside
        """
        resource_config = NetworkingResourceConfig.from_context(
            shell_name=self.SHELL_NAME,
            supported_os=self.SUPPORTED_OS,
            context=context)
        # In order to support vlan ranges on routers
        # "session_pool_timeout" has to be significantly increased

        self._cli = CiscoCli(resource_config, self.SESSION_POOL_TIMEOUT)
        return "Finished initializing"
    def health_check(self, context):
        """Performs device health check

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource Attributes inside
        :return: Success or Error message
        :rtype: str
        """

        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)

        state_operations = StateFlow(logger=logger,
                                     api=api,
                                     resource_config=resource_config,
                                     cli_configurator=cli_handler)
        return state_operations.health_check()
    def _setUp(self, attrs=None):
        if attrs is None:
            attrs = {}
        self.logger = MagicMock()
        self.api = MagicMock(DecryptPassword=lambda password: MagicMock(Value=password))

        self.resource_config = NetworkingResourceConfig.from_context(
            self.SHELL_NAME,
            self.create_context(attrs),
            self.api,
            self.SUPPORTED_OS,
        )
        self._cli = CLI(
            SessionPoolManager(
                max_pool_size=int(self.resource_config.sessions_concurrency_limit)
            )
        )

        self.cli_configurator = AristaCLIConfigurator(
            self.resource_config, self.api, self.logger, self._cli
        )
Ejemplo n.º 22
0
    def get_inventory(self,
                      context: AutoLoadCommandContext) -> AutoLoadDetails:
        """Create a resource structure."""
        with LoggingSessionContext(context):
            resource_config = NetworkingResourceConfig.from_context(
                self.SHELL_NAME, context)
            resource_model = NetworkingResourceModel.from_resource_config(
                resource_config)
            resource_model.system_name = "DUT device"

            chassis1 = resource_model.entities.Chassis(index=1)
            resource_model.connect_chassis(chassis1)

            module1 = resource_model.entities.Module(index=1)
            chassis1.connect_module(module1)

            port1 = resource_model.entities.Port(index=1)
            port2 = resource_model.entities.Port(index=2)
            module1.connect_port(port1)
            module1.connect_port(port2)

        return resource_model.build()
Ejemplo n.º 23
0
    def restore(
        self,
        context: ResourceCommandContext,
        path: str,
        configuration_type: str,
        restore_method: str,
        vrf_management_name: str,
    ):
        """Restore selected file to the provided destination."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Restore' command ...")
            api = CloudShellSessionContext(context).get_api()

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

            configuration_type = configuration_type or "running"
            restore_method = restore_method or "override"
            vrf_management_name = (
                vrf_management_name or resource_config.vrf_management_name
            )

            cli_handler = self._cli.get_cli_handler(resource_config, logger)
            configuration_flow = CiscoNXOSConfigurationFlow(
                cli_handler=cli_handler, logger=logger, resource_config=resource_config
            )

            configuration_flow.restore(
                path=path,
                restore_method=restore_method,
                configuration_type=configuration_type,
                vrf_management_name=vrf_management_name,
            )

            logger.info("'Restore' command completed")
    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
Ejemplo n.º 25
0
    def shutdown(self, context: ResourceCommandContext):
        """Shutdown device."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Shutdown' command ...")
            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)

            state_operations = CiscoStateFlow(
                logger=logger,
                api=api,
                resource_config=resource_config,
                cli_configurator=cli_handler,
            )

            state_operations.shutdown()
            logger.info("'Shutdown' command completed")
Ejemplo n.º 26
0
    def save(
        self,
        context: ResourceCommandContext,
        folder_path: str,
        configuration_type: str,
        vrf_management_name: str,
    ) -> str:
        """Save selected file to the provided destination."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting 'Save' command ...")
            api = CloudShellSessionContext(context).get_api()

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

            configuration_type = configuration_type or "running"
            vrf_management_name = (
                vrf_management_name or resource_config.vrf_management_name
            )

            cli_handler = self._cli.get_cli_handler(resource_config, logger)
            configuration_flow = CiscoNXOSConfigurationFlow(
                cli_handler=cli_handler, logger=logger, resource_config=resource_config
            )

            response = configuration_flow.save(
                folder_path=folder_path,
                configuration_type=configuration_type,
                vrf_management_name=vrf_management_name,
            )

            logger.info("'Save' command completed")
            return response