def execute(self): """ Execute the close-session operation. """ if self.operationReply.isError(): return self.operationReply sessionToKill = sessionManager.getInstance().getSessionFromId(self.sessionId) if sessionToKill == None: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "No Netconf session has this session-id.") moduleReply.addErrorInfo("bad-element",str(self.sessionId)) self.operationReply.setError() elif sessionToKill == self.session: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "A Netconf session can not kill itself.") moduleReply.addErrorInfo("bad-element",str(self.sessionId)) self.operationReply.setError() else: sessionManager.getInstance().closeSession(sessionToKill) moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the close-session operation. """ if self.operationReply.isError(): return self.operationReply sessionToKill = sessionManager.getInstance().getSessionFromId( self.sessionId) if sessionToKill == None: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message="No Netconf session has this session-id.") moduleReply.addErrorInfo("bad-element", str(self.sessionId)) self.operationReply.setError() elif sessionToKill == self.session: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message="A Netconf session can not kill itself.") moduleReply.addErrorInfo("bad-element", str(self.sessionId)) self.operationReply.setError() else: sessionManager.getInstance().closeSession(sessionToKill) moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the lock operation. """ nodeReply = sessionManager.getInstance().lock(self.session, self.target) self.operationReply.setNode(nodeReply) return self.operationReply
def execute(self): """ Execute the copy-config operation. """ if self.operationReply.isError(): return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId(C.CANDIDATE) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.LOCK_DENIED, error_severity = ModuleReply.ERROR, error_message = "The running datastore is locked by another entity.") moduleReply.addErrorInfo("session-id",sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Just do a copy-config from running to candidate op = Copy_config_operation() op.setOperationReply() op.setSession(self.session) op.source = C.RUNNING op.target = C.CANDIDATE # Execute the command operationReply = op.execute() return operationReply
def execute(self): """ Execute the copy-config operation. """ if self.operationReply.isError(): return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId( C.CANDIDATE) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.LOCK_DENIED, error_severity=ModuleReply.ERROR, error_message= "The running datastore is locked by another entity.") moduleReply.addErrorInfo("session-id", sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Just do a copy-config from running to candidate op = Copy_config_operation() op.setOperationReply() op.setSession(self.session) op.source = C.RUNNING op.target = C.CANDIDATE # Execute the command operationReply = op.execute() return operationReply
def exchangeCapabilities(self, clientsock): """ Exchange the capabilities with the manager. First sends the agent capabilities. Then waits for the remote manager capabilities. Creates a Netconf session and returns it. @type clientsock: socket @param clientsock: The socket for the current client @rtype: session @return: The session created by the SessionManager. """ # Loading hello element along with static capabilities from hello.xml file helloRoot = NonvalidatingReader.parseUri(C.HELLO_URI) helloNode = helloRoot.documentElement # Finding a unique session-id for that session self.session = sessionManager.getInstance().createSession( clientsock, self.username) sessionId = self.session.getSessionId() LogManager.getInstance().logInfo( "opening Netconf session: (sessionId=%s)" % (self.session.getSessionId())) # Setup the session-id value within session-id node sessionIdNode = helloRoot.createElementNS(C.NETCONF_XMLNS, C.SESSION_ID) helloNode.appendChild(sessionIdNode) sessionIdNode.appendChild(helloRoot.createTextNode(str(sessionId))) # Get the unique instance of the singleton ModuleManager: moduleManager = ModuleManager.getInstance() # Add the capabilities related to the modules to the hello message: for node in helloNode.childNodes: if (node.nodeType == Node.ELEMENT_NODE and node.tagName == C.CAPABILITIES): for module in moduleManager.getModules(): capabNode = helloRoot.createElementNS( C.NETCONF_XMLNS, C.CAPABILITY) capabText = helloRoot.createTextNode(module.namespace) capabNode.appendChild(capabText) node.appendChild(capabNode) # Convert the hello element to String before sending hellostr = util.convertNodeToString(helloNode) # Sending capabilities along with a new session-id self.send(hellostr) # Receiving capabilities of the manager data = self.receive() # Printing Manager capabilities LogManager.getInstance().logInfo( "Manager capabilities received: (sessionId=%s)" % (self.session.getSessionId()))
def exchangeCapabilities(self, clientsock): """ Exchange the capabilities with the manager. First sends the agent capabilities. Then waits for the remote manager capabilities. Creates a Netconf session and returns it. @type clientsock: socket @param clientsock: The socket for the current client @rtype: session @return: The session created by the SessionManager. """ # Loading hello element along with static capabilities from hello.xml file helloRoot = NonvalidatingReader.parseUri(C.HELLO_URI) helloNode = helloRoot.documentElement # Finding a unique session-id for that session self.session = sessionManager.getInstance().createSession(clientsock, self.username) sessionId = self.session.getSessionId() LogManager.getInstance().logInfo("opening Netconf session: (sessionId=%s)" % (self.session.getSessionId())) # Setup the session-id value within session-id node sessionIdNode = helloRoot.createElementNS(C.NETCONF_XMLNS, C.SESSION_ID) helloNode.appendChild(sessionIdNode) sessionIdNode.appendChild(helloRoot.createTextNode(str(sessionId))) # Get the unique instance of the singleton ModuleManager: moduleManager = ModuleManager.getInstance() # Add the capabilities related to the modules to the hello message: for node in helloNode.childNodes: if (node.nodeType== Node.ELEMENT_NODE and node.tagName == C.CAPABILITIES): for module in moduleManager.getModules(): capabNode = helloRoot.createElementNS(C.NETCONF_XMLNS, C.CAPABILITY) capabText = helloRoot.createTextNode(module.namespace) capabNode.appendChild(capabText) node.appendChild(capabNode) # Convert the hello element to String before sending hellostr = util.convertNodeToString(helloNode) # Sending capabilities along with a new session-id self.send(hellostr) # Receiving capabilities of the manager data = self.receive() # Printing Manager capabilities LogManager.getInstance().logInfo("Manager capabilities received: (sessionId=%s)" % (self.session.getSessionId()))
def loop(self): # Loop until ending connection while 1: # Receving a new Netconf request data = self.receive() processeddata = "" if (data == -1): msg = "Problem while receiving message. Please check application protocol and options (sessionId=%s)" % ( self.session.getSessionId()) LogManager.getInstance().logError(msg) sessionManager.getInstance().closeSession(self.session) break elif (data == -2): # Remote socket was badly closed. Closing session. msg = "Remote socket seems closed: (sessionId=%s). Closing Netconf session..." % ( self.session.getSessionId()) LogManager.getInstance().logError(msg) sessionManager.getInstance().closeSession(self.session) break else: # Processing the Netconf request try: processeddata = self.processrequest(data) except Exception, exp: LogManager.getInstance().logError( "remote socket seems closed: (sessionId=%s,error=%s)" % (self.session.getSessionId(), str(exp))) if (processeddata == ''): moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="The response is unexpectedly empty.") nodeReply = moduleReply.getXMLNodeReply() processeddata = util.convertNodeToString(nodeReply) # Sending the response self.send(processeddata) if self.session.mustBeClosed == 1: LogManager.getInstance().logInfo( "closing Netconf session: (sessionId=%s)" % (self.session.getSessionId())) sessionManager.getInstance().closeSession(self.session) break
def loop(self): # Loop until ending connection while 1 : # Receving a new Netconf request data = self.receive() processeddata = "" if (data == -1): msg = "Problem while receiving message. Please check application protocol and options (sessionId=%s)" % (self.session.getSessionId()) LogManager.getInstance().logError(msg) sessionManager.getInstance().closeSession(self.session) break elif (data == -2): # Remote socket was badly closed. Closing session. msg = "Remote socket seems closed: (sessionId=%s). Closing Netconf session..." % (self.session.getSessionId()) LogManager.getInstance().logError(msg) sessionManager.getInstance().closeSession(self.session) break else: # Processing the Netconf request try: processeddata = self.processrequest(data) except Exception,exp: LogManager.getInstance().logError("remote socket seems closed: (sessionId=%s,error=%s)" % (self.session.getSessionId() , str(exp))) if (processeddata == ''): moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="The response is unexpectedly empty.") nodeReply = moduleReply.getXMLNodeReply() processeddata = util.convertNodeToString(nodeReply) # Sending the response self.send(processeddata) if self.session.mustBeClosed == 1: LogManager.getInstance().logInfo("closing Netconf session: (sessionId=%s)" % (self.session.getSessionId())) sessionManager.getInstance().closeSession(self.session) break
def execute(self): """ Execute the copy-config operation. """ if self.operationReply.isError(): return self.operationReply if self.target == self.source: if (self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING] or (self.target == C.URL and self.urlSource == self.urlTarget)): moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message="Source and target are equals.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId( self.target) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.LOCK_DENIED, error_severity=ModuleReply.ERROR, error_message= "Access to the requested lock is denied because the lock is currently held by another entity" ) moduleReply.addErrorInfo("session-id", sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessCopyConfig( self.session) if (not operation_allowed): moduleReply = ModuleReply(error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.ACCESS_DENIED, error_severity=ModuleReply.ERROR, error_message="Access denied.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply sourceDoc = None if self.source == C.URL: sourceUri = self.urlSource try: # get the DOM tree of the candidate configuration sourceDoc = NonvalidatingReader.parseUri( sourceUri).documentElement except Exception, exp: moduleReply = ModuleReply(error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.DATA_MISSING, error_severity=ModuleReply.ERROR, error_message=self.source + " is not well-formed.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the edit-config operation. """ if self.operationReply.isError(): return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId( self.target) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.IN_USE, error_severity=ModuleReply.ERROR, error_message= "Operation failed, target is locked by another session.") moduleReply.addErrorInfo("session-id", sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessEditConfig( self.config, self.session) if (not operation_allowed): moduleReply = ModuleReply(error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.ACCESS_DENIED, error_severity=ModuleReply.ERROR, error_message="Access denied.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Execute edit-config #if (self.target == C.RUNNING or self.target == C.URL): if (self.target in [C.RUNNING, C.STARTUP, C.CANDIDATE, C.URL]): moduleReply = None for module in self.moduleManager.getModules(): currentPath = module.path # Finding the nodes (related to the current module) # in the received configuration ctx = Context(self.config, processorNss=self.moduleManager.getPrefixes()) nodeList = Evaluate(currentPath, ctx) # Distributing the nodes to the current module if len(nodeList) > 1: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "More than one node found in the received data for the same module" + module.name) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply elif len(nodeList) == 1: defaultOperation = self.defaultOperation node = nodeList[0] # Search for the first parent node of 'node' having xc:operation attribute and set the default value to its value: tmpNode = node.parentNode boolean = False while tmpNode != None and tmpNode.nodeType != Node.DOCUMENT_NODE and not boolean: for att in tmpNode.attributes: ns, name = att if name == 'operation' and ns == C.NETCONF_XMLNS: # We found the first parent node having an operation attribute. # This is the new default op for this module. defaultOperation = tmpNode.getAttributeNS( ns, str(name)) # Stop the loop. boolean = True tmpNode = tmpNode.parentNode # Send the operation to the module. moduleReply = module.editConfig(defaultOperation, self.testOption, self.errorOption, self.target, node) # Update the cache according to the new config. module.resetConfigTime(self.target) if (moduleReply.isError()): if self.errorOption == C.STOP_ON_ERROR: self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return self.operationReply elif self.errorOption == C.CONTINUE_ON_ERROR: pass elif self.errorOption == C.ROLLBACK_ON_ERROR: # ROLLBACK IS EXPERIMENTAL for previousmodule in self.moduleManager.getModules( ): if previousmodule is module: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "%s module failed and the whole operation has been rolled back." % module.name) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return self.operationReply else: moduleReply = previousmodule.rollBack() if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return self.operationReply if (moduleReply == None): moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="Not any module matched") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the edit-config operation. """ if self.operationReply.isError(): return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId(self.target) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.IN_USE, error_severity = ModuleReply.ERROR, error_message = "Operation failed, target is locked by another session.") moduleReply.addErrorInfo("session-id",sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessEditConfig(self.config, self.session) if (not operation_allowed): moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.ACCESS_DENIED, error_severity = ModuleReply.ERROR, error_message = "Access denied.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Execute edit-config #if (self.target == C.RUNNING or self.target == C.URL): if (self.target in [C.RUNNING, C.STARTUP, C.CANDIDATE, C.URL]): moduleReply = None for module in self.moduleManager.getModules(): currentPath = module.path # Finding the nodes (related to the current module) # in the received configuration ctx = Context(self.config, processorNss = self.moduleManager.getPrefixes()) nodeList = Evaluate(currentPath, ctx) # Distributing the nodes to the current module if len(nodeList)>1: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "More than one node found in the received data for the same module" + module.name) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply elif len(nodeList)==1: defaultOperation = self.defaultOperation node = nodeList[0] # Search for the first parent node of 'node' having xc:operation attribute and set the default value to its value: tmpNode = node.parentNode boolean = False while tmpNode != None and tmpNode.nodeType != Node.DOCUMENT_NODE and not boolean: for att in tmpNode.attributes: ns, name = att if name == 'operation' and ns == C.NETCONF_XMLNS: # We found the first parent node having an operation attribute. # This is the new default op for this module. defaultOperation = tmpNode.getAttributeNS(ns, str(name)) # Stop the loop. boolean = True tmpNode = tmpNode.parentNode # Send the operation to the module. moduleReply = module.editConfig(defaultOperation, self.testOption, self.errorOption, self.target, node) # Update the cache according to the new config. module.resetConfigTime(self.target) if (moduleReply.isError()): if self.errorOption == C.STOP_ON_ERROR: self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply elif self.errorOption == C.CONTINUE_ON_ERROR: pass elif self.errorOption == C.ROLLBACK_ON_ERROR: # ROLLBACK IS EXPERIMENTAL for previousmodule in self.moduleManager.getModules(): if previousmodule is module: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "%s module failed and the whole operation has been rolled back." % module.name) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply else: moduleReply = previousmodule.rollBack() if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply if (moduleReply == None): moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Not any module matched") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the copy-config operation. """ if self.operationReply.isError(): return self.operationReply if self.target == self.source: if (self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING] or (self.target == C.URL and self.urlSource == self.urlTarget)): moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "Source and target are equals.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply sessionId = sessionManager.getInstance().getLockOwnerSessionId(self.target) if sessionId >= 0 and sessionId != self.session.getSessionId(): # Somebody else locked the target. moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.LOCK_DENIED, error_severity = ModuleReply.ERROR, error_message = "Access to the requested lock is denied because the lock is currently held by another entity") moduleReply.addErrorInfo("session-id",sessionId) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessCopyConfig(self.session) if (not operation_allowed): moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.ACCESS_DENIED, error_severity = ModuleReply.ERROR, error_message = "Access denied.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply sourceDoc = None if self.source == C.URL: sourceUri = self.urlSource try: # get the DOM tree of the candidate configuration sourceDoc = NonvalidatingReader.parseUri(sourceUri).documentElement except Exception, exp: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.DATA_MISSING, error_severity = ModuleReply.ERROR, error_message = self.source + " is not well-formed.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply