github.com/osrg/gobgp@v2.0.0+incompatible/internal/pkg/config/serve.go (about)

     1  package config
     2  
     3  import (
     4  	"os"
     5  	"os/signal"
     6  	"syscall"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/spf13/viper"
    10  )
    11  
    12  type BgpConfigSet struct {
    13  	Global            Global             `mapstructure:"global"`
    14  	Neighbors         []Neighbor         `mapstructure:"neighbors"`
    15  	PeerGroups        []PeerGroup        `mapstructure:"peer-groups"`
    16  	RpkiServers       []RpkiServer       `mapstructure:"rpki-servers"`
    17  	BmpServers        []BmpServer        `mapstructure:"bmp-servers"`
    18  	Vrfs              []Vrf              `mapstructure:"vrfs"`
    19  	MrtDump           []Mrt              `mapstructure:"mrt-dump"`
    20  	Zebra             Zebra              `mapstructure:"zebra"`
    21  	Collector         Collector          `mapstructure:"collector"`
    22  	DefinedSets       DefinedSets        `mapstructure:"defined-sets"`
    23  	PolicyDefinitions []PolicyDefinition `mapstructure:"policy-definitions"`
    24  	DynamicNeighbors  []DynamicNeighbor  `mapstructure:"dynamic-neighbors"`
    25  }
    26  
    27  func ReadConfigfileServe(path, format string, configCh chan *BgpConfigSet) {
    28  	sigCh := make(chan os.Signal, 1)
    29  	signal.Notify(sigCh, syscall.SIGHUP)
    30  
    31  	// Update config file type, if detectable
    32  	format = detectConfigFileType(path, format)
    33  
    34  	cnt := 0
    35  	for {
    36  		c := &BgpConfigSet{}
    37  		v := viper.New()
    38  		v.SetConfigFile(path)
    39  		v.SetConfigType(format)
    40  		var err error
    41  		if err = v.ReadInConfig(); err != nil {
    42  			goto ERROR
    43  		}
    44  		if err = v.UnmarshalExact(c); err != nil {
    45  			goto ERROR
    46  		}
    47  		if err = setDefaultConfigValuesWithViper(v, c); err != nil {
    48  			goto ERROR
    49  		}
    50  		if cnt == 0 {
    51  			log.WithFields(log.Fields{
    52  				"Topic": "Config",
    53  			}).Info("Finished reading the config file")
    54  		}
    55  		cnt++
    56  		configCh <- c
    57  		goto NEXT
    58  	ERROR:
    59  		if cnt == 0 {
    60  			log.WithFields(log.Fields{
    61  				"Topic": "Config",
    62  				"Error": err,
    63  			}).Fatalf("Can't read config file %s", path)
    64  		} else {
    65  			log.WithFields(log.Fields{
    66  				"Topic": "Config",
    67  				"Error": err,
    68  			}).Warningf("Can't read config file %s", path)
    69  		}
    70  	NEXT:
    71  		<-sigCh
    72  		log.WithFields(log.Fields{
    73  			"Topic": "Config",
    74  		}).Info("Reload the config file")
    75  	}
    76  }
    77  
    78  func ConfigSetToRoutingPolicy(c *BgpConfigSet) *RoutingPolicy {
    79  	return &RoutingPolicy{
    80  		DefinedSets:       c.DefinedSets,
    81  		PolicyDefinitions: c.PolicyDefinitions,
    82  	}
    83  }
    84  
    85  func UpdatePeerGroupConfig(curC, newC *BgpConfigSet) ([]PeerGroup, []PeerGroup, []PeerGroup) {
    86  	addedPg := []PeerGroup{}
    87  	deletedPg := []PeerGroup{}
    88  	updatedPg := []PeerGroup{}
    89  
    90  	for _, n := range newC.PeerGroups {
    91  		if idx := existPeerGroup(n.Config.PeerGroupName, curC.PeerGroups); idx < 0 {
    92  			addedPg = append(addedPg, n)
    93  		} else if !n.Equal(&curC.PeerGroups[idx]) {
    94  			log.WithFields(log.Fields{
    95  				"Topic": "Config",
    96  			}).Debugf("Current peer-group config:%v", curC.PeerGroups[idx])
    97  			log.WithFields(log.Fields{
    98  				"Topic": "Config",
    99  			}).Debugf("New peer-group config:%v", n)
   100  			updatedPg = append(updatedPg, n)
   101  		}
   102  	}
   103  
   104  	for _, n := range curC.PeerGroups {
   105  		if existPeerGroup(n.Config.PeerGroupName, newC.PeerGroups) < 0 {
   106  			deletedPg = append(deletedPg, n)
   107  		}
   108  	}
   109  	return addedPg, deletedPg, updatedPg
   110  }
   111  
   112  func UpdateNeighborConfig(curC, newC *BgpConfigSet) ([]Neighbor, []Neighbor, []Neighbor) {
   113  	added := []Neighbor{}
   114  	deleted := []Neighbor{}
   115  	updated := []Neighbor{}
   116  
   117  	for _, n := range newC.Neighbors {
   118  		if idx := inSlice(n, curC.Neighbors); idx < 0 {
   119  			added = append(added, n)
   120  		} else if !n.Equal(&curC.Neighbors[idx]) {
   121  			log.WithFields(log.Fields{
   122  				"Topic": "Config",
   123  			}).Debugf("Current neighbor config:%v", curC.Neighbors[idx])
   124  			log.WithFields(log.Fields{
   125  				"Topic": "Config",
   126  			}).Debugf("New neighbor config:%v", n)
   127  			updated = append(updated, n)
   128  		}
   129  	}
   130  
   131  	for _, n := range curC.Neighbors {
   132  		if inSlice(n, newC.Neighbors) < 0 {
   133  			deleted = append(deleted, n)
   134  		}
   135  	}
   136  	return added, deleted, updated
   137  }
   138  
   139  func CheckPolicyDifference(currentPolicy *RoutingPolicy, newPolicy *RoutingPolicy) bool {
   140  
   141  	log.WithFields(log.Fields{
   142  		"Topic": "Config",
   143  	}).Debugf("Current policy:%v", currentPolicy)
   144  	log.WithFields(log.Fields{
   145  		"Topic": "Config",
   146  	}).Debugf("New policy:%v", newPolicy)
   147  
   148  	var result bool
   149  	if currentPolicy == nil && newPolicy == nil {
   150  		result = false
   151  	} else {
   152  		if currentPolicy != nil && newPolicy != nil {
   153  			result = !currentPolicy.Equal(newPolicy)
   154  		} else {
   155  			result = true
   156  		}
   157  	}
   158  	return result
   159  }