This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
memif_interface_converter.go
115 lines (98 loc) · 3.7 KB
/
memif_interface_converter.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
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package converter
import (
"os"
"path"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connection/mechanisms/memif"
"github.com/pkg/errors"
"go.ligato.io/vpp-agent/v3/proto/ligato/configurator"
"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
vpp_l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connection"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connectioncontext"
)
type MemifInterfaceConverter struct {
*connection.Connection
conversionParameters *ConnectionConversionParameters
}
func NewMemifInterfaceConverter(c *connection.Connection, conversionParameters *ConnectionConversionParameters) Converter {
rv := &MemifInterfaceConverter{
Connection: c,
conversionParameters: conversionParameters,
}
return rv
}
func (c *MemifInterfaceConverter) ToDataRequest(rv *configurator.Config, connect bool) (*configurator.Config, error) {
if rv == nil {
rv = &configurator.Config{}
}
if rv.VppConfig == nil {
rv.VppConfig = &vpp.ConfigData{}
}
fullyQualifiedSocketFilename := path.Join(c.conversionParameters.BaseDir, memif.ToMechanism(c.Connection.GetMechanism()).GetSocketFilename())
SocketDir := path.Dir(fullyQualifiedSocketFilename)
var isMaster bool
if c.conversionParameters.Side == DESTINATION {
isMaster = c.conversionParameters.Terminate
} else {
isMaster = !c.conversionParameters.Terminate
}
if isMaster {
if err := os.MkdirAll(SocketDir, os.ModePerm); err != nil {
return nil, err
}
}
var ipAddresses []string
if c.conversionParameters.Terminate && c.conversionParameters.Side == DESTINATION {
ipAddresses = []string{c.Connection.GetContext().GetIpContext().GetDstIpAddr()}
}
if c.conversionParameters.Terminate && c.conversionParameters.Side == SOURCE {
ipAddresses = []string{c.Connection.GetContext().GetIpContext().GetSrcIpAddr()}
}
if c.conversionParameters.Name == "" {
return nil, errors.New("ConnnectionConversionParameters.Name cannot be empty")
}
rv.VppConfig.Interfaces = append(rv.VppConfig.Interfaces, &vpp.Interface{
Name: c.conversionParameters.Name,
Type: vpp_interfaces.Interface_MEMIF,
Enabled: true,
IpAddresses: ipAddresses,
Link: &vpp_interfaces.Interface_Memif{
Memif: &vpp_interfaces.MemifLink{
Master: isMaster,
SocketFilename: path.Join(fullyQualifiedSocketFilename),
},
},
})
// Process static routes
routes := []*connectioncontext.Route{}
switch c.conversionParameters.Side {
case SOURCE:
routes = c.Connection.GetContext().GetIpContext().GetDstRoutes()
case DESTINATION:
routes = c.Connection.GetContext().GetIpContext().GetSrcRoutes()
}
for _, route := range routes {
route := &vpp.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: route.Prefix,
NextHopAddr: extractCleanIPAddress(c.Connection.GetContext().GetIpContext().GetDstIpAddr()),
OutgoingInterface: c.conversionParameters.Name,
}
rv.VppConfig.Routes = append(rv.VppConfig.Routes, route)
}
return rv, nil
}