Exemplo n.º 1
0
	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
Exemplo n.º 3
0
	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()
Exemplo n.º 4
0
	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 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 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)
Exemplo n.º 8
0
	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 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 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
Exemplo n.º 11
0
	def makeRequest(self, parentnode,structnode, followcommand):
		""" 
			Create the list of commands made for the issueNode into the parentnode 
			@type parentnode: cDomelette
			@param parentnode: where the request take place
			@type structnode: cDomelette
			@param structnode: the equivalent node of parentNode but in the XML Language Translation
			@type followcommand : Command
			@param followcommand: The command generated by the envelop that should be appended at the beginning of every	
								  command generated by makeRequest
			@rtype: boolean
			@return: if the request was acomplish (i.e. if it was a create request and the node allready exists it return false)
		"""
		requestmade = True
		
		requestNodes = self.requestNodes(parentnode, structnode)
		if (self.operationType == C.CREATE):
			if(requestNodes == []):
				nodecommands = self.makeNode(self.issueNode,structnode, followcommand,True,True,False)
				self.commands.appendCommands(nodecommands)
			else :
				requestmade = False
		elif (self.operationType == C.MERGE):
			if(requestNodes != []):
				nodecommands = self.makeNode(self.issueNode,structnode, followcommand,True,True,False)
				self.commands.appendCommands(nodecommands)
			else :
				requestmade = False
		elif (self.operationType == C.REPLACE):
			if(requestNodes == []):
				requestmade = False
			else:
				for requestnode in requestNodes:
					nodecommands = self.makeNode(requestnode,structnode, followcommand,False,False,True)
					self.commands.appendCommands(nodecommands)
				nodecommands = self.makeNode(self.issueNode,structnode, followcommand,True,True,False)
				self.commands.appendCommands(nodecommands)				
		elif (self.operationType == C.DELETE):
			if(requestNodes == []):
				requestmade = False
			else :
				for requestnode in requestNodes:
					nodecommands = self.makeNode(requestnode,structnode, followcommand,False,False,True)
					self.commands.appendCommands(nodecommands)
		else :
			reply = ModuleReply(
			error_type=ModuleReply.APPLICATION,
			error_tag=ModuleReply.BAD_ATTRIBUTE,
			error_severity=ModuleReply.ERROR,
			error_message="Attribute value unknown")
			reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,C.OPERATION)
			reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,self.operationType)
			raise XML_Interpreter_Exception(reply)
			
		return requestmade
Exemplo n.º 12
0
	def makeEnvelop(self, structnode, followcommand,nodes):
		""" 
			Create the list of commands for generated the envelop (i.e. generates the commands to get to the path for apply the actual request)
			@type structnode: cDomelette
			@param structnode: The current node of the XML Translation Language equivalent to the node where the request will be apply.
			@type followcommand : Command
			@param followcommand: The command that should be appended at the beginning of every command generated by it.
			@type nodes: [cDomelette]
			@param nodes: The ancestors nodes in the Device's XML Configuration of the node where the request will be aplied
			@rtype: (cDomelette,Command)
			@return: The first elemet of the tuple is the current offset of the XML Translation Language and the second if the 
					followcommand created by the envelop if any.
		"""
	
		for node in nodes:
			
			if (structnode != None):
				structnode = structnode.xpath(node.nodeName)
				if (structnode == []):
					structnode = None
				else:
					structnode = structnode[0]
				
			if (structnode != None and structnode.childNodes != []):
				commands = self.makeCommand(node,structnode,self.COMMAND_ITEM)
				if (commands != None):
					for subcommand in commands:
						subcommand.setHeading(followcommand)
						self.commands.addCommand(subcommand)
					
				commands = self.makeCommand(node,structnode,self.FOLLOWCOMMAND_ITEM)
				if (commands != None and commands != []):
					commands[0].setHeading(followcommand)
					followcommand =  commands[0]
						
				commands = self.makeCommand(node,structnode,self.EXITCOMMAND_ITEM,absolutepathdefault=True)
				if (commands != None):
					for subcommand in commands:
						subcommand.setHeading(followcommand)
						self.commands.addCommand(leavingcommand=subcommand)

				structnode = structnode.xpath("childs")
				if (structnode != []):
					structnode = structnode[0]
				else:
					structnode = None
			else:
				reply = ModuleReply(error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.BAD_ELEMENT, 
						error_severity=ModuleReply.ERROR, error_message="The element not belong to this protocol")
				reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,node.nodeName)
				raise XML_Interpreter_Exception(reply)

		return structnode, followcommand
    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
Exemplo n.º 14
0
    def getInvolvedNodes(self, defaultoperation, targetnode, editnode):
        """
			Look for the parent, in targetnode, of every node that match the editnode request ignoring the node with the 'operation' attribute.
			@type defaultoperation : Module.MERGE_OPERATION | Module.REPLACE_OPERATION | Module.NONE_OPERATION  
			@type targetnode: cDomelette
			@param targetnode : Where the editnode should be look for.
			@type editnode: cDomelette
			@param editnode: The editconfig node representing the request
			@rtype : [cDomelette]
			@return: the list of parent matching the editnode in the targetnode or None in case the matching were in the root node.
		"""

        operationNode = editnode.xpath("//*[@*[local-name() ='" + C.OPERATION +
                                       "' and namespace-uri()='" +
                                       C.NETCONF_XMLNS + "']]")
        if (len(operationNode) == 0):
            self.issueNode = editnode
            self.operationType = defaultoperation
            matchnodes = [None]
        elif (len(operationNode) > 1):
            reply = ModuleReply(
                error_type=ModuleReply.APLICATION,
                error_tag=ModuleReply.BAD_ATTRIBUTE,
                error_severity=ModuleReply.ERROR,
                error_message=
                "Exists more than one node with attribute 'operation'")
            reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO, C.OPERATION)
            for node in operationNode:
                reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO, node.nodeName)

            raise XML_Interpreter_Exception(reply)
        else:
            self.issueNode = operationNode[0]
            self.operationType = self.issueNode.getAttributeNS(
                C.NETCONF_XMLNS, C.OPERATION)

            xpath = ""
            parent = self.issueNode
            ignorechild = None
            while (parent != editnode):
                ignorechild = parent
                parent = parent.parentNode

                xpath = "/" + self.getXpathFromXMLNode(
                    parent, ignoreChilds=[ignorechild]) + xpath
            xpath = ".." + xpath

            matchnodes = targetnode.xpath(xpath, {"ns": self.namespaceURI})

        return matchnodes
    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
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
	def requestNodes(self,node, structnode):
		"""
			Using the key items from strucnode and the values of them in the editconfig request, it search for the nodes that 
			match them in the node paramiter. Also the if the acceptXPathTag is set to true it checks for the xpath attribute tags.
			@type node: cDomelette
			@param node: The node where the search will be apply
			@type strucnode: cDomelette
			@param strucnode: The current node from XML Translation Language equivalent to node.
			Return a list of nodes that are childs from 'node' and the key elements match with the ones from 'self.issueNode'
		"""
		
		requestNodes = None
		if (structnode != None):
			structnode = structnode.xpath(self.issueNode.nodeName)
		
			if (structnode == []):
				reply = ModuleReply(
				error_type=ModuleReply.APPLICATION,
				error_tag=ModuleReply.BAD_ELEMENT,
				error_severity=ModuleReply.ERROR,
				error_message="The element not belong to this protocol")
				reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,node.nodeName)
				raise XML_Interpreter_Exception(reply)
			else:
				structnode = structnode[0]
				if("true" == structnode.getAttributeNS(EMPTY_NAMESPACE,self.KEY_TAG)):
					reply = ModuleReply(
					error_type=ModuleReply.APPLICATION,
					error_tag=ModuleReply.BAD_ATTRIBUTE,
					error_severity=ModuleReply.ERROR,
					error_message="Attemp to do an operation over a key node")
					reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,self.issueNode.nodeName)
					reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,self.operationType)
					raise XML_Interpreter_Exception(reply)
		
			keynodes = structnode.xpath("childs/*[@"+self.KEY_TAG+"='true']")
			allowfields = []
			
			for keynode in keynodes:
				if self.namespaceURI == None :
					allowfields.extend(self.issueNode.xpath("*[local-name()='"+keynode.nodeName+"']"))
				else:
					allowfields.extend(self.issueNode.xpath("*[local-name()='"+keynode.nodeName+"' and namespace-uri()='"+self.namespaceURI+"']"))

				
			xpath =self.getXpathFromXMLNode(self.issueNode,allowChilds=allowfields)
			requestNodes = node.xpath(xpath,{"ns":self.namespaceURI})
			
		else:
			reply = ModuleReply(
			error_type=ModuleReply.APPLICATION,
			error_tag=ModuleReply.BAD_ELEMENT,
			error_severity=ModuleReply.ERROR,
			error_message="The element not belong to this protocol")
			reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,node.nodeName)
			raise XML_Interpreter_Exception(reply)

		return requestNodes
Exemplo n.º 18
0
	def getInvolvedNodes(self,defaultoperation,targetnode,editnode):
		"""
			Look for the parent, in targetnode, of every node that match the editnode request ignoring the node with the 'operation' attribute.
			@type defaultoperation : Module.MERGE_OPERATION | Module.REPLACE_OPERATION | Module.NONE_OPERATION  
			@type targetnode: cDomelette
			@param targetnode : Where the editnode should be look for.
			@type editnode: cDomelette
			@param editnode: The editconfig node representing the request
			@rtype : [cDomelette]
			@return: the list of parent matching the editnode in the targetnode or None in case the matching were in the root node.
		"""
		
		operationNode = editnode.xpath("//*[@*[local-name() ='"+C.OPERATION +"' and namespace-uri()='"+C.NETCONF_XMLNS+"']]")
		if (len(operationNode) == 0):
			self.issueNode = editnode
			self.operationType = defaultoperation
			matchnodes = [None] 
		elif (len(operationNode) > 1):
			reply = ModuleReply(
			error_type=ModuleReply.APLICATION,
			error_tag=ModuleReply.BAD_ATTRIBUTE,
			error_severity=ModuleReply.ERROR,
			error_message="Exists more than one node with attribute 'operation'")
			reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO, C.OPERATION)
			for node in operationNode:
				reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,node.nodeName)
			
			raise XML_Interpreter_Exception(reply) 
		else:
			self.issueNode = operationNode[0]
			self.operationType = self.issueNode.getAttributeNS(C.NETCONF_XMLNS, C.OPERATION)
			
			xpath = ""
			parent = self.issueNode
			ignorechild = None
			while (parent != editnode):
				ignorechild = parent
				parent = parent.parentNode
					
				xpath = "/" + self.getXpathFromXMLNode(parent,ignoreChilds=[ignorechild]) + xpath
			xpath =  ".." + xpath
			
			matchnodes = targetnode.xpath(xpath,{"ns":self.namespaceURI})
			
		return  matchnodes
Exemplo n.º 19
0
	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)
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
    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 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)
Exemplo n.º 24
0
    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 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
Exemplo n.º 27
0
	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.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
Exemplo n.º 29
0
	def makeNode(self,node, structnode, followcommand,stepping, creating,deleting):
		"""
			Creates the device's command for the node paramiters, the commands created will de could be to 
			delete, create or making the envelop for the node in the device.
			@type node: cDomelette
			@param node: Issue Node
			@type structnode: cDomelette
			@param structnode: It is the equivalent node of "node" but in the XML Translation Language
			@type followcommand: Command
			@param followcommand: It is the command that will be appended to the beginning of all the commands generated by
								  this function.
			@type stepping : boolean
			@param stepping: If set to true, it will just look for the <command> and <followcommand> nodes in the structnode, 
							 this is the case for generating the envelop.
			@type creating: boolean
			@param creating: If set to true, it will look for the <creatingcommand> nodes in the structnode, if any , 
							 otherwise will use the <command> node to generated the commands
			@type deleting: boolean
			@param deleting: If set to true, it will look for the <deletingcommand> nodes in the structnode, if any , 
							 otherwise will negate the <command> node and use it to generated the commands
		"""
		
		commandlist = CommandList()
		
		if (structnode != None):
			structnode = structnode.xpath(node.nodeName)
			if(structnode != []):
				structnode = structnode[0]
			else:
				structnode = None
		
		if (structnode != None):
			deletingvar = deleting 
			steppingvar = stepping
			creatingvar = creating 
			while (deletingvar or steppingvar or creatingvar):
				
				if (deletingvar):
					deletingvar = False
					followrecursion = False
					commandstr = self.DELETINGCOMMAND_ITEM
				elif (creatingvar):
					creatingvar = False
					followrecursion = False
					commandstr = self.CREATINGCOMMAND_ITEM
				elif (steppingvar):
					steppingvar = False
					followrecursion = True
					commandstr = self.COMMAND_ITEM
				
				commands = self.makeCommand(node,structnode,commandstr,recursiondefault=False)
				commandadd = False
				if (commands != None):
					commandadd = True
					for subcommand in commands:
						followrecursion = followrecursion or subcommand.getRecursion()
						subcommand.setHeading(followcommand)
						commandlist.addCommand(subcommand)
				commands = self.makeCommand(node,structnode,"exit"+commandstr,absolutepathdefault=True,recursiondefault=False)
				if (commands != None):
					commandadd = True
					for subcommand in commands:
						followrecursion = followrecursion or subcommand.getRecursion()
						subcommand.setHeading(followcommand) 
						commandlist.addCommand(leavingcommand=subcommand)
				
				if (not commandadd and not stepping):
					commands = self.makeCommand(node,structnode,self.COMMAND_ITEM)
					if (commands != None):
						if(deleting):
							tempfollowcommand = followcommand.clone()
							tempfollowcommand.neg()
						else:
							tempfollowcommand = followcommand
						for subcommand in commands:
							followrecursion = followrecursion or subcommand.getRecursion()
							subcommand.setHeading(tempfollowcommand) 
							commandlist.addCommand(subcommand)
					else:
						followrecursion = True
						
				if (followrecursion):
					tempfollowcommand = self.makeCommand(node,structnode,self.FOLLOWCOMMAND_ITEM)
					if (tempfollowcommand != None and tempfollowcommand != []):
						tempfollowcommand[0].setHeading(followcommand)
						tempfollowcommand = tempfollowcommand[0]
					else:
						tempfollowcommand = followcommand
					if (structnode.childNodes != []):
						structchild = structnode.xpath("childs")
						if (structchild != []):
							structchild = structchild[0]
						else :
							structchild = None
						for child in node.childNodes :
							if (child.nodeName != TEXT_NODE_NAME):
								commandlistchild = self.makeNode(child, structchild, tempfollowcommand, stepping, creating,deleting)
								commandlist.appendCommands(commandlistchild)
				
				commandlist.newList()
		else:
			reply = ModuleReply(error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.BAD_ELEMENT, 
						error_severity=ModuleReply.ERROR, error_message="The element not belong to this protocol")
			reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,node.nodeName)
			raise XML_Interpreter_Exception(reply)
		return commandlist
	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
Exemplo n.º 31
0
    def makeRequest(self, parentnode, structnode, followcommand):
        """ 
			Create the list of commands made for the issueNode into the parentnode 
			@type parentnode: cDomelette
			@param parentnode: where the request take place
			@type structnode: cDomelette
			@param structnode: the equivalent node of parentNode but in the XML Language Translation
			@type followcommand : Command
			@param followcommand: The command generated by the envelop that should be appended at the beginning of every	
								  command generated by makeRequest
			@rtype: boolean
			@return: if the request was acomplish (i.e. if it was a create request and the node allready exists it return false)
		"""
        requestmade = True

        requestNodes = self.requestNodes(parentnode, structnode)
        if (self.operationType == C.CREATE):
            if (requestNodes == []):
                nodecommands = self.makeNode(self.issueNode, structnode,
                                             followcommand, True, True, False)
                self.commands.appendCommands(nodecommands)
            else:
                requestmade = False
        elif (self.operationType == C.MERGE):
            if (requestNodes != []):
                nodecommands = self.makeNode(self.issueNode, structnode,
                                             followcommand, True, True, False)
                self.commands.appendCommands(nodecommands)
            else:
                requestmade = False
        elif (self.operationType == C.REPLACE):
            if (requestNodes == []):
                requestmade = False
            else:
                for requestnode in requestNodes:
                    nodecommands = self.makeNode(requestnode, structnode,
                                                 followcommand, False, False,
                                                 True)
                    self.commands.appendCommands(nodecommands)
                nodecommands = self.makeNode(self.issueNode, structnode,
                                             followcommand, True, True, False)
                self.commands.appendCommands(nodecommands)
        elif (self.operationType == C.DELETE):
            if (requestNodes == []):
                requestmade = False
            else:
                for requestnode in requestNodes:
                    nodecommands = self.makeNode(requestnode, structnode,
                                                 followcommand, False, False,
                                                 True)
                    self.commands.appendCommands(nodecommands)
        else:
            reply = ModuleReply(error_type=ModuleReply.APPLICATION,
                                error_tag=ModuleReply.BAD_ATTRIBUTE,
                                error_severity=ModuleReply.ERROR,
                                error_message="Attribute value unknown")
            reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO, C.OPERATION)
            reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,
                               self.operationType)
            raise XML_Interpreter_Exception(reply)

        return requestmade
Exemplo n.º 32
0
    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 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
	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)
Exemplo n.º 35
0
    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
Exemplo n.º 36
0
    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)
Exemplo n.º 37
0
	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
Exemplo n.º 38
0
    def makeEnvelop(self, structnode, followcommand, nodes):
        """ 
			Create the list of commands for generated the envelop (i.e. generates the commands to get to the path for apply the actual request)
			@type structnode: cDomelette
			@param structnode: The current node of the XML Translation Language equivalent to the node where the request will be apply.
			@type followcommand : Command
			@param followcommand: The command that should be appended at the beginning of every command generated by it.
			@type nodes: [cDomelette]
			@param nodes: The ancestors nodes in the Device's XML Configuration of the node where the request will be aplied
			@rtype: (cDomelette,Command)
			@return: The first elemet of the tuple is the current offset of the XML Translation Language and the second if the 
					followcommand created by the envelop if any.
		"""

        for node in nodes:

            if (structnode != None):
                structnode = structnode.xpath(node.nodeName)
                if (structnode == []):
                    structnode = None
                else:
                    structnode = structnode[0]

            if (structnode != None and structnode.childNodes != []):
                commands = self.makeCommand(node, structnode,
                                            self.COMMAND_ITEM)
                if (commands != None):
                    for subcommand in commands:
                        subcommand.setHeading(followcommand)
                        self.commands.addCommand(subcommand)

                commands = self.makeCommand(node, structnode,
                                            self.FOLLOWCOMMAND_ITEM)
                if (commands != None and commands != []):
                    commands[0].setHeading(followcommand)
                    followcommand = commands[0]

                commands = self.makeCommand(node,
                                            structnode,
                                            self.EXITCOMMAND_ITEM,
                                            absolutepathdefault=True)
                if (commands != None):
                    for subcommand in commands:
                        subcommand.setHeading(followcommand)
                        self.commands.addCommand(leavingcommand=subcommand)

                structnode = structnode.xpath("childs")
                if (structnode != []):
                    structnode = structnode[0]
                else:
                    structnode = None
            else:
                reply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.BAD_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message="The element not belong to this protocol")
                reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,
                                   node.nodeName)
                raise XML_Interpreter_Exception(reply)

        return structnode, followcommand
Exemplo n.º 39
0
    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 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 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
Exemplo n.º 42
0
    def makeNode(self, node, structnode, followcommand, stepping, creating,
                 deleting):
        """
			Creates the device's command for the node paramiters, the commands created will de could be to 
			delete, create or making the envelop for the node in the device.
			@type node: cDomelette
			@param node: Issue Node
			@type structnode: cDomelette
			@param structnode: It is the equivalent node of "node" but in the XML Translation Language
			@type followcommand: Command
			@param followcommand: It is the command that will be appended to the beginning of all the commands generated by
								  this function.
			@type stepping : boolean
			@param stepping: If set to true, it will just look for the <command> and <followcommand> nodes in the structnode, 
							 this is the case for generating the envelop.
			@type creating: boolean
			@param creating: If set to true, it will look for the <creatingcommand> nodes in the structnode, if any , 
							 otherwise will use the <command> node to generated the commands
			@type deleting: boolean
			@param deleting: If set to true, it will look for the <deletingcommand> nodes in the structnode, if any , 
							 otherwise will negate the <command> node and use it to generated the commands
		"""

        commandlist = CommandList()

        if (structnode != None):
            structnode = structnode.xpath(node.nodeName)
            if (structnode != []):
                structnode = structnode[0]
            else:
                structnode = None

        if (structnode != None):
            deletingvar = deleting
            steppingvar = stepping
            creatingvar = creating
            while (deletingvar or steppingvar or creatingvar):

                if (deletingvar):
                    deletingvar = False
                    followrecursion = False
                    commandstr = self.DELETINGCOMMAND_ITEM
                elif (creatingvar):
                    creatingvar = False
                    followrecursion = False
                    commandstr = self.CREATINGCOMMAND_ITEM
                elif (steppingvar):
                    steppingvar = False
                    followrecursion = True
                    commandstr = self.COMMAND_ITEM

                commands = self.makeCommand(node,
                                            structnode,
                                            commandstr,
                                            recursiondefault=False)
                commandadd = False
                if (commands != None):
                    commandadd = True
                    for subcommand in commands:
                        followrecursion = followrecursion or subcommand.getRecursion(
                        )
                        subcommand.setHeading(followcommand)
                        commandlist.addCommand(subcommand)
                commands = self.makeCommand(node,
                                            structnode,
                                            "exit" + commandstr,
                                            absolutepathdefault=True,
                                            recursiondefault=False)
                if (commands != None):
                    commandadd = True
                    for subcommand in commands:
                        followrecursion = followrecursion or subcommand.getRecursion(
                        )
                        subcommand.setHeading(followcommand)
                        commandlist.addCommand(leavingcommand=subcommand)

                if (not commandadd and not stepping):
                    commands = self.makeCommand(node, structnode,
                                                self.COMMAND_ITEM)
                    if (commands != None):
                        if (deleting):
                            tempfollowcommand = followcommand.clone()
                            tempfollowcommand.neg()
                        else:
                            tempfollowcommand = followcommand
                        for subcommand in commands:
                            followrecursion = followrecursion or subcommand.getRecursion(
                            )
                            subcommand.setHeading(tempfollowcommand)
                            commandlist.addCommand(subcommand)
                    else:
                        followrecursion = True

                if (followrecursion):
                    tempfollowcommand = self.makeCommand(
                        node, structnode, self.FOLLOWCOMMAND_ITEM)
                    if (tempfollowcommand != None and tempfollowcommand != []):
                        tempfollowcommand[0].setHeading(followcommand)
                        tempfollowcommand = tempfollowcommand[0]
                    else:
                        tempfollowcommand = followcommand
                    if (structnode.childNodes != []):
                        structchild = structnode.xpath("childs")
                        if (structchild != []):
                            structchild = structchild[0]
                        else:
                            structchild = None
                        for child in node.childNodes:
                            if (child.nodeName != TEXT_NODE_NAME):
                                commandlistchild = self.makeNode(
                                    child, structchild, tempfollowcommand,
                                    stepping, creating, deleting)
                                commandlist.appendCommands(commandlistchild)

                commandlist.newList()
        else:
            reply = ModuleReply(
                error_type=ModuleReply.APPLICATION,
                error_tag=ModuleReply.BAD_ELEMENT,
                error_severity=ModuleReply.ERROR,
                error_message="The element not belong to this protocol")
            reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO, node.nodeName)
            raise XML_Interpreter_Exception(reply)
        return commandlist
	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
Exemplo n.º 44
0
    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
Exemplo n.º 45
0
    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
Exemplo n.º 46
0
    def requestNodes(self, node, structnode):
        """
			Using the key items from strucnode and the values of them in the editconfig request, it search for the nodes that 
			match them in the node paramiter. Also the if the acceptXPathTag is set to true it checks for the xpath attribute tags.
			@type node: cDomelette
			@param node: The node where the search will be apply
			@type strucnode: cDomelette
			@param strucnode: The current node from XML Translation Language equivalent to node.
			Return a list of nodes that are childs from 'node' and the key elements match with the ones from 'self.issueNode'
		"""

        requestNodes = None
        if (structnode != None):
            structnode = structnode.xpath(self.issueNode.nodeName)

            if (structnode == []):
                reply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.BAD_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message="The element not belong to this protocol")
                reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO, node.nodeName)
                raise XML_Interpreter_Exception(reply)
            else:
                structnode = structnode[0]
                if ("true" == structnode.getAttributeNS(
                        EMPTY_NAMESPACE, self.KEY_TAG)):
                    reply = ModuleReply(
                        error_type=ModuleReply.APPLICATION,
                        error_tag=ModuleReply.BAD_ATTRIBUTE,
                        error_severity=ModuleReply.ERROR,
                        error_message=
                        "Attemp to do an operation over a key node")
                    reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO,
                                       self.issueNode.nodeName)
                    reply.addErrorInfo(ModuleReply.BAD_ATTRIBUTE_INFO,
                                       self.operationType)
                    raise XML_Interpreter_Exception(reply)

            keynodes = structnode.xpath("childs/*[@" + self.KEY_TAG +
                                        "='true']")
            allowfields = []

            for keynode in keynodes:
                if self.namespaceURI == None:
                    allowfields.extend(
                        self.issueNode.xpath("*[local-name()='" +
                                             keynode.nodeName + "']"))
                else:
                    allowfields.extend(
                        self.issueNode.xpath("*[local-name()='" +
                                             keynode.nodeName +
                                             "' and namespace-uri()='" +
                                             self.namespaceURI + "']"))

            xpath = self.getXpathFromXMLNode(self.issueNode,
                                             allowChilds=allowfields)
            requestNodes = node.xpath(xpath, {"ns": self.namespaceURI})

        else:
            reply = ModuleReply(
                error_type=ModuleReply.APPLICATION,
                error_tag=ModuleReply.BAD_ELEMENT,
                error_severity=ModuleReply.ERROR,
                error_message="The element not belong to this protocol")
            reply.addErrorInfo(ModuleReply.BAD_ELEMENT_INFO, node.nodeName)
            raise XML_Interpreter_Exception(reply)

        return requestNodes