github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/capabilities/capabilities.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package capabilities
     8  
     9  import (
    10  	cb "github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric/common/flogging"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  var logger = flogging.MustGetLogger("common.capabilities")
    16  
    17  // provider is the 'plugin' parameter for registry.
    18  type provider interface {
    19  	// HasCapability should report whether the binary supports this capability.
    20  	HasCapability(capability string) bool
    21  
    22  	// Type is used to make error messages more legible.
    23  	Type() string
    24  }
    25  
    26  // registry is a common structure intended to be used to support specific aspects of capabilities
    27  // such as orderer, application, and channel.
    28  type registry struct {
    29  	provider     provider
    30  	capabilities map[string]*cb.Capability
    31  }
    32  
    33  func newRegistry(p provider, capabilities map[string]*cb.Capability) *registry {
    34  	return &registry{
    35  		provider:     p,
    36  		capabilities: capabilities,
    37  	}
    38  }
    39  
    40  // Supported checks that all of the required capabilities are supported by this binary.
    41  func (r *registry) Supported() error {
    42  	for capabilityName := range r.capabilities {
    43  		if r.provider.HasCapability(capabilityName) {
    44  			logger.Debugf("%s capability %s is supported and is enabled", r.provider.Type(), capabilityName)
    45  			continue
    46  		}
    47  
    48  		return errors.Errorf("%s capability %s is required but not supported", r.provider.Type(), capabilityName)
    49  	}
    50  	return nil
    51  }