Skip to content

Commit

Permalink
feat: increase billing function control capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jin-xiaofeng committed Aug 12, 2024
1 parent 2e8f0e7 commit 1d38c5b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 66 deletions.
2 changes: 2 additions & 0 deletions server/controller/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ const (
VTAP_LICENSE_FUNCTION_MAX
)

const VTAP_ALL_LICENSE_FUNCTIONS = "1,2,3,4,5,6,7,8"

var VTAP_TYPE_TO_DEVICE_TYPE = map[int]int{
VTAP_TYPE_KVM: VIF_DEVICE_TYPE_HOST,
VTAP_TYPE_ESXI: VIF_DEVICE_TYPE_HOST,
Expand Down
98 changes: 43 additions & 55 deletions server/controller/trisolaris/metadata/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,41 @@ func (p *Policy) toSerializeString() {
p.allDataHash = h64.Sum64()
}
}
if p.billingMethod == BILLING_METHOD_LICENSE {
if len(p.npbFlowACLs) > 0 {
npbFlowACLsProto := trident.FlowAcls{
FlowAcl: p.npbFlowACLs,
}
p.npbSerializeString, err = npbFlowACLsProto.Marshal()
if err != nil {
log.Error(p.Log(err.Error()))
} else {
h64 := fnv.New64()
h64.Write(p.npbSerializeString)
p.npbDataHash = h64.Sum64()
}
if len(p.npbFlowACLs) > 0 {
npbFlowACLsProto := trident.FlowAcls{
FlowAcl: p.npbFlowACLs,
}
p.npbSerializeString, err = npbFlowACLsProto.Marshal()
if err != nil {
log.Error(p.Log(err.Error()))
} else {
h64 := fnv.New64()
h64.Write(p.npbSerializeString)
p.npbDataHash = h64.Sum64()
}
}

if len(p.pcapFlowACLs) > 0 {
pcapFlowACLsProto := trident.FlowAcls{
FlowAcl: p.pcapFlowACLs,
}
p.pcapSerializeString, err = pcapFlowACLsProto.Marshal()
if err != nil {
log.Error(p.Log(err.Error()))
} else {
h64 := fnv.New64()
h64.Write(p.pcapSerializeString)
p.pcapDataHash = h64.Sum64()
}
if len(p.pcapFlowACLs) > 0 {
pcapFlowACLsProto := trident.FlowAcls{
FlowAcl: p.pcapFlowACLs,
}
p.pcapSerializeString, err = pcapFlowACLsProto.Marshal()
if err != nil {
log.Error(p.Log(err.Error()))
} else {
h64 := fnv.New64()
h64.Write(p.pcapSerializeString)
p.pcapDataHash = h64.Sum64()
}
}
}

func (p *Policy) addFlowACL(flowACL *trident.FlowAcl, aclType int) {
p.flowACLs = append(p.flowACLs, flowACL)
if p.billingMethod == BILLING_METHOD_LICENSE {
if aclType == APPLICATION_NPB {
p.npbFlowACLs = append(p.npbFlowACLs, flowACL)
} else if aclType == APPLICATION_PCAP {
p.pcapFlowACLs = append(p.pcapFlowACLs, flowACL)
}
if aclType == APPLICATION_NPB {
p.npbFlowACLs = append(p.npbFlowACLs, flowACL)
} else if aclType == APPLICATION_PCAP {
p.pcapFlowACLs = append(p.pcapFlowACLs, flowACL)
}
}

Expand All @@ -143,38 +139,30 @@ var tFunction mapset.Set = mapset.NewSet(VTAP_LICENSE_FUNCTION_TRAFFIC_DISTRIBUT
var nFunction mapset.Set = mapset.NewSet(VTAP_LICENSE_FUNCTION_NETWORK_MONITORING)

func (p *Policy) getPolicyString(functions mapset.Set) []byte {
if p.billingMethod == BILLING_METHOD_LICENSE {
if functions.Cardinality() == 0 {
return nil
}
if tnFunction.IsSubset(functions) {
return p.serializeString
} else if tFunction.IsSubset(functions) {
return p.npbSerializeString
} else if nFunction.IsSubset(functions) {
return p.pcapSerializeString
}
} else {
if functions.Cardinality() == 0 {
return nil
}
if tnFunction.IsSubset(functions) {
return p.serializeString
} else if tFunction.IsSubset(functions) {
return p.npbSerializeString
} else if nFunction.IsSubset(functions) {
return p.pcapSerializeString
}

return nil
}

func (p *Policy) getPolicyVersion(functions mapset.Set) uint64 {
if p.billingMethod == BILLING_METHOD_LICENSE {
if functions.Cardinality() == 0 {
return 0xFFFFFFFF
}
if tnFunction.IsSubset(functions) {
return p.version
} else if tFunction.IsSubset(functions) {
return p.npbVersion
} else if nFunction.IsSubset(functions) {
return p.pcapVersion
}
} else {
if functions.Cardinality() == 0 {
return 0xFFFFFFFF
}
if tnFunction.IsSubset(functions) {
return p.version
} else if tFunction.IsSubset(functions) {
return p.npbVersion
} else if nFunction.IsSubset(functions) {
return p.pcapVersion
}

return 0xFFFFFFFF
Expand Down
12 changes: 3 additions & 9 deletions server/controller/trisolaris/vtap/vtap_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,7 @@ func (c *VTapCache) initVTapConfig() {
realConfig = *v.realDefaultConfig
}
}
if v.config.BillingMethod == BILLING_METHOD_LICENSE {
c.modifyVTapConfigByLicense(&realConfig)
}
c.modifyVTapConfigByLicense(&realConfig)
realConfig.modifyConfig(v)
c.updateVTapConfig(&realConfig)
}
Expand All @@ -1087,9 +1085,7 @@ func (c *VTapCache) updateVTapConfigFromDB() {
}
}

if v.config.BillingMethod == BILLING_METHOD_LICENSE {
c.modifyVTapConfigByLicense(&newConfig)
}
c.modifyVTapConfigByLicense(&newConfig)
newConfig.modifyConfig(v)
c.updateVTapConfig(&newConfig)
}
Expand All @@ -1110,9 +1106,7 @@ func (c *VTapCache) updateVTapCacheFromDB(vtap *mysql.VTap) {
c.updateCtrlMacFromDB(vtap.CtrlMac)
c.state = vtap.State
c.enable = vtap.Enable
if v.config.BillingMethod == BILLING_METHOD_LICENSE {
c.updateLicenseFunctions(vtap.LicenseFunctions)
}
c.updateLicenseFunctions(vtap.LicenseFunctions)
c.updateTapMode(vtap.TapMode)
c.updateTeamID(vtap.TeamID)
if c.vTapType != vtap.Type {
Expand Down
8 changes: 6 additions & 2 deletions server/controller/trisolaris/vtap/vtap_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,20 @@ func (r *VTapRegister) insertToDB(dbVTap *models.VTap, db *gorm.DB) bool {
return false
}
if len(ids) != 1 {
log.Error(r.Logf("request ids=%s err", ids))
log.Error(r.Logf("request ids=%v err", ids))
return false
}
dbVTap.ID = ids[0]
// Voucher mode turns on all features
if r.vTapInfo.config.BillingMethod == BILLING_METHOD_VOUCHER {
dbVTap.LicenseFunctions = VTAP_ALL_LICENSE_FUNCTIONS
}
err = db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(dbVTap).Error; err != nil {
log.Errorf(r.Logf("insert agent(%s) to DB faild, err: %s", r, err))
errID := idmng.ReleaseIDs(r.GetORGID(), RESOURCE_TYPE_VTAP_EN, ids)
if errID != nil {
log.Error(r.Logf("Release ids=%s err: %s", ids, errID))
log.Error(r.Logf("Release ids=%v err: %s", ids, errID))
}
return err
}
Expand Down

0 comments on commit 1d38c5b

Please sign in to comment.