forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_network.go
152 lines (127 loc) · 4.74 KB
/
virtual_network.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
package models
import (
"fmt"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/asf/pkg/format"
"github.com/Juniper/asf/pkg/models"
)
// Virtual network forwarding modes.
const (
L3Mode = "l3"
L2L3Mode = "l2_l3"
)
// TODO: Enums strings should be generated from schema
const (
UserDefinedSubnetOnly = "user-defined-subnet-only"
UserDefinedSubnetPreferred = "user-defined-subnet-preferred"
FlatSubnetOnly = "flat-subnet-only"
)
//MakeNeutronCompatible makes this resource data neutron compatible.
func (m *VirtualNetwork) MakeNeutronCompatible() {
// neutorn <-> vnc sharing
if m.Perms2.GlobalAccess == models.PermsRWX {
m.IsShared = true
}
if m.IsShared {
m.Perms2.GlobalAccess = models.PermsRWX
}
}
//HasVirtualNetworkNetworkID check if the resource has virtual network ID.
func (m *VirtualNetwork) HasVirtualNetworkNetworkID() bool {
return m.VirtualNetworkNetworkID != 0
}
//IsSupportingAnyVPNType checks if this network is l2 and l3 mode
func (m *VirtualNetwork) IsSupportingAnyVPNType() bool {
return m.GetVirtualNetworkProperties().GetForwardingMode() == L2L3Mode
}
//IsSupportingL3VPNType checks if this network is l3 mode
func (m *VirtualNetwork) IsSupportingL3VPNType() bool {
return m.GetVirtualNetworkProperties().GetForwardingMode() == L3Mode
}
// CheckMultiPolicyServiceChainConfig checks if multi policy service chain config is valid.
func (m *VirtualNetwork) CheckMultiPolicyServiceChainConfig() error {
if !m.MultiPolicyServiceChainsEnabled {
return nil
}
if len(m.GetRouteTargetList().GetRouteTarget()) != 0 {
return errutil.ErrorBadRequest("Multi Policy Service Chains enabled: " +
"Route Target List should be empty")
}
if m.isAnyRouteTargetInImportAndExportList() {
return errutil.ErrorBadRequest("Multi Policy Service Chains enabled: " +
"there cannot be same Route Target in Import Route Target List and Export Route Target List")
}
return nil
}
func (m *VirtualNetwork) isAnyRouteTargetInImportAndExportList() bool {
importRTs := make(map[string]bool)
for _, importRT := range m.GetImportRouteTargetList().GetRouteTarget() {
importRTs[importRT] = true
}
for _, exportRT := range m.GetExportRouteTargetList().GetRouteTarget() {
if importRTs[exportRT] {
return true
}
}
return false
}
//ShouldIgnoreAllocation checks if there is ip-fabric or link-local address allocation
func (m *VirtualNetwork) ShouldIgnoreAllocation() bool {
fqName := m.GetFQName()
if format.ContainsString(fqName, "ip-fabric") || format.ContainsString(fqName, "__link_local__") {
return true
}
return false
}
// GetIpamSubnets returns list of subnets contained in IPAM references of this VN.
func (m *VirtualNetwork) GetIpamSubnets() (s *IpamSubnets) {
s = &IpamSubnets{}
for _, networkIpam := range m.GetNetworkIpamRefs() {
s.Subnets = append(s.Subnets, networkIpam.GetAttr().GetIpamSubnets()...)
}
return s
}
// GetAddressAllocationMethod returns address allocation method
func (m *VirtualNetwork) GetAddressAllocationMethod() string {
allocationMethod := UserDefinedSubnetPreferred
if m.GetAddressAllocationMode() != "" {
allocationMethod = m.GetAddressAllocationMode()
}
return allocationMethod
}
// GetDefaultRoutingInstance returns the default routing instance of VN or nil if it doesn't exist
func (m *VirtualNetwork) GetDefaultRoutingInstance() *RoutingInstance {
for _, ri := range m.RoutingInstances {
if ri.GetRoutingInstanceIsDefault() {
return ri
}
}
return nil
}
// HasNetworkBasedAllocationMethod checks if allocation method is userdefined or flat subnet only
func (m *VirtualNetwork) HasNetworkBasedAllocationMethod() bool {
return m.GetAddressAllocationMethod() == UserDefinedSubnetOnly || m.GetAddressAllocationMethod() == FlatSubnetOnly
}
// MakeDefaultRoutingInstance returns the default routing instance for the network.
func (m *VirtualNetwork) MakeDefaultRoutingInstance() *RoutingInstance {
return &RoutingInstance{
Name: m.Name,
FQName: m.DefaultRoutingInstanceFQName(),
ParentUUID: m.UUID,
RoutingInstanceIsDefault: true,
RoutingInstanceFabricSnat: m.FabricSnat,
}
}
// DefaultRoutingInstanceFQName returns the FQName of the network's default RoutingInstance.
func (m *VirtualNetwork) DefaultRoutingInstanceFQName() []string {
return models.ChildFQName(m.FQName, m.FQName[len(m.FQName)-1])
}
// IsLinkLocal returns true if virtual network FQName fits Link Local
func (m *VirtualNetwork) IsLinkLocal() bool {
fq := []string{"default-domain", "default-project", "__link_local__"}
return models.FQNameEquals(fq, m.GetFQName())
}
// VxLANIntOwner returns the owner for allocating the network's VxLAN IDs.
func (m *VirtualNetwork) VxLANIntOwner() string {
return fmt.Sprintf("%s_vxlan", models.FQNameToString(m.GetFQName()))
}