ns-3 CCN tests
[nepi.git] / src / nepi / resources / ns3 / ns3dceapplication.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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.resource import clsinit_copy, ResourceState, reschedule_delay
22 from nepi.resources.ns3.ns3application import NS3BaseApplication
23
24 import os
25
26 @clsinit_copy
27 class NS3BaseDceApplication(NS3BaseApplication):
28     _rtype = "abstract::ns3::DceApplication"
29
30     @classmethod
31     def _register_attributes(cls):
32         binary = Attribute("binary", 
33                 "Name of binary to execute",
34                 flags = Flags.Design)
35
36         stack_size = Attribute("stackSize", 
37                 "Stack Size for DCE",
38                 type = Types.Integer,
39                 default = 1<<20,                
40                 flags = Flags.Design)
41
42         arguments = Attribute("arguments", 
43                 "Semi-colon separated list of arguments for the application",
44                 flags = Flags.Design)
45
46         environment = Attribute("environment", 
47                 "Semi-colon separated list of 'key=value' pairs to set as "
48                 "DCE environment variables.",
49                 flags = Flags.Design)
50
51         cls._register_attribute(binary)
52         cls._register_attribute(stack_size)
53         cls._register_attribute(arguments)
54         cls._register_attribute(environment)
55
56     @property
57     def node(self):
58         from nepi.resources.ns3.ns3node import NS3BaseNode
59         nodes = self.get_connected(NS3BaseNode.get_rtype())
60
61         if not nodes: 
62             msg = "DceApplication not connected to node"
63             self.error(msg)
64             raise RuntimeError, msg
65
66         if nodes[0].get("enableDCE") == False:
67             raise RuntimeError("DceApplication not connected to DCE enabled node")
68
69         return nodes[0]
70     
71     def _instantiate_object(self):
72         pass
73
74     def _connect_object(self):
75         node = self.node
76         if node.uuid not in self.connected:
77             self._connected.add(node.uuid)
78
79             # Preventing concurrent access to the DceApplicationHelper
80             # from different DceApplication RMs
81             with self.simulation.dce_application_lock:
82                 self.simulation.invoke(
83                         self.simulation.dce_application_helper_uuid, 
84                         "ResetArguments") 
85
86                 self.simulation.invoke(
87                         self.simulation.dce_application_helper_uuid, 
88                         "ResetEnvironment") 
89
90                 self.simulation.invoke(
91                         self.simulation.dce_application_helper_uuid, 
92                         "SetBinary", self.get("binary")) 
93
94                 self.simulation.invoke(
95                         self.simulation.dce_application_helper_uuid, 
96                         "SetStackSize", self.get("stackSize")) 
97
98                 arguments = self.get("arguments") or ""
99                 for arg in map(str.strip, arguments.split(";")):
100                     self.simulation.invoke(
101                             self.simulation.dce_application_helper_uuid, 
102                         "AddArgument", arg)
103
104                 environment = self.get("environment") or ""
105                 for env in map(str.strip, environment.split(";")):
106                     key, val = env.split("=")
107                     self.simulation.invoke(
108                             self.simulation.dce_application_helper_uuid, 
109                         "AddEnvironment", key, val)
110
111                 if self.has_attribute("files"):
112                     files = self.get("files") or ""
113                     for files in map(str.strip, files.split(";")):
114                         remotepath, dcepath = env.split("=")
115                         localpath = "${SHARE}/" + os.path.basename(remotepath)
116                         self.simulation.invoke(
117                                 self.simulation.dce_application_helper_uuid, 
118                             "AddFile", localpath, dcepath)
119
120                 if self.has_attribute("stdinFile"):
121                     stdinfile = self.get("stdinFile")
122                     if stdinfile:
123                         if stdinfile != "":
124                             stdinfile = "${SHARE}/" + os.path.basename(stdinfile)
125         
126                         self.simulation.invoke(
127                                 self.simulation.dce_application_helper_uuid, 
128                                 "SetStdinFile", stdinfile)
129
130                 apps_uuid = self.simulation.invoke(
131                         self.simulation.dce_application_helper_uuid, 
132                         "InstallInNode", self.node.uuid)
133
134             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
135
136             if self.has_changed("StartTime"):
137                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
138
139             if self.has_changed("StopTime"):
140                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
141
142     def do_stop(self):
143         if self.state == ResourceState.STARTED:
144             # No need to do anything, simulation.Destroy() will stop every object
145             self.info("Stopping command '%s'" % command)
146             self.simulation.invoke(self.uuid, "Stop")
147             self.set_stopped()
148
149     def do_start(self):
150         if self.simulation.state < ResourceState.STARTED:
151             self.debug("---- RESCHEDULING START ----" )
152             self.ec.schedule(reschedule_delay, self.start)
153         else:
154             self._configure_traces()
155             super(NS3BaseApplication, self).do_start()
156             self._start_time = self.simulation.start_time
157
158     def _configure_traces(self):
159         # Preventing concurrent access to the DceApplicationHelper
160         # from different DceApplication RMs
161         with self.simulation.dce_application_lock:
162             pid = self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
163                     "GetPid", self._uuid)
164         node_id = self.simulation.invoke(self.node.uuid, "GetId")
165         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
166         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
167         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
168         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)
169
170