Zack Williams | 98725e2 | 2021-03-18 10:21:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # rfc3442_words.py |
| 7 | # Input: a dict containing a router ip and multiple CIDR format routes |
| 8 | # Output a list of octets to be appended to option 121 RFC3442 Classless routes option |
| 9 | # |
| 10 | # References: |
| 11 | # https://tools.ietf.org/html/rfc3442 |
| 12 | # https://netaddr.readthedocs.io/en/latest/index.html |
| 13 | |
| 14 | from __future__ import absolute_import |
| 15 | import netaddr |
| 16 | |
| 17 | |
| 18 | class FilterModule(object): |
| 19 | def filters(self): |
| 20 | return { |
| 21 | "rfc3442_words": self.rfc3442_words, |
| 22 | } |
| 23 | |
| 24 | def rfc3442_words(self, var): |
| 25 | |
| 26 | words = [] |
| 27 | |
| 28 | router = var["ip"] |
| 29 | router_words = netaddr.IPNetwork(router).network.words |
| 30 | |
| 31 | for r3442r in var["rfc3442routes"]: |
| 32 | |
| 33 | # add prefix length |
| 34 | prefixlen = netaddr.IPNetwork(r3442r).prefixlen |
| 35 | words.append(prefixlen) |
| 36 | |
| 37 | # add only the relevant portion of the address, depending ow words |
| 38 | (o1, o2, o3, o4) = netaddr.IPNetwork(r3442r).network.words |
| 39 | if prefixlen >= 25: |
| 40 | words += [o1, o2, o3, o4] |
| 41 | elif prefixlen >= 17: |
| 42 | words += [o1, o2, o3] |
| 43 | elif prefixlen >= 9: |
| 44 | words += [o1, o2] |
| 45 | elif prefixlen >= 1: |
| 46 | words += [o1] |
| 47 | # no additional words if prefixlen == 0 |
| 48 | |
| 49 | # add router address |
| 50 | words += list(router_words) |
| 51 | |
| 52 | return words |
| 53 | |
| 54 | |
| 55 | # test when running standalone outside of Ansible |
| 56 | if __name__ == "__main__": |
| 57 | |
| 58 | example = { |
| 59 | "ip": "192.168.1.10", |
| 60 | "rfc3442routes": [ |
| 61 | "10.0.0.0/8", |
| 62 | "172.16.0.0/16", |
| 63 | "172.17.0.0/22", |
| 64 | "172.31.10.0/25", |
| 65 | ], |
| 66 | } |
| 67 | |
| 68 | jfilter = FilterModule() |
| 69 | |
| 70 | words = jfilter.rfc3442_words(example) |
| 71 | |
| 72 | print(words) |
| 73 | |
| 74 | # verify correct functionality |
| 75 | assert words == [ |
Zack Williams | 7f58df0 | 2021-09-14 13:49:57 -0700 | [diff] [blame^] | 76 | 8, |
| 77 | 10, |
| 78 | 192, |
| 79 | 168, |
| 80 | 1, |
| 81 | 10, |
| 82 | 16, |
| 83 | 172, |
| 84 | 16, |
| 85 | 192, |
| 86 | 168, |
| 87 | 1, |
| 88 | 10, |
| 89 | 22, |
| 90 | 172, |
| 91 | 17, |
| 92 | 0, |
| 93 | 192, |
| 94 | 168, |
| 95 | 1, |
| 96 | 10, |
| 97 | 25, |
| 98 | 172, |
| 99 | 31, |
| 100 | 10, |
| 101 | 0, |
| 102 | 192, |
| 103 | 168, |
| 104 | 1, |
| 105 | 10, |
Zack Williams | 98725e2 | 2021-03-18 10:21:26 -0700 | [diff] [blame] | 106 | ] |