Added PlanetlabTAP & PlanetlabTUN
[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, reschedule_delay
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
30
31 # TODO: Add rest of options for ccndc!!!
32 #       Implement ENTRY DELETE!!
33
34 @clsinit_copy
35 class LinuxFIBEntry(LinuxApplication):
36     _rtype = "LinuxFIBEntry"
37
38     @classmethod
39     def _register_attributes(cls):
40         uri = Attribute("uri",
41                 "URI prefix to match and route for this FIB entry",
42                 default = "ccnx:/",
43                 flags = Flags.ExecReadOnly)
44
45         protocol = Attribute("protocol",
46                 "Transport protocol used in network connection to peer "
47                 "for this FIB entry. One of 'udp' or 'tcp'.",
48                 type = Types.Enumerate, 
49                 default = "udp",
50                 allowed = ["udp", "tcp"],
51                 flags = Flags.ExecReadOnly)
52
53         host = Attribute("host",
54                 "Peer host used in network connection for this FIB entry. ",
55                 flags = Flags.ExecReadOnly)
56
57         port = Attribute("port",
58                 "Peer port address used in network connection to peer "
59                 "for this FIB entry.",
60                 flags = Flags.ExecReadOnly)
61
62         cls._register_attribute(uri)
63         cls._register_attribute(protocol)
64         cls._register_attribute(host)
65         cls._register_attribute(port)
66
67     def __init__(self, ec, guid):
68         super(LinuxFIBEntry, self).__init__(ec, guid)
69         self._home = "fib-%s" % self.guid
70
71     @property
72     def ccnd(self):
73         ccnd = self.get_connected(LinuxCCND)
74         if ccnd: return ccnd[0]
75         return None
76
77     @property
78     def node(self):
79         if self.ccnd: return self.ccnd.node
80         return None
81
82     def deploy(self):
83         # Wait until associated ccnd is provisioned
84         if not self.ccnd or self.ccnd.state < ResourceState.READY:
85             # ccnr needs to wait until ccnd is deployed and running
86             self.ec.schedule(reschedule_delay, self.deploy)
87         else:
88             try:
89                 if not self.get("command"):
90                     self.set("command", self._start_command)
91
92                 if not self.get("env"):
93                     self.set("env", self._environment)
94
95                 command = self.get("command")
96
97                 self.info("Deploying command '%s' " % command)
98
99                 self.discover()
100                 self.provision()
101             except:
102                 self.fail()
103                 raise
104  
105             self.debug("----- READY ---- ")
106             self._ready_time = tnow()
107             self._state = ResourceState.READY
108
109     def upload_start_command(self):
110         command = self.get("command")
111         env = self.get("env")
112
113         if command:
114             # We want to make sure the FIB entries are created
115             # before the experiment starts.
116             # Run the command as a bash script in the background, 
117             # in the host ( but wait until the command has
118             # finished to continue )
119             env = env and self.replace_paths(env)
120             command = self.replace_paths(command)
121
122             (out, err), proc = self.execute_command(command, env)
123
124             if proc.poll():
125                 self._state = ResourceState.FAILED
126                 msg = "Failed to execute command"
127                 self.error(msg, out, err)
128                 raise RuntimeError, msg
129
130     def start(self):
131         if self._state in [ResourceState.READY, ResourceState.STARTED]:
132             command = self.get("command")
133             self.info("Starting command '%s'" % command)
134
135             self._start_time = tnow()
136             self._state = ResourceState.STARTED
137         else:
138             msg = " Failed to execute command '%s'" % command
139             self.error(msg, out, err)
140             self._state = ResourceState.FAILED
141             raise RuntimeError, msg
142
143     def stop(self):
144         command = self.get('command')
145         env = self.get('env')
146         
147         if self.state == ResourceState.STARTED:
148             self.info("Stopping command '%s'" % command)
149
150             command = self._stop_command
151             (out, err), proc = self.execute_command(command, env)
152
153             if proc.poll():
154                 pass
155
156             self._stop_time = tnow()
157             self._state = ResourceState.STOPPED
158
159     @property
160     def state(self):
161         return self._state
162
163     @property
164     def _start_command(self):
165         uri = self.get("uri") or ""
166         protocol = self.get("protocol") or ""
167         host = self.get("host") or ""
168         port = self.get("port") or ""
169
170         # add ccnx:/example.com/ udp 224.0.0.204 52428
171         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
172             "uri" : uri,
173             "protocol": protocol,
174             "host": host,
175             "port": port
176             })
177
178     @property
179     def _stop_command(self):
180         uri = self.get("uri") or ""
181         protocol = self.get("protocol") or ""
182         host = self.get("host") or ""
183         port = self.get("port") or ""
184
185         # add ccnx:/example.com/ udp 224.0.0.204 52428
186         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
187             "uri" : uri,
188             "protocol": protocol,
189             "host": host,
190             "port": port
191             })
192
193     @property
194     def _environment(self):
195         return self.ccnd.path
196        
197     def valid_connection(self, guid):
198         # TODO: Validate!
199         return True
200