Adding trace Collector RM
[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             self.execute_command(command, env)
86
87             self.debug("----- READY ---- ")
88             self._ready_time = tnow()
89             self._state = ResourceState.READY
90
91     def start(self):
92         if self._state in [ResourceState.READY, ResourceState.STARTED]:
93             command = self.get("command")
94             self.info("Starting command '%s'" % command)
95
96             self._start_time = tnow()
97             self._state = ResourceState.STARTED
98         else:
99             msg = " Failed to execute command '%s'" % command
100             self.error(msg, out, err)
101             self._state = ResourceState.FAILED
102             raise RuntimeError, msg
103
104     def stop(self):
105         command = self.get('command')
106         env = self.get('env')
107         
108         if self.state == ResourceState.STARTED:
109             self.info("Stopping command '%s'" % command)
110
111             command = self._stop_command
112             self.execute_command(command, env)
113
114             self._stop_time = tnow()
115             self._state = ResourceState.STOPPED
116
117     @property
118     def state(self):
119         return self._state
120
121     @property
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 ""
127
128         # add ccnx:/example.com/ udp 224.0.0.204 52428
129         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
130             "uri" : uri,
131             "protocol": protocol,
132             "host": host,
133             "port": port
134             })
135
136     @property
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 ""
142
143         # add ccnx:/example.com/ udp 224.0.0.204 52428
144         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
145             "uri" : uri,
146             "protocol": protocol,
147             "host": host,
148             "port": port
149             })
150
151     def valid_connection(self, guid):
152         # TODO: Validate!
153         return True
154