def __format__()

in src/sonic-frr-mgmt-framework/frrcfgd/frrcfgd.py [0:0]


    def __format__(self, format):
        bool_format = {'allow-as-in': 'origin',
                       'match-clust-len': 'equal-cluster-length',
                       'network-backdoor': 'backdoor',
                       'aggr-as-set': 'as-set',
                       'aggr-summary-only': 'summary-only',
                       'uchg-as-path': 'as-path',
                       'uchg-med': 'med',
                       'uchg-nh': 'next-hop',
                       'rm-as-all': 'all',
                       'rm-as-repl': 'replace-AS',
                       'mp-as-set': ('as-set', 'no-as-set'),
                       'no-prepend': 'no-prepend',
                       'replace-as': 'replace-as',
                       'blackhole': 'blackhole'
        }
        if format == 'no-prefix':
            return 'no ' if not self.enabled else ''
        elif format == 'enable-only' and not self.enabled:
            return ''
        elif format == 'com-ref':
            com_set = self.daemon.comm_set_list.get(self.value, None)
            if com_set is not None and com_set.is_configurable():
                return ' '.join(com_set.mbr_list)
        elif format == 'ext-com-list':
            if type(self.value) is tuple:
                com_val, is_rt = self.value
            else:
                com_val = self.value
                is_rt = None
            if type(com_val) is list:
                com_list = com_val
            else:
                com_list = [com_val]
            frr_com_list = []
            for comm in com_list:
                frr_comm = self.parse_ext_community(comm, is_rt)
                if frr_comm is not None:
                    frr_com_list.append(frr_comm)
            if is_rt is None:
                return ' '.join(frr_com_list)
            else:
                return ('rt ' if is_rt else 'soo ') + ' '.join(frr_com_list)
        elif format == 'ext-com-ref':
            com_set_name, is_rt = self.value
            com_set = self.daemon.extcomm_set_list.get(com_set_name, None)
            if com_set is not None and com_set.is_configurable():
                frr_com_list = []
                for comm in com_set.mbr_list:
                    frr_comm = self.parse_ext_community(comm, is_rt)
                    if frr_comm is not None:
                        frr_com_list.append(frr_comm)
                return ('rt ' if is_rt else 'soo ') + ' '.join(frr_com_list)
        elif format == 'repeat' and type(self.value) is dict:
            if 1 in self.value:
                rep_cnt = int(self.value[1][0])
            else:
                rep_cnt = 1
            if 0 in self.value:
                return ' '.join([self.value[0][0]] * rep_cnt)
        elif format == 'neighbor-set':
            ret_val = BGPConfigDaemon.get_prefix_set_name(self.value, 'NEIGHBOR_SET')
            return ret_val
        elif format == 'nexthop-set':
            ret_val = BGPConfigDaemon.get_prefix_set_name(self.value, 'NEXTHOP_SET')
            return ret_val
        elif format == 'peer-ip':
            if type(self.value) is list and len(self.value) > 0:
                return self.value[0]
            else:
                return self.value
        elif format == 'tx-add-paths':
            if self.value == 'tx_all_paths':
                return 'addpath-tx-all-paths'
            elif self.value == 'tx_best_path_per_as':
                return 'addpath-tx-bestpath-per-AS'
        elif format == 'shutdown-msg':
            if len(self.value) > 0:
                self.value = 'message %s' % self.value
        elif format == 'default-rmap':
            if len(self.value) > 0:
                self.value = 'route-map %s' % self.value
        elif format in bool_format:
            false_val = ''
            if type(bool_format[format]) is tuple:
                if len(bool_format[format]) == 2:
                    true_val, false_val = bool_format[format]
                else:
                    true_val = bool_format[format][0]
            else:
                true_val = bool_format[format]
            if self.value == 'true':
                self.value = true_val
            elif self.value == 'false':
                self.value = false_val
        elif format == 'restart':
            if self.value == 'true':
                self.value = 'warning-only'
            elif self.value == 'false':
                self.value = ''
            elif len(self.value) > 0:
                self.value = 'restart %s' % self.value
        elif format == 'redist-route-map':
            if len(self.value) > 0:
                self.value = 'route-map %s' % self.to_str()
        elif format == 'redist-metric':
            if len(self.value) > 0:
                self.value = 'metric %s' % self.to_str()
        elif format == 'track':
            if len(self.value) > 0:
                self.value = 'track %s' % self.to_str()
        elif format == 'network-policy':
            if len(self.value) > 0:
                self.value = 'route-map %s' % self.to_str()
        elif format == 'src-proto':
            if self.value == 'ospf3':
                self.value = 'ospf6'
        elif format == 'aggr-policy':
            if len(self.value) > 0:
                self.value = 'route-map %s' % self.to_str()
        elif format == 'asn_list':
            self.value = ' '.join(self.value.split(','))
        elif format == 'nh-tag':
            if len(self.value) > 0:
                self.value = 'tag %s' % self.to_str()
        elif format == 'nh-vrf':
            if len(self.value) > 0:
                self.value = 'nexthop-vrf %s' % self.to_str()
        elif format == 'tolower':
            self.tolower = True
        elif format == 'pim_hello_parms':
            self.value = ' '.join(self.value.split(','))
        return self.to_str()