Example #1
0
  def _findPoliciesToCombine( self, singlePolicyRes ):
    """ method that iterates over the single policy results and checks the CS
    configuration of the policies looking for the option 'doNotCombine'. If it is
    present, that single policy result is discarded.

    :Parameters:
      **singlePolicyRes** - `list( dict )`
        list with every single policy result to be combined ( see _runPolicy for more details )

    :return: `list( dict )`

    """

    # Get policies configuration from the CS. We want to exclude the policies that
    # have set the option `doNotCombine` from this process.
    policiesConfiguration = RssConfiguration.getPolicies()
    if not policiesConfiguration['OK']:
      return policiesConfiguration
    policiesConfiguration = policiesConfiguration['Value']

    # Function that let's us know if we should combine the result of a single policy
    # or not.
    def combinePolicy( policyResult ):
      # Extract policy name from the dictionary returned by PolicyCaller
      policyName = policyResult['Policy']['name']
      try:
        # If doNotCombineResult is defined, the policy is not taken into account
        # to create the combined result. However, the single policy result remains
        _ = policiesConfiguration[ policyName ]['doNotCombineResult']
        return False
      except KeyError:
        return True

    # Make a list of policies of which we want to merge their results
    return [ policyResult for policyResult in singlePolicyRes if combinePolicy( policyResult ) ]
Example #2
0
File: PDP.py Project: pmusset/DIRAC
  def _findPoliciesToCombine(self, singlePolicyRes):
    """ method that iterates over the single policy results and checks the CS
    configuration of the policies looking for the option 'doNotCombine'. If it is
    present, that single policy result is discarded.

    :Parameters:
      **singlePolicyRes** - `list( dict )`
        list with every single policy result to be combined ( see _runPolicy for more details )

    :return: `list( dict )`

    """

    # Get policies configuration from the CS. We want to exclude the policies that
    # have set the option `doNotCombine` from this process.
    policiesConfiguration = RssConfiguration.getPolicies()
    if not policiesConfiguration['OK']:
      return policiesConfiguration
    policiesConfiguration = policiesConfiguration['Value']

    # Function that let's us know if we should combine the result of a single policy
    # or not.
    def combinePolicy(policyResult):
      # Extract policy name from the dictionary returned by PolicyCaller
      policyName = policyResult['Policy']['name']
      try:
        # If doNotCombineResult is defined, the policy is not taken into account
        # to create the combined result. However, the single policy result remains
        _ = policiesConfiguration[policyName]['doNotCombineResult']
        return False
      except KeyError:
        return True

    # Make a list of policies of which we want to merge their results
    return [policyResult for policyResult in singlePolicyRes if combinePolicy(policyResult)]
Example #3
0
    def __getPoliciesThatApply(self, decissionParams):
        '''
      Method that matches the input dictionary with the policies configuration in
      the CS. It returns a list of policy dictionaries that matched.
    '''

        policiesThatApply = []

        # Get policies configuration metadata from CS.
        policiesConfig = RssConfiguration.getPolicies()
        if not policiesConfig['OK']:
            return policiesConfig
        policiesConfig = policiesConfig['Value']

        # Each policy, has the following format
        # <policyName>
        # \
        #  policyType = <policyType>
        #  matchParams
        #  \
        #   ...
        #  configParams
        #  \
        #   ...

        # Get policies that match the given decissionParameters
        for policyName, policySetup in policiesConfig.items():

            # The parameter policyType replaces policyName, so if it is not present,
            # we pick policyName
            try:
                policyType = policySetup['policyType'][0]
            except KeyError:
                policyType = policyName
                #continue

            # The section matchParams is not mandatory, so we set {} as default.
            policyMatchParams = policySetup.get('matchParams', {})

            # FIXME: make sure the values in the policyConfigParams dictionary are typed !!
            policyConfigParams = {}
            #policyConfigParams = policySetup.get( 'configParams', {} )

            policyMatch = Utils.configMatch(decissionParams, policyMatchParams)
            if policyMatch:
                policiesThatApply.append(
                    (policyName, policyType, policyConfigParams))

        policiesToBeLoaded = []

        # Gets policies parameters from code.
        for policyName, policyType, _policyConfigParams in policiesThatApply:

            try:
                policyMeta = self.policies[policyType]
            except KeyError:
                continue

            # We are not going to use name / type anymore, but we keep them for debugging
            # and future usage.
            policyDict = {'name': policyName, 'type': policyType, 'args': {}}

            # args is one of the parameters we are going to use on the policies. We copy
            # the defaults and then we update if with whatever comes from the CS.
            policyDict.update(policyMeta)
            # FIXME: watch out, args can be None !
            #policyDict[ 'args' ].update( policyConfigParams )

            policiesToBeLoaded.append(policyDict)

        return S_OK(policiesToBeLoaded)
Example #4
0
 def __getPoliciesThatApply( self, decisionParams ):
   '''
     Method that matches the input dictionary with the policies configuration in
     the CS. It returns a list of policy dictionaries that matched.
   '''
   
   policiesThatApply = []
   
   # Get policies configuration metadata from CS.
   policiesConfig = RssConfiguration.getPolicies()
   if not policiesConfig[ 'OK' ]:
     return policiesConfig
   policiesConfig = policiesConfig[ 'Value' ]
   
   # Each policy, has the following format
   # <policyName>
   # \
   #  policyType = <policyType>
   #  matchParams
   #  \
   #   ...        
   #  configParams
   #  \
   #   ...
   
   # Get policies that match the given decissionParameters
   for policyName, policySetup in policiesConfig.items():
     
     # The parameter policyType replaces policyName, so if it is not present,
     # we pick policyName
     try:
       policyType = policySetup[ 'policyType' ][ 0 ]
     except KeyError:
       policyType = policyName
       #continue
     
     # The section matchParams is not mandatory, so we set {} as default.
     policyMatchParams  = policySetup.get( 'matchParams',  {} )
     
     # FIXME: make sure the values in the policyConfigParams dictionary are typed !!
     policyConfigParams = {}
     #policyConfigParams = policySetup.get( 'configParams', {} )
     
     policyMatch = Utils.configMatch( decisionParams, policyMatchParams )
     if policyMatch:
       policiesThatApply.append( ( policyName, policyType, policyConfigParams ) )
       
   policiesToBeLoaded = []    
   
   # Gets policies parameters from code.    
   for policyName, policyType, _policyConfigParams in policiesThatApply:
     
     try:
       policyMeta = self.policies[ policyType ]
     except KeyError:
       continue  
     
     # We are not going to use name / type anymore, but we keep them for debugging
     # and future usage.
     policyDict = { 
                    'name' : policyName, 
                    'type' : policyType,
                    'args' : {}
                  }
     
     # args is one of the parameters we are going to use on the policies. We copy
     # the defaults and then we update if with whatever comes from the CS.
     policyDict.update( policyMeta )
     # FIXME: watch out, args can be None !
     #policyDict[ 'args' ].update( policyConfigParams )
     
     policiesToBeLoaded.append( policyDict )
      
   return S_OK( policiesToBeLoaded )
Example #5
0
def getPoliciesThatApply( decisionParams ):
  """
    Method that sanitizes the input parameters and returns the policies that
    match them. Matches the input dictionary with the policies configuration in
    the CS. It returns a list of policy dictionaries that matched.
  """

  decisionParams = _sanitizedecisionParams( decisionParams )
  gLogger.debug("Sanitized decisionParams: %s" % str(decisionParams))

  policiesThatApply = []

  # Get policies configuration metadata from CS.
  policiesConfig = RssConfiguration.getPolicies()
  if not policiesConfig[ 'OK' ]:
    return policiesConfig
  policiesConfig = policiesConfig[ 'Value' ]
  gLogger.debug("All policies: %s" %str(policiesConfig))

  # Each policy, has the following format
  # <policyName>
  # \
  #  policyType = <policyType>
  #  matchParams
  #  \
  #   ...
  #  configParams
  #  \
  #   ...

  # Get policies that match the given decisionParameters
  for policyName, policySetup in policiesConfig.items():

    # The parameter policyType replaces policyName, so if it is not present,
    # we pick policyName
    try:
      policyType = policySetup[ 'policyType' ][ 0 ]
    except KeyError:
      policyType = policyName
      #continue

    # The section matchParams is not mandatory, so we set {} as default.
    policyMatchParams  = policySetup.get( 'matchParams',  {} )
    gLogger.debug("matchParams of %s: %s" %(policyName, str(policyMatchParams)))

    # FIXME: make sure the values in the policyConfigParams dictionary are typed !!
    policyConfigParams = {}
    #policyConfigParams = policySetup.get( 'configParams', {} )
    policyMatch = Utils.configMatch( decisionParams, policyMatchParams )
    gLogger.debug("PolicyMatch for decisionParams %s: %s" %(decisionParams, str(policyMatch)))
    policyFilter = _filterPolicies( decisionParams, policyMatchParams )

    #WARNING: we need an additional filtering function when the matching
    #is not straightforward (e.g. when the policy specify a 'domain', while
    #the decisionParams has only the name of the element)
    if policyMatch and policyFilter:
      policiesThatApply.append( ( policyName, policyType, policyConfigParams ) )

  gLogger.debug("policies that apply: %s" %str(policiesThatApply))

  policiesToBeLoaded = []

  # Gets policies parameters from code.
  for policyName, policyType, _policyConfigParams in policiesThatApply:

    try:
      configModule = Utils.voimport( 'DIRAC.ResourceStatusSystem.Policy.Configurations' )
      policies = copy.deepcopy( configModule.POLICIESMETA )
      policyMeta = policies[ policyType ]
    except KeyError:
      continue

    # We are not going to use name / type anymore, but we keep them for debugging
    # and future usage.
    policyDict = { 'name' : policyName,
                   'type' : policyType,
                   'args' : {}
                 }

    # args is one of the parameters we are going to use on the policies. We copy
    # the defaults and then we update if with whatever comes from the CS.
    policyDict.update( policyMeta )

    policiesToBeLoaded.append( policyDict )

  return S_OK( policiesToBeLoaded )
Example #6
0
 def getPoliciesThatApply( self, decisionParams ):
   """ Given a dictionary, it matches it against all the policies configuration
   dictionaries as they are on the CS. Returns the policy dictionaries that 
   produced a positive match plus their configuration in <self.policies>. 
   
   examples:
     >>> # This matches all policies !
     >>> iGetter.getPoliciesThatApply( {} )
         [ { 
            'name'        : 'AlwaysActiveForResource',
            'type'        : 'AlwaysActive',
            'module'      : 'AlwaysActivePolicy',
            'description' : 'This is the AlwaysActive policy'
            'command'     : None,
            'args'        : {}
           },... ]
     >>> # There is no policy that matches BlahSite      
     >>> iGetter.getPoliciesThatApply( { 'name' : 'BlahSite' } )
         []
   
   :Parameters:
     **decisionParams** - `dict`
       dictionary with the parameters to match policies.
       
   :return: S_OK() / S_ERROR
   
   """
   
   policiesToBeLoaded = []
   
   # Get policies configuration metadata from CS.
   policiesConfig = RssConfiguration.getPolicies()
   if not policiesConfig[ 'OK' ]:
     return policiesConfig
   policiesConfig = policiesConfig[ 'Value' ]
   
   # Get policies that match the given decissionParameters
   for policyName, policySetup in policiesConfig.iteritems():
         
     # The section matchParams is not mandatory, so we set {} as default.
     policyMatchParams = policySetup.get( 'matchParams',  {} )   
     if not Utils.configMatch( decisionParams, policyMatchParams ):
       continue
       
     # the policyName replaces the policyTipe if not present. This allows us to
     # define several policies of the same type on the CS. We just need to
     # give them different names and assign them the same policyType. 
     try:
       policyType = policySetup[ 'policyType' ][ 0 ]
     except KeyError:
       policyType = policyName
     
     policyDict = {
                    'name' : policyName, 
                    'type' : policyType,
                    'args' : {}
                  }
     
     # Get policy static configuration        
     try:
       policyDict.update( self.policies[ policyType ] )
     except KeyError:
       continue  
     
     policiesToBeLoaded.append( policyDict )
      
   return S_OK( policiesToBeLoaded )