Merge "Add VNF descriptor for vIMS testcase"
[yardstick.git] / ansible / filter_plugins / networks.py
1 # Copyright (c) 2017 Intel Corporation
2 #
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 try:
17     from urlparse import urlsplit
18 except ImportError:
19     from urllib.parse import urlsplit
20
21
22 def do_prefix_to_netmask(arg):
23     return '.'.join(str((0xffffffff << (32 - int(arg)) >> i) & 0xff) for i in
24                     range(24, -8, -8))
25
26
27 def do_netmask_to_prefix(arg):
28     return sum([bin(int(x)).count('1') for x in arg.split('.')])
29
30
31 def do_urlsplit(arg):
32     return urlsplit(arg)
33
34
35 class FilterModule(object):
36     def filters(self):
37         return {
38             'prefix_to_netmask': do_prefix_to_netmask,
39             'netmask_to_prefix': do_netmask_to_prefix,
40             'urlsplit': do_urlsplit,
41         }