#!/usr/bin/env python
#
# NEPI, a framework to manage network experiments
# Copyright (C) 2013 INRIA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Author: Alina Quereilhac
#
#
# This example must be executed as root:
# $ sudo PYTHONPATH=$PYTHONPATH:src python examples/netns/local_switch_ping.py
from __future__ import print_function
from nepi.execution.ec import ExperimentController
ec = ExperimentController(exp_id = "netns-local-p2p-ping")
# Simulation will executed in the local machine
node = ec.register_resource("linux::Node")
ec.set(node, "hostname", "localhost")
# Add an emulation resource
emu = ec.register_resource("linux::netns::Emulation")
ec.set(emu, "verbose", True)
ec.register_connection(emu, node)
# Add two emulated nodes
netnsnode1 = ec.register_resource("netns::Node")
ec.register_connection(netnsnode1, emu)
iface1 = ec.register_resource("netns::NodeInterface")
ec.register_connection(iface1, netnsnode1)
ip1 = ec.register_resource("netns::IPv4Address")
ec.set(ip1, "ip", "10.0.0.1")
ec.set(ip1, "prefix", "30")
ec.register_connection(ip1, iface1)
netnsnode2 = ec.register_resource("netns::Node")
ec.register_connection(netnsnode2, emu)
iface2 = ec.register_resource("netns::NodeInterface")
ec.register_connection(iface2, netnsnode2)
ip2 = ec.register_resource("netns::IPv4Address")
ec.set(ip2, "ip", "10.0.0.2")
ec.set(ip2, "prefix", "30")
ec.register_connection(ip2, iface2)
switch = ec.register_resource("netns::Switch")
ec.register_connection(iface1, switch)
ec.register_connection(iface2, switch)
ping = ec.register_resource("netns::Application")
ec.set(ping, "command", "ping -c20 10.0.0.2")
ec.register_connection(ping, netnsnode1)
ec.deploy()
ec.wait_finished([ping])
stdout = ec.trace(ping, "stdout")
ec.shutdown()
print("PING OUTPUT", stdout)