# NsOranEnv

The `NsOranEnv` class is an abstract base class for creating environments compliant with the Gymnasium framework, specifically designed for ns-O-RAN simulations. This class provides a structured way to initialize, configure, and manage simulations using the ns-3 network simulator, facilitating the integration of reinforcement learning algorithms with network simulations.

### Initialization
The class constructor initializes various attributes, including the paths to the ns-3 folder, the simulation scenario, configuration parameters, output folders, and control files. It also sets up the rendering mode, which defaults to `None` unless specified. Key boolean flags, such as `optimized` and `skip_configuration`, dictate whether the simulation runs in optimized mode and whether the configuration phase is skipped.

### Simulation Setup
The `setup_sim` method configures the simulation environment, setting library paths based on the optimization mode and initiating the configuration and build process for ns-3. This involves determining the correct executable for the simulation scenario and setting up environment variables necessary for running the simulation on different operating systems.

### Starting the Simulation
The `start_sim` method begins the simulation by creating necessary directories, initializing the Datalake and ActionController, and creating semaphores for inter-process communication. It then launches the ns-3 simulation with the appropriate parameters and sets up non-blocking I/O for capturing stdout and stderr streams.

### Interaction with the Environment
The `step` method executes a step in the simulation based on the provided action. It updates the simulation state, computes the reward, and handles the synchronization between the simulation process and the agent using semaphores. This method ensures the environment state is updated and actions are logged appropriately.

### Data Management
The `_init_datalake` method initializes tables in the Datalake specific to the use case, setting up necessary structures for storing and retrieving data effectively. This method is essential for preparing the Datalake to handle the incoming data from various sources throughout the simulation process.
The `_fill_datalake` method collects metrics from various CSV files generated by the simulation, updates the Datalake, and ensures the latest timestamp is tracked. This method is crucial for maintaining an accurate and up-to-date representation of the simulation state.

### Abstract Methods
Several abstract methods (`_compute_action`, `_get_obs`, `_compute_reward`, `_init_datalake`, `_fill_datalake_usecase`) must be implemented by subclasses to define specific actions, observations, rewards, and additional data handling according to the use case. These methods provide the flexibility to tailor the environment to different simulation scenarios and objectives.

### Utility Methods
- `read_streams`: Reads and processes output streams from the simulation.
- `is_simulation_over`: Checks if the simulation has finished and processes the remaining output.
- `_set_nonblocking`: Configures file descriptors to non-blocking mode to ensure non-blocking I/O operations.
- `reset`: Resets the environment, restarting the simulation and reinitializing necessary components.
- `render`: Provides a way to render the environment's state, primarily for debugging and visualization purposes.
- `close`: Terminates the simulation, releases resources, and cleans up semaphores.
- `__del__`: Ensures resources are released when the object is destroyed.

### Metadata and Attributes
The class defines metadata for rendering modes and various attributes related to the simulation setup, such as paths, configuration parameters, and control mechanisms. These attributes are essential for configuring and running the simulation, managing outputs, and handling inter-process communication.

Overall, the `NsOranEnv` class serves as a comprehensive framework for integrating ns-3 simulations with reinforcement learning environments, providing the necessary tools and methods to manage the entire lifecycle of a network simulation within the Gymnasium framework.
