diff options
author | elie <elie> | 2015-10-17 19:14:29 +0000 |
---|---|---|
committer | elie <elie> | 2015-10-17 19:14:29 +0000 |
commit | 9e73d5b89c6121437b92f02482b4423ab526b51a (patch) | |
tree | 84e69ffdabad2f8b71dcdceac03234d26ba08c28 /pysnmp/entity/rfc3413 | |
parent | 7ebcb3d0c1ce6b7722bf7b06b4826d27c2fa487b (diff) | |
download | pysnmp-git-9e73d5b89c6121437b92f02482b4423ab526b51a.tar.gz |
linted the bad-continuation issue as well as some others
Diffstat (limited to 'pysnmp/entity/rfc3413')
-rw-r--r-- | pysnmp/entity/rfc3413/cmdrsp.py | 193 | ||||
-rw-r--r-- | pysnmp/entity/rfc3413/config.py | 129 | ||||
-rw-r--r-- | pysnmp/entity/rfc3413/ntforg.py | 270 | ||||
-rw-r--r-- | pysnmp/entity/rfc3413/ntfrcv.py | 61 |
4 files changed, 214 insertions, 439 deletions
diff --git a/pysnmp/entity/rfc3413/cmdrsp.py b/pysnmp/entity/rfc3413/cmdrsp.py index 2e811b7f..dfe9ec13 100644 --- a/pysnmp/entity/rfc3413/cmdrsp.py +++ b/pysnmp/entity/rfc3413/cmdrsp.py @@ -17,9 +17,9 @@ class CommandResponderBase: self.snmpContext = snmpContext self.__pendingReqs = {} - def handleMgmtOperation( - self, snmpEngine, stateReference, contextName, PDU, acInfo - ): pass + def handleMgmtOperation(self, snmpEngine, stateReference, contextName, + PDU, acInfo): + pass def close(self, snmpEngine): snmpEngine.msgAndPduDsp.unregisterContextEngineId( @@ -28,18 +28,11 @@ class CommandResponderBase: self.snmpContext = self.__pendingReqs = None def sendVarBinds(self, snmpEngine, stateReference, - errorStatus, errorIndex, varBinds): - ( messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - PDU, - origPdu, - maxSizeResponseScopedPDU, - statusInformation ) = self.__pendingReqs[stateReference] + errorStatus, errorIndex, varBinds): + (messageProcessingModel, securityModel, securityName, + securityLevel, contextEngineId, contextName, + pduVersion, PDU, origPdu, maxSizeResponseScopedPDU, + statusInformation) = self.__pendingReqs[stateReference] v2c.apiPDU.setErrorStatus(PDU, errorStatus) v2c.apiPDU.setErrorIndex(PDU, errorIndex) @@ -53,17 +46,10 @@ class CommandResponderBase: sendRsp = sendVarBinds def sendPdu(self, snmpEngine, stateReference, PDU): - ( messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - _, - origPdu, - maxSizeResponseScopedPDU, - statusInformation ) = self.__pendingReqs[stateReference] + (messageProcessingModel, securityModel, securityName, + securityLevel, contextEngineId, contextName, + pduVersion, _, origPdu, maxSizeResponseScopedPDU, + statusInformation) = self.__pendingReqs[stateReference] # Agent-side API complies with SMIv2 if messageProcessingModel == 0: @@ -84,11 +70,12 @@ class CommandResponderBase: maxSizeResponseScopedPDU, stateReference, statusInformation - ) + ) + except error.StatusInformation: debug.logger & debug.flagApp and debug.logger('sendPdu: stateReference %s, statusInformation %s' % (stateReference, sys.exc_info()[1])) snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'snmpSilentDrops') - snmpSilentDrops.syntax = snmpSilentDrops.syntax + 1 + snmpSilentDrops.syntax += 1 _getRequestType = rfc1905.GetRequestPDU.tagSet _getNextRequestType = rfc1905.GetNextRequestPDU.tagSet @@ -99,20 +86,9 @@ class CommandResponderBase: if stateReference in self.__pendingReqs: del self.__pendingReqs[stateReference] - def processPdu( - self, - snmpEngine, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - PDU, - maxSizeResponseScopedPDU, - stateReference - ): + def processPdu(self, snmpEngine, messageProcessingModel, securityModel, + securityName, securityLevel, contextEngineId, contextName, + pduVersion, PDU, maxSizeResponseScopedPDU, stateReference): # Agent-side API complies with SMIv2 if messageProcessingModel == 0: @@ -134,18 +110,10 @@ class CommandResponderBase: statusInformation = {} self.__pendingReqs[stateReference] = ( - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - rspPDU, - origPdu, - maxSizeResponseScopedPDU, - statusInformation - ) + messageProcessingModel, securityModel, securityName, + securityLevel, contextEngineId, contextName, pduVersion, + rspPDU, origPdu, maxSizeResponseScopedPDU, statusInformation + ) # 3.2.5 varBinds = v2c.apiPDU.getVarBinds(PDU) @@ -154,10 +122,9 @@ class CommandResponderBase: debug.logger & debug.flagApp and debug.logger('processPdu: stateReference %s, varBinds %s' % (stateReference, varBinds)) try: - self.handleMgmtOperation( - snmpEngine, stateReference, - contextName, PDU, (self.__verifyAccess, snmpEngine) - ) + self.handleMgmtOperation(snmpEngine, stateReference, + contextName, PDU, + (self.__verifyAccess, snmpEngine)) # SNMPv2 SMI exceptions except pysnmp.smi.error.GenError: errorIndication = sys.exc_info()[1] @@ -202,31 +169,23 @@ class CommandResponderBase: else: # successful request processor must release state info return - self.sendVarBinds( - snmpEngine, stateReference, errorStatus, errorIndex, varBinds - ) + self.sendVarBinds(snmpEngine, stateReference, errorStatus, + errorIndex, varBinds) self.releaseStateInformation(stateReference) def __verifyAccess(self, name, syntax, idx, viewType, acCtx): snmpEngine = acCtx - execCtx = snmpEngine.observer.getExecutionContext( - 'rfc3412.receiveMessage:request' - ) - ( securityModel, - securityName, - securityLevel, - contextName, - pduType ) = ( execCtx['securityModel'], - execCtx['securityName'], - execCtx['securityLevel'], - execCtx['contextName'], - execCtx['pdu'].getTagSet() ) + execCtx = snmpEngine.observer.getExecutionContext('rfc3412.receiveMessage:request') + (securityModel, securityName, securityLevel, contextName, + pduType) = (execCtx['securityModel'], execCtx['securityName'], + execCtx['securityLevel'], execCtx['contextName'], + execCtx['pdu'].getTagSet()) try: snmpEngine.accessControlModel[self.acmID].isAccessAllowed( snmpEngine, securityModel, securityName, securityLevel, viewType, contextName, name - ) + ) # Map ACM errors onto SMI ones except error.StatusInformation: statusInformation = sys.exc_info()[1] @@ -236,69 +195,55 @@ class CommandResponderBase: if errorIndication == errind.noSuchView or \ errorIndication == errind.noAccessEntry or \ errorIndication == errind.noGroupName: - raise pysnmp.smi.error.AuthorizationError( - name=name, idx=idx - ) + raise pysnmp.smi.error.AuthorizationError(name=name, idx=idx) elif errorIndication == errind.otherError: raise pysnmp.smi.error.GenError(name=name, idx=idx) elif errorIndication == errind.noSuchContext: snmpUnknownContexts, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-TARGET-MIB', 'snmpUnknownContexts') - snmpUnknownContexts.syntax = snmpUnknownContexts.syntax + 1 + snmpUnknownContexts.syntax += 1 # Request REPORT generation - raise pysnmp.smi.error.GenError( - name=name, idx=idx, - oid=snmpUnknownContexts.name, - val=snmpUnknownContexts.syntax - ) + raise pysnmp.smi.error.GenError(name=name, idx=idx, + oid=snmpUnknownContexts.name, + val=snmpUnknownContexts.syntax) elif errorIndication == errind.notInView: return 1 else: - raise error.ProtocolError( - 'Unknown ACM error %s' % errorIndication - ) + raise error.ProtocolError('Unknown ACM error %s' % errorIndication) else: # rfc2576: 4.1.2.1 - if securityModel == 1 and \ - syntax is not None and \ - self._counter64Type == syntax.getTagSet() and \ - self._getNextRequestType == pduType: + if securityModel == 1 and syntax is not None and \ + self._counter64Type == syntax.getTagSet() and \ + self._getNextRequestType == pduType: # This will cause MibTree to skip this OID-value raise pysnmp.smi.error.NoAccessError(name=name, idx=idx) class GetCommandResponder(CommandResponderBase): - pduTypes = ( rfc1905.GetRequestPDU.tagSet, ) + pduTypes = (rfc1905.GetRequestPDU.tagSet,) # rfc1905: 4.2.1 - def handleMgmtOperation( - self, snmpEngine, stateReference, contextName, PDU, acInfo - ): + def handleMgmtOperation(self, snmpEngine, stateReference, + contextName, PDU, acInfo): (acFun, acCtx) = acInfo # rfc1905: 4.2.1.1 mgmtFun = self.snmpContext.getMibInstrum(contextName).readVars - self.sendVarBinds( - snmpEngine, stateReference, 0, 0, mgmtFun( - v2c.apiPDU.getVarBinds(PDU), (acFun, acCtx) - ) - ) + self.sendVarBinds(snmpEngine, stateReference, 0, 0, + mgmtFun(v2c.apiPDU.getVarBinds(PDU), (acFun, acCtx))) self.releaseStateInformation(stateReference) class NextCommandResponder(CommandResponderBase): - pduTypes = ( rfc1905.GetNextRequestPDU.tagSet, ) + pduTypes = (rfc1905.GetNextRequestPDU.tagSet,) # rfc1905: 4.2.2 - def handleMgmtOperation( - self, snmpEngine, stateReference, contextName, PDU, acInfo - ): + def handleMgmtOperation(self, snmpEngine, stateReference, + contextName, PDU, acInfo): (acFun, acCtx) = acInfo # rfc1905: 4.2.2.1 mgmtFun = self.snmpContext.getMibInstrum(contextName).readNextVars varBinds = v2c.apiPDU.getVarBinds(PDU) - while 1: + while True: rspVarBinds = mgmtFun(varBinds, (acFun, acCtx)) try: - self.sendVarBinds( - snmpEngine, stateReference, 0, 0, rspVarBinds - ) + self.sendVarBinds(snmpEngine, stateReference, 0, 0, rspVarBinds) except error.StatusInformation: idx = sys.exc_info()[1]['idx'] varBinds[idx] = (rspVarBinds[idx][0], varBinds[idx][1]) @@ -307,13 +252,12 @@ class NextCommandResponder(CommandResponderBase): self.releaseStateInformation(stateReference) class BulkCommandResponder(CommandResponderBase): - pduTypes = ( rfc1905.GetBulkRequestPDU.tagSet, ) + pduTypes = (rfc1905.GetBulkRequestPDU.tagSet,) maxVarBinds = 64 # rfc1905: 4.2.3 - def handleMgmtOperation( - self, snmpEngine, stateReference, contextName, PDU, acInfo - ): + def handleMgmtOperation(self, snmpEngine, stateReference, + contextName, PDU, acInfo): (acFun, acCtx) = acInfo nonRepeaters = v2c.apiBulkPDU.getNonRepeaters(PDU) if nonRepeaters < 0: @@ -342,39 +286,32 @@ class BulkCommandResponder(CommandResponderBase): varBinds = reqVarBinds[-R:] while M and R: - rspVarBinds.extend( - mgmtFun(varBinds, (acFun, acCtx)) - ) + rspVarBinds.extend(mgmtFun(varBinds, (acFun, acCtx))) varBinds = rspVarBinds[-R:] M = M - 1 if len(rspVarBinds): - self.sendVarBinds( - snmpEngine, stateReference, 0, 0, rspVarBinds - ) + self.sendVarBinds(snmpEngine, stateReference, 0, 0, rspVarBinds) self.releaseStateInformation(stateReference) else: raise pysnmp.smi.error.SmiError() class SetCommandResponder(CommandResponderBase): - pduTypes = ( rfc1905.SetRequestPDU.tagSet, ) + pduTypes = (rfc1905.SetRequestPDU.tagSet,) # rfc1905: 4.2.5 - def handleMgmtOperation( - self, snmpEngine, stateReference, contextName, PDU, acInfo - ): + def handleMgmtOperation(self, snmpEngine, stateReference, + contextName, PDU, acInfo): (acFun, acCtx) = acInfo mgmtFun = self.snmpContext.getMibInstrum(contextName).writeVars # rfc1905: 4.2.5.1-13 try: - self.sendVarBinds( - snmpEngine, stateReference, 0, 0, mgmtFun( - v2c.apiPDU.getVarBinds(PDU), (acFun, acCtx) - ) - ) + self.sendVarBinds(snmpEngine, stateReference, 0, 0, + mgmtFun(v2c.apiPDU.getVarBinds(PDU), + (acFun, acCtx))) self.releaseStateInformation(stateReference) - except ( pysnmp.smi.error.NoSuchObjectError, - pysnmp.smi.error.NoSuchInstanceError ): + except (pysnmp.smi.error.NoSuchObjectError, + pysnmp.smi.error.NoSuchInstanceError): e = pysnmp.smi.error.NotWritableError() e.update(sys.exc_info()[1]) raise e diff --git a/pysnmp/entity/rfc3413/config.py b/pysnmp/entity/rfc3413/config.py index 61a0580c..8add3901 100644 --- a/pysnmp/entity/rfc3413/config.py +++ b/pysnmp/entity/rfc3413/config.py @@ -20,22 +20,16 @@ def getTargetAddr(snmpEngine, snmpTargetAddrName): nameToTargetMap = cache['nameToTargetMap'] if snmpTargetAddrName not in nameToTargetMap: - ( snmpTargetAddrTDomain, - snmpTargetAddrTAddress, - snmpTargetAddrTimeout, - snmpTargetAddrRetryCount, - snmpTargetAddrParams ) = mibBuilder.importSymbols( - 'SNMP-TARGET-MIB', - 'snmpTargetAddrTDomain', - 'snmpTargetAddrTAddress', - 'snmpTargetAddrTimeout', - 'snmpTargetAddrRetryCount', - 'snmpTargetAddrParams' - ) - ( snmpSourceAddrTAddress, ) = mibBuilder.importSymbols( - 'PYSNMP-SOURCE-MIB', - 'snmpSourceAddrTAddress' - ) + (snmpTargetAddrTDomain, + snmpTargetAddrTAddress, + snmpTargetAddrTimeout, + snmpTargetAddrRetryCount, + snmpTargetAddrParams) = mibBuilder.importSymbols( + 'SNMP-TARGET-MIB', 'snmpTargetAddrTDomain', + 'snmpTargetAddrTAddress', 'snmpTargetAddrTimeout', + 'snmpTargetAddrRetryCount', 'snmpTargetAddrParams' + ) + snmpSourceAddrTAddress, = mibBuilder.importSymbols('PYSNMP-SOURCE-MIB', 'snmpSourceAddrTAddress') tblIdx = snmpTargetAddrEntry.getInstIdFromIndices(snmpTargetAddrName) @@ -47,7 +41,7 @@ def getTargetAddr(snmpEngine, snmpTargetAddrName): snmpTargetAddrTAddress.name + tblIdx ).syntax snmpTargetAddrTimeout = snmpTargetAddrTimeout.getNode( - snmpTargetAddrTimeout.name + tblIdx + snmpTargetAddrTimeout.name + tblIdx ).syntax snmpTargetAddrRetryCount = snmpTargetAddrRetryCount.getNode( snmpTargetAddrRetryCount.name + tblIdx @@ -109,16 +103,13 @@ def getTargetParams(snmpEngine, paramsName): nameToParamsMap = cache['nameToParamsMap'] if paramsName not in nameToParamsMap: - ( snmpTargetParamsMPModel, - snmpTargetParamsSecurityModel, - snmpTargetParamsSecurityName, - snmpTargetParamsSecurityLevel ) = mibBuilder.importSymbols( - 'SNMP-TARGET-MIB', - 'snmpTargetParamsMPModel', - 'snmpTargetParamsSecurityModel', - 'snmpTargetParamsSecurityName', - 'snmpTargetParamsSecurityLevel' - ) + (snmpTargetParamsMPModel, snmpTargetParamsSecurityModel, + snmpTargetParamsSecurityName, + snmpTargetParamsSecurityLevel) = mibBuilder.importSymbols( + 'SNMP-TARGET-MIB', 'snmpTargetParamsMPModel', + 'snmpTargetParamsSecurityModel', 'snmpTargetParamsSecurityName', + 'snmpTargetParamsSecurityLevel' + ) tblIdx = snmpTargetParamsEntry.getInstIdFromIndices(paramsName) @@ -138,12 +129,10 @@ def getTargetParams(snmpEngine, paramsName): except NoSuchInstanceError: raise SmiError('Parameters %s not configured at LCD' % paramsName) - nameToParamsMap[paramsName] = ( - snmpTargetParamsMPModel, - snmpTargetParamsSecurityModel, - snmpTargetParamsSecurityName, - snmpTargetParamsSecurityLevel - ) + nameToParamsMap[paramsName] = (snmpTargetParamsMPModel, + snmpTargetParamsSecurityModel, + snmpTargetParamsSecurityName, + snmpTargetParamsSecurityLevel) cache['id'] = snmpTargetParamsEntry.branchVersionId @@ -151,36 +140,32 @@ def getTargetParams(snmpEngine, paramsName): def getTargetInfo(snmpEngine, snmpTargetAddrName): # Transport endpoint - ( snmpTargetAddrTDomain, - snmpTargetAddrTAddress, - snmpTargetAddrTimeout, - snmpTargetAddrRetryCount, - snmpTargetAddrParams ) = getTargetAddr(snmpEngine, snmpTargetAddrName) - - ( snmpTargetParamsMPModel, - snmpTargetParamsSecurityModel, - snmpTargetParamsSecurityName, - snmpTargetParamsSecurityLevel ) = getTargetParams(snmpEngine, snmpTargetAddrParams) - - return ( snmpTargetAddrTDomain, - snmpTargetAddrTAddress, - snmpTargetAddrTimeout, - snmpTargetAddrRetryCount, - snmpTargetParamsMPModel, - snmpTargetParamsSecurityModel, - snmpTargetParamsSecurityName, - snmpTargetParamsSecurityLevel ) + (snmpTargetAddrTDomain, + snmpTargetAddrTAddress, + snmpTargetAddrTimeout, + snmpTargetAddrRetryCount, + snmpTargetAddrParams) = getTargetAddr(snmpEngine, snmpTargetAddrName) + + (snmpTargetParamsMPModel, + snmpTargetParamsSecurityModel, + snmpTargetParamsSecurityName, + snmpTargetParamsSecurityLevel) = getTargetParams(snmpEngine, + snmpTargetAddrParams) + + return (snmpTargetAddrTDomain, snmpTargetAddrTAddress, + snmpTargetAddrTimeout, snmpTargetAddrRetryCount, + snmpTargetParamsMPModel, snmpTargetParamsSecurityModel, + snmpTargetParamsSecurityName, snmpTargetParamsSecurityLevel) def getNotificationInfo(snmpEngine, notificationTarget): mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder - snmpNotifyEntry, = mibBuilder.importSymbols( - 'SNMP-NOTIFICATION-MIB', 'snmpNotifyEntry' - ) + snmpNotifyEntry, = mibBuilder.importSymbols('SNMP-NOTIFICATION-MIB', + 'snmpNotifyEntry') cache = snmpEngine.getUserContext('getNotificationInfo') if cache is None: - cache = { 'id': -1 } + cache = {'id': -1} snmpEngine.setUserContext(getNotificationInfo=cache) if cache['id'] != snmpNotifyEntry.branchVersionId: @@ -189,12 +174,10 @@ def getNotificationInfo(snmpEngine, notificationTarget): targetToNotifyMap = cache['targetToNotifyMap'] if notificationTarget not in targetToNotifyMap: - ( snmpNotifyTag, - snmpNotifyType ) = mibBuilder.importSymbols( - 'SNMP-NOTIFICATION-MIB', - 'snmpNotifyTag', - 'snmpNotifyType' - ) + (snmpNotifyTag, + snmpNotifyType) = mibBuilder.importSymbols('SNMP-NOTIFICATION-MIB', + 'snmpNotifyTag', + 'snmpNotifyType') tblIdx = snmpNotifyEntry.getInstIdFromIndices(notificationTarget) @@ -221,13 +204,12 @@ def getNotificationInfo(snmpEngine, notificationTarget): def getTargetNames(snmpEngine, tag): mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder - snmpTargetAddrEntry, = mibBuilder.importSymbols( - 'SNMP-TARGET-MIB', 'snmpTargetAddrEntry' - ) + snmpTargetAddrEntry, = mibBuilder.importSymbols('SNMP-TARGET-MIB', + 'snmpTargetAddrEntry') cache = snmpEngine.getUserContext('getTargetNames') if cache is None: - cache = { 'id': -1 } + cache = {'id': -1} snmpEngine.setUserContext(getTargetNames=cache) if cache['id'] == snmpTargetAddrEntry.branchVersionId: @@ -237,17 +219,14 @@ def getTargetNames(snmpEngine, tag): tagToTargetsMap = cache['tagToTargetsMap'] - ( SnmpTagValue, - snmpTargetAddrName, - snmpTargetAddrTagList ) = mibBuilder.importSymbols( - 'SNMP-TARGET-MIB', - 'SnmpTagValue', - 'snmpTargetAddrName', - 'snmpTargetAddrTagList' - ) + (SnmpTagValue, snmpTargetAddrName, + snmpTargetAddrTagList) = mibBuilder.importSymbols( + 'SNMP-TARGET-MIB', 'SnmpTagValue', 'snmpTargetAddrName', + 'snmpTargetAddrTagList' + ) targetNames = [] mibNode = snmpTargetAddrTagList - while 1: + while True: try: mibNode = snmpTargetAddrTagList.getNextNode(mibNode.name) except NoSuchInstanceError: diff --git a/pysnmp/entity/rfc3413/ntforg.py b/pysnmp/entity/rfc3413/ntforg.py index 998c62df..42274069 100644 --- a/pysnmp/entity/rfc3413/ntforg.py +++ b/pysnmp/entity/rfc3413/ntforg.py @@ -18,37 +18,21 @@ class NotificationOriginator: self.__pendingNotifications = {} self.snmpContext = snmpContext # this is deprecated - def processResponsePdu(self, - snmpEngine, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - PDU, - statusInformation, - sendPduHandle, - cbInfo): + def processResponsePdu(self, snmpEngine, messageProcessingModel, + securityModel, securityName, securityLevel, + contextEngineId, contextName, pduVersion, + PDU, statusInformation, sendPduHandle, cbInfo): sendRequestHandle, cbFun, cbCtx = cbInfo # 3.3.6d if sendPduHandle not in self.__pendingReqs: raise error.ProtocolError('Missing sendPduHandle %s' % sendPduHandle) - ( origTransportDomain, - origTransportAddress, - origMessageProcessingModel, - origSecurityModel, - origSecurityName, - origSecurityLevel, - origContextEngineId, - origContextName, - origPdu, - origTimeout, - origRetryCount, - origRetries ) = self.__pendingReqs.pop(sendPduHandle) + (origTransportDomain, origTransportAddress, + origMessageProcessingModel, origSecurityModel, + origSecurityName, origSecurityLevel, origContextEngineId, + origContextName, origPdu, origTimeout, + origRetryCount, origRetries) = self.__pendingReqs.pop(sendPduHandle) snmpEngine.transportDispatcher.jobFinished(id(self)) @@ -56,11 +40,8 @@ class NotificationOriginator: debug.logger & debug.flagApp and debug.logger('processResponsePdu: sendRequestHandle %s, sendPduHandle %s statusInformation %s' % (sendRequestHandle, sendPduHandle, statusInformation)) if origRetries == origRetryCount: debug.logger & debug.flagApp and debug.logger('processResponsePdu: sendRequestHandle %s, sendPduHandle %s retry count %d exceeded' % (sendRequestHandle, sendPduHandle, origRetries)) - cbFun(snmpEngine, - sendRequestHandle, - statusInformation['errorIndication'], - None, - cbCtx) + cbFun(snmpEngine, sendRequestHandle, + statusInformation['errorIndication'], None, cbCtx) return # Convert timeout in seconds into timeout in timer ticks @@ -77,30 +58,18 @@ class NotificationOriginator: # 3.3.6a try: sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu( - snmpEngine, - origTransportDomain, - origTransportAddress, - origMessageProcessingModel, - origSecurityModel, - origSecurityName, - origSecurityLevel, - origContextEngineId, - origContextName, - pduVersion, - reqPDU, - 1, # expectResponse - timeoutInTicks, - self.processResponsePdu, + snmpEngine, origTransportDomain, origTransportAddress, + origMessageProcessingModel, origSecurityModel, + origSecurityName, origSecurityLevel, + origContextEngineId, origContextName, pduVersion, + reqPDU, True, timeoutInTicks, self.processResponsePdu, (sendRequestHandle, cbFun, cbCtx) ) except error.StatusInformation: statusInformation = sys.exc_info()[1] debug.logger & debug.flagApp and debug.logger('processResponsePdu: sendRequestHandle %s: sendPdu() failed with %r ' % (sendRequestHandle, statusInformation)) - cbFun(snmpEngine, - sendRequestHandle, - statusInformation['errorIndication'], - None, - cbCtx) + cbFun(snmpEngine, sendRequestHandle, + statusInformation['errorIndication'], None, cbCtx) return snmpEngine.transportDispatcher.jobStarted(id(self)) @@ -109,18 +78,11 @@ class NotificationOriginator: # 3.3.6b self.__pendingReqs[sendPduHandle] = ( - origTransportDomain, - origTransportAddress, - origMessageProcessingModel, - origSecurityModel, - origSecurityName, - origSecurityLevel, - origContextEngineId, - origContextName, - origPdu, - origTimeout, - origRetryCount, - origRetries + 1 + origTransportDomain, origTransportAddress, + origMessageProcessingModel, origSecurityModel, + origSecurityName, origSecurityLevel, + origContextEngineId, origContextName, origPdu, + origTimeout, origRetryCount, origRetries + 1 ) return @@ -131,24 +93,13 @@ class NotificationOriginator: cbFun(snmpEngine, sendRequestHandle, None, PDU, cbCtx) - def sendPdu(self, - snmpEngine, - targetName, - contextEngineId, - contextName, - pdu, - cbFun=None, - cbCtx=None): - ( transportDomain, - transportAddress, - timeout, - retryCount, - params ) = config.getTargetAddr(snmpEngine, targetName) - - ( messageProcessingModel, - securityModel, - securityName, - securityLevel ) = config.getTargetParams(snmpEngine, params) + def sendPdu(self, snmpEngine, targetName, contextEngineId, + contextName, pdu, cbFun=None, cbCtx=None): + (transportDomain, transportAddress, timeout, + retryCount, params) = config.getTargetAddr(snmpEngine, targetName) + + (messageProcessingModel, securityModel, securityName, + securityLevel) = config.getTargetParams(snmpEngine, params) # User-side API assumes SMIv2 if messageProcessingModel == 0: @@ -167,55 +118,29 @@ class NotificationOriginator: # 3.3.6a sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu( - snmpEngine, - transportDomain, - transportAddress, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - reqPDU, - # expectResponse - 1, - timeoutInTicks, - self.processResponsePdu, - (sendRequestHandle, cbFun, cbCtx) + snmpEngine, transportDomain, transportAddress, + messageProcessingModel, securityModel, securityName, + securityLevel, contextEngineId, contextName, + pduVersion, reqPDU, True, timeoutInTicks, + self.processResponsePdu, (sendRequestHandle, cbFun, cbCtx) ) debug.logger & debug.flagApp and debug.logger('sendPdu: sendPduHandle %s, timeout %d' % (sendPduHandle, timeout)) # 3.3.6b self.__pendingReqs[sendPduHandle] = ( - transportDomain, - transportAddress, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pdu, - timeout, - retryCount, - 1 + transportDomain, transportAddress, messageProcessingModel, + securityModel, securityName, securityLevel, contextEngineId, + contextName, pdu, timeout, retryCount, True ) snmpEngine.transportDispatcher.jobStarted(id(self)) else: - snmpEngine.msgAndPduDsp.sendPdu(snmpEngine, - transportDomain, - transportAddress, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - reqPDU, - None) # do not expectResponse + snmpEngine.msgAndPduDsp.sendPdu( + snmpEngine, transportDomain, transportAddress, + messageProcessingModel, securityModel, + securityName, securityLevel, contextEngineId, + contextName, pduVersion, reqPDU, False + ) sendRequestHandle = None @@ -223,12 +148,8 @@ class NotificationOriginator: return sendRequestHandle - def processResponseVarBinds(self, - snmpEngine, - sendRequestHandle, - errorIndication, - pdu, - cbCtx): + def processResponseVarBinds(self, snmpEngine, sendRequestHandle, + errorIndication, pdu, cbCtx): notificationHandle, cbFun, cbCtx = cbCtx self.__pendingNotifications[notificationHandle].remove(sendRequestHandle) @@ -238,9 +159,7 @@ class NotificationOriginator: if not self.__pendingNotifications[notificationHandle]: debug.logger & debug.flagApp and debug.logger('processResponseVarBinds: notificationHandle %s, sendRequestHandle %s -- completed' % (notificationHandle, sendRequestHandle)) del self.__pendingNotifications[notificationHandle] - cbFun(snmpEngine, - sendRequestHandle, - errorIndication, + cbFun(snmpEngine, sendRequestHandle, errorIndication, pdu and v2c.apiPDU.getErrorStatus(pdu) or 0, pdu and v2c.apiPDU.getErrorIndex(pdu, muteErrors=True) or 0, pdu and v2c.apiPDU.getVarBinds(pdu) or (), @@ -250,14 +169,8 @@ class NotificationOriginator: # Higher-level API to Notification Originator. Supports multiple # targets, automatic var-binding formation and is fully LCD-driven. # - def sendVarBinds(self, - snmpEngine, - notificationTarget, - contextEngineId, - contextName, - varBinds=(), - cbFun=None, - cbCtx=None): + def sendVarBinds(self, snmpEngine, notificationTarget, contextEngineId, + contextName, varBinds=(), cbFun=None, cbCtx=None): debug.logger & debug.flagApp and debug.logger('sendVarBinds: notificationTarget %s, contextEngineId %s, contextName "%s", varBinds %s' % (notificationTarget, contextEngineId or '<default>', contextName, varBinds)) if contextName: @@ -265,16 +178,13 @@ class NotificationOriginator: contextName = __SnmpAdminString(contextName) # 3.3 - ( notifyTag, - notifyType ) = config.getNotificationInfo( - snmpEngine, notificationTarget - ) + (notifyTag, notifyType) = config.getNotificationInfo(snmpEngine, notificationTarget) notificationHandle = getNextHandle() debug.logger & debug.flagApp and debug.logger('sendVarBinds: notificationHandle %s, notifyTag %s, notifyType %s' % (notificationHandle, notifyTag, notifyType)) - varBinds = [ (v2c.ObjectIdentifier(x),y) for x,y in varBinds ] + varBinds = [(v2c.ObjectIdentifier(x),y) for x,y in varBinds] # 3.3.2 & 3.3.3 snmpTrapOID, sysUpTime = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'snmpTrapOID', 'sysUpTime') @@ -298,23 +208,18 @@ class NotificationOriginator: debug.logger & debug.flagApp and debug.logger('sendVarBinds: final varBinds %s' % (varBinds,)) for targetAddrName in config.getTargetNames(snmpEngine, notifyTag): - ( transportDomain, - transportAddress, - timeout, - retryCount, - params ) = config.getTargetAddr(snmpEngine, targetAddrName) - ( messageProcessingModel, - securityModel, - securityName, - securityLevel ) = config.getTargetParams(snmpEngine, params) + (transportDomain, transportAddress, timeout, + retryCount, params) = config.getTargetAddr(snmpEngine, + targetAddrName) + (messageProcessingModel, securityModel, securityName, + securityLevel) = config.getTargetParams(snmpEngine, params) # 3.3.1 XXX # XXX filtering's yet to be implemented # filterProfileName = config.getNotifyFilterProfile(params) -# ( filterSubtree, -# filterMask, -# filterType ) = config.getNotifyFilter(filterProfileName) +# (filterSubtree, filterMask, +# filterType) = config.getNotifyFilter(filterProfileName) debug.logger & debug.flagApp and debug.logger('sendVarBinds: notificationHandle %s, notifyTag %s yields: transportDomain %s, transportAddress %r, securityModel %s, securityName %s, securityLevel %s' % (notificationHandle, notifyTag, transportDomain, transportAddress, securityModel, securityName, securityLevel)) @@ -325,7 +230,7 @@ class NotificationOriginator: snmpEngine.accessControlModel[self.acmID].isAccessAllowed( snmpEngine, securityModel, securityName, securityLevel, 'notify', contextName, varName - ) + ) debug.logger & debug.flagApp and debug.logger('sendVarBinds: ACL succeeded for OID %s securityName %s' % (varName, securityName)) @@ -346,14 +251,11 @@ class NotificationOriginator: # 3.3.5 try: - sendRequestHandle = self.sendPdu(snmpEngine, - targetAddrName, - contextEngineId, - contextName, - pdu, - self.processResponseVarBinds, - (notificationHandle, - cbFun, cbCtx)) + sendRequestHandle = self.sendPdu( + snmpEngine, targetAddrName, contextEngineId, + contextName, pdu, self.processResponseVarBinds, + (notificationHandle, cbFun, cbCtx) + ) except error.StatusInformation: statusInformation = sys.exc_info()[1] @@ -362,10 +264,9 @@ class NotificationOriginator: not self.__pendingNotifications[notificationHandle]: if notificationHandle in self.__pendingNotifications: del self.__pendingNotifications[notificationHandle] - cbFun(snmpEngine, - notificationHandle, - statusInformation['errorIndication'], - 0, 0, (), cbCtx) + cbFun(snmpEngine, notificationHandle, + statusInformation['errorIndication'], 0, 0, (), + cbCtx) return notificationHandle debug.logger & debug.flagApp and debug.logger('sendVarBinds: notificationHandle %s, sendRequestHandle %s, timeout %d' % (notificationHandle, sendRequestHandle, timeout)) @@ -383,13 +284,8 @@ class NotificationOriginator: # Obsolete, compatibility interfaces. # -def _sendNotificationCbFun(snmpEngine, - sendRequestHandle, - errorIndication, - errorStatus, - errorIndex, - varBinds, - cbCtx): +def _sendNotificationCbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): cbFun, cbCtx = cbCtx try: @@ -400,15 +296,9 @@ def _sendNotificationCbFun(snmpEngine, # a backward compatible way of calling user function cbFun(sendRequestHandle, errorIndication, cbCtx) -def _sendNotification(self, - snmpEngine, - notificationTarget, - notificationName, - additionalVarBinds=(), - cbFun=None, - cbCtx=None, - contextName=null, - instanceIndex=None): +def _sendNotification(self, snmpEngine, notificationTarget, notificationName, + additionalVarBinds=(), cbFun=None, cbCtx=None, + contextName=null, instanceIndex=None): if self.snmpContext is None: raise error.ProtocolError('SNMP context not specified') @@ -432,25 +322,21 @@ def _sendNotification(self, else: notificationName = rfc1902.ObjectIdentity(notificationName) - varBinds = rfc1902.NotificationType( - notificationName, instanceIndex=instanceIndex - ).resolveWithMib(mibViewController) + varBinds = rfc1902.NotificationType(notificationName, + instanceIndex=instanceIndex) + varBinds.resolveWithMib(mibViewController) mibInstrumController = self.snmpContext.getMibInstrum(contextName) varBinds = varBinds[:1] + mibInstrumController.readVars(varBinds[1:]) - return self.sendVarBinds(snmpEngine, - notificationTarget, + return self.sendVarBinds(snmpEngine, notificationTarget, self.snmpContext.contextEngineId, - contextName, - varBinds + list(additionalVarBinds), - _sendNotificationCbFun, - (cbFun, cbCtx)) + contextName, varBinds + list(additionalVarBinds), + _sendNotificationCbFun, (cbFun, cbCtx)) # install compatibility wrapper NotificationOriginator.sendNotification = _sendNotification # XXX # move/group/implement config setting/retrieval at a stand-alone module - diff --git a/pysnmp/entity/rfc3413/ntfrcv.py b/pysnmp/entity/rfc3413/ntfrcv.py index 0d3675aa..e78c512a 100644 --- a/pysnmp/entity/rfc3413/ntfrcv.py +++ b/pysnmp/entity/rfc3413/ntfrcv.py @@ -7,11 +7,8 @@ from pysnmp import debug # 3.4 class NotificationReceiver: - pduTypes = ( - v1.TrapPDU.tagSet, - v2c.SNMPv2TrapPDU.tagSet, - v2c.InformRequestPDU.tagSet - ) + pduTypes = (v1.TrapPDU.tagSet, v2c.SNMPv2TrapPDU.tagSet, + v2c.InformRequestPDU.tagSet) def __init__(self, snmpEngine, cbFun, cbCtx=None): snmpEngine.msgAndPduDsp.registerContextEngineId( @@ -27,20 +24,10 @@ class NotificationReceiver: ) self.__cbFun = self.__cbCtx = None - def processPdu( - self, - snmpEngine, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - PDU, - maxSizeResponseScopedPDU, - stateReference - ): + def processPdu(self, snmpEngine, messageProcessingModel, + securityModel, securityName, securityLevel, + contextEngineId, contextName, pduVersion, PDU, + maxSizeResponseScopedPDU, stateReference): # Agent-side API complies with SMIv2 if messageProcessingModel == 0: @@ -77,19 +64,11 @@ class NotificationReceiver: # 3.4.3 try: snmpEngine.msgAndPduDsp.returnResponsePdu( - snmpEngine, - messageProcessingModel, - securityModel, - securityName, - securityLevel, - contextEngineId, - contextName, - pduVersion, - rspPDU, - maxSizeResponseScopedPDU, - stateReference, - statusInformation - ) + snmpEngine, messageProcessingModel, securityModel, + securityName, securityLevel, contextEngineId, + contextName, pduVersion, rspPDU, maxSizeResponseScopedPDU, + stateReference, statusInformation) + except error.StatusInformation: debug.logger & debug.flagApp and debug.logger('processPdu: stateReference %s, statusInformation %s' % (stateReference, sys.exc_info()[1])) snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'snmpSilentDrops') @@ -103,20 +82,14 @@ class NotificationReceiver: debug.logger & debug.flagApp and debug.logger('processPdu: stateReference %s, user cbFun %s, cbCtx %s, varBinds %s' % (stateReference, self.__cbFun, self.__cbCtx, varBinds)) if self.__cbFunVer: - self.__cbFun( - snmpEngine, stateReference, contextEngineId, contextName, - varBinds, self.__cbCtx - ) + self.__cbFun(snmpEngine, stateReference, contextEngineId, + contextName, varBinds, self.__cbCtx) else: # Compatibility stub (handle legacy cbFun interface) try: - self.__cbFun( - snmpEngine, contextEngineId, contextName, - varBinds, self.__cbCtx - ) + self.__cbFun(snmpEngine, contextEngineId, contextName, + varBinds, self.__cbCtx) except TypeError: self.__cbFunVer = 1 - self.__cbFun( - snmpEngine, stateReference, contextEngineId, contextName, - varBinds, self.__cbCtx - ) + self.__cbFun(snmpEngine, stateReference, contextEngineId, + contextName, varBinds, self.__cbCtx) |