def packed_attributes (self, negotiated, maximum=Negotiated.FREE_SIZE): if not self.nlris: return # addpath = negotiated.addpath.send(self.afi,self.safi) # nexthopself = negotiated.nexthopself(self.afi) mpnlri = {} for nlri in self.nlris: if nlri.family() != self.family(): # nlri is not part of specified family continue if nlri.nexthop is NoNextHop: # EOR and Flow may not have any next_hop nexthop = b'' else: _,rd_size = Family.size.get(self.family(),(0,0)) nh_rd = character(0)*rd_size if rd_size else b'' nexthop = nh_rd + nlri.nexthop.ton(negotiated,nlri.afi) # mpunli[nexthop] = nlri mpnlri.setdefault(nexthop,[]).append(nlri.pack(negotiated)) for nexthop,nlris in six.iteritems(mpnlri): payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0)) header_length = len(payload) for nlri in nlris: if self._len(payload + nlri) > maximum: if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPRNLRI') yield self._attribute(payload) payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0), nlri) continue payload = concat_bytes(payload, nlri) if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPRNLRI') yield self._attribute(payload)
def pack (self): rs = [] for k,capabilities in six.iteritems(self): for capability in capabilities.extract(): rs.append(concat_bytes(character(k),character(len(capability)),capability)) parameters = concat_bytes_i(concat_bytes(character(2),character(len(r)),r) for r in rs) return concat_bytes(character(len(parameters)),parameters)
def transfer (source,destination): for key,value in six.iteritems(source): if key not in destination: destination[key] = value elif isinstance(source[key], dict): transfer(source[key],destination[key]) elif isinstance(source[key], list): destination.setdefault(key,[]).extend(value) else: self.throw('can not recursively copy this type of data')
def json (self): d = { 'name':'"graceful restart"', 'time':self.restart_time, 'address-family-flags':'{%s } ' % ( ', '.join('"%s/%s": [%s ]' % ( ' %s' % afi, safi, ' "restart"' if family & 0x80 else '') for afi, safi, family in [ (str(a), str(s), self[(a,s)]) for (a,s) in self.keys() ] ) ), 'restart-flags':'[%s] ' % (' "forwarding" ' if self.restart_flag & 0x8 else ' ') } return '{ %s}' % ','.join('"%s": %s' % (k,v) for k,v in six.iteritems(d))
def json (self): d = { 'name':'"graceful restart"', 'time':self.restart_time, 'address-family-flags':'{ %s}' % ( ', '.join('"%s/%s": [%s ] ' % ( '%s' % afi, safi, ' "restart"' if family & 0x80 else '') for afi, safi, family in [ (str(a), str(s), self[(a,s)]) for (a,s) in self.keys() ] ) ), 'restart-flags':'[%s] ' % (' "forwarding" ' if self.restart_flag & 0x8 else ' ') } return '{ %s}' % ', '.join('"%s": %s' % (k,v) for k,v in six.iteritems(d))
def transfer(self, source, destination): for key, value in six.iteritems(source): if key not in destination: destination[key] = value elif isinstance(source[key], list): destination[key].extend(value) elif isinstance(source[key], dict): if key not in destination: destination[key] = source[key] else: self.transfer(source[key], destination[key]) elif isinstance(source[key], int): destination[key] = value elif isinstance(source[key], long): destination[key] = value else: self.throw('can not recursively copy this type of data %s' % type(source[key]))
def transfer (self, source, destination): for key,value in six.iteritems(source): if key not in destination: destination[key] = value elif isinstance(source[key], list): destination[key].extend(value) elif isinstance(source[key], dict): if key not in destination: destination[key] = source[key] else: self.transfer(source[key],destination[key]) elif isinstance(source[key], int): destination[key] = value elif isinstance(source[key], long): destination[key] = value elif isinstance(source[key],IP): destination[key] = value elif isinstance(source[key],str): destination[key] = value else: self.throw('can not copy "%s" (as it is of type %s) and it exists in both the source and destination' % (key,type(source[key])))
def transfer(self, source, destination): for key, value in six.iteritems(source): if key not in destination: destination[key] = value elif isinstance(source[key], list): destination[key].extend(value) elif isinstance(source[key], dict): if key not in destination: destination[key] = source[key] else: self.transfer(source[key], destination[key]) elif isinstance(source[key], int): destination[key] = value elif isinstance(source[key], long): destination[key] = value elif isinstance(source[key], IP): destination[key] = value elif isinstance(source[key], str): destination[key] = value else: self.throw( 'can not copy "%s" (as it is of type %s) and it exists in both the source and destination' % (key, type(source[key])))
def _json_kv(self, extra): return ", ".join('"%s": %s' % (k, v.json()) for (k, v) in six.iteritems(extra))
def packed_attributes(self, negotiated, maximum=Negotiated.FREE_SIZE): if not self.nlris: return # addpath = negotiated.addpath.send(self.afi,self.safi) # nexthopself = negotiated.nexthopself(self.afi) mpnlri = {} for nlri in self.nlris: if nlri.family() != self.family( ): # nlri is not part of specified family continue if nlri.nexthop is NoNextHop: # EOR and Flow may not have any next_hop nexthop = b'' else: _, rd_size = Family.size.get(self.family(), (0, 0)) nh_rd = character(0) * rd_size if rd_size else b'' try: nexthop = nh_rd + nlri.nexthop.ton(negotiated, nlri.afi) except TypeError: # we could not match "next-hop self" with the BGP AFI of the BGP sesion # attempting invalid IPv4 next-hop (0.0.0.0) to try to not kill the session # and preserve some form of backward compatibility (for some vendors) # the next-hop may have been IPv6 but not valided as the RFC says # # An UPDATE message that carries no NLRI, other than the one encoded in # the MP_REACH_NLRI attribute, SHOULD NOT carry the NEXT_HOP attribute. # If such a message contains the NEXT_HOP attribute, the BGP speaker # that receives the message SHOULD ignore this attribute. # # Some vendors may have therefore not valided the next-hop # and accepted invalid IPv6 next-hop in the past nexthop = character(0) * 4 # mpunli[nexthop] = nlri mpnlri.setdefault(nexthop, []).append(nlri.pack(negotiated)) for nexthop, nlris in six.iteritems(mpnlri): payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0)) header_length = len(payload) for nlri in nlris: if self._len(payload + nlri) > maximum: if len(payload) == header_length or len(payload) > maximum: raise Notify( 6, 0, 'attributes size is so large we can not even pack on MPRNLRI' ) yield self._attribute(payload) payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0), nlri) continue payload = concat_bytes(payload, nlri) if len(payload) == header_length or len(payload) > maximum: raise Notify( 6, 0, 'attributes size is so large we can not even pack on MPRNLRI' ) yield self._attribute(payload)
def _json_list (self,extra): return ", ".join('%s' % (v.json()) for v in six.iteritems(extra))
def do_show(self, _): print('attribute %s ' % self.name + ' '.join('%s %s' % (key, value) for key, value in six.iteritems(self.attribute)))
def _minimalkv (self, extra): return ", ".join('"%s": %s' % (k,self._string(v)) for (k,v) in six.iteritems(extra) if v)
def _json_list(self, extra): return ", ".join('%s' % (v.json()) for v in six.iteritems(extra))
def _minimalkv(self, extra): return ", ".join('"%s": %s' % (k, self._string(v)) for (k, v) in six.iteritems(extra) if v)
def _json_kv (self, extra): return ", ".join('"%s": %s' % (k,v.json()) for (k,v) in six.iteritems(extra))