-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(models): add new package to handle protobuf translation
Signed-off-by: Boris Glimcher <[email protected]>
- Loading branch information
Showing
8 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
"net" | ||
|
||
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" | ||
) | ||
|
||
type BridgePortType int32 | ||
|
||
const ( | ||
UNKNOWN BridgePortType = iota | ||
ACCESS | ||
TRUNK | ||
) | ||
|
||
// Port object, separate from protobuf for decoupling | ||
type Port struct { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 { | ||
Vni uint32 | ||
LoopbackIP net.IPNet | ||
VtepIP net.IPNet | ||
} | ||
|
||
// NewVrf creates new VRF object from protobuf message | ||
func NewVrf(in *pb.Vrf) *Vrf { | ||
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} | ||
} | ||
|
||
// 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 | ||
} |