Proper formatting of xml reply

Change-Id: Ibbbfae0835436b1cab0f25aa4fecdfa79afcdb55
diff --git a/netconf/nc_rpc/base/get.py b/netconf/nc_rpc/base/get.py
index 39e095c..05542dc 100644
--- a/netconf/nc_rpc/base/get.py
+++ b/netconf/nc_rpc/base/get.py
@@ -36,7 +36,11 @@
 			return self.rpc_response
 
 	def _validate_parameters(self):
-		log.info('validate-parameters', session=self.session.session_id)
+		log.info('validate-parameters',
+				 session=self.session.session_id,
+				 request=self.rpc_request,
+				 method=self.rpc_method
+				 )
 		self.params = self.rpc_method.getchildren()
 		if len(self.params) > 1:
 			self.rpc_response.is_error = True
diff --git a/netconf/nc_rpc/ext/get_voltha.py b/netconf/nc_rpc/ext/get_voltha.py
index a619875..8977b89 100644
--- a/netconf/nc_rpc/ext/get_voltha.py
+++ b/netconf/nc_rpc/ext/get_voltha.py
@@ -35,7 +35,8 @@
 
     @inlineCallbacks
     def execute(self):
-        log.info('get-voltha-request', session=self.session.session_id)
+        log.info('get-voltha-request', session=self.session.session_id,
+                 method=self.rpc_method)
         if self.rpc_response.is_error:
             returnValue(self.rpc_response)
 
@@ -46,7 +47,11 @@
         xml = dicttoxml.dicttoxml(res_dict)
         log.info('voltha-info', res=res_dict, xml=xml)
 
-        self.rpc_response.node = self.get_root_element(xml)
+        root_elem = self.get_root_element(xml)
+        root_elem.tag = 'data'
+
+        self.rpc_method.append(root_elem)
+        self.rpc_response.node = self.rpc_method
         self.rpc_response.is_error = False
 
         returnValue(self.rpc_response)
diff --git a/netconf/nc_rpc/rpc_factory.py b/netconf/nc_rpc/rpc_factory.py
index eb13b8d..72b4734 100644
--- a/netconf/nc_rpc/rpc_factory.py
+++ b/netconf/nc_rpc/rpc_factory.py
@@ -32,11 +32,34 @@
 from netconf import NSMAP, qmap
 import netconf.nc_common.error as ncerror
 log = structlog.get_logger()
+from lxml import etree
+
 
 class RpcFactory:
 
 	instance = None
 
+	def __init__(self):
+		self.rpc_map = {}
+		#TODO:  This will be loaded after the yang modules have been
+		# generated from proto files
+		self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}',
+						 'VolthaGlobalService', 'GetVoltha', GetVoltha)
+
+	def _get_key(self, namespace, service, name):
+		return ''.join([namespace,service,name])
+
+	def register_rpc(self, namespace, service, name, klass):
+		key = self._get_key(namespace, service, name)
+		if key not in self.rpc_map.keys():
+			self.rpc_map[key] = klass
+
+	def get_handler(self, namespace, service, name):
+		key = self._get_key(namespace, service, name)
+		if key in self.rpc_map.keys():
+			return self.rpc_map[key]
+
+
 	def get_rpc_handler(self, rpc_node, msg, grpc_channel, session):
 		try:
 			msg_id = rpc_node.get('message-id')
@@ -46,6 +69,8 @@
 			raise ncerror.SessionError(msg,
 									   "No valid message-id attribute found")
 
+		log.info("rpc-node", node=etree.tostring(rpc_node, pretty_print=True))
+
 		# Get the first child of rpc as the method name
 		rpc_method = rpc_node.getchildren()
 		if len(rpc_method) != 1:
@@ -54,11 +79,28 @@
 
 		rpc_method = rpc_method[0]
 
-		rpc_name = rpc_method.tag.replace(qmap('nc'), "")
+		if rpc_method.prefix is None:
+			log.error("rpc-method-has-no-prefix", msg_id=msg_id)
+			raise ncerror.BadMsg(rpc_node)
 
-		log.info("rpc-request", rpc=rpc_name)
+		try:
+			# extract the namespace, service and name
+			namespace = ''.join(['{', rpc_method.nsmap[rpc_method.prefix], '}'])
+			# rpc_name = rpc_method.tag.replace(qmap('nc'), "")
+			rpc = rpc_method.tag.replace(namespace, "").split('-')
+			rpc_service = rpc[0]
+			rpc_name = rpc[1]
+			log.info("rpc-request",
+					 namespace=namespace,
+					 service=rpc_service,
+					 name=rpc_name)
+		except Exception as e:
+			log.error("rpc-parsing-error", exception=repr(e))
+			raise ncerror.BadMsg(rpc_node)
 
-		class_handler = self.rpc_class_handlers.get(rpc_name, None)
+		class_handler = self.get_handler(namespace, rpc_service, rpc_name)
+
+		# class_handler = self.rpc_class_handlers.get(rpc_name, None)
 		if class_handler is not None:
 			return class_handler(rpc_node, rpc_method, grpc_channel, session)
 
@@ -80,6 +122,7 @@
 	}
 
 
+
 def get_rpc_factory_instance():
 	if RpcFactory.instance == None:
 		RpcFactory.instance = RpcFactory()
@@ -88,5 +131,9 @@
 
 if __name__ == '__main__':
    fac = get_rpc_factory_instance()
-   rpc = fac.rpc_class_handlers.get('getvoltha', None)
-   print rpc(None,None,None)
\ No newline at end of file
+   fac.register_rpc('urn:opencord:params:xml:ns:voltha:ietf-voltha',
+					'VolthaGlobalService', 'GetVoltha', GetVoltha)
+   rpc = fac.get_handler('urn:opencord:params:xml:ns:voltha:ietf-voltha',
+					'VolthaGlobalService', 'GetVoltha')
+   # rpc = fac.rpc_class_handlers.get('getvoltha', None)
+   print rpc(None,None,None, None)
\ No newline at end of file