-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(models): add new package to handle protobuf translation #267
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. | ||
|
||
// Package models translates frontend protobuf messages to backend messages | ||
package models | ||
|
||
import ( | ||
// "encoding/binary" | ||
"net" | ||
|
||
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" | ||
) | ||
|
||
// Bridge object, separate from protobuf for decoupling | ||
type Bridge struct { | ||
Vni uint32 | ||
VlanID uint32 | ||
VtepIP net.IPNet | ||
} | ||
|
||
// NewBridge creates new SVI object from protobuf message | ||
func NewBridge(in *pb.LogicalBridge) *Bridge { | ||
// vtepip := make(net.IP, 4) | ||
// binary.BigEndian.PutUint32(vtepip, in.Spec.VtepIpPrefix.Addr.GetV4Addr()) | ||
// vip := net.IPNet{IP: vtepip, Mask: net.CIDRMask(int(in.Spec.VtepIpPrefix.Len), 32)} | ||
// TODO: Vni: *in.Spec.Vni | ||
return &Bridge{VlanID: in.Spec.VlanId} | ||
} | ||
|
||
// ToPb transforms SVI object to protobuf message | ||
func (in *Bridge) ToPb() (*pb.LogicalBridge, error) { | ||
bridge := &pb.LogicalBridge{ | ||
Spec: &pb.LogicalBridgeSpec{ | ||
Vni: &in.Vni, | ||
VlanId: in.VlanID, | ||
}, | ||
Status: &pb.LogicalBridgeStatus{ | ||
OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP, | ||
}, | ||
} | ||
// TODO: add VtepIpPrefix | ||
return bridge, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. | ||
|
||
// Package models translates frontend protobuf messages to backend messages | ||
package models | ||
|
||
import ( | ||
"net" | ||
|
||
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" | ||
) | ||
|
||
// BridgePortType reflects the different types of a Bridge Port | ||
type BridgePortType int32 | ||
|
||
const ( | ||
// UNKNOWN bridge port type | ||
UNKNOWN BridgePortType = iota | ||
// ACCESS bridge port type | ||
ACCESS | ||
// TRUNK bridge port type | ||
TRUNK | ||
) | ||
|
||
// Port object, separate from protobuf for decoupling | ||
type Port struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like we can also add interface here |
||
Ptype BridgePortType | ||
MacAddress net.HardwareAddr | ||
LogicalBridgeRefKeys []string | ||
} | ||
|
||
// NewPort creates new SVI object from protobuf message | ||
func NewPort(in *pb.BridgePort) *Port { | ||
mac := net.HardwareAddr(in.Spec.MacAddress) | ||
return &Port{Ptype: BridgePortType(in.Spec.Ptype), MacAddress: mac, LogicalBridgeRefKeys: in.Spec.LogicalBridges} | ||
} | ||
|
||
// ToPb transforms SVI object to protobuf message | ||
func (in *Port) ToPb() (*pb.BridgePort, error) { | ||
port := &pb.BridgePort{ | ||
Spec: &pb.BridgePortSpec{ | ||
Ptype: pb.BridgePortType(in.Ptype), | ||
MacAddress: in.MacAddress, | ||
LogicalBridges: in.LogicalBridgeRefKeys, | ||
}, | ||
Status: &pb.BridgePortStatus{ | ||
OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP, | ||
}, | ||
} | ||
// TODO: add VtepIpPrefix | ||
return port, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. | ||
|
||
// Package models translates frontend protobuf messages to backend messages | ||
package models | ||
|
||
import ( | ||
"encoding/binary" | ||
"net" | ||
|
||
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" | ||
) | ||
|
||
// Svi object, separate from protobuf for decoupling | ||
type Svi struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like we can also add interface here |
||
VrfRefKey string | ||
LogicalBridgeRefKey string | ||
MacAddress net.HardwareAddr | ||
GwIP []net.IPNet | ||
EnableBgp bool | ||
RemoteAs uint32 | ||
} | ||
|
||
// NewSvi creates new SVI object from protobuf message | ||
func NewSvi(in *pb.Svi) *Svi { | ||
mac := net.HardwareAddr(in.Spec.MacAddress) | ||
gwIPList := []net.IPNet{} | ||
for _, item := range in.Spec.GwIpPrefix { | ||
myip := make(net.IP, 4) | ||
binary.BigEndian.PutUint32(myip, item.Addr.GetV4Addr()) | ||
gip := net.IPNet{IP: myip, Mask: net.CIDRMask(int(item.Len), 32)} | ||
gwIPList = append(gwIPList, gip) | ||
} | ||
svi := &Svi{ | ||
VrfRefKey: in.Spec.Vrf, | ||
LogicalBridgeRefKey: in.Spec.LogicalBridge, | ||
MacAddress: mac, | ||
GwIP: gwIPList, | ||
EnableBgp: in.Spec.EnableBgp, | ||
RemoteAs: in.Spec.RemoteAs, | ||
} | ||
return svi | ||
} | ||
|
||
// ToPb transforms SVI object to protobuf message | ||
func (in *Svi) ToPb() (*pb.Svi, error) { | ||
svi := &pb.Svi{ | ||
Spec: &pb.SviSpec{ | ||
Vrf: in.VrfRefKey, | ||
LogicalBridge: in.LogicalBridgeRefKey, | ||
MacAddress: in.MacAddress, | ||
EnableBgp: in.EnableBgp, | ||
RemoteAs: in.RemoteAs, | ||
}, | ||
Status: &pb.SviStatus{ | ||
OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP, | ||
}, | ||
} | ||
// TODO: add GwIpPrefix | ||
return svi, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. | ||
|
||
// Package models translates frontend protobuf messages to backend messages | ||
package models | ||
|
||
import ( | ||
"encoding/binary" | ||
"net" | ||
|
||
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" | ||
) | ||
|
||
// Vrf object, separate from protobuf for decoupling | ||
type Vrf struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like we can also add interface here |
||
Vni uint32 | ||
LoopbackIP net.IPNet | ||
VtepIP net.IPNet | ||
LocalAs int | ||
RoutingTable uint32 | ||
MacAddress net.HardwareAddr | ||
} | ||
|
||
// NewVrf creates new VRF object from protobuf message | ||
func NewVrf(in *pb.Vrf) *Vrf { | ||
mac := net.HardwareAddr(in.Status.Rmac) | ||
loopip := make(net.IP, 4) | ||
binary.BigEndian.PutUint32(loopip, in.Spec.LoopbackIpPrefix.Addr.GetV4Addr()) | ||
lip := net.IPNet{IP: loopip, Mask: net.CIDRMask(int(in.Spec.LoopbackIpPrefix.Len), 32)} | ||
// vtepip := make(net.IP, 4) | ||
// binary.BigEndian.PutUint32(vtepip, in.Spec.VtepIpPrefix.Addr.GetV4Addr()) | ||
// vip := net.IPNet{IP: vtepip, Mask: net.CIDRMask(int(in.Spec.VtepIpPrefix.Len), 32)} | ||
return &Vrf{LoopbackIP: lip, MacAddress: mac, RoutingTable: in.Status.RoutingTable} | ||
} | ||
|
||
// ToPb transforms VRF object to protobuf message | ||
func (in *Vrf) ToPb() (*pb.Vrf, error) { | ||
vrf := &pb.Vrf{ | ||
Spec: &pb.VrfSpec{ | ||
Vni: &in.Vni, | ||
}, | ||
Status: &pb.VrfStatus{ | ||
LocalAs: 4, | ||
}, | ||
} | ||
// TODO: add LocalAs, LoopbackIP, VtepIP | ||
return vrf, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like we can also add interface here