2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
31 # TODO: Add rest of options for ccndc!!!
32 # Implement ENTRY DELETE!!
35 class LinuxFIBEntry(LinuxApplication):
36 _rtype = "LinuxFIBEntry"
39 def _register_attributes(cls):
40 uri = Attribute("uri",
41 "URI prefix to match and route for this FIB entry",
43 flags = Flags.ExecReadOnly)
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,
50 allowed = ["udp", "tcp"],
51 flags = Flags.ExecReadOnly)
53 host = Attribute("host",
54 "Peer host used in network connection for this FIB entry. ",
55 flags = Flags.ExecReadOnly)
57 port = Attribute("port",
58 "Peer port address used in network connection to peer "
59 "for this FIB entry.",
60 flags = Flags.ExecReadOnly)
62 cls._register_attribute(uri)
63 cls._register_attribute(protocol)
64 cls._register_attribute(host)
65 cls._register_attribute(port)
67 def __init__(self, ec, guid):
68 super(LinuxFIBEntry, self).__init__(ec, guid)
69 self._home = "fib-%s" % self.guid
73 ccnd = self.get_connected(LinuxCCND.rtype())
74 if ccnd: return ccnd[0]
79 if self.ccnd: return self.ccnd.node
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)
88 command = self._start_command
89 env = self._environment
91 self.set("command", command)
94 self.info("Deploying command '%s' " % command)
96 # create run dir for application
97 self.node.mkdir(self.run_home)
99 (out, err), proc = self.execute_command(command, env)
102 self._state = ResourceState.FAILED
103 msg = "Failed to execute command"
104 self.error(msg, out, err)
105 raise RuntimeError, msg
107 self.debug("----- READY ---- ")
108 self._ready_time = tnow()
109 self._state = ResourceState.READY
112 if self._state in [ResourceState.READY, ResourceState.STARTED]:
113 command = self.get("command")
114 self.info("Starting command '%s'" % command)
116 self._start_time = tnow()
117 self._state = ResourceState.STARTED
119 msg = " Failed to execute command '%s'" % command
120 self.error(msg, out, err)
121 self._state = ResourceState.FAILED
122 raise RuntimeError, msg
125 command = self.get('command')
126 env = self.get('env')
128 if self.state == ResourceState.STARTED:
129 self.info("Stopping command '%s'" % command)
131 command = self._stop_command
132 (out, err), proc = self.execute_command(command, env)
137 self._stop_time = tnow()
138 self._state = ResourceState.STOPPED
145 def _start_command(self):
146 uri = self.get("uri") or ""
147 protocol = self.get("protocol") or ""
148 host = self.get("host") or ""
149 port = self.get("port") or ""
151 # add ccnx:/example.com/ udp 224.0.0.204 52428
152 return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
154 "protocol": protocol,
160 def _stop_command(self):
161 uri = self.get("uri") or ""
162 protocol = self.get("protocol") or ""
163 host = self.get("host") or ""
164 port = self.get("port") or ""
166 # add ccnx:/example.com/ udp 224.0.0.204 52428
167 return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
169 "protocol": protocol,
175 def _environment(self):
176 return self.ccnd.path
178 def execute_command(self, command, env):
179 environ = self.node.format_environment(env, inline = True)
180 command = environ + command
181 command = self.replace_paths(command)
183 return self.node.execute(command)
185 def valid_connection(self, guid):