Adding CCNFIBEntry 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
24 from nepi.resources.linux.ccn.ccnapplication import LinuxCCNApplication
25 from nepi.util.timefuncs import tnow
26
27 import os
28
29 reschedule_delay = "0.5s"
30
31 # TODO: Add rest of options for ccndc!!!
32 #       Implement ENTRY DELETE!!
33
34 @clsinit_copy
35 class LinuxFIBEntry(LinuxCCNApplication):
36     _rtype = "LinuxFIBEntry"
37
38     @classmethod
39     def _register_attributes(cls):
40         uri = Attribute("uri",
41                 "URI prefix to match and route for this FIB entry",
42                 default = "ccnx:/",
43                 flags = Flags.ExecReadOnly)
44
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, 
49                 default = "udp",
50                 allowed = ["udp", "tcp"],
51                 flags = Flags.ExecReadOnly)
52
53         host = Attribute("host",
54                 "Peer host used in network connection for this FIB entry. ",
55                 flags = Flags.ExecReadOnly)
56
57         port = Attribute("port",
58                 "Peer port address used in network connection to peer "
59                 "for this FIB entry.",
60                 flags = Flags.ExecReadOnly)
61
62         cls._register_attribute(uri)
63         cls._register_attribute(protocol)
64         cls._register_attribute(host)
65         cls._register_attribute(port)
66
67     def __init__(self, ec, guid):
68         super(LinuxFIBEntry, self).__init__(ec, guid)
69         self._home = "fib-%s" % self.guid
70
71     def deploy(self):
72         # Wait until associated ccnd is provisioned
73         if not self.ccnd or self.ccnd.state < ResourceState.READY:
74             # ccnr needs to wait until ccnd is deployed and running
75             self.ec.schedule(reschedule_delay, self.deploy)
76         else:
77             command = self._start_command
78             env = self._environment
79
80             self.set("command", command)
81             self.set("env", env)
82
83             self.info("Deploying command '%s' " % command)
84
85             self.node.mkdir(self.app_home)
86             self.execute_command(command, env)
87
88             self.debug("----- READY ---- ")
89             self._ready_time = tnow()
90             self._state = ResourceState.READY
91
92     def start(self):
93         if self._state in [ResourceState.READY, ResourceState.STARTED]:
94             command = self.get("command")
95             self.info("Starting command '%s'" % command)
96
97             self._start_time = tnow()
98             self._state = ResourceState.STARTED
99         else:
100             msg = " Failed to execute command '%s'" % command
101             self.error(msg, out, err)
102             self._state = ResourceState.FAILED
103             raise RuntimeError, msg
104
105     def stop(self):
106         command = self.get('command')
107         env = self.get('env')
108         
109         if self.state == ResourceState.STARTED:
110             self.info("Stopping command '%s'" % command)
111
112             command = self._stop_command
113             self.execute_command(command, env)
114
115             self._stop_time = tnow()
116             self._state = ResourceState.STOPPED
117
118     @property
119     def state(self):
120         return self._state
121
122     @property
123     def _start_command(self):
124         uri = self.get("uri") or ""
125         protocol = self.get("protocol") or ""
126         host = self.get("host") or ""
127         port = self.get("port") or ""
128
129         # add ccnx:/example.com/ udp 224.0.0.204 52428
130         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
131             "uri" : uri,
132             "protocol": protocol,
133             "host": host,
134             "port": port
135             })
136
137     @property
138     def _stop_command(self):
139         uri = self.get("uri") or ""
140         protocol = self.get("protocol") or ""
141         host = self.get("host") or ""
142         port = self.get("port") or ""
143
144         # add ccnx:/example.com/ udp 224.0.0.204 52428
145         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
146             "uri" : uri,
147             "protocol": protocol,
148             "host": host,
149             "port": port
150             })
151
152     def valid_connection(self, guid):
153         # TODO: Validate!
154         return True
155