Making UdpTunnel inherite from abstract Tunnel RM
[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         reschedule_delay
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow, tdiffsec
24
25 import os
26 import time
27
28 state_check_delay = 0.5
29
30 @clsinit_copy
31 class LinuxTunnel(LinuxApplication):
32     _rtype = "abstract::LinuxTunnel"
33     _help = "Constructs a tunnel between two Linux endpoints"
34     _backend = "linux"
35
36     def __init__(self, ec, guid):
37         super(LinuxTunnel, self).__init__(ec, guid)
38         self._home = "tunnel-%s" % self.guid
39
40     def log_message(self, msg):
41         return " guid %d - tunnel %s - %s - %s " % (self.guid, 
42                 self.endpoint1.node.get("hostname"), 
43                 self.endpoint2.node.get("hostname"), 
44                 msg)
45
46     def get_endpoints(self):
47         """ Returns the list of RM that are endpoints to the tunnel 
48         """
49         connected = []
50         for guid in self.connections:
51             rm = self.ec.get_resource(guid)
52             if hasattr(rm, "udp_connect_command"):
53                 connected.append(rm)
54         return connected
55
56     @property
57     def endpoint1(self):
58         endpoints = self.get_endpoints()
59         if endpoints: return endpoints[0]
60         return None
61
62     @property
63     def endpoint2(self):
64         endpoints = self.get_endpoints()
65         if endpoints and len(endpoints) > 1: return endpoints[1]
66         return None
67
68     def app_home(self, endpoint):
69         return os.path.join(endpoint.node.exp_home, self._home)
70
71     def run_home(self, endpoint):
72         return os.path.join(self.app_home(endpoint), self.ec.run_id)
73
74     def initiate_connection(self, endpoint, remote_endpoint):
75         raise NotImplementedError
76
77     def establish_connection(self, endpoint, remote_endpoint, data):
78         raise NotImplementedError
79
80     def verify_connection(self, endpoint, remote_endpoint):
81         raise NotImplementedError
82
83     def terminate_connection(self, endpoint, remote_endpoint):
84         raise NotImplementedError
85
86     def check_state_connection(self, endpoint, remote_endpoint):
87         raise NotImplementedError
88
89     def do_provision(self):
90         # create run dir for tunnel on each node 
91         self.endpoint1.node.mkdir(self.run_home(self.endpoint1))
92         self.endpoint2.node.mkdir(self.run_home(self.endpoint2))
93
94         # Start 2 step connection
95         # Initiate connection from endpoint 1 to endpoint 2
96         data1 = self.initiate_connection(self.endpoint1, self.endpoint2)
97
98         # Initiate connection from endpoint 2 to endpoint 1
99         data2 = self.initiate_connection(self.endpoint2, self.endpoint1)
100
101         # Establish connection from endpoint 1 to endpoint 2
102         self.establish_connection(self.endpoint1, self.endpoint2, data2)
103         
104         # Establish connection from endpoint 2 to endpoint 1
105         self.establish_connection(self.endpoint2, self.endpoint1, data1)
106
107         # check if connection was successful on both sides
108         self.verify_connection(self.endpoint1, self.endpoint2)
109         self.verify_connection(self.endpoint2, self.endpoint1)
110        
111         self.info("Provisioning finished")
112  
113         self.set_provisioned()
114
115     def do_deploy(self):
116         if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
117             (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
118             self.ec.schedule(reschedule_delay, self.deploy)
119         else:
120             self.do_discover()
121             self.do_provision()
122  
123             self.set_ready()
124
125     def do_start(self):
126         if self.state == ResourceState.READY:
127             command = self.get("command")
128             self.info("Starting command '%s'" % command)
129             
130             self.set_started()
131         else:
132             msg = " Failed to execute command '%s'" % command
133             self.error(msg, out, err)
134             raise RuntimeError, msg
135
136     def do_stop(self):
137         """ Stops application execution
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
164     def valid_connection(self, guid):
165         # TODO: Validate!
166         return True
167