API: Change ip4_address and ip6_address to use type alias.
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_format.py
index b1800d8..908606a 100644 (file)
 #
 
 from socket import inet_pton, inet_ntop, AF_INET6, AF_INET
+import socket
 
 
-class VPPFormat:
+class VPPFormatError(Exception):
+    pass
+
+
+class VPPFormat(object):
+    VPPFormatError = VPPFormatError
+
     @staticmethod
     def format_vl_api_ip6_prefix_t(args):
         prefix, len = args.split('/')
@@ -25,7 +32,7 @@ class VPPFormat:
 
     @staticmethod
     def unformat_vl_api_ip6_prefix_t(args):
-        return "{}/{}".format(inet_ntop(AF_INET6, args.prefix.address),
+        return "{}/{}".format(inet_ntop(AF_INET6, args.prefix),
                               args.len)
 
     @staticmethod
@@ -36,7 +43,7 @@ class VPPFormat:
 
     @staticmethod
     def unformat_vl_api_ip4_prefix_t(args):
-        return "{}/{}".format(inet_ntop(AF_INET, args.prefix.address),
+        return "{}/{}".format(inet_ntop(AF_INET, args.prefix),
                               args.len)
 
     @staticmethod
@@ -50,19 +57,19 @@ class VPPFormat:
     @staticmethod
     def format_vl_api_address_t(args):
         try:
-            return {'un': {'ip6': {'address': inet_pton(AF_INET6, args)}},
+            return {'un': {'ip6': inet_pton(AF_INET6, args)},
                     'af': int(1)}
-        except Exception as e:
-            return {'un': {'ip4': {'address': inet_pton(AF_INET, args)}},
+        except socket.error as e:
+            return {'un': {'ip4': inet_pton(AF_INET, args)},
                     'af': int(0)}
 
     @staticmethod
     def unformat_vl_api_address_t(arg):
         if arg.af == 1:
-            return inet_ntop(AF_INET6, arg.un.ip6.address)
+            return inet_ntop(AF_INET6, arg.un.ip6)
         if arg.af == 0:
-            return inet_ntop(AF_INET, arg.un.ip4.address)
-        raise
+            return inet_ntop(AF_INET, arg.un.ip4)
+        raise VPPFormatError
 
     @staticmethod
     def format_vl_api_prefix_t(args):
@@ -74,13 +81,13 @@ class VPPFormat:
     def unformat_vl_api_prefix_t(arg):
         if arg.address.af == 1:
             return "{}/{}".format(inet_ntop(AF_INET6,
-                                            arg.address.un.ip6.address),
+                                            arg.address.un.ip6),
                                   arg.address_length)
         if arg.address.af == 0:
             return "{}/{}".format(inet_ntop(AF_INET,
-                                            arg.address.un.ip4.address),
+                                            arg.address.un.ip4),
                                   arg.address_length)
-        raise
+        raise VPPFormatError
 
     @staticmethod
     def format_u8(args):
@@ -101,7 +108,7 @@ class VPPFormat:
     def unformat_bytes(args):
         try:
             return args.decode('utf-8')
-        except Exception as e:
+        except ValueError as e:
             return args
 
     @staticmethod