<s> import logging <EOL> import socket <EOL> from bagpipe . bgp . common import utils <EOL> from bagpipe . bgp . common import logDecorator <EOL> from bagpipe . bgp . vpn . vpn_instance import VPNInstance <EOL> from bagpipe . bgp . engine import RouteEvent <EOL> from bagpipe . bgp . vpn . dataplane_drivers import DummyDataplaneDriver as _DummyDataplaneDriver <EOL> from bagpipe . bgp . common . looking_glass import LookingGlass , LGMap <EOL> from bagpipe . exabgp . structure . vpn import RouteDistinguisher , VPNLabelledPrefix <EOL> from bagpipe . exabgp . structure . mpls import LabelStackEntry <EOL> from bagpipe . exabgp . structure . address import AFI , SAFI <EOL> from bagpipe . exabgp . structure . ip import Inet , Prefix <EOL> from bagpipe . exabgp . message . update . route import Route <EOL> from bagpipe . exabgp . message . update . attribute . nexthop import NextHop <EOL> from bagpipe . exabgp . message . update . attribute . communities import ECommunities <EOL> class DummyDataplaneDriver ( _DummyDataplaneDriver ) : <EOL> pass <EOL> class VRF ( VPNInstance , LookingGlass ) : <EOL> type = "<STR_LIT>" <EOL> afi = AFI ( AFI . ipv4 ) <EOL> safi = SAFI ( SAFI . mpls_vpn ) <EOL> @ logDecorator . log <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> VPNInstance . __init__ ( self , * args , ** kwargs ) <EOL> self . readvertised = set ( ) <EOL> def _routeFrom ( self , prefix , label , rd ) : <EOL> return Route ( VPNLabelledPrefix ( self . afi , self . safi , prefix , rd , <EOL> [ LabelStackEntry ( label , True ) ] <EOL> ) ) <EOL> def generateVifBGPRoute ( self , macAdress , ipPrefix , prefixLen , label ) : <EOL> route = self . _routeFrom ( Prefix ( self . afi , ipPrefix , prefixLen ) , label , <EOL> RouteDistinguisher ( <EOL> RouteDistinguisher . TYPE_IP_LOC , None , <EOL> self . bgpManager . getLocalAddress ( ) , <EOL> self . instanceId ) <EOL> ) <EOL> self . log . debug ( "<STR_LIT>" , route . attributes ) <EOL> return self . _newRouteEntry ( self . afi , self . safi , self . exportRTs , <EOL> route . nlri , route . attributes ) <EOL> def _getLocalLabels ( self ) : <EOL> for portData in self . macAddress2LocalPortData . itervalues ( ) : <EOL> yield portData [ '<STR_LIT:label>' ] <EOL> def _getRDFromLabel ( self , label ) : <EOL> return RouteDistinguisher ( RouteDistinguisher . TYPE_IP_LOC , None , <EOL> self . bgpManager . getLocalAddress ( ) , <EOL> <NUM_LIT> + label ) <EOL> def _routeForReAdvertisement ( self , prefix , label ) : <EOL> route = self . _routeFrom ( prefix , label , <EOL> self . _getRDFromLabel ( label ) ) <EOL> nh = Inet ( <NUM_LIT:1> , socket . inet_pton ( socket . AF_INET , <EOL> self . dataplane . driver . getLocalAddress ( ) ) ) <EOL> route . attributes . add ( NextHop ( nh ) ) <EOL> route . attributes . add ( ECommunities ( self . readvertiseToRTs ) ) <EOL> routeEntry = self . _newRouteEntry ( self . afi , self . safi , <EOL> self . readvertiseToRTs , <EOL> route . nlri , route . attributes ) <EOL> return routeEntry <EOL> @ logDecorator . log <EOL> def _readvertise ( self , nlri ) : <EOL> self . log . debug ( "<STR_LIT>" , nlri . prefix ) <EOL> for label in self . _getLocalLabels ( ) : <EOL> self . log . debug ( "<STR_LIT>" , <EOL> nlri . prefix , label ) <EOL> routeEntry = self . _routeForReAdvertisement ( nlri . prefix , label ) <EOL> self . _pushEvent ( RouteEvent ( RouteEvent . ADVERTISE , routeEntry ) ) <EOL> self . readvertised . add ( nlri . prefix ) <EOL> @ logDecorator . log <EOL> def _readvertiseStop ( self , nlri ) : <EOL> self . log . debug ( "<STR_LIT>" , nlri . prefix ) <EOL> for label in self . _getLocalLabels ( ) : <EOL> self . log . debug ( "<STR_LIT>" , <EOL> nlri . prefix , label ) <EOL> routeEntry = self . _routeForReAdvertisement ( nlri . prefix , label ) <EOL> self . _pushEvent ( RouteEvent ( RouteEvent . WITHDRAW , routeEntry ) ) <EOL> self . readvertised . remove ( nlri . prefix ) <EOL> def vifPlugged ( self , macAddress , ipAddressPrefix , localPort , <EOL> advertiseSubnet ) : <EOL> VPNInstance . vifPlugged ( self , macAddress , ipAddressPrefix , localPort , <EOL> advertiseSubnet ) <EOL> label = self . macAddress2LocalPortData [ macAddress ] [ '<STR_LIT:label>' ] <EOL> for prefix in self . readvertised : <EOL> self . log . debug ( "<STR_LIT>" , <EOL> prefix ) <EOL> routeEntry = self . _routeForReAdvertisement ( prefix , label ) <EOL> self . _pushEvent ( RouteEvent ( RouteEvent . ADVERTISE , routeEntry ) ) <EOL> def vifUnplugged ( self , macAddress , ipAddressPrefix , advertiseSubnet ) : <EOL> label = self . macAddress2LocalPortData [ macAddress ] [ '<STR_LIT:label>' ] <EOL> for prefix in self . readvertised : <EOL> self . log . debug ( "<STR_LIT>" , <EOL> prefix ) <EOL> routeEntry = self . _routeForReAdvertisement ( prefix , label ) <EOL> self . _pushEvent ( RouteEvent ( RouteEvent . WITHDRAW , routeEntry ) ) <EOL> VPNInstance . vifUnplugged ( self , macAddress , ipAddressPrefix , <EOL> advertiseSubnet ) <EOL> def _route2trackedEntry ( self , route ) : <EOL> if isinstance ( route . nlri , VPNLabelledPrefix ) : <EOL> return route . nlri . prefix <EOL> else : <EOL> self . log . error ( "<STR_LIT>" , <EOL> type ( route . nlri ) ) <EOL> return None <EOL> def _toReadvertise ( self , route ) : <EOL> return ( len ( set ( route . routeTargets ) . intersection ( <EOL> set ( self . readvertiseFromRTs ) ) ) > <NUM_LIT:0> ) <EOL> def _imported ( self , route ) : <EOL> return ( len ( set ( route . routeTargets ) . intersection ( <EOL> set ( self . importRTs ) ) ) > <NUM_LIT:0> ) <EOL> @ utils . synchronized <EOL> @ logDecorator . log <EOL> def _newBestRoute ( self , entry , newRoute ) : <EOL> prefix = entry <EOL> if self . readvertise : <EOL> self . log . debug ( "<STR_LIT>" , newRoute . routeTargets ) <EOL> self . log . debug ( "<STR_LIT>" , self . readvertiseFromRTs ) <EOL> if self . _toReadvertise ( newRoute ) : <EOL> self . log . debug ( "<STR_LIT>" , prefix ) <EOL> self . _readvertise ( newRoute . nlri ) <EOL> if not self . _imported ( newRoute ) : <EOL> self . log . debug ( "<STR_LIT>" , prefix ) <EOL> return <EOL> encaps = self . _checkEncaps ( newRoute ) <EOL> if not encaps : <EOL> return <EOL> self . dataplane . setupDataplaneForRemoteEndpoint ( <EOL> prefix , newRoute . attributes . get ( NextHop . ID ) . next_hop , <EOL> newRoute . nlri . labelStack [ <NUM_LIT:0> ] . labelValue , newRoute . nlri , encaps ) <EOL> @ utils . synchronized <EOL> @ logDecorator . log <EOL> def _bestRouteRemoved ( self , entry , oldRoute , last ) : <EOL> prefix = entry <EOL> if self . readvertise and last : <EOL> if self . _toReadvertise ( oldRoute ) : <EOL> self . log . debug ( "<STR_LIT>" , prefix ) <EOL> self . _readvertiseStop ( oldRoute . nlri ) <EOL> if not self . _imported ( oldRoute ) : <EOL> self . log . debug ( "<STR_LIT>" , prefix ) <EOL> return <EOL> if self . _skipRouteRemoval ( last ) : <EOL> self . log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> self . dataplane . removeDataplaneForRemoteEndpoint ( <EOL> prefix , oldRoute . attributes . get ( NextHop . ID ) . next_hop , <EOL> oldRoute . nlri . labelStack [ <NUM_LIT:0> ] . labelValue , oldRoute . nlri ) <EOL> def getLGMap ( self ) : <EOL> return { <EOL> "<STR_LIT>" : ( LGMap . VALUE , [ repr ( prefix ) for prefix in <EOL> self . readvertised ] ) <EOL> } </s>
