forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_system_config.go
181 lines (149 loc) · 5.06 KB
/
global_system_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package types
import (
"context"
"github.com/pkg/errors"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/asf/pkg/services/baseservices"
"github.com/Juniper/contrail/pkg/models"
"github.com/Juniper/contrail/pkg/services"
asfmodels "github.com/Juniper/asf/pkg/models"
)
const defaultGSCName = "default-global-system-config"
// CreateGlobalSystemConfig by design should never be called.
// GlobalSystemConfig can only be created by DBInit, never by user request
func (sv *ContrailTypeLogicService) CreateGlobalSystemConfig(
ctx context.Context, request *services.CreateGlobalSystemConfigRequest,
) (resp *services.CreateGlobalSystemConfigResponse, err error) {
return nil, errutil.ErrorBadRequest("Trying to call create GlobalSystemConfig outside of DBInit")
}
// UpdateGlobalSystemConfig performs type specific checks for update.
func (sv *ContrailTypeLogicService) UpdateGlobalSystemConfig(
ctx context.Context, request *services.UpdateGlobalSystemConfigRequest,
) (*services.UpdateGlobalSystemConfigResponse, error) {
var resp *services.UpdateGlobalSystemConfigResponse
err := sv.InTransactionDoer.DoInTransaction(
ctx,
func(ctx context.Context) (err error) {
// TODO(Jan Darowski) JBE-431 - Add proper uuid update when using draft object
// (enable_security_policy_draft)
oldObjResp, err := sv.ReadService.GetGlobalSystemConfig(ctx, &services.GetGlobalSystemConfigRequest{
ID: request.GetGlobalSystemConfig().GetUUID(),
})
updateObj := request.GlobalSystemConfig
if err != nil {
return err
}
if oldObjResp == nil {
return errors.Errorf("No GlobalSystemConfig found to update")
}
oldObj := oldObjResp.GlobalSystemConfig
err = sv.checkUdc(updateObj)
if err != nil {
return err
}
err = sv.checkAsn(ctx, updateObj)
if err != nil {
return err
}
err = sv.checkBgpaasPorts(ctx, updateObj, oldObj)
if err != nil {
return err
}
resp, err = sv.BaseService.UpdateGlobalSystemConfig(ctx, request)
return err
})
if err != nil {
return nil, errors.Wrap(err, "GlobalSystemConfig update validation failed")
}
return resp, nil
}
func (sv *ContrailTypeLogicService) checkUdc(obj *models.GlobalSystemConfig) (err error) {
if obj.UserDefinedLogStatistics == nil {
return nil
}
return obj.UserDefinedLogStatistics.ValidateRegexps()
}
func (sv *ContrailTypeLogicService) checkAsn(ctx context.Context, updateObj *models.GlobalSystemConfig) (err error) {
globalAsn := updateObj.AutonomousSystem
if globalAsn == 0 {
return nil
}
vnList, err := sv.ReadService.ListVirtualNetwork(ctx, &services.ListVirtualNetworkRequest{Spec: &baseservices.ListSpec{
Fields: []string{models.VirtualNetworkFieldRouteTargetList}},
})
if err != nil {
return err
}
var multiError errutil.MultiError
for _, vn := range vnList.VirtualNetworks {
rtList := vn.RouteTargetList
for _, rt := range rtList.RouteTarget {
userDefined, err := models.IsRouteTargetUserDefined(rt, globalAsn)
if err != nil {
return err
}
if !userDefined {
multiError = append(multiError, errors.Errorf("\t- %s (%s) have route target %s configured\n",
vn.FQName, vn.UUID, rt))
}
}
}
if multiError != nil {
return errors.Wrapf(multiError, "Virtual networks are configured with a route target with this ASN %d "+
"and route target value in the same range as used by automatically allocated route targets:\n", globalAsn)
}
return nil
}
func (sv *ContrailTypeLogicService) checkBgpaasPorts(ctx context.Context, updateObj *models.GlobalSystemConfig,
oldObj *models.GlobalSystemConfig) (err error) {
if updateObj.BgpaasParameters == nil {
return nil
}
err = updateObj.BgpaasParameters.ValidatePortRange()
if err != nil {
return err
}
newPortsRange := updateObj.GetBgpaasParameters()
oldPortsRange := models.GetDefaultBGPaaServiceParameters()
if oldObj.BgpaasParameters != nil {
oldPortsRange = oldObj.BgpaasParameters
}
bgpaasList, err := sv.ReadService.ListBGPAsAService(ctx, &services.ListBGPAsAServiceRequest{
Spec: &baseservices.ListSpec{Count: true},
})
if err != nil {
return err
}
if len(bgpaasList.BGPAsAServices) > 0 {
if !newPortsRange.EnclosesRange(oldPortsRange) {
return errors.Errorf("BGP Port range cannot be shrunk. Old: (%d %d) New: (%d %d)",
oldPortsRange.PortStart,
oldPortsRange.PortEnd,
newPortsRange.PortStart,
newPortsRange.PortEnd)
}
}
return nil
}
func (sv *ContrailTypeLogicService) getDefaultGlobalSystemConfigUUID(ctx context.Context) (string, error) {
if sv.defaultGSCUUID != "" {
return sv.defaultGSCUUID, nil
}
defaultGSCFqName := sv.getDefaultGlobalSystemConfigFqName()
defaultGSCMeta, err := sv.MetadataGetter.GetMetadata(
ctx,
asfmodels.Metadata{
FQName: defaultGSCFqName,
Type: models.KindGlobalSystemConfig,
},
)
if err != nil {
return "", errutil.ErrorBadRequestf("Cannot resolve Global System Config with FQName %v: %v",
defaultGSCFqName, err)
}
sv.defaultGSCUUID = defaultGSCMeta.UUID
return sv.defaultGSCUUID, nil
}
func (sv *ContrailTypeLogicService) getDefaultGlobalSystemConfigFqName() []string {
return []string{defaultGSCName}
}