forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_interface.go
229 lines (216 loc) · 8.46 KB
/
logical_interface.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package types
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/contrail/pkg/models"
"github.com/Juniper/contrail/pkg/services"
)
//CreateLogicalInterface does pre-check for create logical_interface
func (sv *ContrailTypeLogicService) CreateLogicalInterface(
ctx context.Context,
request *services.CreateLogicalInterfaceRequest,
) (response *services.CreateLogicalInterfaceResponse, err error) {
logicalInterface := request.GetLogicalInterface()
err = sv.InTransactionDoer.DoInTransaction(
ctx,
func(ctx context.Context) error {
var physicalRouter *models.PhysicalRouter
var physicalInterface *models.PhysicalInterface
physicalRouter, physicalInterface, err = sv.getLogicalInterfaceParents(ctx, logicalInterface)
if err != nil {
return errors.Wrapf(err, "failed to get parent objects")
}
err = validateLogicalInterfaceVLanTag(logicalInterface, physicalRouter)
if err != nil {
return errors.Wrapf(err, "failed to check logical interface vlan id")
}
err = validateLogicalInterfaceDisplayName(logicalInterface, physicalInterface, physicalRouter)
if err != nil {
return errors.Wrapf(err, "failed to validate display name")
}
err = sv.validateLogicalInterfaceESI(ctx, logicalInterface, physicalInterface)
if err != nil {
return errors.Wrapf(err, "failed to validate logical interface ESI")
}
response, err = sv.BaseService.CreateLogicalInterface(ctx, request)
return err
})
return response, err
}
//UpdateLogicalInterface does pre-check for update logical_interface
func (sv *ContrailTypeLogicService) UpdateLogicalInterface(
ctx context.Context,
request *services.UpdateLogicalInterfaceRequest,
) (response *services.UpdateLogicalInterfaceResponse, err error) {
logicalInterface := request.GetLogicalInterface()
err = sv.InTransactionDoer.DoInTransaction(
ctx,
func(ctx context.Context) error {
var storedLogicalInterface *models.LogicalInterface
storedLogicalInterface, err = sv.getLogicalInterface(ctx, logicalInterface.GetUUID())
if err != nil {
return errors.Wrapf(err, "failed to get logical interface with uuid %s", logicalInterface.GetUUID())
}
var physicalRouter *models.PhysicalRouter
var physicalInterface *models.PhysicalInterface
physicalRouter, physicalInterface, err = sv.getLogicalInterfaceParents(ctx, storedLogicalInterface)
if err != nil {
return errors.Wrapf(err, "failed to get parent objects")
}
err = validateLogicalInterfaceUpdateDisplayName(logicalInterface, storedLogicalInterface)
if err != nil {
return errors.Wrapf(err, "failed to validate logical interface display name")
}
err = validateLogicalInterfaceDisplayName(logicalInterface, physicalInterface, physicalRouter)
if err != nil {
return errors.Wrapf(err, "failed to validate logical interface display name")
}
err = validateLogicalInterfaceVLanTag(logicalInterface, physicalRouter)
if err != nil {
return errors.Wrapf(err, "failed to validate logical interface vlan id")
}
err = validateLogicalInterfaceUpdateVLanTag(logicalInterface, storedLogicalInterface)
if err != nil {
return errors.Wrapf(err, "failed to validate logical interface vlan id")
}
err = sv.validateLogicalInterfaceESI(ctx, logicalInterface, physicalInterface)
if err != nil {
return err
}
response, err = sv.BaseService.UpdateLogicalInterface(ctx, request)
return err
})
return response, err
}
func validateLogicalInterfaceVLanTag(
logicalInterface *models.LogicalInterface,
physicalRouter *models.PhysicalRouter,
) error {
if logicalInterface.GetLogicalInterfaceVlanTag() < 0 || logicalInterface.GetLogicalInterfaceVlanTag() > 4094 {
return errutil.ErrorForbiddenf("invalid logical interface vlan id %d", logicalInterface.GetLogicalInterfaceVlanTag())
}
// In case of QFX, check that VLANs 1, 2 and 4094 are not used
if strings.HasPrefix(strings.ToLower(physicalRouter.GetPhysicalRouterProductName()), "qfx") &&
logicalInterface.IsReservedQFXVlanTag() {
return errutil.ErrorBadRequestf("vlan id %d is not allowed on QFX logical interface type %s",
logicalInterface.GetLogicalInterfaceVlanTag(), logicalInterface.GetLogicalInterfaceType())
}
return nil
}
func validateLogicalInterfaceUpdateVLanTag(
logicalInterface *models.LogicalInterface,
storedLogicalInterface *models.LogicalInterface,
) error {
if logicalInterfaceVlanTagExists(logicalInterface) &&
logicalInterfaceVlanTagExists(storedLogicalInterface) &&
logicalInterface.GetLogicalInterfaceVlanTag() != storedLogicalInterface.GetLogicalInterfaceVlanTag() {
return errutil.ErrorForbiddenf("cannot change vlan tag %d", logicalInterface.GetLogicalInterfaceVlanTag())
}
return nil
}
func (sv *ContrailTypeLogicService) getLogicalInterfaceParents(
ctx context.Context,
logicalInterface *models.LogicalInterface,
) (physicalRouter *models.PhysicalRouter, physicalInterface *models.PhysicalInterface, err error) {
physicalRouterUUID := logicalInterface.GetParentUUID()
if logicalInterface.ParentType == "physical-interface" {
physicalInterface, err = sv.getPhysicalInterface(ctx, logicalInterface.GetParentUUID())
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get logical interface physical interface with uuid %s",
logicalInterface.GetParentUUID())
}
physicalRouterUUID = physicalInterface.GetParentUUID()
}
physicalRouter, err = sv.getPhysicalRouter(ctx, physicalRouterUUID)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get logical interface physical router width uuid %s",
physicalRouterUUID)
}
return physicalRouter, physicalInterface, nil
}
func validateLogicalInterfaceDisplayName(
logicalInterface *models.LogicalInterface,
physicalInterface *models.PhysicalInterface,
physicalRouter *models.PhysicalRouter,
) error {
for _, pi := range physicalRouter.GetPhysicalInterfaces() {
if pi.GetDisplayName() == logicalInterface.GetDisplayName() {
return errutil.ErrorConflictf("display name %s of logical interface already in use",
logicalInterface.GetDisplayName())
}
}
if physicalInterface == nil {
return nil
}
for _, li := range physicalInterface.GetLogicalInterfaces() {
if li.GetUUID() != logicalInterface.GetUUID() &&
logicalInterfaceVlanTagExists(li) &&
li.GetLogicalInterfaceVlanTag() == logicalInterface.GetLogicalInterfaceVlanTag() {
return errutil.ErrorConflictf("vlan id of logical interface %d already used"+
"in another interface %s", logicalInterface.GetLogicalInterfaceVlanTag(), li.GetUUID())
}
}
return nil
}
func (sv *ContrailTypeLogicService) validateLogicalInterfaceESI(
ctx context.Context,
logicalInterface *models.LogicalInterface,
physicalInterface *models.PhysicalInterface,
) error {
if physicalInterface == nil || len(physicalInterface.GetEthernetSegmentIdentifier()) == 0 {
return nil
}
physicalInterfaceList, err := sv.listPhysicalInterfacesByESI(ctx, physicalInterface.GetEthernetSegmentIdentifier())
if err != nil {
return err
}
for _, pi := range physicalInterfaceList {
for _, li := range pi.GetLogicalInterfaces() {
if li.GetUUID() == logicalInterface.GetUUID() {
continue
}
otherLogicalInterface, err := sv.getLogicalInterface(ctx, li.GetUUID())
if err != nil {
return err
}
if otherLogicalInterface.GetLogicalInterfaceVlanTag() != logicalInterface.GetLogicalInterfaceVlanTag() {
continue
}
if !isEqualVMIRefs(logicalInterface.GetVirtualMachineInterfaceRefs(),
otherLogicalInterface.GetVirtualMachineInterfaceRefs()) {
return errutil.ErrorForbiddenf(
"LI should refer to the same set of VMIs as peer LIs belonging to the same ESI")
}
}
}
return nil
}
func validateLogicalInterfaceUpdateDisplayName(
logicalInterface *models.LogicalInterface,
storedLogicalInterface *models.LogicalInterface,
) error {
if len(logicalInterface.GetDisplayName()) > 0 &&
logicalInterface.GetDisplayName() != storedLogicalInterface.GetDisplayName() {
return errutil.ErrorForbiddenf("cannot change display name to %s", logicalInterface.GetDisplayName())
}
return nil
}
func logicalInterfaceVlanTagExists(
logicalInterface *models.LogicalInterface,
) bool {
return logicalInterface.GetLogicalInterfaceVlanTag() != 0
}
func (sv *ContrailTypeLogicService) getPhysicalRouter(
ctx context.Context,
uuid string,
) (*models.PhysicalRouter, error) {
physicalRouterResponse, err := sv.ReadService.GetPhysicalRouter(ctx, &services.GetPhysicalRouterRequest{
ID: uuid,
})
if err != nil {
return nil, err
}
return physicalRouterResponse.GetPhysicalRouter(), nil
}