github.com/osrg/gobgp/v3@v3.30.0/pkg/config/oc/serve.go (about) 1 package oc 2 3 import ( 4 "github.com/fsnotify/fsnotify" 5 "github.com/spf13/viper" 6 7 "github.com/osrg/gobgp/v3/pkg/log" 8 ) 9 10 type BgpConfigSet struct { 11 Global Global `mapstructure:"global"` 12 Neighbors []Neighbor `mapstructure:"neighbors"` 13 PeerGroups []PeerGroup `mapstructure:"peer-groups"` 14 RpkiServers []RpkiServer `mapstructure:"rpki-servers"` 15 BmpServers []BmpServer `mapstructure:"bmp-servers"` 16 Vrfs []Vrf `mapstructure:"vrfs"` 17 MrtDump []Mrt `mapstructure:"mrt-dump"` 18 Zebra Zebra `mapstructure:"zebra"` 19 Collector Collector `mapstructure:"collector"` 20 DefinedSets DefinedSets `mapstructure:"defined-sets"` 21 PolicyDefinitions []PolicyDefinition `mapstructure:"policy-definitions"` 22 DynamicNeighbors []DynamicNeighbor `mapstructure:"dynamic-neighbors"` 23 } 24 25 func ReadConfigfile(path, format string) (*BgpConfigSet, error) { 26 // Update config file type, if detectable 27 format = detectConfigFileType(path, format) 28 29 config := &BgpConfigSet{} 30 v := viper.New() 31 v.SetConfigFile(path) 32 v.SetConfigType(format) 33 var err error 34 if err = v.ReadInConfig(); err != nil { 35 return nil, err 36 } 37 if err = v.UnmarshalExact(config); err != nil { 38 return nil, err 39 } 40 if err = setDefaultConfigValuesWithViper(v, config); err != nil { 41 return nil, err 42 } 43 return config, nil 44 } 45 46 func WatchConfigFile(path, format string, callBack func()) { 47 v := viper.New() 48 v.SetConfigFile(path) 49 v.SetConfigType(format) 50 51 v.OnConfigChange(func(e fsnotify.Event) { 52 callBack() 53 }) 54 55 v.WatchConfig() 56 } 57 58 func ConfigSetToRoutingPolicy(c *BgpConfigSet) *RoutingPolicy { 59 return &RoutingPolicy{ 60 DefinedSets: c.DefinedSets, 61 PolicyDefinitions: c.PolicyDefinitions, 62 } 63 } 64 65 func UpdatePeerGroupConfig(logger log.Logger, curC, newC *BgpConfigSet) ([]PeerGroup, []PeerGroup, []PeerGroup) { 66 addedPg := []PeerGroup{} 67 deletedPg := []PeerGroup{} 68 updatedPg := []PeerGroup{} 69 70 for _, n := range newC.PeerGroups { 71 if idx := existPeerGroup(n.Config.PeerGroupName, curC.PeerGroups); idx < 0 { 72 addedPg = append(addedPg, n) 73 } else if !n.Equal(&curC.PeerGroups[idx]) { 74 logger.Debug("Current peer-group config", 75 log.Fields{ 76 "Topic": "Config", 77 "Key": curC.PeerGroups[idx]}) 78 logger.Debug("New peer-group config", 79 log.Fields{ 80 "Topic": "Config", 81 "Key": n}) 82 updatedPg = append(updatedPg, n) 83 } 84 } 85 86 for _, n := range curC.PeerGroups { 87 if existPeerGroup(n.Config.PeerGroupName, newC.PeerGroups) < 0 { 88 deletedPg = append(deletedPg, n) 89 } 90 } 91 return addedPg, deletedPg, updatedPg 92 } 93 94 func UpdateNeighborConfig(logger log.Logger, curC, newC *BgpConfigSet) ([]Neighbor, []Neighbor, []Neighbor) { 95 added := []Neighbor{} 96 deleted := []Neighbor{} 97 updated := []Neighbor{} 98 99 for _, n := range newC.Neighbors { 100 if idx := inSlice(n, curC.Neighbors); idx < 0 { 101 added = append(added, n) 102 } else if !n.Equal(&curC.Neighbors[idx]) { 103 logger.Debug("Current neighbor config", 104 log.Fields{ 105 "Topic": "Config", 106 "Key": curC.Neighbors[idx]}) 107 logger.Debug("New neighbor config", 108 log.Fields{ 109 "Topic": "Config", 110 "Key": n}) 111 updated = append(updated, n) 112 } 113 } 114 115 for _, n := range curC.Neighbors { 116 if inSlice(n, newC.Neighbors) < 0 { 117 deleted = append(deleted, n) 118 } 119 } 120 return added, deleted, updated 121 } 122 123 func CheckPolicyDifference(logger log.Logger, currentPolicy *RoutingPolicy, newPolicy *RoutingPolicy) bool { 124 logger.Debug("Current policy", 125 log.Fields{ 126 "Topic": "Config", 127 "Key": currentPolicy}) 128 logger.Debug("New policy", 129 log.Fields{ 130 "Topic": "Config", 131 "Key": newPolicy}) 132 133 var result bool 134 if currentPolicy == nil && newPolicy == nil { 135 result = false 136 } else { 137 if currentPolicy != nil && newPolicy != nil { 138 result = !currentPolicy.Equal(newPolicy) 139 } else { 140 result = true 141 } 142 } 143 return result 144 }