blob: 230c57a3f13b63fd12f060f9f0c3cd95d5e7a049 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/usr/bin/env python
2#
3# Copyright 2016 the original author or authors.
4#
5# Code adapted from https://github.com/choppsv1/netconf
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19import structlog
20from base.commit import Commit
21from base.copy_config import CopyConfig
22from base.delete_config import DeleteConfig
23from base.discard_changes import DiscardChanges
24from base.edit_config import EditConfig
25from base.get import Get
26from base.get_config import GetConfig
27from base.lock import Lock
28from base.unlock import UnLock
29from base.close_session import CloseSession
30from base.kill_session import KillSession
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050031from ext.get_voltha import GetVoltha
Khen Nursimulua7b842a2016-12-03 23:28:42 -050032from netconf import NSMAP, qmap
33import netconf.nc_common.error as ncerror
Khen Nursimulue0d53f82016-12-14 11:05:44 -080034
Khen Nursimulua7b842a2016-12-03 23:28:42 -050035log = structlog.get_logger()
Khen Nursimulu3b212942016-12-12 23:09:53 -050036from lxml import etree
37
Khen Nursimulua4972742016-12-23 17:15:20 -050038ns_map = {
39 'base': '{urn:ietf:params:xml:ns:netconf:base:1.0}',
40 'voltha': '{urn:opencord:params:xml:ns:voltha:ietf-voltha}'
41}
42
Khen Nursimulua7b842a2016-12-03 23:28:42 -050043
44class RpcFactory:
Khen Nursimulue0d53f82016-12-14 11:05:44 -080045 instance = None
Khen Nursimulua7b842a2016-12-03 23:28:42 -050046
Khen Nursimulue0d53f82016-12-14 11:05:44 -080047 def __init__(self):
48 self.rpc_map = {}
49 # TODO: This will be loaded after the yang modules have been
50 # generated from proto files
51 self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}',
52 'VolthaGlobalService', 'GetVoltha', GetVoltha)
53 self.register_rpc('{urn:opencord:params:xml:ns:voltha:ietf-voltha}',
54 'any', 'any', GetVoltha)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050055
Khen Nursimulue0d53f82016-12-14 11:05:44 -080056 def _get_key(self, namespace, service, name):
57 return ''.join([namespace, service, name])
Khen Nursimulu3b212942016-12-12 23:09:53 -050058
Khen Nursimulua4972742016-12-23 17:15:20 -050059 def get_attribute_value(self, name, attributes):
60 for tup in attributes.items():
61 if tup[0] == name:
62 return tup[1]
63
64 # Parse a request (node is an ElementTree) and return a dictionary
65 # TODO: This parser is specific to a GET request. Need to be it more
66 # generic
67 def parse_xml_request(self, node):
68 request = {}
69 if not len(node):
70 return request
71 for elem in node.iter():
72 if elem.tag.find(ns_map['base']) != -1: # found
73 elem_name = elem.tag.replace(ns_map['base'], "")
74 if elem_name == 'rpc':
75 request['type'] = 'rpc'
76 request['message_id'] = self.get_attribute_value(
77 'message-id', elem.attrib)
78 elif elem_name == 'filter':
79 request['filter'] = self.get_attribute_value('type',
80 elem.attrib)
81 else:
82 request[
83 'command'] = elem_name # attribute is empty for now
84 elif elem.tag.find(ns_map['voltha']) != -1: # found
85 if request.has_key('class'):
86 request['subclass'] = elem.tag.replace(ns_map['voltha'],
87 "")
88 else:
89 request['class'] = elem.tag.replace(ns_map['voltha'], "")
90 return request
91
Khen Nursimulue0d53f82016-12-14 11:05:44 -080092 def register_rpc(self, namespace, service, name, klass):
93 key = self._get_key(namespace, service, name)
94 if key not in self.rpc_map.keys():
95 self.rpc_map[key] = klass
Khen Nursimulu3b212942016-12-12 23:09:53 -050096
Khen Nursimulue0d53f82016-12-14 11:05:44 -080097 def get_handler(self, namespace, service, name):
98 key = self._get_key(namespace, service, name)
99 if key in self.rpc_map.keys():
100 return self.rpc_map[key]
Khen Nursimulu3b212942016-12-12 23:09:53 -0500101
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800102 def get_rpc_handler(self, rpc_node, msg, grpc_channel, session):
103 try:
Khen Nursimulua4972742016-12-23 17:15:20 -0500104 # Parse the request into a dictionary
105 log.info("rpc-node",
106 node=etree.tostring(rpc_node, pretty_print=True))
Khen Nursimulu3b212942016-12-12 23:09:53 -0500107
Khen Nursimulua4972742016-12-23 17:15:20 -0500108 request = self.parse_xml_request(rpc_node)
109 if not request:
110 log.error("request-bad-format")
111 raise ncerror.BadMsg(rpc_node)
Khen Nursimulu3b212942016-12-12 23:09:53 -0500112
Khen Nursimulua4972742016-12-23 17:15:20 -0500113 if not request.has_key('message_id') or \
114 not request.has_key('command'):
115 log.error("request-no-message-id")
116 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500117
Khen Nursimulua4972742016-12-23 17:15:20 -0500118 log.info("parsed-request", request=request)
119
120 class_handler = self.rpc_class_handlers.get(request['command'],
121 None)
122 if class_handler is not None:
123 return class_handler(request, grpc_channel, session)
124
125 log.error("rpc-not-implemented", rpc=request['command'])
126
127 except Exception as e:
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800128 raise ncerror.BadMsg(rpc_node)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500129
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800130 rpc_class_handlers = {
131 'getvoltha': GetVoltha,
132 'get-config': GetConfig,
133 'get': Get,
134 'edit-config': EditConfig,
135 'copy-config': CopyConfig,
136 'delete-config': DeleteConfig,
137 'commit': Commit,
138 'lock': Lock,
139 'unlock': UnLock,
140 'close-session': CloseSession,
141 'kill-session': KillSession
142 }
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500143
Khen Nursimulu3b212942016-12-12 23:09:53 -0500144
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500145def get_rpc_factory_instance():
Khen Nursimulue0d53f82016-12-14 11:05:44 -0800146 if RpcFactory.instance == None:
147 RpcFactory.instance = RpcFactory()
148 return RpcFactory.instance