| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 1 | |
| 2 | # |
| 3 | # Copyright 2017 RIFT.IO Inc |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import asyncio |
| 19 | import rift.tasklets |
| 20 | |
| 21 | from gi.repository import( |
| 22 | RwSdnYang, |
| 23 | RwDts as rwdts, |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | class SDNAccountNotFound(Exception): |
| 28 | pass |
| 29 | |
| 30 | |
| 31 | class SDNAccountDtsOperdataHandler(object): |
| 32 | def __init__(self, dts, log, loop): |
| 33 | self._dts = dts |
| 34 | self._log = log |
| 35 | self._loop = loop |
| 36 | |
| 37 | self.sdn_accounts = {} |
| 38 | |
| 39 | def add_sdn_account(self, account): |
| 40 | self.sdn_accounts[account.name] = account |
| 41 | account.start_validate_credentials(self._loop) |
| 42 | |
| 43 | def delete_sdn_account(self, account_name): |
| 44 | del self.sdn_accounts[account_name] |
| 45 | |
| 46 | def get_saved_sdn_accounts(self, sdn_account_name): |
| 47 | ''' Get SDN Account corresponding to passed name, or all saved accounts if name is None''' |
| 48 | saved_sdn_accounts = [] |
| 49 | |
| 50 | if sdn_account_name is None or sdn_account_name == "": |
| 51 | sdn_accounts = list(self.sdn_accounts.values()) |
| 52 | saved_sdn_accounts.extend(sdn_accounts) |
| 53 | elif sdn_account_name in self.sdn_accounts: |
| 54 | account = self.sdn_accounts[sdn_account_name] |
| 55 | saved_sdn_accounts.append(account) |
| 56 | else: |
| 57 | errstr = "SDN account {} does not exist".format(sdn_account_name) |
| 58 | raise KeyError(errstr) |
| 59 | |
| 60 | return saved_sdn_accounts |
| 61 | |
| 62 | def _register_show_status(self): |
| 63 | def get_xpath(sdn_name=None): |
| 64 | return "D,/rw-sdn:sdn/account{}/connection-status".format( |
| 65 | "[name='%s']" % sdn_name if sdn_name is not None else '' |
| 66 | ) |
| 67 | |
| 68 | @asyncio.coroutine |
| 69 | def on_prepare(xact_info, action, ks_path, msg): |
| 70 | self._log.debug("Got show SDN connection status request: %s", ks_path.create_string()) |
| 71 | path_entry = RwSdnYang.SDNAccount.schema().keyspec_to_entry(ks_path) |
| 72 | sdn_account_name = path_entry.key00.name |
| 73 | |
| 74 | try: |
| 75 | saved_accounts = self.get_saved_sdn_accounts(sdn_account_name) |
| 76 | for account in saved_accounts: |
| 77 | connection_status = account.connection_status |
| 78 | self._log.debug("Responding to SDN connection status request: %s", connection_status) |
| 79 | xact_info.respond_xpath( |
| 80 | rwdts.XactRspCode.MORE, |
| 81 | xpath=get_xpath(account.name), |
| 82 | msg=account.connection_status, |
| 83 | ) |
| 84 | except KeyError as e: |
| 85 | self._log.warning(str(e)) |
| 86 | xact_info.respond_xpath(rwdts.XactRspCode.NA) |
| 87 | return |
| 88 | |
| 89 | xact_info.respond_xpath(rwdts.XactRspCode.ACK) |
| 90 | |
| 91 | yield from self._dts.register( |
| 92 | xpath=get_xpath(), |
| 93 | handler=rift.tasklets.DTS.RegistrationHandler( |
| 94 | on_prepare=on_prepare), |
| 95 | flags=rwdts.Flag.PUBLISHER, |
| 96 | ) |
| 97 | |
| 98 | def _register_validate_rpc(self): |
| 99 | def get_xpath(): |
| 100 | return "/rw-sdn:update-sdn-status" |
| 101 | |
| 102 | @asyncio.coroutine |
| 103 | def on_prepare(xact_info, action, ks_path, msg): |
| 104 | if not msg.has_field("sdn_account"): |
| 105 | raise SDNAccountNotFound("SDN account name not provided") |
| 106 | |
| 107 | sdn_account_name = msg.sdn_account |
| 108 | try: |
| 109 | account = self.sdn_accounts[sdn_account_name] |
| 110 | except KeyError: |
| 111 | raise SDNAccountNotFound("SDN account name %s not found" % sdn_account_name) |
| 112 | |
| 113 | account.start_validate_credentials(self._loop) |
| 114 | |
| 115 | xact_info.respond_xpath(rwdts.XactRspCode.ACK) |
| 116 | |
| 117 | yield from self._dts.register( |
| 118 | xpath=get_xpath(), |
| 119 | handler=rift.tasklets.DTS.RegistrationHandler( |
| 120 | on_prepare=on_prepare |
| 121 | ), |
| 122 | flags=rwdts.Flag.PUBLISHER, |
| 123 | ) |
| 124 | |
| 125 | @asyncio.coroutine |
| 126 | def register(self): |
| 127 | yield from self._register_show_status() |
| 128 | yield from self._register_validate_rpc() |