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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from socket import inet_pton, inet_ntop, AF_INET6, AF_INET
21 def format_vl_api_ip6_prefix_t(args):
22 prefix, len = args.split('/')
23 return {'prefix': {'address': inet_pton(AF_INET6, prefix)},
27 def unformat_vl_api_ip6_prefix_t(args):
28 return "{}/{}".format(inet_ntop(AF_INET6, args.prefix.address),
32 def format_vl_api_ip4_prefix_t(args):
33 prefix, len = args.split('/')
34 return {'prefix': {'address': inet_pton(AF_INET, prefix)},
38 def unformat_vl_api_ip4_prefix_t(args):
39 return "{}/{}".format(inet_ntop(AF_INET, args.prefix.address),
43 def format_vl_api_ip6_address_t(args):
44 return {'address': inet_pton(AF_INET6, args)}
47 def format_vl_api_ip4_address_t(args):
48 return {'address': inet_pton(AF_INET, args)}
51 def format_vl_api_address_t(args):
53 return {'un': {'ip6': {'address': inet_pton(AF_INET6, args)}},
55 except Exception as e:
56 return {'un': {'ip4': {'address': inet_pton(AF_INET, args)}},
60 def unformat_vl_api_address_t(arg):
62 return inet_ntop(AF_INET6, arg.un.ip6.address)
64 return inet_ntop(AF_INET, arg.un.ip4.address)
68 def format_vl_api_prefix_t(args):
69 prefix, len = args.split('/')
70 return {'address': VPPFormat.format_vl_api_address_t(prefix),
71 'address_length': int(len)}
74 def unformat_vl_api_prefix_t(arg):
75 if arg.address.af == 1:
76 return "{}/{}".format(inet_ntop(AF_INET6,
77 arg.address.un.ip6.address),
79 if arg.address.af == 0:
80 return "{}/{}".format(inet_ntop(AF_INET,
81 arg.address.un.ip4.address),
89 except Exception as e:
93 def format(typename, args):
95 return getattr(VPPFormat, 'format_' + typename)(args)
96 except AttributeError:
101 def unformat_bytes(args):
103 return args.decode('utf-8')
104 except Exception as e:
108 def unformat_list(args):
115 s2 = VPPFormat.unformat_t(t, f)
116 s += '{} '.format(s2)
122 return VPPFormat.unformat_t(type(args).__name__, args)
124 for i, f in enumerate(args):
130 s2 = VPPFormat.unformat_t(t, f)
131 s += '{} {} '.format(args._fields[i], s2)
136 def unformat_t(typename, args):
138 return getattr(VPPFormat, 'unformat_' + typename)(args)
139 except AttributeError:
140 # Type without explicit override
141 return VPPFormat.unformat(args)