API: Change ip4_address and ip6_address to use type alias.
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_format.py
1 #
2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 from socket import inet_pton, inet_ntop, AF_INET6, AF_INET
17 import socket
18
19
20 class VPPFormatError(Exception):
21     pass
22
23
24 class VPPFormat(object):
25     VPPFormatError = VPPFormatError
26
27     @staticmethod
28     def format_vl_api_ip6_prefix_t(args):
29         prefix, len = args.split('/')
30         return {'prefix': {'address': inet_pton(AF_INET6, prefix)},
31                 'len': int(len)}
32
33     @staticmethod
34     def unformat_vl_api_ip6_prefix_t(args):
35         return "{}/{}".format(inet_ntop(AF_INET6, args.prefix),
36                               args.len)
37
38     @staticmethod
39     def format_vl_api_ip4_prefix_t(args):
40         prefix, len = args.split('/')
41         return {'prefix': {'address': inet_pton(AF_INET, prefix)},
42                 'len': int(len)}
43
44     @staticmethod
45     def unformat_vl_api_ip4_prefix_t(args):
46         return "{}/{}".format(inet_ntop(AF_INET, args.prefix),
47                               args.len)
48
49     @staticmethod
50     def format_vl_api_ip6_address_t(args):
51         return {'address': inet_pton(AF_INET6, args)}
52
53     @staticmethod
54     def format_vl_api_ip4_address_t(args):
55         return {'address': inet_pton(AF_INET, args)}
56
57     @staticmethod
58     def format_vl_api_address_t(args):
59         try:
60             return {'un': {'ip6': inet_pton(AF_INET6, args)},
61                     'af': int(1)}
62         except socket.error as e:
63             return {'un': {'ip4': inet_pton(AF_INET, args)},
64                     'af': int(0)}
65
66     @staticmethod
67     def unformat_vl_api_address_t(arg):
68         if arg.af == 1:
69             return inet_ntop(AF_INET6, arg.un.ip6)
70         if arg.af == 0:
71             return inet_ntop(AF_INET, arg.un.ip4)
72         raise VPPFormatError
73
74     @staticmethod
75     def format_vl_api_prefix_t(args):
76         prefix, len = args.split('/')
77         return {'address': VPPFormat.format_vl_api_address_t(prefix),
78                 'address_length': int(len)}
79
80     @staticmethod
81     def unformat_vl_api_prefix_t(arg):
82         if arg.address.af == 1:
83             return "{}/{}".format(inet_ntop(AF_INET6,
84                                             arg.address.un.ip6),
85                                   arg.address_length)
86         if arg.address.af == 0:
87             return "{}/{}".format(inet_ntop(AF_INET,
88                                             arg.address.un.ip4),
89                                   arg.address_length)
90         raise VPPFormatError
91
92     @staticmethod
93     def format_u8(args):
94         try:
95             return int(args)
96         except Exception as e:
97             return args.encode()
98
99     @staticmethod
100     def format(typename, args):
101         try:
102             return getattr(VPPFormat, 'format_' + typename)(args)
103         except AttributeError:
104             # Default
105             return (int(args))
106
107     @staticmethod
108     def unformat_bytes(args):
109         try:
110             return args.decode('utf-8')
111         except ValueError as e:
112             return args
113
114     @staticmethod
115     def unformat_list(args):
116         s = '['
117         for f in args:
118             t = type(f).__name__
119             if type(f) is int:
120                 s2 = str(f)
121             else:
122                 s2 = VPPFormat.unformat_t(t, f)
123             s += '{} '.format(s2)
124         return s[:-1] + ']'
125
126     @staticmethod
127     def unformat(args):
128         s = ''
129         return VPPFormat.unformat_t(type(args).__name__, args)
130         '''
131         for i, f in enumerate(args):
132             print('F', f)
133             t = type(f).__name__
134             if type(f) is int:
135                 s2 = str(f)
136             else:
137                 s2 = VPPFormat.unformat_t(t, f)
138             s += '{} {} '.format(args._fields[i], s2)
139         return s[:-1]
140         '''
141
142     @staticmethod
143     def unformat_t(typename, args):
144         try:
145             return getattr(VPPFormat, 'unformat_' + typename)(args)
146         except AttributeError:
147             # Type without explicit override
148             return VPPFormat.unformat(args)
149
150         # Default handling
151         return args