Added "Up" attribute to ns3::Node in the ns-3 testbed, to enable/disable traffic
authorAlina Quereilhac <alina.quereilhac@inria.fr>
Sun, 18 Mar 2012 19:43:52 +0000 (20:43 +0100)
committerAlina Quereilhac <alina.quereilhac@inria.fr>
Sun, 18 Mar 2012 19:43:52 +0000 (20:43 +0100)
src/nepi/testbeds/ns3/attributes_metadata.py
src/nepi/testbeds/ns3/execute.py
src/nepi/testbeds/ns3/factories_metadata.py
src/nepi/testbeds/ns3/util.py [new file with mode: 0644]
test/testbeds/netns/execute.py

index a0c065a..1441bef 100644 (file)
@@ -2554,4 +2554,11 @@ attributes = dict({
         "flags" : Attribute.ExecImmutable | Attribute.Metadata,
         "validation_function" : validation.is_enum,
         }),
+    "Up" : dict({
+        "name" : "Up", 
+        "help" : "Flag to enable or disable interface",
+        "type" : Attribute.BOOL,
+        "value" : True,
+        "validation_function" : validation.is_integer,
+        }),
 })
index 9c60e05..6faf6da 100644 (file)
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+from util import  _get_ipv4_protocol_guid, _get_node_guid, _get_dev_number
 from nepi.core import testbed_impl
 from nepi.core.attributes import Attribute
 from constants import TESTBED_ID, TESTBED_VERSION
@@ -108,8 +109,22 @@ class TestbedController(testbed_impl.TestbedController):
         if factory_id in self.LOCAL_FACTORIES:
             setattr(element, name, value)
         elif not factory.box_attributes.is_attribute_metadata(name):
-            ns3_value = self._to_ns3_value(guid, name, value)
-            self._set_attribute(name, ns3_value, element)
+            if name == "Up":
+                ipv4_guid =  _get_ipv4_protocol_guid(self, guid)
+                if not ipv4_guid in self._elements:
+                    return
+                ipv4 = self._elements[ipv4_guid]
+                if value == False:
+                    nint = ipv4.GetNInterfaces()
+                    for i in xrange(0, nint):
+                        ipv4.SetDown(i)
+                else:
+                    nint = ipv4.GetNInterfaces()
+                    for i in xrange(0, nint):
+                        ipv4.SetUp(i)
+            else:
+                ns3_value = self._to_ns3_value(guid, name, value)
+                self._set_attribute(name, ns3_value, element)
 
     def get(self, guid, name, time = TIME_NOW):
         value = super(TestbedController, self).get(guid, name, time)
@@ -122,6 +137,19 @@ class TestbedController(testbed_impl.TestbedController):
                 return getattr(element, name)
             else:
                 return value
+        else: 
+            if name == "Up":
+                ipv4_guid =  _get_ipv4_protocol_guid(self, guid)
+                if not ipv4_guid in self._elements:
+                    return True
+                ipv4 = self._elements[ipv4_guid]
+                nint = ipv4.GetNInterfaces()
+                value = True
+                for i in xrange(0, nint):
+                    value = ipv4.IsUp(i)
+                    if not value: break
+                return value
+
         if factory.box_attributes.is_attribute_metadata(name):
             return value
 
@@ -136,6 +164,7 @@ class TestbedController(testbed_impl.TestbedController):
         self._get_attribute(name, ns3_value, element)
         value = ns3_value.SerializeToString(checker)
         attr_type = factory.box_attributes.get_attribute_type(name)
+
         if attr_type == Attribute.INTEGER:
             return int(value)
         if attr_type == Attribute.DOUBLE:
index 6f0d42f..7132e31 100644 (file)
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+from util import  _get_ipv4_protocol_guid, _get_node_guid, _get_dev_number
 from nepi.util import tags
 from nepi.util.constants import AF_INET, ApplicationStatus as AS, \
         FactoryCategories as FC
@@ -41,39 +42,6 @@ service_flow_scheduling_type = dict ({
     "SF_TYPE_ALL": 255
 })
 
-def _get_ipv4_protocol_guid(testbed_instance, node_guid):
-    # search for the Ipv4L3Protocol asociated with the device
-    protos_guids = testbed_instance.get_connected(node_guid, "protos", "node")
-    if len(protos_guids) == 0:
-        raise RuntimeError("No protocols where found for the node %d" % node_guid)
-    ipv4_guid = None
-    for proto_guid in protos_guids:
-        proto_factory_id = testbed_instance._create[proto_guid]
-        if proto_factory_id == "ns3::Ipv4L3Protocol":
-            ipv4_guid = proto_guid
-            break
-    if not ipv4_guid:
-        raise RuntimeError("No Ipv4L3Protocol associated to node %d. Can't add Ipv4 addresses" % node_guid)
-    return ipv4_guid
-
-def _get_node_guid(testbed_instance, guid):
-    # search for the node asociated with the device
-    node_guids = testbed_instance.get_connected(guid, "node", "devs")
-    if len(node_guids) == 0:
-        raise RuntimeError("Can't instantiate interface %d outside node" % guid)
-    node_guid = node_guids[0]
-    return node_guid
-
-def _get_dev_number(testbed_instance, guid):
-    node_guid = _get_node_guid(testbed_instance, guid)
-    dev_guids = testbed_instance.get_connected(node_guid, "devs", "node")
-    interface_number = 0
-    for guid_ in dev_guids:
-        if guid_ == guid:
-            break
-        interface_number += 1
-    return interface_number
-
 def _follow_trace(testbed_instance, guid, trace_id, filename):
     testbed_instance.follow_trace(guid, trace_id, filename)
     filepath = testbed_instance.trace_filepath(guid, trace_id)
@@ -895,6 +863,7 @@ factories_info = dict({
         "configure_function": configure_node,
         "help": "",
         "connector_types": ["devs", "apps", "protos", "mobility"],
+        "box_attributes": ["Up"],
         "tags": [tags.NODE, tags.ALLOW_ROUTES],
     }),
      "ns3::GridPositionAllocator": dict({
diff --git a/src/nepi/testbeds/ns3/util.py b/src/nepi/testbeds/ns3/util.py
new file mode 100644 (file)
index 0000000..a5ee731
--- /dev/null
@@ -0,0 +1,33 @@
+def _get_ipv4_protocol_guid(testbed_instance, node_guid):
+    # search for the Ipv4L3Protocol asociated with the device
+    protos_guids = testbed_instance.get_connected(node_guid, "protos", "node")
+    if len(protos_guids) == 0:
+        raise RuntimeError("No protocols where found for the node %d" % node_guid)
+    ipv4_guid = None
+    for proto_guid in protos_guids:
+        proto_factory_id = testbed_instance._create[proto_guid]
+        if proto_factory_id == "ns3::Ipv4L3Protocol":
+            ipv4_guid = proto_guid
+            break
+    if not ipv4_guid:
+        raise RuntimeError("No Ipv4L3Protocol associated to node %d. Can't add Ipv4 addresses" % node_guid)
+    return ipv4_guid
+
+def _get_node_guid(testbed_instance, guid):
+    # search for the node asociated with the device
+    node_guids = testbed_instance.get_connected(guid, "node", "devs")
+    if len(node_guids) == 0:
+        raise RuntimeError("Can't instantiate interface %d outside node" % guid)
+    node_guid = node_guids[0]
+    return node_guid
+
+def _get_dev_number(testbed_instance, guid):
+    node_guid = _get_node_guid(testbed_instance, guid)
+    dev_guids = testbed_instance.get_connected(node_guid, "devs", "node")
+    interface_number = 0
+    for guid_ in dev_guids:
+        if guid_ == guid:
+            break
+        interface_number += 1
+    return interface_number
+
index ec27e9e..1ab3fc2 100755 (executable)
@@ -109,6 +109,7 @@ class NetnsExecuteTestCase(unittest.TestCase):
         user = getpass.getuser()
         instance = netns.TestbedController()
         instance.defer_configure("homeDirectory", self.root_dir)
+        #instance.defer_configure("enableDebug", True)
         instance.defer_create(2, "Node")
         instance.defer_create(3, "Node")
         instance.defer_create(4, "Node")