def filterResponse(self): # Creating the XML response filter moduleReply = None abstractFilter = None if self.filterType == C.SUBTREE: if self.subtree != None: abstractFilter = SubtreeFilter(self.operationReply.getNode(), self.subtree) moduleReply = abstractFilter.applyFilter() else: moduleReply = ModuleReply(replynode=self.operationReply.getNode()) elif self.filterType == C.XPATH: abstractFilter = XpathFilter(self.operationReply.getNode(), [self.xpathrequest], self.prefixes) moduleReply = abstractFilter.applyFilter() if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: self.operationReply.setNode(moduleReply.getXMLNodeReply()) rm = rbacManager.getInstance() if rm.isActive(): moduleReply = rm.filterNode(self.operationReply.getNode(), self.session) if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: self.operationReply.setNode(moduleReply.getXMLNodeReply())
def setParameters(self, operation, NSS = None): """ Set the parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.SESSION_ID: for node in child.childNodes: if node.nodeType==Node.TEXT_NODE: try: self.sessionId = int(node.nodeValue) except Exception,exp: moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "This is not a valid format for session-id parameter.") moduleReply.addErrorInfo("bad-element",str(node.nodeValue)) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS=None): """ Set the target parameter @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP]: self.target = node.tagName elif node.tagName == C.URL: self.urlTarget = string.strip( str(node.childNodes[0].nodeValue)) elif node.tagName == C.RUNNING: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message= "The running configuration can not be deleted." ) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS=None): """ Set the target parameter @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP]: self.target = node.tagName elif node.tagName == C.URL: self.urlTarget = string.strip(str(node.childNodes[0].nodeValue)) elif node.tagName == C.RUNNING: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message="The running configuration can not be deleted.", ) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.", ) moduleReply.addErrorInfo("bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.", ) moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def processrequest(self, data): """ Forward the received message to the requestScanner and returns its reply @type data: string @param data: The message received from the client socket @rtype: string @return: The serialized XML reply to be sent back to the Netconf Manager """ self.netconfLock.acquire() try: # try to build the DOM, checking well-formness # http://madynes.loria.fr is there to avoid a warning... doc = NonvalidatingReader.parseString(data, 'http://madynes.loria.fr') # XML Schema validation #self.valid = util.validate(data,[C.NETCONF_SCHEMA_URI]) mainNode = doc.documentElement if mainNode.tagName == C.RPC: rpcRequest = Rpc(mainNode, self.session) response = rpcRequest.execute() elif mainNode.tagName == C.HELLO: response = '' else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "An element is not known. It should be an rpc or hello tag." ) moduleReply.addErrorInfo("bad-element", mainNode.tagName) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply) except Exception, exp: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="The Netconf message is not well-formed." + str(exp)) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply)
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 deactivate(self, session, roles): for role in roles: if (role in session.roles): session.roles.remove(role) moduleReply = ModuleReply() return moduleReply.getXMLNodeReply()
def activate(self, session, roles): moduleReply = None allowedRoles = self.getAllowedRoles(session.user) for role in roles: if (role in allowedRoles): if (role not in session.roles): session.roles.append(role) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.PARTIAL_OPERATION, error_severity = ModuleReply.WARNING, error_message = "Role " + role.name + " is already active. Some other roles may have been activated.") moduleReply.addErrorInfo("bad-element",role.name) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.PARTIAL_OPERATION, error_severity = ModuleReply.WARNING, error_message = "User "+ session.user.login +" can not activate role " + role.name + ". Some other roles may have been activated.") moduleReply.addErrorInfo("bad-element",role.name) if moduleReply == None: moduleReply = ModuleReply() return moduleReply.getXMLNodeReply()
def unlock(self, session, target): """ Try to unlock a target configuration (One of RUNNING, CANDIDATE, STARTUP) @type session: Session @param session: The current session that tries to acquire the lock @type target: String @param target: The target to unlock @rtype: moduleReply @return: A module reply """ sessionId = self.getLockOwnerSessionId(target) if sessionId == session.getSessionId() : session.unlock(str(target)) moduleReply = ModuleReply() elif sessionId < 0: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.OPERATION_FAILED, error_severity = ModuleReply.ERROR, error_message = "The specified target (%s) is not locked." % str(target)) elif sessionId != session.getSessionId : moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.OPERATION_FAILED, error_severity = ModuleReply.ERROR, error_message = "Unlock failed, another session holds the lock.") moduleReply.addErrorInfo(C.SESSION_ID,str(sessionId)) return moduleReply.getXMLNodeReply()
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 lock(self, session, target): """ Try to lock a target configuration (One of RUNNING, CANDIDATE, STARTUP) @type session: Session @param session: The current session that tries to acquire the lock @type target: String @param target: The target to lock @rtype: moduleReply @return: A module reply """ sessionId = self.getLockOwnerSessionId(target) if sessionId >= 0 : moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.IN_USE, error_severity = ModuleReply.ERROR, error_message = "Lock failed, lock is already held") moduleReply.addErrorInfo(C.SESSION_ID,str(sessionId)) else: session.lock(target) moduleReply = ModuleReply() return moduleReply.getXMLNodeReply()
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 setParameters(self, operation): """ Set the parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == "command": for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in ["install","update","remove"]: self.command = node.tagName else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element value is not correct.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == "packages": for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName == "package": pkgName = node.childNodes[0].nodeValue self.packages.append(pkgName)
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 checkInternalNestingValidity(self, selectedNodes): for elem in selectedNodes: deepvalue = elem.getAttributeNS( "urn:ietf:params:xml:ns:netconf:base:1.0", "operation") tmp = elem while (tmp.parentNode != elem.ownerDocument.documentElement): if tmp.parentNode in selectedNodes: # It means that elem is a child (direct or not) of another SelectedNode highvalue = tmp.parentNode.getAttributeNS( "urn:ietf:params:xml:ns:netconf:base:1.0", "operation") if highvalue != "merge": moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.MISSING_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "%s can not have %s operation in its subelements." % (highvalue, deepvalue)) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return #return "%s can not have %s operation in its subelements." % (highvalue, deepvalue) else: tmp = tmp.parentNode
def processrequest(self, data): """ Forward the received message to the requestScanner and returns its reply @type data: string @param data: The message received from the client socket @rtype: string @return: The serialized XML reply to be sent back to the Netconf Manager """ self.netconfLock.acquire() try: # try to build the DOM, checking well-formness # http://madynes.loria.fr is there to avoid a warning... doc = NonvalidatingReader.parseString(data, 'http://madynes.loria.fr') # XML Schema validation #self.valid = util.validate(data,[C.NETCONF_SCHEMA_URI]) mainNode = doc.documentElement if mainNode.tagName == C.RPC: rpcRequest = Rpc(mainNode, self.session) response = rpcRequest.execute() elif mainNode.tagName == C.HELLO: response ='' else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message = "An element is not known. It should be an rpc or hello tag.") moduleReply.addErrorInfo("bad-element",mainNode.tagName) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply) except Exception,exp: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="The Netconf message is not well-formed."+str(exp)) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply)
def checkNestingValidity(self, prefixes): prefixes["xc"] = C.NETCONF_XMLNS xpathRequest = "//*[@xc:operation]" ctx = Context(self.config, processorNss=prefixes) selectedNodes = Evaluate(xpathRequest, ctx) if self.defaultOperation == "replace": if len(selectedNodes) != 0: #print "When default operation is replace, no other operation attribute is allowed." moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "When default operation is replace, no other operation attribute is allowed." ) moduleReply.addErrorInfo("bad-element", selectedNodes[0]) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: return elif self.defaultOperation == "none": if len(selectedNodes) == 0: #print "When default operation is none, at least one operation attribute must appear." moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.MISSING_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "When default operation is none, at least one operation attribute must appear." ) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: # Check the internal nestings, independently of default operation. self.checkInternalNestingValidity(selectedNodes) elif self.defaultOperation == "merge": # Check the internal nestings, independently of default operation. valid = self.checkInternalNestingValidity(selectedNodes)
def execute(self): """ Execute the delete-config operation. """ if self.operationReply.isError(): return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessDeleteConfig( 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 if self.target in [C.CANDIDATE, C.STARTUP]: moduleReply = self.datastoreManager.removeConfig( self.target, "config") elif (self.target == C.URL): # BUG : should delete the specified url (e.g. on a ftp server) moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.OPERATION_NOT_SUPPORTED, error_severity=ModuleReply.ERROR, error_message="Cannot delete the document at the specified url." ) if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the delete-config operation. """ if self.operationReply.isError(): return self.operationReply # Check access here if self.rbacManager.isActive(): operation_allowed = self.rbacManager.checkAccessDeleteConfig(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 if self.target in [C.CANDIDATE, C.STARTUP]: moduleReply = self.datastoreManager.removeConfig(self.target, "config") elif self.target == C.URL: # BUG : should delete the specified url (e.g. on a ftp server) moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.OPERATION_NOT_SUPPORTED, error_severity=ModuleReply.ERROR, error_message="Cannot delete the document at the specified url.", ) if moduleReply.isError(): self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def execute(self): """ Execute the close-session operation. """ if self.operationReply.isError(): return self.operationReply self.session.mustBeClosed = 1 moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def checkNestingValidity(self, prefixes): prefixes["xc"] = C.NETCONF_XMLNS xpathRequest = "//*[@xc:operation]" ctx = Context(self.config, processorNss = prefixes) selectedNodes = Evaluate(xpathRequest, ctx) if self.defaultOperation == "replace": if len(selectedNodes) != 0: #print "When default operation is replace, no other operation attribute is allowed." moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "When default operation is replace, no other operation attribute is allowed.") moduleReply.addErrorInfo("bad-element",selectedNodes[0]) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: return elif self.defaultOperation == "none": if len(selectedNodes) == 0: #print "When default operation is none, at least one operation attribute must appear." moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.MISSING_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "When default operation is none, at least one operation attribute must appear.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: # Check the internal nestings, independently of default operation. self.checkInternalNestingValidity(selectedNodes) elif self.defaultOperation == "merge": # Check the internal nestings, independently of default operation. valid = self.checkInternalNestingValidity(selectedNodes)
def setParameters(self, operation, NSS=None): """ Set the parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.SESSION_ID: for node in child.childNodes: if node.nodeType == Node.TEXT_NODE: try: self.sessionId = int(node.nodeValue) except Exception, exp: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message= "This is not a valid format for session-id parameter." ) moduleReply.addErrorInfo( "bad-element", str(node.nodeValue)) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
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 getConfig(self, sourceName): doc = self.datastoreManager.getConfig(sourceName, "config") if doc == None: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="%s configuration does not exist." % sourceName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: self.operationReply.setNode(doc)
def getConfig(self, sourceName): doc = self.datastoreManager.getConfig(sourceName, "state") if doc == None: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "%s configuration does not exist." % sourceName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: self.operationReply.setNode(doc.documentElement)
def execute(self): """ Execute the operation. """ print "restart Operation executed" module = self.moduleManager.getModuleFromName(self.targetModule) if module == None: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "No Module s% ."%self.targetModule) else: moduleReply = module.restart() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
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 setParameters(self, operation, NSS=None): """ Set the parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS = None): """ Set the parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def checkInternalNestingValidity(self, selectedNodes): for elem in selectedNodes: deepvalue = elem.getAttributeNS("urn:ietf:params:xml:ns:netconf:base:1.0", "operation") tmp = elem while (tmp.parentNode != elem.ownerDocument.documentElement): if tmp.parentNode in selectedNodes: # It means that elem is a child (direct or not) of another SelectedNode highvalue = tmp.parentNode.getAttributeNS("urn:ietf:params:xml:ns:netconf:base:1.0", "operation") if highvalue != "merge": moduleReply = ModuleReply( error_type = ModuleReply.APPLICATION, error_tag = ModuleReply.MISSING_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "%s can not have %s operation in its subelements." % (highvalue, deepvalue)) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return #return "%s can not have %s operation in its subelements." % (highvalue, deepvalue) else: tmp = tmp.parentNode
def execute(self): """ Execute the yum operation. """ stringCmd = "/etc/init.d/" + self.service + " " + self.command res = os.popen3(stringCmd) resultat = res[1].read() print "-----" print resultat resultat = res[2].read() print "-----" print resultat #doc = implementation.createDocument(C.NETCONF_XMLNS, "report", None) #nameNode = doc.createElementNS(None,module.name) #modulesNode.appendChild(nameNode) moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def setParameters(self, operation, NSS=None): """ Set the source parameter and the config parameter, config then contains the configuration to validate. @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP, C.RUNNING]: self.target = node.tagName elif node.tagName == C.CONFIG: self.target = node.tagName for blub in node.childNodes: if blub.nodeType == Node.ELEMENT_NODE: if blub.localName == "netconf" and blub.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.config = implementation.createDocument(C.YENCAP_XMLNS, "netconf", None) clone = self.config.importNode(blub, True) # self.config is a document which documentElement is "netconf" self.config.replaceChild(clone, self.config.documentElement) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.", ) moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS = None): """ Set the source parameter and the config parameter, config then contains the configuration to validate. @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP, C.RUNNING]: self.target = node.tagName elif node.tagName == C.CONFIG: self.target = node.tagName for blub in node.childNodes: if blub.nodeType==Node.ELEMENT_NODE: if blub.localName == "netconf" and blub.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.config = implementation.createDocument(C.YENCAP_XMLNS, "netconf", None) clone = self.config.importNode(blub, True) # self.config is a document which documentElement is "netconf" self.config.replaceChild(clone, self.config.documentElement) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS=None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.FILTER: self.filter = child for att in self.filter.attributes.values(): if att.name == "type": if att.value == C.SUBTREE: self.filterType = C.SUBTREE elif att.value == C.XPATH: self.filterType = C.XPATH else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ATTRIBUTE, error_severity=ModuleReply.ERROR, error_message= "An attribute value is not correct") moduleReply.addErrorInfo( "bad-attribute", att.name) moduleReply.addErrorInfo( "bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif att.name == "select": self.xpathrequest = att.value else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ATTRIBUTE, error_severity=ModuleReply.ERROR, error_message= "An unexpected attribute is present") moduleReply.addErrorInfo("bad-attribute", att.name) moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return # if self.filterType == C.XPATH: # tmpval = self.filter.childNodes[0].nodeValue # self.xpathrequest = string.strip(str(tmpval)) if (self.xpathrequest != None and self.filterType != C.XPATH): moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ATTRIBUTE, error_severity=ModuleReply.ERROR, error_message="select without xpath type") moduleReply.addErrorInfo("bad-attribute", att.name) moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif self.filterType == C.SUBTREE: for node in self.filter.childNodes: if (node.nodeType == Node.ELEMENT_NODE): self.subtree = node # Add by manu 03/09 # to deal with the special case <get><filter></filter> if self.subtree == None: self.subtree = implementation.createDocument( C.YENCAP_XMLNS, 'data', None).documentElement else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return # Add by manu 03/09 # to deal with the special case <get/> if self.filter == None: self.subtree = implementation.createDocument( C.YENCAP_XMLNS, 'netconf', None).documentElement
def setParameters(self, operation, NSS = None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP, C.RUNNING]: self.target = node.tagName if node.tagName == C.URL: self.urlTarget = string.strip(str(node.childNodes[0].nodeValue)) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.CONFIG: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.localName == "netconf" and node.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.config = implementation.createDocument(C.YENCAP_XMLNS, "netconf", None) clone = self.config.importNode(node, True) # self.config is a document which documentElement is "netconf" self.config.replaceChild(clone, self.config.documentElement) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "The root node of the configuration must be netconf.") moduleReply.addErrorInfo("bad-element",node.localName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.DEFAULT_OPERATION: for node in child.childNodes: if node.nodeType==Node.TEXT_NODE: if node.nodeValue in [C.MERGE,C.REPLACE,C.NONE]: self.defaultOperation = node.nodeValue else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element value is not correct.") moduleReply.addErrorInfo("bad-element",node.nodeValue) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.TEST_OPTION: for node in child.childNodes: if node.nodeType==Node.TEXT_NODE: if node.nodeValue in [C.TEST_THEN_SET,C.SET]: self.testOption = node.nodeValue else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "An element value is not correct.") moduleReply.addErrorInfo("bad-element",node.nodeValue) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.ERROR_OPTION: for node in child.childNodes: if node.nodeType==Node.TEXT_NODE: if node.nodeValue in [C.STOP_ON_ERROR, C.CONTINUE_ON_ERROR, C.ROLLBACK_ON_ERROR]: self.errorOption = node.nodeValue else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "An element value is not correct.") moduleReply.addErrorInfo("bad-element",node.nodeValue) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.checkNestingValidity(NSS)
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 setParameters(self, operation, NSS=None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [ C.RUNNING, C.CANDIDATE, C.STARTUP ]: self.source = node.tagName else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "An element value is not correct.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.FILTER: self.filter = child for att in self.filter.attributes.values(): if att.name == "type": if att.value == C.SUBTREE: self.filterType = C.SUBTREE elif att.value == C.XPATH: self.filterType = C.XPATH else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ATTRIBUTE, error_severity=ModuleReply.ERROR, error_message= "An attribute value is not correct") moduleReply.addErrorInfo( "bad-attribute", att.name) moduleReply.addErrorInfo( "bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ATTRIBUTE, error_severity=ModuleReply.ERROR, error_message= "An unexpected attribute is present") moduleReply.addErrorInfo("bad-attribute", att.name) moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return if self.filterType == C.XPATH: tmpval = self.filter.childNodes[0].nodeValue self.xpathrequest = string.strip(str(tmpval)) if self.xpathrequest == "": moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message= "The request specifies an unacceptable value for one or more parameters." ) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif self.filterType == C.SUBTREE: for node in self.filter.childNodes: if (node.nodeType == Node.ELEMENT_NODE): self.subtree = node # Add by manu 03/09 # to deal with the special case <get-config><filter></filter> if self.subtree == None: self.subtree = implementation.createDocument( C.YENCAP_XMLNS, 'data', None).documentElement else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return if self.filter == None: self.subtree = implementation.createDocument( C.YENCAP_XMLNS, 'data', None).documentElement
def execute(self): """ Execute the Manage_modules operation. """ moduleReply = ModuleReply() operationType = self.operationItem["operation"] name = self.operationItem["name"] moduleManager = ModuleManager.getInstance() if operationType == "deploy": if moduleManager.getModuleFromName(name) != None: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s is loaded and therefore can not be deployed again."%name) else: # Decode the Foo_Module.zip fileContent = self.operationItem["file"] fileContent = fileContent.decode("string_escape") # Save it in tmp dir: f = open("/tmp/toto.zip",'wb') f.write(fileContent) f.close() # Install it in YencaP modules directory: os.popen3("unzip /tmp/toto.zip " + '-d ' + C.YENCAP_HOME + '/Modules') #self.extract('/tmp/toto.zip', '/tmp/' + name + '_Module') # Update modules.xml moduleReader = ModuleReader.getInstance() moduleReader.addModule(name, self.operationItem["xpath"], self.operationItem["namespace"], self.operationItem["pref"], self.operationItem["cachelifetime"], self.operationItem["dictionnary"]) moduleReader.writeModules() # Update hello.xml: No. This is done automatically after loading the deployed module. See Server.py elif operationType == "undeploy": if moduleManager.getModuleFromName(name) != None: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s is loaded and therefore can not be undeployed. Unload it first."%name) else: # Remove the module from the file system modulepath = C.YENCAP_HOME + '/Modules/' + name + "_Module" # Remove the content of the module directory (os.rmdir works only if the dir is empty !!!) self.removeall(modulepath) # Remove the directory itself os.rmdir(modulepath) # Update modules.xml moduleReader = ModuleReader.getInstance() moduleReader.removeModule(name) moduleReader.writeModules() # Update hello.xml: No. This is done automatically. See Server.py elif operationType == "load": res = moduleManager.loadModule(name) if res == 0: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s could not be loaded."%name) elif res == 2: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s is already loaded."%name) elif res == 3: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s does not exist and therefore can not be loaded."%name) elif res == 1: moduleReply = ModuleReply() elif operationType == "unload": res = moduleManager.unloadModule(name) if res == 0: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Module %s is not loaded and therefore could not be unloaded."%name) elif res == 1: moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def setParameters(self, operation, NSS = None): """ Set the target parameter @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName in [C.ACTIVATE,C.DEACTIVATE]: self.action = child.tagName for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in C.ROLES: for nodeee in node.childNodes: if nodeee.nodeType==Node.ELEMENT_NODE: if nodeee.tagName in C.ROLE: tmpval = nodeee.childNodes[0].nodeValue roleName = string.strip(str(tmpval)) role = rbacManager.getInstance().getRoleFromName(roleName) if role == None: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "Role " + roleName + " does not exist.") moduleReply.addErrorInfo("bad-element", roleName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: self.roles.append(role) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",nodeee.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
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 setParameters(self, operation, NSS=None): """ Set the source and target parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [ C.CANDIDATE, C.STARTUP, C.RUNNING, C.URL ]: self.source = node.tagName if node.tagName == C.URL: self.sourceTarget = string.strip( str(node.childNodes[0].nodeValue)) elif node.tagName == "config": self.source = node.tagName for cn in node.childNodes: if cn.nodeType == Node.ELEMENT_NODE: if cn.localName == "netconf" and cn.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.sourceInbound = implementation.createDocument( C.YENCAP_XMLNS, "netconf", None) clone = self.sourceInbound.importNode( cn, True) # self.config is a document which documentElement is "netconf" self.sourceInbound.replaceChild( clone, self.sourceInbound. documentElement) else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [ C.CANDIDATE, C.STARTUP, C.RUNNING, C.URL ]: self.target = node.tagName if node.tagName == C.URL: self.urlTarget = string.strip( str(node.childNodes[0].nodeValue)) else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
# Finding the nodes (related to the current module) in the received configuration try: ctx = Context(sourceDoc, processorNss=self.moduleManager.prefixes) nodeList = Evaluate(currentPath, ctx) except Exception, exp: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message=str(exp)) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return self.operationReply # 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
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
def setParameters(self, operation, NSS=None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in [ C.CANDIDATE, C.STARTUP, C.RUNNING ]: self.target = node.tagName if node.tagName == C.URL: self.urlTarget = string.strip( str(node.childNodes[0].nodeValue)) else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.CONFIG: for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.localName == "netconf" and node.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.config = implementation.createDocument( C.YENCAP_XMLNS, "netconf", None) clone = self.config.importNode(node, True) # self.config is a document which documentElement is "netconf" self.config.replaceChild( clone, self.config.documentElement) else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "The root node of the configuration must be netconf." ) moduleReply.addErrorInfo( "bad-element", node.localName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.DEFAULT_OPERATION: for node in child.childNodes: if node.nodeType == Node.TEXT_NODE: if node.nodeValue in [C.MERGE, C.REPLACE, C.NONE]: self.defaultOperation = node.nodeValue else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "An element value is not correct.") moduleReply.addErrorInfo( "bad-element", node.nodeValue) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.TEST_OPTION: for node in child.childNodes: if node.nodeType == Node.TEXT_NODE: if node.nodeValue in [C.TEST_THEN_SET, C.SET]: self.testOption = node.nodeValue else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message= "An element value is not correct.") moduleReply.addErrorInfo( "bad-element", node.nodeValue) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return elif child.tagName == C.ERROR_OPTION: for node in child.childNodes: if node.nodeType == Node.TEXT_NODE: if node.nodeValue in [ C.STOP_ON_ERROR, C.CONTINUE_ON_ERROR, C.ROLLBACK_ON_ERROR ]: self.errorOption = node.nodeValue else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.INVALID_VALUE, error_severity=ModuleReply.ERROR, error_message= "An element value is not correct.") moduleReply.addErrorInfo( "bad-element", node.nodeValue) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.checkNestingValidity(NSS)
def execute(self): """ Execute the Manage_modules operation. """ moduleReply = ModuleReply() operationType = self.operationItem["operation"] name = self.operationItem["name"] moduleManager = ModuleManager.getInstance() if operationType == "deploy": if moduleManager.getModuleFromName(name) != None: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "Module %s is loaded and therefore can not be deployed again." % name) else: # Decode the Foo_Module.zip fileContent = self.operationItem["file"] fileContent = fileContent.decode("string_escape") # Save it in tmp dir: f = open("/tmp/toto.zip", 'wb') f.write(fileContent) f.close() # Install it in YencaP modules directory: os.popen3("unzip /tmp/toto.zip " + '-d ' + C.YENCAP_HOME + '/Modules') #self.extract('/tmp/toto.zip', '/tmp/' + name + '_Module') # Update modules.xml moduleReader = ModuleReader.getInstance() moduleReader.addModule(name, self.operationItem["xpath"], self.operationItem["namespace"], self.operationItem["pref"], self.operationItem["cachelifetime"], self.operationItem["dictionnary"]) moduleReader.writeModules() # Update hello.xml: No. This is done automatically after loading the deployed module. See Server.py elif operationType == "undeploy": if moduleManager.getModuleFromName(name) != None: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "Module %s is loaded and therefore can not be undeployed. Unload it first." % name) else: # Remove the module from the file system modulepath = C.YENCAP_HOME + '/Modules/' + name + "_Module" # Remove the content of the module directory (os.rmdir works only if the dir is empty !!!) self.removeall(modulepath) # Remove the directory itself os.rmdir(modulepath) # Update modules.xml moduleReader = ModuleReader.getInstance() moduleReader.removeModule(name) moduleReader.writeModules() # Update hello.xml: No. This is done automatically. See Server.py elif operationType == "load": res = moduleManager.loadModule(name) if res == 0: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message="Module %s could not be loaded." % name) elif res == 2: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message="Module %s is already loaded." % name) elif res == 3: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "Module %s does not exist and therefore can not be loaded." % name) elif res == 1: moduleReply = ModuleReply() elif operationType == "unload": res = moduleManager.unloadModule(name) if res == 0: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.BAD_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "Module %s is not loaded and therefore could not be unloaded." % name) elif res == 1: moduleReply = ModuleReply() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply
def setParameters(self, operation, NSS = None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in [C.RUNNING, C.CANDIDATE, C.STARTUP]: self.source = node.tagName else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element value is not correct.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.FILTER: self.filter = child for att in self.filter.attributes.values(): if att.name == "type": if att.value == C.SUBTREE: self.filterType = C.SUBTREE elif att.value == C.XPATH: self.filterType = C.XPATH else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ATTRIBUTE, error_severity = ModuleReply.ERROR, error_message = "An attribute value is not correct") moduleReply.addErrorInfo("bad-attribute",att.name) moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ATTRIBUTE, error_severity = ModuleReply.ERROR, error_message = "An unexpected attribute is present") moduleReply.addErrorInfo("bad-attribute",att.name) moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return if self.filterType == C.XPATH: tmpval = self.filter.childNodes[0].nodeValue self.xpathrequest = string.strip(str(tmpval)) if self.xpathrequest == "": moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.INVALID_VALUE, error_severity = ModuleReply.ERROR, error_message = "The request specifies an unacceptable value for one or more parameters.") self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif self.filterType == C.SUBTREE: for node in self.filter.childNodes: if (node.nodeType==Node.ELEMENT_NODE): self.subtree = node else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return if self.filter == None: self.subtree = implementation.createDocument(C.YENCAP_XMLNS, 'netconf', None).documentElement
def setParameters(self, operation, NSS=None): """ Set the target parameter @type operation: Node @param operation: The operation node of the current Netconf request. """ for child in operation.childNodes: if child.nodeType == Node.ELEMENT_NODE: if child.tagName in [C.ACTIVATE, C.DEACTIVATE]: self.action = child.tagName for node in child.childNodes: if node.nodeType == Node.ELEMENT_NODE: if node.tagName in C.ROLES: for nodeee in node.childNodes: if nodeee.nodeType == Node.ELEMENT_NODE: if nodeee.tagName in C.ROLE: tmpval = nodeee.childNodes[ 0].nodeValue roleName = string.strip( str(tmpval)) role = rbacManager.getInstance( ).getRoleFromName(roleName) if role == None: moduleReply = ModuleReply( error_type=ModuleReply. PROTOCOL, error_tag=ModuleReply. UNKNOWN_ELEMENT, error_severity=ModuleReply. ERROR, error_message="Role " + roleName + " does not exist.") moduleReply.addErrorInfo( "bad-element", roleName) self.operationReply.setError() self.operationReply.setNode( moduleReply. getXMLNodeReply()) return else: self.roles.append(role) else: moduleReply = ModuleReply( error_type=ModuleReply. PROTOCOL, error_tag=ModuleReply. UNKNOWN_ELEMENT, error_severity=ModuleReply. ERROR, error_message= "An element is not known.") moduleReply.addErrorInfo( "bad-element", nodeee.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo( "bad-element", node.tagName) self.operationReply.setError() self.operationReply.setNode( moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="An element is not known.") moduleReply.addErrorInfo("bad-element", child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def setParameters(self, operation, NSS = None): """ Set the source and filter parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.FILTER: self.filter = child for att in self.filter.attributes.values(): if att.name == "type": if att.value == C.SUBTREE: self.filterType = C.SUBTREE elif att.value == C.XPATH: self.filterType = C.XPATH else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.BAD_ATTRIBUTE, error_severity = ModuleReply.ERROR, error_message = "An attribute value is not correct") moduleReply.addErrorInfo("bad-attribute",att.name) moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ATTRIBUTE, error_severity = ModuleReply.ERROR, error_message = "An unexpected attribute is present") moduleReply.addErrorInfo("bad-attribute",att.name) moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return if self.filterType == C.XPATH: tmpval = self.filter.childNodes[0].nodeValue self.xpathrequest = string.strip(str(tmpval)) elif self.filterType == C.SUBTREE: for node in self.filter.childNodes: if (node.nodeType==Node.ELEMENT_NODE): self.subtree = node else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return
def execute(self): """ Execute the validate operation. """ if self.operationReply.isError(): return self.operationReply # Execute validate if self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING]: moduleReply = None for module in self.moduleManager.getModules(): moduleReply = module.validate(self.target) if moduleReply.isError(): self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply self.operationReply.setNode(moduleReply.getXMLNodeReply()) return self.operationReply elif self.target == C.CONFIG: 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: # Get the configuration to validate. node = nodeList[0] # Send the operation to the module. moduleReply = module.validate(self.target, self.config) 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 # If somebody has the strange idea to try this: elif self.target == C.URL: xmlreply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.OPERATION_NOT_SUPPORTED, error_severity=ModuleReply.ERROR, error_message="OPERATION-NOT-SUPPORTED", ) self.operationReply.setNode(xmlReply.getXMLNodeReply()) return self.operationReply
def setParameters(self, operation, NSS = None): """ Set the source and target parameters @type operation: Node @param operation: The operation node of the current Netconf request. """ self.prefixes = NSS for child in operation.childNodes: if child.nodeType==Node.ELEMENT_NODE: if child.tagName == C.SOURCE: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP, C.RUNNING, C.URL]: self.source = node.tagName if node.tagName == C.URL: self.sourceTarget = string.strip(str(node.childNodes[0].nodeValue)) elif node.tagName == "config": self.source = node.tagName for cn in node.childNodes: if cn.nodeType==Node.ELEMENT_NODE: if cn.localName == "netconf" and cn.namespaceURI == C.YENCAP_XMLNS: # We build a new document containing the "filter" to compare more easily self.sourceInbound = implementation.createDocument(C.YENCAP_XMLNS, "netconf", None) clone = self.sourceInbound.importNode(cn, True) # self.config is a document which documentElement is "netconf" self.sourceInbound.replaceChild(clone, self.sourceInbound.documentElement) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return elif child.tagName == C.TARGET: for node in child.childNodes: if node.nodeType==Node.ELEMENT_NODE: if node.tagName in [C.CANDIDATE, C.STARTUP, C.RUNNING, C.URL]: self.target = node.tagName if node.tagName == C.URL: self.urlTarget = string.strip(str(node.childNodes[0].nodeValue)) else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",node.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return else: moduleReply = ModuleReply( error_type = ModuleReply.PROTOCOL, error_tag = ModuleReply.UNKNOWN_ELEMENT, error_severity = ModuleReply.ERROR, error_message = "An element is not known.") moduleReply.addErrorInfo("bad-element",child.tagName) self.operationReply.setError() self.operationReply.setNode(moduleReply.getXMLNodeReply()) return