Changing reschedule_delay internals
[nepi.git] / src / nepi / resources / linux / ccn / fibentry.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.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import clsinit_copy, ResourceState, \
23     ResourceAction
24 from nepi.resources.linux.application import LinuxApplication
25 from nepi.resources.linux.ccn.ccnd import LinuxCCND
26 from nepi.util.timefuncs import tnow
27
28 import os
29 import socket
30 import time
31
32
33 # TODO: Add rest of options for ccndc!!!
34 #       Implement ENTRY DELETE!!
35
36 @clsinit_copy
37 class LinuxFIBEntry(LinuxApplication):
38     _rtype = "LinuxFIBEntry"
39
40     @classmethod
41     def _register_attributes(cls):
42         uri = Attribute("uri",
43                 "URI prefix to match and route for this FIB entry",
44                 default = "ccnx:/",
45                 flags = Flags.Design)
46
47         protocol = Attribute("protocol",
48                 "Transport protocol used in network connection to peer "
49                 "for this FIB entry. One of 'udp' or 'tcp'.",
50                 type = Types.Enumerate, 
51                 default = "udp",
52                 allowed = ["udp", "tcp"],
53                 flags = Flags.Design)
54
55         host = Attribute("host",
56                 "Peer hostname used in network connection for this FIB entry. ",
57                 flags = Flags.Design)
58
59         port = Attribute("port",
60                 "Peer port address used in network connection to peer "
61                 "for this FIB entry.",
62                 flags = Flags.Design)
63
64         ip = Attribute("ip",
65                 "Peer host public IP used in network connection for this FIB entry. ",
66                 flags = Flags.Design)
67
68         cls._register_attribute(uri)
69         cls._register_attribute(protocol)
70         cls._register_attribute(host)
71         cls._register_attribute(port)
72         cls._register_attribute(ip)
73
74     @classmethod
75     def _register_traces(cls):
76         ping = Trace("ping", "Ping to the peer end")
77         mtr = Trace("mtr", "Mtr to the peer end")
78         traceroute = Trace("traceroute", "Tracerout to the peer end")
79
80         cls._register_trace(ping)
81         cls._register_trace(mtr)
82         cls._register_trace(traceroute)
83
84     def __init__(self, ec, guid):
85         super(LinuxFIBEntry, self).__init__(ec, guid)
86         self._home = "fib-%s" % self.guid
87         self._ping = None
88         self._traceroute = None
89         self._ccnd = None
90
91     @property
92     def ccnd(self):
93         if not self._ccnd:
94             ccnd = self.get_connected(LinuxCCND.get_rtype())
95             if ccnd: 
96                 self._ccnd = ccnd[0]
97             
98         return self._ccnd
99
100     @property
101     def ping(self):
102         if not self._ping:
103             from nepi.resources.linux.ping import LinuxPing
104             ping = self.get_connected(LinuxPing.get_rtype())
105             if ping: 
106                 self._ping = ping[0]
107             
108         return self._ping
109
110     @property
111     def traceroute(self):
112         if not self._traceroute:
113             from nepi.resources.linux.traceroute import LinuxTraceroute
114             traceroute = self.get_connected(LinuxTraceroute.get_rtype())
115             if traceroute: 
116                 self._traceroute = traceroute[0]
117             
118         return self._traceroute
119
120     @property
121     def node(self):
122         if self.ccnd: return self.ccnd.node
123         return None
124
125     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
126         if name == "ping":
127             if not self.ping:
128                 return None
129             return self.ec.trace(self.ping.guid, "stdout", attr, block, offset)
130
131         if name == "traceroute":
132             if not self.traceroute:
133                 return None
134             return self.ec.trace(self.traceroute.guid, "stdout", attr, block, offset)
135
136         return super(LinuxFIBEntry, self).trace(name, attr, block, offset)
137     
138     def do_deploy(self):
139         # Wait until associated ccnd is provisioned
140         if not self.ccnd or self.ccnd.state < ResourceState.READY:
141             # ccnr needs to wait until ccnd is deployed and running
142             self.ec.schedule(self.reschedule_delay, self.deploy)
143         else:
144             if not self.get("ip"):
145                 host = self.get("host")
146                 ip = socket.gethostbyname(host)
147                 self.set("ip", ip)
148
149             if not self.get("command"):
150                 self.set("command", self._start_command)
151
152             if not self.get("env"):
153                 self.set("env", self._environment)
154
155             command = self.get("command")
156
157             self.info("Deploying command '%s' " % command)
158
159             self.do_discover()
160             self.do_provision()
161             self.configure()
162
163             self.set_ready()
164
165     def upload_start_command(self):
166         command = self.get("command")
167         env = self.get("env")
168
169         # We want to make sure the FIB entries are created
170         # before the experiment starts.
171         # Run the command as a bash script in the background, 
172         # in the host ( but wait until the command has
173         # finished to continue )
174         env = env and self.replace_paths(env)
175         command = self.replace_paths(command)
176
177         # ccndc seems to return exitcode OK even if a (dns) error
178         # occurred, so we need to account for this case here. 
179         (out, err), proc = self.execute_command(command, 
180                 env, blocking = True)
181
182         if proc.poll():
183             msg = "Failed to execute command"
184             self.error(msg, out, err)
185             raise RuntimeError, msg
186         
187     def configure(self):
188         if self.trace_enabled("ping") and not self.ping:
189             self.info("Configuring PING trace")
190             ping = self.ec.register_resource("LinuxPing")
191             self.ec.set(ping, "printTimestamp", True)
192             self.ec.set(ping, "target", self.get("host"))
193             self.ec.set(ping, "earlyStart", True)
194             self.ec.register_connection(ping, self.node.guid)
195             self.ec.register_connection(ping, self.guid)
196             # schedule ping deploy
197             self.ec.deploy(guids=[ping], group = self.deployment_group)
198
199         if self.trace_enabled("traceroute") and not self.traceroute:
200             self.info("Configuring TRACEROUTE trace")
201             traceroute = self.ec.register_resource("LinuxTraceroute")
202             self.ec.set(traceroute, "printTimestamp", True)
203             self.ec.set(traceroute, "continuous", True)
204             self.ec.set(traceroute, "target", self.get("host"))
205             self.ec.set(traceroute, "earlyStart", True)
206             self.ec.register_connection(traceroute, self.node.guid)
207             self.ec.register_connection(traceroute, self.guid)
208             # schedule mtr deploy
209             self.ec.deploy(guids=[traceroute], group = self.deployment_group)
210
211     def do_start(self):
212         if self.state == ResourceState.READY:
213             command = self.get("command")
214             self.info("Starting command '%s'" % command)
215
216             self.set_started()
217         else:
218             msg = " Failed to execute command '%s'" % command
219             self.error(msg, out, err)
220             raise RuntimeError, msg
221
222     def do_stop(self):
223         command = self.get('command')
224         env = self.get('env')
225         
226         if self.state == ResourceState.STARTED:
227             self.info("Stopping command '%s'" % command)
228
229             command = self._stop_command
230             (out, err), proc = self.execute_command(command, env,
231                     blocking = True)
232
233             self.set_stopped()
234
235             if err:
236                 msg = " Failed to execute command '%s'" % command
237                 self.error(msg, out, err)
238                 raise RuntimeError, msg
239
240     @property
241     def _start_command(self):
242         uri = self.get("uri") or ""
243         protocol = self.get("protocol") or ""
244         ip = self.get("ip") or "" 
245         port = self.get("port") or ""
246
247         # add ccnx:/example.com/ udp 224.0.0.204 52428
248         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
249             "uri" : uri,
250             "protocol": protocol,
251             "host": ip,
252             "port": port
253             })
254
255     @property
256     def _stop_command(self):
257         uri = self.get("uri") or ""
258         protocol = self.get("protocol") or ""
259         ip = self.get("ip") or ""
260         port = self.get("port") or ""
261
262         # add ccnx:/example.com/ udp 224.0.0.204 52428
263         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
264             "uri" : uri,
265             "protocol": protocol,
266             "host": ip,
267             "port": port
268             })
269
270     @property
271     def _environment(self):
272         return self.ccnd.path
273        
274     def valid_connection(self, guid):
275         # TODO: Validate!
276         return True
277