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>
19 # Alexandros Kouvakas <alexandros.kouvakas@gmail.com>
21 from nepi.execution.attribute import Attribute, Flags, Types
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
24 from nepi.resources.planetlab.openvswitch.ovs import OVSWitch
25 from nepi.resources.planetlab.node import PlanetlabNode
26 from nepi.resources.linux.application import LinuxApplication
28 reschedule_delay = "0.5s"
31 class OVSPort(LinuxApplication):
33 .. class:: Class Args :
35 :param ec: The Experiment controller
36 :type ec: ExperimentController
37 :param guid: guid of the RM
43 _help = "Runs an OpenVSwitch on a PlanetLab host"
44 _backend = "planetlab"
46 _authorized_connections = ["OVSWitch", "Tunnel"]
49 def _register_attributes(cls):
50 """ Register the attributes of OVSPort RM
53 port_name = Attribute("port_name", "Name of the port",
54 flags = Flags.ExecReadOnly)
56 cls._register_attribute(port_name)
58 def __init__(self, ec, guid):
60 :param ec: The Experiment controller
61 :type ec: ExperimentController
62 :param guid: guid of the RM
66 super(OVSPort, self).__init__(ec, guid)
67 self._port_number = None
72 rm_list = self.get_connected(OVSWitch.get_rtype())
75 node = elt.get_connected(PlanetlabNode.get_rtype())
76 if node: return node[0]
81 ovswitch = self.get_connected(OVSWitch.get_rtype())
82 if ovswitch: return ovswitch[0]
86 def port_number(self):
87 return self._port_number
89 def valid_connection(self, guid):
93 # def valid_connection(self, guid):
94 # """ Check if the connection is available.
96 # :param guid: Guid of the current RM
101 # rm = self.ec.get_resource(guid)
102 # if rm.get_rtype() in self._authorized_connections:
103 # msg = "Connection between %s %s and %s %s accepted" % (self.get_rtype(), self._guid, rm.get_rtype(), guid)
106 # msg = "Connection between %s %s and %s %s refused" % (self.get_rtype(), self._guid, rm.get_rtype(), guid)
109 def get_host_ip(self):
110 """ Get the hostname of the node that
111 the port belongs to. We use it for tunnel.
113 get_host_ip = self.node
115 msg = "info_list is empty"
117 raise RuntimeError, msg
120 self.port_info.append(get_host_ip.get('hostname'))
121 self.port_info.append(socket.gethostbyname(self.port_info[0]))
123 def create_port(self):
124 """ Create the desired port
126 port_name = self.get('port_name')
128 if not (port_name or self.ovswitch):
129 msg = "The rm_list is empty or the port name is not assigned\n Failed to create port"
131 self.debug("ovswitch_list = %s and port_name = %s" % (self.ovswitch, port_name) )
132 raise AttributeError, msg
134 self.info("Create the port %s on switch %s" % (port_name, self.ovswitch.get('bridge_name')))
135 self.port_info.append(port_name)
136 self.port_info.append(self.ovswitch.get('virtual_ip_pref'))
137 cmd = "sliver-ovs create-port %s %s" % (self.ovswitch.get('bridge_name'), port_name)
138 self.node.run(cmd, self.ovswitch.ovs_checks,
139 stderr = "stdout-%s" % port_name,
140 stdout = "stderr-%s" % port_name,
143 def get_local_end(self):
144 """ Get the local_endpoint of the port
146 msg = "Discovering the number of the port %s"\
147 % self.get('port_name')
150 command = "sliver-ovs get-local-endpoint %s"\
151 % self.get('port_name')
153 (out, err), proc = self.node.run_and_wait(command, self.ovswitch.ovs_checks,
154 shfile = "port_number-%s.sh" % self.get('port_name'),
155 pidfile = "port_number_pidfile-%s" % self.get('port_name'),
156 ecodefile = "port_number_exitcode-%s" % self.get('port_name'),
158 stdout = "stdout-%s" % self.get('port_name'),
159 stderr = "stderr-%s" % self.get('port_name'))
162 msg = "No assignment in attribute port_name"
164 self.debug("You are in the method get_local_end and the port_name = %s" % self.get('port_name'))
165 raise AttributeError, msg
167 self._port_number = None
168 self._port_number = int(out)
169 self.port_info.append(self._port_number)
170 self.info("The number of the %s is %s" % (self.get('port_name'), self._port_number))
172 def switch_connect_command(self, local_port_name,
173 remote_ip, remote_port_num):
174 """ Script for switch links
176 command = ["sliver-ovs"]
177 command.append("set-remote-endpoint ")
178 command.append("%s " % local_port_name)
179 command.append("%s " % remote_ip)
180 command.append("%s " % remote_port_num)
181 command = " ".join(command)
182 command = self.replace_paths(command)
186 """ Wait until ovswitch is started
188 ovswitch = self.ovswitch
190 if not ovswitch or ovswitch.state < ResourceState.READY:
191 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.ovswitch.state )
192 self.ec.schedule(reschedule_delay, self.deploy)
200 self.ovswitch.ovs_status()
202 super(OVSPort, self).do_deploy()
204 def do_release(self):
205 """ Release the port RM means delete the ports
207 # OVS needs to wait until all associated RMs are released
209 from nepi.resources.planetlab.openvswitch.tunnel import OVSTunnel
210 rm = self.get_connected(OVSTunnel.get_rtype())
212 if rm and rm[0].state < ResourceState.STOPPED:
213 self.ec.schedule(reschedule_delay, self.release)
216 msg = "Deleting the port %s" % self.get('port_name')
218 cmd = "sliver-ovs del_port %s" % self.get('port_name')
219 (out, err), proc = self.node.run(cmd, self.ovswitch.ovs_checks,
223 self.error(msg, out, err)
224 raise RuntimeError, msg
226 super(OVSPort, self).do_release()