| 1 |
# Copyright 2014 Orange |
| 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 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 16 |
|
| 17 |
|
| 18 |
def prefix_to_packed_ip_mask(prefix): |
| 19 |
ip_string, mask = prefix.split("/") |
| 20 |
return (exa.IP.pton(ip_string), int(mask)) |
| 21 |
|
| 22 |
|
| 23 |
@exa.NLRI.register(exa.AFI.ipv4, exa.SAFI.mpls_vpn, force=True) |
| 24 |
@exa.NLRI.register(exa.AFI.ipv6, exa.SAFI.mpls_vpn, force=True) |
| 25 |
class IPVPN(exa.IPVPN): |
| 26 |
# two NLRIs with same RD and prefix, but different labels need to |
| 27 |
# be equal and have the same hash |
| 28 |
|
| 29 |
def __eq__(self, other): |
| 30 |
return self.rd == other.rd and self.cidr == other.cidr |
| 31 |
|
| 32 |
def __hash__(self): |
| 33 |
return hash((self.rd, self.cidr._packed)) |
| 34 |
|
| 35 |
|
| 36 |
def IPVPNRouteFactory(afi, prefix, label, rd, nexthop): |
| 37 |
packed_prefix, mask = prefix_to_packed_ip_mask(prefix) |
| 38 |
|
| 39 |
return IPVPN.new(afi, exa.SAFI.mpls_vpn, packed_prefix, mask, |
| 40 |
exa.Labels([label], True), rd, nexthop) |