forked from singnet/snet-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anandrgitnirman
authored and
anandrgitnirman
committed
Oct 13, 2021
1 parent
76b67d9
commit b15b534
Showing
8 changed files
with
468 additions
and
90 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package license_server | ||
|
||
import ( | ||
"github.com/singnet/snet-daemon/authutils" | ||
"github.com/singnet/snet-daemon/blockchain" | ||
"github.com/singnet/snet-daemon/escrow" | ||
"golang.org/x/net/context" | ||
"math/big" | ||
"strings" | ||
) | ||
|
||
type LicenseContract struct { | ||
serviceMetaData *blockchain.ServiceMetadata | ||
orgMetaData *blockchain.OrganizationMetaData | ||
allowedBlockNumberCheck func(blockNumber *big.Int) (err error) | ||
service LicenseService | ||
channelService escrow.PaymentChannelService | ||
} | ||
|
||
//Will be used in the components to create a new instance of LicenseService | ||
func NewLicenseContract( | ||
channelService escrow.PaymentChannelService, licenseService LicenseService, orgData *blockchain.OrganizationMetaData, | ||
servMetaData *blockchain.ServiceMetadata) *LicenseContract { | ||
return &LicenseContract{ | ||
channelService: channelService, | ||
service: licenseService, | ||
orgMetaData: orgData, | ||
serviceMetaData: servMetaData, | ||
} | ||
} | ||
|
||
type BlockChainDisabledLicenseContract struct { | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) CreateLicense(c context.Context, request *LicenseCreateRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) CreateAddOns(c context.Context, request *AddOnCreateRequest) (*AddOnDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) GetLicenseForChannel(c context.Context, request *LicenseReadRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) RenewLicense(c context.Context, request *LicenseUpdateRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) CancelLicense(c context.Context, request *LicenseUpdateRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) GetAllLicensesForUser(c context.Context, authentication *CallerAuthentication) (*AllLicensesResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) GetLicensesSupportedByProvider(c context.Context, request *LicenseProviderReadRequest) (*LicenseTypesSupportedResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) GetAllLicenseByServiceIds(c context.Context, request *LicenseProviderReadRequest) (*AllLicensesResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) GetLicenseUsage(c context.Context, request *LicenseReadRequest) (*UsageResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) CheckEligibilityAndIncrementUsage(c context.Context, request *CheckLicenseUsageRequest) (*UsageResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (b BlockChainDisabledLicenseContract) DecrementUsage(c context.Context, request *CheckLicenseUsageRequest) (*UsageResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (licenseContract *LicenseContract) verifySignerForCreateLicense(request *LicenseCreateRequest) error { | ||
return nil | ||
} | ||
|
||
func (licenseContract *LicenseContract) GetFixedPriceFromServiceMetadata(license License) (fixedPricingDetails ServiceMethodCostDetails, err error) { | ||
fixedPricingDetails = ServiceMethodCostDetails{} | ||
serviceMetadata := licenseContract.serviceMetaData | ||
fixedPricingDetails.Price = serviceMetadata.GetDefaultPricing().PriceInCogs | ||
fixedPricingDetails.PlanName = serviceMetadata.GetDefaultPricing().PriceModel | ||
return | ||
} | ||
|
||
func (licenseContract *LicenseContract) verifyLicenseDetailsFromRequest(request *LicenseCreateRequest) (license License, err error) { | ||
//Get the associated fixed Pricing details | ||
fixedPricingDetails, err := licenseContract.GetFixedPriceFromServiceMetadata(license) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if strings.Compare(request.GetLicenseType(), SUBSCRIPTION) == 0 { | ||
subscription := &Subscription{} | ||
subscription.FixedPricing = fixedPricingDetails | ||
license = subscription | ||
} else if strings.Compare(request.GetLicenseType(), TIER) == 0 { | ||
tier := &Tier{} | ||
tier.FixedPricing = fixedPricingDetails | ||
license = tier | ||
} else if strings.Compare(request.GetLicenseType(), ADD_ON) == 0 { | ||
addOn := &AddOn{} | ||
addOn.FixedPricing = fixedPricingDetails | ||
license = addOn | ||
} else { | ||
return nil, nil | ||
} | ||
|
||
return license, nil | ||
} | ||
|
||
func (licenseContract *LicenseContract) CreateLicense(ctx context.Context, request *LicenseCreateRequest) (*LicenseDataResponse, error) { | ||
//Check if the block number is within the permissible limits | ||
if err := authutils.CompareWithLatestBlockNumber(big.NewInt(int64(request.GetAuth().GetCurrentBlock()))); err != nil { | ||
return nil, err | ||
} | ||
//Check if the signer is valid | ||
if err := licenseContract.verifySignerForCreateLicense(request); err != nil { | ||
return nil, err | ||
} | ||
|
||
//Check if all license creation details are provided correctly | ||
license, err := licenseContract.verifyLicenseDetailsFromRequest(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
channelId := big.NewInt(int64(request.GetChannelId())) | ||
serviceId := request.ServiceId | ||
//First persist the License Details , this is to stamp the License that was created. | ||
//todo , for upgrade or downgrade , we need to keep track of audit trail | ||
err = licenseContract.service.CreateOrUpdateLicenseDetails(channelId, serviceId, license) | ||
if err != nil { | ||
return nil, err | ||
} | ||
//todo , get the planned amount | ||
licenseContract.service.CreateOrUpdateLicenseUsage(channelId, serviceId, big.NewInt(10), PLANNED, license.GetID()) | ||
return nil, nil | ||
} | ||
func (licenseContract *LicenseContract) CreateAddOns(context.Context, *AddOnCreateRequest) (*AddOnDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// You need to pass the ChannelId | ||
func (licenseContract *LicenseContract) GetLicenseForChannel(context.Context, *LicenseReadRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// this is to renew the License | ||
func (licenseContract *LicenseContract) RenewLicense(context.Context, *LicenseUpdateRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
func (licenseContract *LicenseContract) CancelLicense(context.Context, *LicenseUpdateRequest) (*LicenseDataResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// All license_server associated with this user will be sent back | ||
func (licenseContract *LicenseContract) GetAllLicensesForUser(context.Context, *CallerAuthentication) (*AllLicensesResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// this is to only tell you the types of licenses supported by provider , we will start with subscription | ||
// and then scale this up to Tier Pricing | ||
func (licenseContract *LicenseContract) GetLicensesSupportedByProvider(context.Context, *LicenseProviderReadRequest) (*LicenseTypesSupportedResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// filter by licenses created for given service Id | ||
func (licenseContract *LicenseContract) GetAllLicenseByServiceIds(context.Context, *LicenseProviderReadRequest) (*AllLicensesResponse, error) { | ||
return nil, nil | ||
} | ||
func (licenseContract *LicenseContract) GetLicenseUsage(context.Context, *LicenseReadRequest) (*UsageResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// To Be called By Daemon ONLY, Daemons's address is listed in service metadata, this will be used by the licensing server | ||
// to check for authorizations on Signature. | ||
func (licenseContract *LicenseContract) CheckEligibilityAndIncrementUsage(context.Context, *CheckLicenseUsageRequest) (*UsageResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
// To Be called By Daemon ONLY, Daemons's address is listed in service metadata, this will be used by the licensing server | ||
// to check for authorizations on Signature. | ||
func (licenseContract *LicenseContract) DecrementUsage(context.Context, *CheckLicenseUsageRequest) (*UsageResponse, error) { | ||
return nil, 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
Oops, something went wrong.