To develop the Network Automation Projects utilized their tool like NS3. Here, we will walk through the steps to simulate a network automation scenario using NS3.
Steps to Develop Network Automation Projects Using NS3
- Introduction to Network Automation in NS3
The Network automation permits for dynamic setting, tracking, and control of networks using scripts, artificial intelligence, or centralized controllers. In NS3, network automation can be executed using:
✅ Software-Defined Networking (SDN)
✅ Machine Learning (ML) for network optimization
✅ Dynamic protocol adaptation (self-healing, load balancing, etc.)
✅ Automated Quality of Service (QoS) management
- Setting up NS-3 for Network Automation
(a) Install NS-3
sudo apt update
sudo apt install git g++ python3 python3-pip cmake
git clone
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Network Automation Approaches in NS-3
The Network automation in NS-3 can be completed by:
- SDN-Based Automation (OpenFlow Controllers).
- AI/ML-Based Automation (Neural Networks, Reinforcement Learning).
- Self-Healing Networks (Automated failure recovery).
- Traffic Load Balancing (Dynamic bandwidth allocation).
- Implementing SDN-Based Network Automation
(A) Using OpenFlow in NS-3
- The NS-3 assists the OpenFlow SDN through ns3::OpenFlowSwitchNetDevice.
- We build a SDN controller which dynamically setting the routing.
Example: OpenFlow SDN in NS-3
- State the SDN Controller (sdn-controller.h)
#ifndef SDN_CONTROLLER_H
#define SDN_CONTROLLER_H
#include “ns3/openflow-switch-net-device.h”
#include “ns3/openflow-module.h”
class SdnController : public ns3::ofi::Controller {
public:
static ns3::TypeId GetTypeId(void);
SdnController();
virtual void ReceiveFromSwitch(ns3::Ptr<ns3::ofi::Switch> swtch,
uint32_t portNo,
ns3::Ptr<ns3::Packet> packet);
};
#endif // SDN_CONTROLLER_H
- Execute the SDN Controller Logic (sdn-controller.cc)
#include “sdn-controller.h”
#include “ns3/log.h”
NS_LOG_COMPONENT_DEFINE(“SdnController”);
NS_OBJECT_ENSURE_REGISTERED(SdnController);
ns3::TypeId SdnController::GetTypeId(void) {
static ns3::TypeId tid = ns3::TypeId(“ns3::SdnController”)
.SetParent<ns3::ofi::Controller>()
.SetGroupName(“Network”);
return tid;
}
SdnController::SdnController() {}
void SdnController::ReceiveFromSwitch(ns3::Ptr<ns3::ofi::Switch> swtch,
uint32_t portNo,
ns3::Ptr<ns3::Packet> packet) {
NS_LOG_INFO(“SDN Controller received a packet”);
// Automate routing decisions dynamically
}
- Incorporate the SDN Controller in NS-3 Replication (sdn-simulation.cc)
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/openflow-module.h”
#include “sdn-controller.h”
using namespace ns3;
int main() {
NodeContainer switches, hosts;
switches.Create(2);
hosts.Create(4);
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“1Gbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“1ms”));
NetDeviceContainer switchDevices;
for (uint32_t i = 0; i < 2; i++) {
switchDevices.Add(p2p.Install(switches.Get(i), switches.Get((i+1) % 2)));
}
NetDeviceContainer hostDevices;
for (uint32_t i = 0; i < 4; i++) {
hostDevices.Add(p2p.Install(hosts.Get(i), switches.Get(i % 2)));
}
OpenFlowSwitchHelper ofSwitch;
Ptr<SdnController> controller = CreateObject<SdnController>();
ofSwitch.Install(switches, controller);
InternetStackHelper stack;
stack.Install(hosts);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
address.Assign(hostDevices);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Implement the process for a replication
./waf –run “scratch/sdn-simulation”
- Implementing AI/ML-Based Network Automation
(B) Using Reinforcement Learning for Traffic Optimization
- We test the RL agent to dynamically alter a bandwidth allocation.
- The AI models execute the process for outside NS-3 nevertheless interacts by using Python.
Example: AI-Based Network Traffic Optimization
- Train the RL Model in Python (rl-trainer.py)
import numpy as np
import tensorflow as tf
# Define RL environment for NS-3
class NetworkEnv:
def __init__(self):
self.bandwidth = 10 # Mbps
self.latency = 5 # ms
def step(self, action):
if action == 0:
self.bandwidth -= 1
else:
self.bandwidth += 1
reward = -self.latency / self.bandwidth
return np.array([self.bandwidth, self.latency]), reward
env = NetworkEnv()
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation=”relu”, input_shape=(2,)),
tf.keras.layers.Dense(2, activation=”softmax”)
])
model.compile(optimizer=”adam”, loss=”mse”)
for episode in range(100):
state = np.array([env.bandwidth, env.latency])
action = np.argmax(model.predict(state.reshape(1, -1)))
next_state, reward = env.step(action)
model.fit(state.reshape(1, -1), np.array([reward]), verbose=0)
model.save(“network_rl_model.h5”)
- The RL Model used in NS-3 (ai-network-simulation.cc)
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include <fstream>
using namespace ns3;
int main() {
std::ifstream modelFile(“network_rl_model.h5”);
if (!modelFile) {
NS_LOG_ERROR(“RL Model Not Found!”);
return 1;
}
NodeContainer nodes;
nodes.Create(4);
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“10Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“5ms”));
NetDeviceContainer devices;
devices = p2p.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“192.168.1.0”, “255.255.255.0”);
address.Assign(devices);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Estimate the AI-based on NS3 network
./waf –run “scratch/ai-network-simulation”
- Example Network Automation Project Ideas in NS-3
✅ 1. Self-Healing Networks
- Apply the automated failure recovery system which find and reroutes the broken connections.
✅ 2. AI-Optimized Load Balancing
- Test the AI model to balance congestion through several paths.
✅ 3. SDN-Based Dynamic Firewall
- Automate the firewall rule bring up-to-date in NS-3 SDN networks.
✅ 4. Predictive Network Fault Management
- We utilized the machine learning for earlier they happen in detect the network failures.
Finally, we had successfully delivered the significant procedures to simulate the Network Automation in ns3 tool and also we deliver the sample snippets and their explanation. More information will be shared about this process in another manual.