Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/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 | # |
| 19 | import structlog |
| 20 | from base.commit import Commit |
| 21 | from base.copy_config import CopyConfig |
| 22 | from base.delete_config import DeleteConfig |
| 23 | from base.discard_changes import DiscardChanges |
| 24 | from base.edit_config import EditConfig |
| 25 | from base.get import Get |
| 26 | from base.get_config import GetConfig |
| 27 | from base.lock import Lock |
| 28 | from base.unlock import UnLock |
| 29 | from base.close_session import CloseSession |
| 30 | from base.kill_session import KillSession |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 31 | from ext.get_voltha import GetVoltha |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 32 | from netconf import NSMAP, qmap |
| 33 | import netconf.nc_common.error as ncerror |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 34 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 35 | log = structlog.get_logger() |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 36 | from lxml import etree |
| 37 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 38 | ns_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 Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 43 | |
| 44 | class RpcFactory: |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 45 | instance = None |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 46 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 47 | 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 Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 55 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 56 | def _get_key(self, namespace, service, name): |
| 57 | return ''.join([namespace, service, name]) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 58 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 59 | 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 Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 92 | 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 Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 96 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 97 | 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 Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 101 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 102 | def get_rpc_handler(self, rpc_node, msg, grpc_channel, session): |
| 103 | try: |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 104 | # Parse the request into a dictionary |
| 105 | log.info("rpc-node", |
| 106 | node=etree.tostring(rpc_node, pretty_print=True)) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 107 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 108 | request = self.parse_xml_request(rpc_node) |
| 109 | if not request: |
| 110 | log.error("request-bad-format") |
| 111 | raise ncerror.BadMsg(rpc_node) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 112 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 113 | 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 Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 117 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 118 | 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 Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 128 | raise ncerror.BadMsg(rpc_node) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 129 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 130 | 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 Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 143 | |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 144 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 145 | def get_rpc_factory_instance(): |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 146 | if RpcFactory.instance == None: |
| 147 | RpcFactory.instance = RpcFactory() |
| 148 | return RpcFactory.instance |