| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | |
| 2 | # |
| 3 | # Copyright 2016 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 | RwCloudYang, |
| 23 | RwDts as rwdts, |
| 24 | ) |
| 25 | |
| 26 | class CloudAccountNotFound(Exception): |
| 27 | pass |
| 28 | |
| 29 | |
| 30 | class CloudAccountDtsOperdataHandler(object): |
| 31 | def __init__(self, dts, log, loop): |
| 32 | self._dts = dts |
| 33 | self._log = log |
| 34 | self._loop = loop |
| 35 | |
| 36 | self.cloud_accounts = {} |
| 37 | |
| 38 | def add_cloud_account(self, account): |
| 39 | self.cloud_accounts[account.name] = account |
| 40 | account.start_validate_credentials(self._loop) |
| 41 | |
| 42 | def delete_cloud_account(self, account_name): |
| 43 | del self.cloud_accounts[account_name] |
| 44 | |
| 45 | def get_saved_cloud_accounts(self, cloud_account_name): |
| 46 | ''' Get Cloud Account corresponding to passed name, or all saved accounts if name is None''' |
| 47 | saved_cloud_accounts = [] |
| 48 | |
| 49 | if cloud_account_name is None or cloud_account_name == "": |
| 50 | cloud_accounts = list(self.cloud_accounts.values()) |
| 51 | saved_cloud_accounts.extend(cloud_accounts) |
| 52 | elif cloud_account_name in self.cloud_accounts: |
| 53 | account = self.cloud_accounts[cloud_account_name] |
| 54 | saved_cloud_accounts.append(account) |
| 55 | else: |
| 56 | errstr = "Cloud account {} does not exist".format(cloud_account_name) |
| 57 | raise KeyError(errstr) |
| 58 | |
| 59 | return saved_cloud_accounts |
| 60 | |
| 61 | @asyncio.coroutine |
| 62 | def create_notification(self, account): |
| 63 | xpath = "N,/rw-cloud:cloud-notif" |
| 64 | ac_status = RwCloudYang.YangNotif_RwCloud_CloudNotif() |
| 65 | ac_status.name = account.name |
| 66 | ac_status.message = account.connection_status.details |
| 67 | |
| 68 | yield from self._dts.query_create(xpath, rwdts.XactFlag.ADVISE, ac_status) |
| 69 | self._log.info("Notification called by creating dts query: %s", ac_status) |
| 70 | |
| 71 | |
| 72 | def _register_show_status(self): |
| 73 | def get_xpath(cloud_name=None): |
| 74 | return "D,/rw-cloud:cloud/account{}/connection-status".format( |
| 75 | "[name='%s']" % cloud_name if cloud_name is not None else '' |
| 76 | ) |
| 77 | |
| 78 | @asyncio.coroutine |
| 79 | def on_prepare(xact_info, action, ks_path, msg): |
| 80 | path_entry = RwCloudYang.CloudAccount.schema().keyspec_to_entry(ks_path) |
| 81 | cloud_account_name = path_entry.key00.name |
| 82 | self._log.debug("Got show cloud connection status request: %s", ks_path.create_string()) |
| 83 | |
| 84 | try: |
| 85 | saved_accounts = self.get_saved_cloud_accounts(cloud_account_name) |
| 86 | for account in saved_accounts: |
| 87 | connection_status = account.connection_status |
| 88 | self._log.debug("Responding to cloud connection status request: %s", connection_status) |
| 89 | xact_info.respond_xpath( |
| 90 | rwdts.XactRspCode.MORE, |
| 91 | xpath=get_xpath(account.name), |
| 92 | msg=account.connection_status, |
| 93 | ) |
| 94 | except KeyError as e: |
| 95 | self._log.warning(str(e)) |
| 96 | xact_info.respond_xpath(rwdts.XactRspCode.NA) |
| 97 | return |
| 98 | |
| 99 | xact_info.respond_xpath(rwdts.XactRspCode.ACK) |
| 100 | |
| 101 | yield from self._dts.register( |
| 102 | xpath=get_xpath(), |
| 103 | handler=rift.tasklets.DTS.RegistrationHandler( |
| 104 | on_prepare=on_prepare), |
| 105 | flags=rwdts.Flag.PUBLISHER, |
| 106 | ) |
| 107 | |
| 108 | def _register_validate_rpc(self): |
| 109 | def get_xpath(): |
| 110 | return "/rw-cloud:update-cloud-status" |
| 111 | |
| 112 | @asyncio.coroutine |
| 113 | def on_prepare(xact_info, action, ks_path, msg): |
| 114 | if not msg.has_field("cloud_account"): |
| 115 | raise CloudAccountNotFound("Cloud account name not provided") |
| 116 | |
| 117 | cloud_account_name = msg.cloud_account |
| 118 | try: |
| 119 | account = self.cloud_accounts[cloud_account_name] |
| 120 | except KeyError: |
| 121 | raise CloudAccountNotFound("Cloud account name %s not found" % cloud_account_name) |
| 122 | |
| 123 | account.start_validate_credentials(self._loop) |
| 124 | |
| 125 | yield from self.create_notification(account) |
| 126 | |
| 127 | xact_info.respond_xpath(rwdts.XactRspCode.ACK) |
| 128 | |
| 129 | yield from self._dts.register( |
| 130 | xpath=get_xpath(), |
| 131 | handler=rift.tasklets.DTS.RegistrationHandler( |
| 132 | on_prepare=on_prepare |
| 133 | ), |
| 134 | flags=rwdts.Flag.PUBLISHER, |
| 135 | ) |
| 136 | |
| 137 | @asyncio.coroutine |
| 138 | def register(self): |
| 139 | yield from self._register_show_status() |
| 140 | yield from self._register_validate_rpc() |