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.ccn.ccnapplication import LinuxCCNApplication
25 from nepi.util.timefuncs import tnow
30 # TODO: Add rest of options for ccndc!!!
31 # Implement ENTRY DELETE!!
34 class LinuxFIBEntry(LinuxCCNApplication):
35 _rtype = "LinuxFIBEntry"
38 def _register_attributes(cls):
39 uri = Attribute("uri",
40 "URI prefix to match and route for this FIB entry",
42 flags = Flags.ExecReadOnly)
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,
49 allowed = ["udp", "tcp"],
50 flags = Flags.ExecReadOnly)
52 host = Attribute("host",
53 "Peer host used in network connection for this FIB entry. ",
54 flags = Flags.ExecReadOnly)
56 port = Attribute("port",
57 "Peer port address used in network connection to peer "
58 "for this FIB entry.",
59 flags = Flags.ExecReadOnly)
61 cls._register_attribute(uri)
62 cls._register_attribute(protocol)
63 cls._register_attribute(host)
64 cls._register_attribute(port)
66 def __init__(self, ec, guid):
67 super(LinuxFIBEntry, self).__init__(ec, guid)
68 self._home = "fib-%s" % self.guid
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)
76 command = self._start_command
77 env = self._environment
79 self.set("command", command)
82 self.info("Deploying command '%s' " % command)
84 self.node.mkdir(self.app_home)
85 self.execute_command(command, env)
87 self.debug("----- READY ---- ")
88 self._ready_time = tnow()
89 self._state = ResourceState.READY
92 if self._state in [ResourceState.READY, ResourceState.STARTED]:
93 command = self.get("command")
94 self.info("Starting command '%s'" % command)
96 self._start_time = tnow()
97 self._state = ResourceState.STARTED
99 msg = " Failed to execute command '%s'" % command
100 self.error(msg, out, err)
101 self._state = ResourceState.FAILED
102 raise RuntimeError, msg
105 command = self.get('command')
106 env = self.get('env')
108 if self.state == ResourceState.STARTED:
109 self.info("Stopping command '%s'" % command)
111 command = self._stop_command
112 self.execute_command(command, env)
114 self._stop_time = tnow()
115 self._state = ResourceState.STOPPED
122 def _start_command(self):
123 uri = self.get("uri") or ""
124 protocol = self.get("protocol") or ""
125 host = self.get("host") or ""
126 port = self.get("port") or ""
128 # add ccnx:/example.com/ udp 224.0.0.204 52428
129 return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
131 "protocol": protocol,
137 def _stop_command(self):
138 uri = self.get("uri") or ""
139 protocol = self.get("protocol") or ""
140 host = self.get("host") or ""
141 port = self.get("port") or ""
143 # add ccnx:/example.com/ udp 224.0.0.204 52428
144 return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
146 "protocol": protocol,
151 def valid_connection(self, guid):