def filterResponse(self):
		# Creating the XML response filter
		moduleReply = None

		abstractFilter = None
		
		if self.filterType == C.SUBTREE:
			if self.subtree != None:
				abstractFilter = SubtreeFilter(self.operationReply.getNode(), self.subtree)
				moduleReply = abstractFilter.applyFilter()
			else:
				moduleReply = ModuleReply(replynode=self.operationReply.getNode())

		elif self.filterType == C.XPATH:
			abstractFilter = XpathFilter(self.operationReply.getNode(), [self.xpathrequest], self.prefixes)
			moduleReply = abstractFilter.applyFilter()

		if moduleReply.isError():
			self.operationReply.setError()
			self.operationReply.setNode(moduleReply.getXMLNodeReply())
			return
		else:
			self.operationReply.setNode(moduleReply.getXMLNodeReply())

		rm = rbacManager.getInstance()
		
		if rm.isActive():
			moduleReply = rm.filterNode(self.operationReply.getNode(), self.session)
			if moduleReply.isError():
				self.operationReply.setError()
				self.operationReply.setNode(moduleReply.getXMLNodeReply())
				return
			else:
				self.operationReply.setNode(moduleReply.getXMLNodeReply())
    def execute(self):
        """
			Execute the delete-config operation.
		"""

        if self.operationReply.isError():
            return self.operationReply

            # Check access here
        if self.rbacManager.isActive():
            operation_allowed = self.rbacManager.checkAccessDeleteConfig(self.session)

            if not operation_allowed:
                moduleReply = ModuleReply(
                    error_type=ModuleReply.PROTOCOL,
                    error_tag=ModuleReply.ACCESS_DENIED,
                    error_severity=ModuleReply.ERROR,
                    error_message="Access denied.",
                )
                self.operationReply.setError()
                self.operationReply.setNode(moduleReply.getXMLNodeReply())
                return self.operationReply

        if self.target in [C.CANDIDATE, C.STARTUP]:
            moduleReply = self.datastoreManager.removeConfig(self.target, "config")

        elif self.target == C.URL:
            # BUG : should delete the specified url (e.g. on a ftp server)
            moduleReply = ModuleReply(
                error_type=ModuleReply.APPLICATION,
                error_tag=ModuleReply.OPERATION_NOT_SUPPORTED,
                error_severity=ModuleReply.ERROR,
                error_message="Cannot delete the document at the specified url.",
            )

        if moduleReply.isError():
            self.operationReply.setError()

        self.operationReply.setNode(moduleReply.getXMLNodeReply())

        return self.operationReply
    def execute(self):
        """
			Execute the delete-config operation.
		"""

        if self.operationReply.isError():
            return self.operationReply

        # Check access here
        if self.rbacManager.isActive():
            operation_allowed = self.rbacManager.checkAccessDeleteConfig(
                self.session)

            if (not operation_allowed):
                moduleReply = ModuleReply(error_type=ModuleReply.PROTOCOL,
                                          error_tag=ModuleReply.ACCESS_DENIED,
                                          error_severity=ModuleReply.ERROR,
                                          error_message="Access denied.")
                self.operationReply.setError()
                self.operationReply.setNode(moduleReply.getXMLNodeReply())
                return self.operationReply

        if self.target in [C.CANDIDATE, C.STARTUP]:
            moduleReply = self.datastoreManager.removeConfig(
                self.target, "config")

        elif (self.target == C.URL):
            # BUG : should delete the specified url (e.g. on a ftp server)
            moduleReply = ModuleReply(
                error_type=ModuleReply.APPLICATION,
                error_tag=ModuleReply.OPERATION_NOT_SUPPORTED,
                error_severity=ModuleReply.ERROR,
                error_message="Cannot delete the document at the specified url."
            )

        if moduleReply.isError():
            self.operationReply.setError()

        self.operationReply.setNode(moduleReply.getXMLNodeReply())

        return self.operationReply
    def execute(self):
        """
			Execute the validate operation.
		"""
        if self.operationReply.isError():
            return self.operationReply

            # Execute validate
        if self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING]:
            moduleReply = None
            for module in self.moduleManager.getModules():

                moduleReply = module.validate(self.target)

                if moduleReply.isError():
                    self.operationReply.setNode(moduleReply.getXMLNodeReply())
                    return self.operationReply

            self.operationReply.setNode(moduleReply.getXMLNodeReply())
            return self.operationReply

        elif self.target == C.CONFIG:
            moduleReply = None
            for module in self.moduleManager.getModules():
                currentPath = module.path
                # Finding the nodes (related to the current module)
                # in the received configuration
                ctx = Context(self.config, processorNss=self.moduleManager.getPrefixes())
                nodeList = Evaluate(currentPath, ctx)

                # Distributing the nodes to the current module
                if len(nodeList) > 1:
                    moduleReply = ModuleReply(
                        error_type=ModuleReply.APPLICATION,
                        error_tag=ModuleReply.UNKNOWN_ELEMENT,
                        error_severity=ModuleReply.ERROR,
                        error_message="More than one node found in the received data for the same module" + module.name,
                    )
                    self.operationReply.setError()
                    self.operationReply.setNode(moduleReply.getXMLNodeReply())
                    return self.operationReply

                elif len(nodeList) == 1:
                    # Get the configuration to validate.
                    node = nodeList[0]

                    # Send the operation to the module.
                    moduleReply = module.validate(self.target, self.config)

                    if moduleReply.isError():
                        self.operationReply.setError()
                        self.operationReply.setNode(moduleReply.getXMLNodeReply())
                        return self.operationReply

            if moduleReply == None:
                moduleReply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.UNKNOWN_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message="Not any module matched",
                )
                self.operationReply.setError()

            self.operationReply.setNode(moduleReply.getXMLNodeReply())
            return self.operationReply

            # If somebody has the strange idea to try this:
        elif self.target == C.URL:
            xmlreply = ModuleReply(
                error_type=ModuleReply.APPLICATION,
                error_tag=ModuleReply.OPERATION_NOT_SUPPORTED,
                error_severity=ModuleReply.ERROR,
                error_message="OPERATION-NOT-SUPPORTED",
            )

            self.operationReply.setNode(xmlReply.getXMLNodeReply())
            return self.operationReply
                        self.operationReply.setNode(
                            moduleReply.getXMLNodeReply())
                        return self.operationReply
                    elif len(nodeList) == 1:
                        print "nodeList"
                        print nodeList[0]
                        sourceNode = nodeList[0]

                moduleReply = module.copyConfig(self.source,
                                                self.target,
                                                sourceNode=sourceNode)

                # Update the cache according to the new config.
                module.resetConfigTime(self.target)

                if moduleReply.isError():
                    self.operationReply.setError()

                self.operationReply.setNode(moduleReply.getXMLNodeReply())

            return self.operationReply

        elif self.target == C.URL:
            # WARNING : THIS HAS NEVER BEEN TESTED
            buf = cStringIO.StringIO()
            PrettyPrint(sourceDoc, stream=buf, encoding='UTF-8')
            # connect to host, default port
            ftp = FTP(self.urlTarget)
            # user anonymous, passwd anonymous@
            ftp.login()
            ftp.storbinary('STOR ' + self.urlTarget, buf.read)
示例#6
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
	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
						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:
						print "nodeList"
						print nodeList[0]
						sourceNode = nodeList[0]

				moduleReply = module.copyConfig(self.source, self.target, sourceNode = sourceNode)
				
				# Update the cache according to the new config.
				module.resetConfigTime(self.target)

				if moduleReply.isError():
					self.operationReply.setError()

				self.operationReply.setNode(moduleReply.getXMLNodeReply())

			return self.operationReply


		elif self.target == C.URL:
			# WARNING : THIS HAS NEVER BEEN TESTED
			buf = cStringIO.StringIO()
			PrettyPrint(sourceDoc, stream=buf, encoding='UTF-8')
			# connect to host, default port
			ftp = FTP(self.urlTarget)
			# user anonymous, passwd anonymous@
			ftp.login()
	def execute(self):
		"""
			Execute the validate operation.
		"""
		if self.operationReply.isError():
			return self.operationReply
		
		# Execute validate
		if (self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING]):
			moduleReply = None
			for module in self.moduleManager.getModules():
				
				moduleReply = module.validate(self.target)
				
    				if moduleReply.isError():
        				self.operationReply.setNode(moduleReply.getXMLNodeReply())
					return self.operationReply
			
			self.operationReply.setNode(moduleReply.getXMLNodeReply())
			return self.operationReply
				
		elif (self.target == C.CONFIG):
			moduleReply = None
			for module in self.moduleManager.getModules():
				currentPath = module.path
				# Finding the nodes (related to the current module)
				# in the received configuration
				ctx = Context(self.config, processorNss = self.moduleManager.getPrefixes())
				nodeList = Evaluate(currentPath, ctx)

				# Distributing the nodes to the current module
				if len(nodeList)>1:
					moduleReply = ModuleReply(
					error_type = ModuleReply.APPLICATION,
					error_tag = ModuleReply.UNKNOWN_ELEMENT,
					error_severity = ModuleReply.ERROR,
					error_message = "More than one node found in the received data for the same module" + module.name)
					self.operationReply.setError()
					self.operationReply.setNode(moduleReply.getXMLNodeReply())
					return self.operationReply

				elif len(nodeList)==1:
					#Get the configuration to validate.
					node = nodeList[0]
					
					# Send the operation to the module.
					moduleReply = module.validate(self.target, self.config)

					if (moduleReply.isError()):
						self.operationReply.setError()
						self.operationReply.setNode(moduleReply.getXMLNodeReply())
						return self.operationReply
	

			if (moduleReply == None):
				moduleReply = ModuleReply(
				error_type = ModuleReply.APPLICATION,
				error_tag = ModuleReply.UNKNOWN_ELEMENT,
				error_severity = ModuleReply.ERROR,
				error_message = "Not any module matched")
				self.operationReply.setError()

			self.operationReply.setNode(moduleReply.getXMLNodeReply())
			return self.operationReply

		#If somebody has the strange idea to try this:
		elif (self.target == C.URL):
			xmlreply = ModuleReply(
			error_type=ModuleReply.APPLICATION,
			error_tag=ModuleReply.OPERATION_NOT_SUPPORTED,
			error_severity=ModuleReply.ERROR,
			error_message="OPERATION-NOT-SUPPORTED")
			
			self.operationReply.setNode(xmlReply.getXMLNodeReply())
			return self.operationReply