Ns-3/linux tunnel experiments
[nepi.git] / src / nepi / resources / linux / tunnel.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 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.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow, tdiffsec
23
24 import os
25 import time
26
27 state_check_delay = 0.5
28
29 @clsinit_copy
30 class LinuxTunnel(LinuxApplication):
31     _rtype = "abstract::linux::Tunnel"
32     _help = "Constructs a tunnel between two Linux endpoints"
33
34     def __init__(self, ec, guid):
35         super(LinuxTunnel, self).__init__(ec, guid)
36         self._home = "tunnel-%s" % self.guid
37
38     def log_message(self, msg):
39         return " guid %d - tunnel %s - %s - %s " % (self.guid, 
40                 self.endpoint1.node.get("hostname"), 
41                 self.endpoint2.node.get("hostname"), 
42                 msg)
43
44     def get_endpoints(self):
45         """ Returns the list of RM that are endpoints to the tunnel 
46         """
47         raise NotImplementedError
48
49     @property
50     def endpoint1(self):
51         endpoints = self.get_endpoints()
52         if endpoints: return endpoints[0]
53         return None
54
55     @property
56     def endpoint2(self):
57         endpoints = self.get_endpoints()
58         if endpoints and len(endpoints) > 1: return endpoints[1]
59         return None
60
61     def app_home(self, endpoint):
62         return os.path.join(endpoint.node.exp_home, self._home)
63
64     def run_home(self, endpoint):
65         return os.path.join(self.app_home(endpoint), self.ec.run_id)
66
67     def endpoint_mkdir(self, endpoint):
68         self.endpoint.node.mkdir(self.run_home(self.endpoint))
69
70     def initiate_connection(self, endpoint, remote_endpoint):
71         raise NotImplementedError
72
73     def establish_connection(self, endpoint, remote_endpoint, data):
74         raise NotImplementedError
75
76     def verify_connection(self, endpoint, remote_endpoint):
77         raise NotImplementedError
78
79     def terminate_connection(self, endpoint, remote_endpoint):
80         raise NotImplementedError
81
82     def check_state_connection(self, endpoint, remote_endpoint):
83         raise NotImplementedError
84
85     def do_provision(self):
86         # create run dir for tunnel on each node 
87         self.endpoint_mkdir(self.endpoint1)
88         self.endpoint_mkdir(self.endpoint2)
89
90         self.debug("Initiate the connection")
91         # Start 2 step connection
92         # Initiate connection from endpoint 1 to endpoint 2
93         data1 = self.initiate_connection(self.endpoint1, self.endpoint2)
94
95         # Initiate connection from endpoint 2 to endpoint 1
96         data2 = self.initiate_connection(self.endpoint2, self.endpoint1)
97
98         self.debug("Establish the connection")
99         # Establish connection from endpoint 1 to endpoint 2
100         self.establish_connection(self.endpoint1, self.endpoint2, data2)
101         
102         # Establish connection from endpoint 2 to endpoint 1
103         self.establish_connection(self.endpoint2, self.endpoint1, data1)
104
105         self.debug("Verify the connection")
106         # check if connection was successful on both sides
107         self.verify_connection(self.endpoint1, self.endpoint2)
108         self.verify_connection(self.endpoint2, self.endpoint1)
109        
110         self.info("Provisioning finished")
111  
112         self.set_provisioned()
113
114     def do_deploy(self):
115         if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
116             (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
117             self.ec.schedule(self.reschedule_delay, self.deploy)
118         else:
119             self.do_discover()
120             self.do_provision()
121  
122             self.set_ready()
123
124     def do_start(self):
125         if self.state == ResourceState.READY:
126             command = self.get("command")
127             self.info("Starting command '%s'" % command)
128             
129             self.set_started()
130         else:
131             msg = " Failed to execute command '%s'" % command
132             self.error(msg, out, err)
133             raise RuntimeError, msg
134
135     def do_stop(self):
136         """ Stops application execution
137         """
138
139         if self.state == ResourceState.STARTED:
140             self.info("Stopping tunnel")
141
142             self.terminate_connection(self.endpoint1, self.endpoint2)
143             self.terminate_connection(self.endpoint2, self.endpoint1)
144
145             self.set_stopped()
146
147     @property
148     def state(self):
149         """ Returns the state of the application
150         """
151         if self._state == ResourceState.STARTED:
152             # In order to avoid overwhelming the remote host and
153             # the local processor with too many ssh queries, the state is only
154             # requested every 'state_check_delay' seconds.
155             if tdiffsec(tnow(), self._last_state_check) > state_check_delay:
156                 
157                 self.check_state_connection()
158
159                 self._last_state_check = tnow()
160
161         return self._state
162
163     def valid_connection(self, guid):
164         # TODO: Validate!
165         return True
166