applied the except and raise fixers to the master branch to close the gap with py3
[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 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 import ipaddr
25
26 @clsinit_copy
27 class NS3Route(NS3Base):
28     _rtype = "ns3::Route"
29
30     @classmethod
31     def _register_attributes(cls):
32         network = Attribute("network", "Destination network address",
33                 flags = Flags.Design)
34
35         prefix = Attribute("prefix", "Network prefix for the network",
36                 flags = Flags.Design)
37
38         nexthop = Attribute("nexthop", "Address of next hop in the route",
39                 flags = Flags.Design)
40
41         cls._register_attribute(network)
42         cls._register_attribute(prefix)
43         cls._register_attribute(nexthop)
44
45     def __init__(self, ec, guid):
46         super(NS3Route, self).__init__(ec, guid)
47
48     @property
49     def node(self):
50         from nepi.resources.ns3.ns3node import NS3BaseNode
51         nodes = self.get_connected(NS3BaseNode.get_rtype())
52
53         if not nodes: 
54             msg = "Device not connected to node"
55             self.error(msg)
56             raise RuntimeError(msg)
57
58         return nodes[0]
59
60     @property
61     def _rms_to_wait(self):
62         # Wait for all network devices connected to the node to be ready
63         # before configuring the routes, else the route might refer to a
64         # non yet existing interface
65
66         rms = set()
67         rms.update(self.node.devices)
68         return rms
69
70     def _instantiate_object(self):
71         pass
72
73     def _configure_object(self):
74         network = self.get("network")
75         prefix = self.get("prefix")
76         nexthop = self.get("nexthop")
77         ipv4_uuid = self.node.ipv4.uuid
78
79         ret = self.simulation.invoke(ipv4_uuid, "addStaticRoute", network, 
80             prefix, nexthop)
81
82         if not ret: 
83             msg = "Could not configure route %s/%s hop: %s" % (network, prefix, 
84                     nexthop)
85             self.error(msg)
86             raise RuntimeError(msg)
87
88     def _connect_object(self):
89         node = self.node
90         if node and node.uuid not in self.connected:
91             self._connected.add(node.uuid)
92