First CCN RMs working example for Linux
[nepi.git] / src / nepi / resources / linux / ccn / ccnr.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 ResourceManager, 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 strfnow, strfdiff
27
28 import os
29
30 reschedule_delay = "0.5s"
31
32 @clsinit_copy
33 class LinuxCCNR(LinuxApplication):
34     _rtype = "LinuxCCNR"
35
36     @classmethod
37     def _register_attributes(cls):
38         max_fanout = Attribute("maxFanout",
39             "Sets the CCNR_BTREE_MAX_FANOUT environmental variable. ",
40             flags = Flags.ExecReadOnly)
41
42         max_leaf_entries = Attribute("maxLeafEntries",
43             "Sets the CCNR_BTREE_MAX_LEAF_ENTRIES environmental variable. ",
44             flags = Flags.ExecReadOnly)
45
46         max_node_bytes = Attribute("maxNodeBytes",
47             "Sets the CCNR_BTREE_MAX_NODE_BYTES environmental variable. ",
48             flags = Flags.ExecReadOnly)
49
50         max_node_pool = Attribute("maxNodePool",
51             "Sets the CCNR_BTREE_MAX_NODE_POOL environmental variable. ",
52             flags = Flags.ExecReadOnly)
53
54         content_cache = Attribute("contentCache",
55             "Sets the CCNR_CONTENT_CACHE environmental variable. ",
56             flags = Flags.ExecReadOnly)
57
58         debug = Attribute("debug",
59             "Sets the CCNR_DEBUG environmental variable. "
60             "Logging level for ccnr. Defaults to WARNING.",
61             type = Types.Enumerate,
62             allowed = [
63                     "NONE",
64                     "SEVERE",
65                     "ERROR",
66                     "WARNING",
67                     "INFO",
68                     "FINE, FINER, FINEST"],
69             flags = Flags.ExecReadOnly)
70
71         directory = Attribute("directory",
72             "Sets the CCNR_DIRECTORY environmental variable. ",
73             flags = Flags.ExecReadOnly)
74
75         global_prefix = Attribute("globalPrefix",
76             "Sets the CCNR_GLOBAL_PREFIX environmental variable. ",
77             flags = Flags.ExecReadOnly)
78
79         listen_on = Attribute("listenOn",
80             "Sets the CCNR_LISTEN_ON environmental variable. ",
81             flags = Flags.ExecReadOnly)
82
83         min_send_bufsize = Attribute("minSendBufsize",
84             "Sets the CCNR_MIN_SEND_BUFSIZE environmental variable. ",
85             flags = Flags.ExecReadOnly)
86
87         proto = Attribute("proto",
88             "Sets the CCNR_PROTO environmental variable. ",
89             flags = Flags.ExecReadOnly)
90
91         status_port = Attribute("statusPort",
92             "Sets the CCNR_STATUS_PORT environmental variable. ",
93             flags = Flags.ExecReadOnly)
94
95         start_write_scope_limit = Attribute("startWriteScopeLimit",
96             "Sets the CCNR_START_WRITE_SCOPE_LIMIT environmental variable. ",
97             flags = Flags.ExecReadOnly)
98
99         ccns_debug = Attribute("ccnsDebug",
100             "Sets the CCNS_DEBUG environmental variable. ",
101             flags = Flags.ExecReadOnly)
102
103         ccns_enable = Attribute("ccnsEnable",
104             "Sets the CCNS_ENABLE environmental variable. ",
105             flags = Flags.ExecReadOnly)
106
107         ccns_faux_error = Attribute("ccnsFauxError",
108             "Sets the CCNS_FAUX_ERROR environmental variable. ",
109             flags = Flags.ExecReadOnly)
110
111         ccns_heartbeat_micros = Attribute("ccnsHeartBeatMicros",
112             "Sets the CCNS_HEART_BEAT_MICROS environmental variable. ",
113             flags = Flags.ExecReadOnly)
114
115         ccns_max_compares_busy = Attribute("ccnsMaxComparesBusy",
116             "Sets the CCNS_MAX_COMPARES_BUSY environmental variable. ",
117             flags = Flags.ExecReadOnly)
118
119         ccns_max_fetch_busy = Attribute("ccnsMaxFetchBusy",
120             "Sets the CCNS_MAX_FETCH_BUSY environmental variable. ",
121             flags = Flags.ExecReadOnly)
122
123         ccns_node_fetch_lifetime = Attribute("ccnsNodeFetchLifetime",
124             "Sets the CCNS_NODE_FETCH_LIFETIME environmental variable. ",
125             flags = Flags.ExecReadOnly)
126
127         ccns_note_err = Attribute("ccnsNoteErr",
128             "Sets the CCNS_NOTE_ERR environmental variable. ",
129             flags = Flags.ExecReadOnly)
130
131         ccns_repo_store = Attribute("ccnsRepoStore",
132             "Sets the CCNS_REPO_STORE environmental variable. ",
133             flags = Flags.ExecReadOnly)
134
135         ccns_root_advise_fresh = Attribute("ccnsRootAdviseFresh",
136             "Sets the CCNS_ROOT_ADVISE_FRESH environmental variable. ",
137             flags = Flags.ExecReadOnly)
138
139         ccns_root_advise_lifetime = Attribute("ccnsRootAdviseLifetime",
140             "Sets the CCNS_ROOT_ADVISE_LIFETIME environmental variable. ",
141             flags = Flags.ExecReadOnly)
142
143         ccns_stable_enabled = Attribute("ccnsStableEnabled",
144             "Sets the CCNS_STABLE_ENABLED environmental variable. ",
145             flags = Flags.ExecReadOnly)
146
147         ccns_sync_scope = Attribute("ccnsSyncScope",
148             "Sets the CCNS_SYNC_SCOPE environmental variable. ",
149             flags = Flags.ExecReadOnly)
150
151         cls._register_attribute(max_fanout)
152         cls._register_attribute(max_leaf_entries)
153         cls._register_attribute(max_node_bytes)
154         cls._register_attribute(max_node_pool)
155         cls._register_attribute(content_cache)
156         cls._register_attribute(debug)
157         cls._register_attribute(directory)
158         cls._register_attribute(global_prefix)
159         cls._register_attribute(listen_on)
160         cls._register_attribute(min_send_bufsize)
161         cls._register_attribute(proto)
162         cls._register_attribute(status_port)
163         cls._register_attribute(start_write_scope_limit)
164         cls._register_attribute(ccns_debug)
165         cls._register_attribute(ccns_enable)
166         cls._register_attribute(ccns_faux_error)
167         cls._register_attribute(ccns_heartbeat_micros)
168         cls._register_attribute(ccns_max_compares_busy)
169         cls._register_attribute(ccns_max_fetch_busy)
170         cls._register_attribute(ccns_node_fetch_lifetime)
171         cls._register_attribute(ccns_note_err)
172         cls._register_attribute(ccns_repo_store)
173         cls._register_attribute(ccns_root_advise_fresh)
174         cls._register_attribute(ccns_root_advise_lifetime)
175         cls._register_attribute(ccns_stable_enabled)
176         cls._register_attribute(ccns_sync_scope)
177
178     @classmethod
179     def _register_traces(cls):
180         log = Trace("log", "CCND log output")
181
182         cls._register_trace(log)
183
184     def __init__(self, ec, guid):
185         super(LinuxCCNR, self).__init__(ec, guid)
186         self._home = "ccnr-%s" % self.guid
187
188         # Marks whether ccnr is running
189         self._running = False
190
191     @property
192     def ccnd(self):
193         ccnd = self.get_connected(LinuxCCND.rtype())
194         if ccnd: return ccnd[0]
195         return None
196
197     @property
198     def node(self):
199         if self.ccnd: return self.ccnd.node
200         return None
201
202     def deploy(self):
203         if not self.get("command"):
204             self.set("command", self._default_command)
205         
206         if not self.get("env"):
207             self.set("env", self._default_environment)
208
209         # Wait until associated ccnd is provisioned
210         ccnd = self.ccnd
211
212         if not ccnd or ccnd.state < ResourceState.READY:
213             # ccnr needs to wait until ccnd is deployed and running
214             self.ec.schedule(reschedule_delay, self.deploy)
215         else:
216             # Invoke the actual deployment
217             super(LinuxCCNR, self).deploy()
218
219             # As soon as deployment is finished, we launch the ccnr
220             # command ( we don't want to lose time ccnr later on )
221             if self._state == ResourceState.READY:
222                 self._start_in_background()
223                 self._running = True
224
225     def start(self):
226         # CCND should already be started by now.
227         # Nothing to do but to set the state to STARTED
228         if self._running:
229             self._start_time = strfnow()
230             self._state = ResourceState.STARTED
231         else:
232             msg = " Failed to execute command '%s'" % command
233             self.error(msg, out, err)
234             self._state = ResourceState.FAILED
235             raise RuntimeError, msg
236
237     @property
238     def state(self):
239         state = super(LinuxCCNR, self).state
240         if self._state in [ResourceState.FINISHED, ResourceState.FAILED]:
241             self._running = False
242
243         if self._state == ResourceState.READY:
244             # CCND is really deployed only when ccn daemon is running 
245             if not self._running:
246                 return ResourceState.PROVISIONED
247  
248         return self._state
249
250     @property
251     def _default_command(self):
252         return "ccnr"
253
254     @property
255     def _default_environment(self):
256         envs = dict({
257             "maxFanout": "CCNR_BTREE_MAX_FANOUT",
258             "maxLeafEntries": "CCNR_BTREE_MAX_LEAF_ENTRIES",
259             "maxNodeBytes": "CCNR_BTREE_MAX_NODE_BYTES",
260             "maxNodePool": "CCNR_BTREE_MAX_NODE_POOL",
261             "contentCache": "CCNR_CONTENT_CACHE",
262             "debug": "CCNR_DEBUG",
263             "directory": "CCNR_DIRECTORY",
264             "globalPrefix": "CCNR_GLOBAL_PREFIX",
265             "listenOn": "CCNR_LISTEN_ON",
266             "minSendBufsize": "CCNR_MIN_SEND_BUFSIZE",
267             "proto": "CCNR_PROTO",
268             "statusPort": "CCNR_STATUS_PORT",
269             "startWriteScopeLimit": "CCNR_START_WRITE_SCOPE_LIMIT",
270             "ccnsDebug": "CCNS_DEBUG",
271             "ccnsEnable": "CCNS_ENABLE",
272             "ccnsFauxError": "CCNS_FAUX_ERROR",
273             "ccnsHeartBeatMicros": "CCNS_HEART_BEAT_MICROS",
274             "ccnsMaxComparesBusy": "CCNS_MAX_COMPARES_BUSY",
275             "ccnsMaxFetchBusy": "CCNS_MAX_FETCH_BUSY",
276             "ccnsNodeFetchLifetime": "CCNS_NODE_FETCH_LIFETIME",
277             "ccnsNoteErr": "CCNS_NOTE_ERR",
278             "ccnsRepoStore": "CCNS_REPO_STORE",
279             "ccnsRootAdviseFresh": "CCNS_ROOT_ADVISE_FRESH",
280             "ccnsRootAdviseLifetime": "CCNS_ROOT_ADVISE_LIFETIME",
281             "ccnsStableEnabled": "CCNS_STABLE_ENABLED",
282             "ccnsSyncScope": "CCNS_SYNC_SCOPE",
283             })
284
285         env = "PATH=$PATH:${EXP_HOME}/ccnx/bin "
286         env += " ".join(map(lambda k: "%s=%s" % (envs.get(k), self.get(k)) \
287             if self.get(k) else "", envs.keys()))
288         
289         return env            
290         
291     def valid_connection(self, guid):
292         # TODO: Validate!
293         return True
294