This commit consists of:
1) Add session management to netconf
2) Modularize the rpc call
3) Improve the error handling
4) Small bug fixes

Change-Id: I023edb76e3743b633ac87be4967d656e09e2b970
diff --git a/netconf/nc_rpc/base/__init__.py b/netconf/nc_rpc/base/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/netconf/nc_rpc/base/__init__.py
diff --git a/netconf/nc_rpc/base/close_session.py b/netconf/nc_rpc/base/close_session.py
new file mode 100644
index 0000000..43babb8
--- /dev/null
+++ b/netconf/nc_rpc/base/close_session.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class CloseSession(Rpc):
+
+    def __init__(self, rpc_request, rpc_method, session):
+        super(CloseSession, self).__init__(rpc_request, rpc_method, session)
+        self._validate_parameters()
+
+    def execute(self):
+        log.info('close-session-request', session=self.session.session_id)
+        if self.rpc_response.is_error:
+            return self.rpc_response
+
+        self.rpc_response.node = etree.Element("ok")
+        return self.rpc_response
+
+
+    def _validate_parameters(self):
+        for child in self.rpc_method.getchildren():
+            # There cannot be parameters to a close session request
+            self.rpc_response.is_error = True
+            self.rpc_response.node = ncerror.BadMsg(self.rpc_request)
+            return
\ No newline at end of file
diff --git a/netconf/nc_rpc/base/commit.py b/netconf/nc_rpc/base/commit.py
new file mode 100644
index 0000000..61b7604
--- /dev/null
+++ b/netconf/nc_rpc/base/commit.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class Commit(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(Commit, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('commit-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/copy_config.py b/netconf/nc_rpc/base/copy_config.py
new file mode 100644
index 0000000..cf2fc82
--- /dev/null
+++ b/netconf/nc_rpc/base/copy_config.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class CopyConfig(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(CopyConfig, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('copy-config-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/delete_config.py b/netconf/nc_rpc/base/delete_config.py
new file mode 100644
index 0000000..7163ee6
--- /dev/null
+++ b/netconf/nc_rpc/base/delete_config.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class DeleteConfig(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(DeleteConfig, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('delete-config-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/discard_changes.py b/netconf/nc_rpc/base/discard_changes.py
new file mode 100644
index 0000000..c41d32e
--- /dev/null
+++ b/netconf/nc_rpc/base/discard_changes.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class DiscardChanges(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(DiscardChanges, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('discard-changes-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/edit_config.py b/netconf/nc_rpc/base/edit_config.py
new file mode 100644
index 0000000..5c7599a
--- /dev/null
+++ b/netconf/nc_rpc/base/edit_config.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class EditConfig(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(EditConfig, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('edit-config-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/get.py b/netconf/nc_rpc/base/get.py
new file mode 100644
index 0000000..c6cdfab
--- /dev/null
+++ b/netconf/nc_rpc/base/get.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+from netconf.constants import Constants as C
+from netconf.utils import filter_tag_match
+
+log = structlog.get_logger()
+
+class Get(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(Get, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('get-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
+		self.params = self.rpc_method.getchildren()
+		if len(self.params) > 1:
+			self.rpc_response.is_error = True
+			self.rpc_response.node = ncerror.BadMsg(self.rpc_request)
+			return
+
+		if self.params and not filter_tag_match(self.params[0], C.NC_FILTER):
+			self.rpc_response.is_error = True
+			self.rpc_response.node = ncerror.UnknownElement(
+				self.rpc_request, self.params[0])
+			return
+
+		if not self.params:
+			self.params = [None]
+
+
diff --git a/netconf/nc_rpc/base/get_config.py b/netconf/nc_rpc/base/get_config.py
new file mode 100644
index 0000000..dffe0d6
--- /dev/null
+++ b/netconf/nc_rpc/base/get_config.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+from netconf.constants import Constants as C
+from netconf.utils import elm
+from netconf import NSMAP
+
+log = structlog.get_logger()
+
+class GetConfig(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(GetConfig, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('get-config-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
+
+		self.params = self.rpc_method.getchildren()
+		paramslen = len(self.params)
+		# Verify that the source parameter is present
+		if paramslen > 2:
+			# TODO: need to specify all elements not known
+			self.rpc_response.is_error = True
+			self.rpc_response.node = ncerror.BadMsg(self.rpc_request)
+			return
+
+		self.source_param = self.rpc_method.find(C.NC_SOURCE, namespaces=NSMAP)
+		if self.source_param is None:
+			self.rpc_response.is_error = True
+			self.rpc_response.node = ncerror.MissingElement(
+				self.rpc_request, elm(C.NC_SOURCE))
+			return
+
+		self.filter_param = None
+		if paramslen == 2:
+			self.filter_param = self.rpc_method.find(C.NC_FILTER,
+												  namespaces=NSMAP)
+			if self.filter_param is None:
+				unknown_elm = self.params[0] if self.params[0] != \
+												self.source_param else \
+												self.params[1]
+				self.rpc_response.is_error = True
+				self.rpc_response.node = ncerror.UnknownElement(
+					self.rpc_request, unknown_elm)
+
+		self.params = [self.source_param, self.filter_param]
diff --git a/netconf/nc_rpc/base/kill_session.py b/netconf/nc_rpc/base/kill_session.py
new file mode 100644
index 0000000..08a2e7a
--- /dev/null
+++ b/netconf/nc_rpc/base/kill_session.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+
+class KillSession(Rpc):
+
+    def __init__(self, rpc_request, rpc_method, session):
+        super(KillSession, self).__init__(rpc_request, rpc_method, session)
+        self._validate_parameters()
+
+    def execute(self):
+        log.info('kill-session-request', session=self.session.session_id)
+        if self.rpc_response.error:
+            return self.rpc_response
+
+        self.rpc_response.node = etree.Element("ok")
+        return self.rpc_response
+
+    def _validate_parameters(self):
+        log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/lock.py b/netconf/nc_rpc/base/lock.py
new file mode 100644
index 0000000..fc74e83
--- /dev/null
+++ b/netconf/nc_rpc/base/lock.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class Lock(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(Lock, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('Lock-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/unlock.py b/netconf/nc_rpc/base/unlock.py
new file mode 100644
index 0000000..78c59f1
--- /dev/null
+++ b/netconf/nc_rpc/base/unlock.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class UnLock(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(UnLock, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('UnLock-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)
diff --git a/netconf/nc_rpc/base/validate.py b/netconf/nc_rpc/base/validate.py
new file mode 100644
index 0000000..1cb84af
--- /dev/null
+++ b/netconf/nc_rpc/base/validate.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from lxml import etree
+import structlog
+from netconf.nc_rpc.rpc import Rpc
+import netconf.nc_common.error as ncerror
+
+log = structlog.get_logger()
+
+class Validate(Rpc):
+
+	def __init__(self, rpc_request, rpc_method, session):
+		super(Validate, self).__init__(rpc_request, rpc_method, session)
+		self._validate_parameters()
+
+	def execute(self):
+		log.info('Validate-request', session=self.session.session_id)
+		if self.rpc_response.is_error:
+			return self.rpc_response
+
+	def _validate_parameters(self):
+		log.info('validate-parameters', session=self.session.session_id)