applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / interface.py
index b36adef..04d7959 100644 (file)
@@ -1,73 +1,74 @@
-"""
-    NEPI, a framework to manage network experiments
-    Copyright (C) 2013 INRIA
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-"""
+#
+#    NEPI, a framework to manage network experiments
+#    Copyright (C) 2013 INRIA
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.execution.attribute import Attribute, Types, Flags
-from nepi.execution.resource import ResourceManager, clsinit, ResourceState
+from nepi.execution.resource import ResourceManager, clsinit_copy, \
+        ResourceState
 from nepi.resources.linux.node import LinuxNode
 from nepi.resources.linux.channel import LinuxChannel
 
 import collections
-import logging
 import os
 import random
 import re
 import tempfile
 import time
 
-# TODO: UP, MTU attributes!
-
-reschedule_delay = "0.5s"
+# TODO: 
+#     - check UP, MTU attributes!
+#     - clean up code and test!
 
-@clsinit
+@clsinit_copy
 class LinuxInterface(ResourceManager):
-    _rtype = "LinuxInterface"
+    _rtype = "linux::Interface"
+    _help = "Controls network devices on Linux hosts through the ifconfig tool"
+    _platform = "linux"
 
     @classmethod
     def _register_attributes(cls):
         ip4 = Attribute("ip4", "IPv4 Address",
-                flags = Flags.ExecReadOnly)
+              flags = Flags.Design)
 
         ip6 = Attribute("ip6", "IPv6 Address",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         mac = Attribute("mac", "MAC Address",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         mask4 = Attribute("mask4", "IPv4 network mask",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         mask6 = Attribute("mask6", "IPv6 network mask",
                 type = Types.Integer,
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         mtu = Attribute("mtu", "Maximum transmition unit for device",
-            type = Types.Integer)
+                type = Types.Integer)
 
         devname = Attribute("deviceName", 
                 "Name of the network interface (e.g. eth0, wlan0, etc)",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         up = Attribute("up", "Link up", type = Types.Bool)
 
         tear_down = Attribute("tearDown", "Bash script to be executed before " + \
                 "releasing the resource",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)
 
         cls._register_attribute(ip4)
         cls._register_attribute(ip6)
@@ -82,8 +83,6 @@ class LinuxInterface(ResourceManager):
     def __init__(self, ec, guid):
         super(LinuxInterface, self).__init__(ec, guid)
         self._configured = False
-
-        self._logger = logging.getLogger("LinuxInterface")
         
         self.add_set_hooks()
 
@@ -93,17 +92,17 @@ class LinuxInterface(ResourceManager):
 
     @property
     def node(self):
-        node = self.get_connected(LinuxNode.rtype())
+        node = self.get_connected(LinuxNode.get_rtype())
         if node: return node[0]
         return None
 
     @property
     def channel(self):
-        chan = self.get_connected(LinuxChannel.rtype())
+        chan = self.get_connected(LinuxChannel.get_rtype())
         if chan: return chan[0]
         return None
 
-    def discover(self, filters = None):
+    def do_discover(self):
         devname = self.get("deviceName")
         ip4 = self.get("ip4")
         ip6 = self.get("ip4")
@@ -113,12 +112,12 @@ class LinuxInterface(ResourceManager):
         mtu = self.get("mtu")
 
         # Get current interfaces information
-        (out, err), proc = self.node.execute("ifconfig", sudo = True)
+        (out, err), proc = self.node.execute("ifconfig", sudo = True, tty = True)
 
         if err and proc.poll():
             msg = " Error retrieving interface information "
             self.error(msg, out, err)
-            raise RuntimeError, "%s - %s - %s" % (msg, out, err)
+            raise RuntimeError("%s - %s - %s" % (msg, out, err))
         
         # Check if an interface is found matching the RM attributes
         ifaces = out.split("\n\n")
@@ -180,11 +179,11 @@ class LinuxInterface(ResourceManager):
         if not self.get("deviceName"):
             msg = "Unable to resolve interface "
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
-        super(LinuxInterface, self).discover(filters = filters)
+        super(LinuxInterface, self).do_discover()
 
-    def provision(self, filters = None):
+    def do_provision(self):
         devname = self.get("deviceName")
         ip4 = self.get("ip4")
         ip6 = self.get("ip4")
@@ -210,7 +209,7 @@ class LinuxInterface(ResourceManager):
             if err and proc.poll():
                 msg = "Error configuring interface with command '%s'" % cmd
                 self.error(msg, out, err)
-                raise RuntimeError, "%s - %s - %s" % (msg, out, err)
+                raise RuntimeError("%s - %s - %s" % (msg, out, err))
 
             if ip6 and mask6:
                 cmd = "ifconfig %(devname)s inet6 add %(ip6)s/%(mask6)d" % ({
@@ -223,37 +222,33 @@ class LinuxInterface(ResourceManager):
             if err and proc.poll():
                 msg = "Error seting ipv6 for interface using command '%s' " % cmd
                 self.error(msg, out, err)
-                raise RuntimeError, "%s - %s - %s" % (msg, out, err)
+                raise RuntimeError("%s - %s - %s" % (msg, out, err))
 
-        super(LinuxInterface, self).provision(filters = filters)
+        super(LinuxInterface, self).do_provision()
 
-    def deploy(self):
+    def do_deploy(self):
         # Wait until node is provisioned
         node = self.node
         chan = self.channel
 
         if not node or node.state < ResourceState.PROVISIONED:
-            self.ec.schedule(reschedule_delay, self.deploy)
+            self.ec.schedule(self.reschedule_delay, self.deploy)
         elif not chan or chan.state < ResourceState.READY:
-            self.ec.schedule(reschedule_delay, self.deploy)
+            self.ec.schedule(self.reschedule_delay, self.deploy)
         else:
             # Verify if the interface exists in node. If not, configue
             # if yes, load existing configuration
-            try:
-                self.discover()
-                self.provision()
-            except:
-                self._state = ResourceState.FAILED
-                raise
+            self.do_discover()
+            self.do_provision()
 
-            super(LinuxInterface, self).deploy()
+            super(LinuxInterface, self).do_deploy()
 
-    def release(self):
+    def do_release(self):
         tear_down = self.get("tearDown")
-        if tear_down:
+        if tear_down:   
             self.execute(tear_down)
 
-        super(LinuxInterface, self).release()
+        super(LinuxInterface, self).do_release()
 
     def valid_connection(self, guid):
         # TODO: Validate!
@@ -271,6 +266,7 @@ class LinuxInterface(ResourceManager):
         attr = self._attrs["up"]
         attr._value = up
         attr = self._attrs["mtu"]
+        attr._value = mtu 
 
     def add_set_hooks(self):
         attrup = self._attrs["up"]
@@ -280,7 +276,7 @@ class LinuxInterface(ResourceManager):
         attrmtu.set_hook = self.set_hook_mtu
 
     def set_hook_up(self, oldval, newval):
-        if oldval == newval:
+        if self.state == ResourceState.NEW or oldval == newval:
             return oldval
 
         # configure interface up
@@ -299,7 +295,7 @@ class LinuxInterface(ResourceManager):
         return newval
 
     def set_hook_mtu(self, oldval, newval):
-        if oldval == newval:
+        if self.state == ResourceState.NEW or oldval == newval:
             return oldval
 
         cmd = "ifconfig %s mtu %d" % (self.get("deviceName"), newval)