Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 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 |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 20 | from netconf.constants import Constants as C |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 21 | from base.commit import Commit |
| 22 | from base.copy_config import CopyConfig |
| 23 | from base.delete_config import DeleteConfig |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 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 | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 31 | from ext.get_schemas import GetSchemas |
| 32 | from ext.get_schema import GetSchema |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 33 | from ext.get_voltha import GetVoltha |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 34 | import netconf.nc_common.error as ncerror |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 35 | from netconf.nc_common.utils import qmap, ns |
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 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 39 | log = structlog.get_logger() |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 40 | |
| 41 | class RpcFactory: |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 42 | instance = None |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 43 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 44 | def __init__(self): |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 45 | pass |
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 _get_key(self, namespace, service, name): |
| 48 | return ''.join([namespace, service, name]) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 49 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 50 | def get_attribute_value(self, name, attributes): |
| 51 | for tup in attributes.items(): |
| 52 | if tup[0] == name: |
| 53 | return tup[1] |
| 54 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 55 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 56 | # Parse a request (node is an ElementTree) and return a dictionary |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 57 | # TODO: This parser is specific for a GET/GET SCHEMAS request. Need to be |
| 58 | # it more generic |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 59 | def parse_xml_request(self, node): |
| 60 | request = {} |
| 61 | if not len(node): |
| 62 | return request |
| 63 | for elem in node.iter(): |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 64 | if elem.tag.find(qmap(C.NC)) != -1: # found |
| 65 | elem_name = elem.tag.replace(qmap(C.NC), "") |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 66 | if elem_name == 'rpc': |
| 67 | request['type'] = 'rpc' |
| 68 | request['message_id'] = self.get_attribute_value( |
| 69 | 'message-id', elem.attrib) |
| 70 | elif elem_name == 'filter': |
| 71 | request['filter'] = self.get_attribute_value('type', |
| 72 | elem.attrib) |
| 73 | else: |
Khen Nursimulu | 5b7b3fb | 2017-01-13 16:00:00 -0500 | [diff] [blame] | 74 | request['command'] = elem_name # attribute is empty for now |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 75 | elif elem.tag.find(qmap(C.VOLTHA)) != -1: # found |
| 76 | request['namespace'] = ns(C.VOLTHA) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 77 | if request.has_key('class'): |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 78 | request['subclass'] = elem.tag.replace(qmap(C.VOLTHA),"") |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 79 | else: |
Khen Nursimulu | 5b7b3fb | 2017-01-13 16:00:00 -0500 | [diff] [blame] | 80 | elem_name = elem.tag.replace(qmap(C.VOLTHA), "") |
| 81 | request['class'] = elem_name |
| 82 | if not request.has_key('command'): |
| 83 | request['command'] = elem_name |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 84 | elif elem.tag.find(qmap(C.NCM)) != -1: # found |
| 85 | request['namespace'] = ns(C.NCM) |
| 86 | elem_name = elem.tag.replace(qmap(C.NCM), "") |
| 87 | if elem_name == 'get-schema': |
| 88 | request['command'] = elem_name |
| 89 | request['class'] = elem_name |
| 90 | elif request.has_key('class'): |
| 91 | request['subclass'] = elem_name |
| 92 | elif elem_name == 'netconf-state': |
| 93 | request['command'] = 'get-schemas' |
| 94 | request['class'] = elem_name |
| 95 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 96 | return request |
| 97 | |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 98 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 99 | def get_rpc_handler(self, rpc_node, msg, grpc_channel, session, |
| 100 | capabilities): |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 101 | try: |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 102 | # Parse the request into a dictionary |
| 103 | log.info("rpc-node", |
| 104 | node=etree.tostring(rpc_node, pretty_print=True)) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 105 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 106 | request = self.parse_xml_request(rpc_node) |
| 107 | if not request: |
| 108 | log.error("request-bad-format") |
| 109 | raise ncerror.BadMsg(rpc_node) |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 110 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 111 | log.info("parsed-request", request=request) |
| 112 | |
| 113 | if not request.has_key('message_id'): |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 114 | log.error("request-no-message-id") |
| 115 | raise ncerror.BadMsg(rpc_node) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 116 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 117 | class_handler = self.rpc_class_handlers.get(request['command'], |
| 118 | None) |
| 119 | if class_handler is not None: |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 120 | return class_handler(request, rpc_node, grpc_channel, session, |
| 121 | capabilities) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 122 | |
| 123 | log.error("rpc-not-implemented", rpc=request['command']) |
| 124 | |
Khen Nursimulu | 5b7b3fb | 2017-01-13 16:00:00 -0500 | [diff] [blame] | 125 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 126 | except ncerror.BadMsg as err: |
| 127 | log.info('ncerror.BadMsg') |
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 | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 130 | except Exception as e: |
| 131 | raise ncerror.ServerException(rpc_node, exception=e) |
| 132 | |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 133 | rpc_class_handlers = { |
| 134 | 'getvoltha': GetVoltha, |
| 135 | 'get-config': GetConfig, |
| 136 | 'get': Get, |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 137 | 'get-schemas': GetSchemas, |
| 138 | 'get-schema': GetSchema, |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 139 | 'edit-config': EditConfig, |
| 140 | 'copy-config': CopyConfig, |
| 141 | 'delete-config': DeleteConfig, |
| 142 | 'commit': Commit, |
| 143 | 'lock': Lock, |
| 144 | 'unlock': UnLock, |
| 145 | 'close-session': CloseSession, |
| 146 | 'kill-session': KillSession |
| 147 | } |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 148 | |
Khen Nursimulu | 3b21294 | 2016-12-12 23:09:53 -0500 | [diff] [blame] | 149 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 150 | def get_rpc_factory_instance(): |
Khen Nursimulu | e0d53f8 | 2016-12-14 11:05:44 -0800 | [diff] [blame] | 151 | if RpcFactory.instance == None: |
| 152 | RpcFactory.instance = RpcFactory() |
| 153 | return RpcFactory.instance |