LinuxApplication: Changed directory structure to store experiment files in the Linux...
[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.ccn.ccnapplication import LinuxCCNApplication
25 from nepi.util.timefuncs import tnow
26
27 import os
28
29
30 # TODO: Add rest of options for ccndc!!!
31 #       Implement ENTRY DELETE!!
32
33 @clsinit_copy
34 class LinuxFIBEntry(LinuxCCNApplication):
35     _rtype = "LinuxFIBEntry"
36
37     @classmethod
38     def _register_attributes(cls):
39         uri = Attribute("uri",
40                 "URI prefix to match and route for this FIB entry",
41                 default = "ccnx:/",
42                 flags = Flags.ExecReadOnly)
43
44         protocol = Attribute("protocol",
45                 "Transport protocol used in network connection to peer "
46                 "for this FIB entry. One of 'udp' or 'tcp'.",
47                 type = Types.Enumerate, 
48                 default = "udp",
49                 allowed = ["udp", "tcp"],
50                 flags = Flags.ExecReadOnly)
51
52         host = Attribute("host",
53                 "Peer host used in network connection for this FIB entry. ",
54                 flags = Flags.ExecReadOnly)
55
56         port = Attribute("port",
57                 "Peer port address used in network connection to peer "
58                 "for this FIB entry.",
59                 flags = Flags.ExecReadOnly)
60
61         cls._register_attribute(uri)
62         cls._register_attribute(protocol)
63         cls._register_attribute(host)
64         cls._register_attribute(port)
65
66     def __init__(self, ec, guid):
67         super(LinuxFIBEntry, self).__init__(ec, guid)
68         self._home = "fib-%s" % self.guid
69
70     def deploy(self):
71         # Wait until associated ccnd is provisioned
72         if not self.ccnd or self.ccnd.state < ResourceState.READY:
73             # ccnr needs to wait until ccnd is deployed and running
74             self.ec.schedule(reschedule_delay, self.deploy)
75         else:
76             command = self._start_command
77             env = self._environment
78
79             self.set("command", command)
80             self.set("env", env)
81
82             self.info("Deploying command '%s' " % command)
83
84             self.node.mkdir(self.app_home)
85             (out, err), proc = self.execute_command(command, env)
86
87             if proc.poll():
88                 self._state = ResourceState.FAILED
89                 msg = "Failed to execute command"
90                 self.error(msg, out, err)
91                 raise RuntimeError, msg
92
93
94             self.debug("----- READY ---- ")
95             self._ready_time = tnow()
96             self._state = ResourceState.READY
97
98     def start(self):
99         if self._state in [ResourceState.READY, ResourceState.STARTED]:
100             command = self.get("command")
101             self.info("Starting command '%s'" % command)
102
103             self._start_time = tnow()
104             self._state = ResourceState.STARTED
105         else:
106             msg = " Failed to execute command '%s'" % command
107             self.error(msg, out, err)
108             self._state = ResourceState.FAILED
109             raise RuntimeError, msg
110
111     def stop(self):
112         command = self.get('command')
113         env = self.get('env')
114         
115         if self.state == ResourceState.STARTED:
116             self.info("Stopping command '%s'" % command)
117
118             command = self._stop_command
119             (out, err), proc = self.execute_command(command, env)
120
121             if proc.poll():
122                 pass
123
124             self._stop_time = tnow()
125             self._state = ResourceState.STOPPED
126
127     @property
128     def state(self):
129         return self._state
130
131     @property
132     def _start_command(self):
133         uri = self.get("uri") or ""
134         protocol = self.get("protocol") or ""
135         host = self.get("host") or ""
136         port = self.get("port") or ""
137
138         # add ccnx:/example.com/ udp 224.0.0.204 52428
139         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
140             "uri" : uri,
141             "protocol": protocol,
142             "host": host,
143             "port": port
144             })
145
146     @property
147     def _stop_command(self):
148         uri = self.get("uri") or ""
149         protocol = self.get("protocol") or ""
150         host = self.get("host") or ""
151         port = self.get("port") or ""
152
153         # add ccnx:/example.com/ udp 224.0.0.204 52428
154         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
155             "uri" : uri,
156             "protocol": protocol,
157             "host": host,
158             "port": port
159             })
160
161     def valid_connection(self, guid):
162         # TODO: Validate!
163         return True
164