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