Files
PrimAITE/src/primaite/game/agent/actions/file.py

103 lines
3.1 KiB
Python
Raw Normal View History

# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
from typing import ClassVar
from primaite.game.agent.actions.manager import AbstractAction
from primaite.interface.request import RequestFormat
__all__ = (
"NodeFileCreateAction",
"NodeFileScanAction",
"NodeFileDeleteAction",
"NodeFileRestoreAction",
"NodeFileCorruptAction",
"NodeFileAccessAction",
)
class NodeFileAbstractAction(AbstractAction, identifier="node_file_abstract_action"):
"""Abstract base class for file actions.
2024-10-17 16:59:44 +01:00
Any action which applies to a file and uses node_name, folder_name, and file_name as its
only three parameters can inherit from this base class.
"""
class ConfigSchema(AbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration Schema for NodeFileAbstractAction."""
node_name: str
folder_name: str
file_name: str
verb: ClassVar[str]
@classmethod
def form_request(cls, config: ConfigSchema) -> RequestFormat:
"""Return the action formatted as a request which can be ingested by the PrimAITE simulation."""
if config.node_name is None or config.folder_name is None or config.file_name is None:
return ["do_nothing"]
return [
"network",
"node",
config.node_name,
"file_system",
"folder",
config.folder_name,
"file",
config.file_name,
cls.verb,
]
class NodeFileCreateAction(NodeFileAbstractAction, identifier="node_file_create"):
"""Action which creates a new file in a given folder."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileCreateAction."""
verb: str = "create"
class NodeFileScanAction(NodeFileAbstractAction, identifier="node_file_scan"):
"""Action which scans a file."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileScanAction."""
verb: str = "scan"
class NodeFileDeleteAction(NodeFileAbstractAction, identifier="node_file_delete"):
"""Action which deletes a file."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileDeleteAction."""
verb: str = "delete"
class NodeFileRestoreAction(NodeFileAbstractAction, identifier="node_file_restore"):
"""Action which restores a file."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileRestoreAction."""
verb: str = "restore"
class NodeFileCorruptAction(NodeFileAbstractAction, identifier="node_file_corrupt"):
"""Action which corrupts a file."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileCorruptAction."""
verb: str = "corrupt"
class NodeFileAccessAction(NodeFileAbstractAction, identifier="node_file_access"):
"""Action which increases a file's access count."""
class ConfigSchema(NodeFileAbstractAction.ConfigSchema):
2024-10-17 16:59:44 +01:00
"""Configuration schema for NodeFileAccessAction."""
verb: str = "access"