Adding CCN RMs for Linux backend
[nepi.git] / src / nepi / resources / linux / ccn / ccncontent.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.ccnr import LinuxCCNR
26 from nepi.resources.linux.node import OSType
27
28 from nepi.util.sshfuncs import ProcStatus
29 from nepi.util.timefuncs import strfnow, strfdiff
30 import os
31
32 reschedule_delay = "0.5s"
33
34 @clsinit_copy
35 class LinuxCCNR(LinuxApplication):
36     _rtype = "LinuxCCNContent"
37
38     @classmethod
39     def _register_attributes(cls):
40         content_name = Attribute("contentName",
41                 "The name of the content to publish (e.g. ccn:/VIDEO) ",
42             flags = Flags.ExecReadOnly)
43         content = Attribute("content",
44                 "The content to publish. It can be a path to a file or plain text ",
45             flags = Flags.ExecReadOnly)
46
47
48         cls._register_attribute(content_name)
49         cls._register_attribute(content)
50
51     @classmethod
52     def _register_traces(cls):
53         log = Trace("log", "CCND log output")
54
55         cls._register_trace(log)
56
57     def __init__(self, ec, guid):
58         super(LinuxCCNContent, self).__init__(ec, guid)
59
60     @property
61     def ccnr(self):
62         ccnr = self.get_connected(LinuxCCNR.rtype())
63         if ccnr: return ccnr[0]
64         return None
65
66     def deploy(self):
67         if not self.get("command"):
68             self.set("command", self._default_command)
69         
70         if not self.get("env"):
71             self.set("env", self._default_environment)
72
73         # Wait until associated ccnd is provisioned
74         ccnr = self.ccnr
75
76         if not ccnr or ccnr.state < ResourceState.STARTED:
77             self.ec.schedule(reschedule_delay, self.deploy)
78         else:
79             # Add a start after condition so CCNR will not start
80             # before CCND does
81             self.ec.register_condition(self.guid, ResourceAction.START, 
82                 ccnd.guid, ResourceState.STARTED)
83  
84             # Invoke the actual deployment
85             super(LinuxCCNContent, self).deploy()
86
87     @property
88     def _default_command(self):
89         return "ccnseqwriter -r %s " % self.get("contentName")
90
91     @property
92     def _default_environment(self):
93         env = "PATH=$PATH:${EXP_HOME}/ccnx/bin "
94         return env            
95         
96     def valid_connection(self, guid):
97         # TODO: Validate!
98         return True
99