README moves to markdown
[nepi.git] / nepi / resources / ns3 / ns3route.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags
20 from nepi.execution.resource import clsinit_copy
21 from nepi.execution.trace import Trace
22 from nepi.resources.ns3.ns3base import NS3Base
23
24 @clsinit_copy
25 class NS3Route(NS3Base):
26     _rtype = "ns3::Route"
27
28     @classmethod
29     def _register_attributes(cls):
30         network = Attribute("network", "Destination network address",
31                 flags = Flags.Design)
32
33         prefix = Attribute("prefix", "Network prefix for the network",
34                 flags = Flags.Design)
35
36         nexthop = Attribute("nexthop", "Address of next hop in the route",
37                 flags = Flags.Design)
38
39         cls._register_attribute(network)
40         cls._register_attribute(prefix)
41         cls._register_attribute(nexthop)
42
43     def __init__(self, ec, guid):
44         super(NS3Route, self).__init__(ec, guid)
45
46     @property
47     def node(self):
48         from nepi.resources.ns3.ns3node import NS3BaseNode
49         nodes = self.get_connected(NS3BaseNode.get_rtype())
50
51         if not nodes: 
52             msg = "Device not connected to node"
53             self.error(msg)
54             raise RuntimeError(msg)
55
56         return nodes[0]
57
58     @property
59     def _rms_to_wait(self):
60         # Wait for all network devices connected to the node to be ready
61         # before configuring the routes, else the route might refer to a
62         # non yet existing interface
63
64         rms = set()
65         rms.update(self.node.devices)
66         return rms
67
68     def _instantiate_object(self):
69         pass
70
71     def _configure_object(self):
72         network = self.get("network")
73         prefix = self.get("prefix")
74         nexthop = self.get("nexthop")
75         ipv4_uuid = self.node.ipv4.uuid
76
77         ret = self.simulation.invoke(ipv4_uuid, "addStaticRoute", network, 
78             prefix, nexthop)
79
80         if not ret: 
81             msg = "Could not configure route %s/%s hop: %s" % (network, prefix, 
82                     nexthop)
83             self.error(msg)
84             raise RuntimeError(msg)
85
86     def _connect_object(self):
87         node = self.node
88         if node and node.uuid not in self.connected:
89             self._connected.add(node.uuid)
90