blob: 59084ef529566c1232319e663b16690831f95e9c [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001
2#
Philip Joseph0f5e8c02017-03-03 01:54:51 +05303# Copyright 2016-2017 RIFT.IO Inc
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04004#
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
18import asyncio
19import rift.tasklets
20
21from gi.repository import(
22 RwCloudYang,
23 RwDts as rwdts,
Philip Josephf4937572017-03-03 01:55:37 +053024 RwTypes,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040025 )
26
27class CloudAccountNotFound(Exception):
28 pass
29
30
31class CloudAccountDtsOperdataHandler(object):
Philip Josephf4937572017-03-03 01:55:37 +053032 def __init__(self, dts, log, loop, project):
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040033 self._dts = dts
34 self._log = log
35 self._loop = loop
Philip Josephf4937572017-03-03 01:55:37 +053036 self._project = project
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040037
Philip Josephf4937572017-03-03 01:55:37 +053038 self._regh = None
39 self._rpc = None
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040040 self.cloud_accounts = {}
41
42 def add_cloud_account(self, account):
43 self.cloud_accounts[account.name] = account
44 account.start_validate_credentials(self._loop)
45
46 def delete_cloud_account(self, account_name):
47 del self.cloud_accounts[account_name]
48
49 def get_saved_cloud_accounts(self, cloud_account_name):
50 ''' Get Cloud Account corresponding to passed name, or all saved accounts if name is None'''
51 saved_cloud_accounts = []
52
53 if cloud_account_name is None or cloud_account_name == "":
54 cloud_accounts = list(self.cloud_accounts.values())
55 saved_cloud_accounts.extend(cloud_accounts)
56 elif cloud_account_name in self.cloud_accounts:
57 account = self.cloud_accounts[cloud_account_name]
58 saved_cloud_accounts.append(account)
59 else:
60 errstr = "Cloud account {} does not exist".format(cloud_account_name)
61 raise KeyError(errstr)
62
63 return saved_cloud_accounts
64
65 @asyncio.coroutine
66 def create_notification(self, account):
67 xpath = "N,/rw-cloud:cloud-notif"
68 ac_status = RwCloudYang.YangNotif_RwCloud_CloudNotif()
69 ac_status.name = account.name
70 ac_status.message = account.connection_status.details
71
72 yield from self._dts.query_create(xpath, rwdts.XactFlag.ADVISE, ac_status)
73 self._log.info("Notification called by creating dts query: %s", ac_status)
74
75
Philip Josephb0a38052017-03-31 01:25:29 +053076 @asyncio.coroutine
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040077 def _register_show_status(self):
78 def get_xpath(cloud_name=None):
Philip Josephf4937572017-03-03 01:55:37 +053079 return "D,/rw-cloud:cloud/account{}/connection-status".format(
80 "[name='%s']" % cloud_name if cloud_name is not None else ''
81 )
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040082
83 @asyncio.coroutine
84 def on_prepare(xact_info, action, ks_path, msg):
Philip Josephf4937572017-03-03 01:55:37 +053085 path_entry = RwCloudYang.CloudAcc.schema().keyspec_to_entry(ks_path)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040086 cloud_account_name = path_entry.key00.name
Philip Josephb0a38052017-03-31 01:25:29 +053087 self._log.debug("Got show cloud connection status request (action: %s): %s",
88 xact_info.query_action, ks_path.create_string())
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040089
90 try:
91 saved_accounts = self.get_saved_cloud_accounts(cloud_account_name)
92 for account in saved_accounts:
93 connection_status = account.connection_status
94 self._log.debug("Responding to cloud connection status request: %s", connection_status)
Philip Josephf4937572017-03-03 01:55:37 +053095 xpath = self._project.add_project(get_xpath(account.name))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040096 xact_info.respond_xpath(
97 rwdts.XactRspCode.MORE,
Philip Josephf4937572017-03-03 01:55:37 +053098 xpath=xpath,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040099 msg=account.connection_status,
100 )
101 except KeyError as e:
102 self._log.warning(str(e))
103 xact_info.respond_xpath(rwdts.XactRspCode.NA)
104 return
105
106 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
107
Philip Josephf4937572017-03-03 01:55:37 +0530108 xpath = self._project.add_project(get_xpath())
109 self._regh = yield from self._dts.register(
110 xpath=xpath,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400111 handler=rift.tasklets.DTS.RegistrationHandler(
112 on_prepare=on_prepare),
113 flags=rwdts.Flag.PUBLISHER,
114 )
115
Philip Josephb0a38052017-03-31 01:25:29 +0530116 @asyncio.coroutine
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400117 def _register_validate_rpc(self):
118 def get_xpath():
119 return "/rw-cloud:update-cloud-status"
120
121 @asyncio.coroutine
122 def on_prepare(xact_info, action, ks_path, msg):
123 if not msg.has_field("cloud_account"):
124 raise CloudAccountNotFound("Cloud account name not provided")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400125 cloud_account_name = msg.cloud_account
Philip Josephf4937572017-03-03 01:55:37 +0530126
127 if not self._project.rpc_check(msg, xact_info=xact_info):
128 return
129
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400130 try:
131 account = self.cloud_accounts[cloud_account_name]
132 except KeyError:
Philip Josephf4937572017-03-03 01:55:37 +0530133 errmsg = "Cloud account name {} not found in project {}". \
134 format(cloud_account_name, self._project.name)
135 xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
136 get_xpath(),
137 errmsg)
138 raise CloudAccountNotFound(errmsg)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400139
140 account.start_validate_credentials(self._loop)
141
142 yield from self.create_notification(account)
143
144 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
145
Philip Josephf4937572017-03-03 01:55:37 +0530146 self._rpc = yield from self._dts.register(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400147 xpath=get_xpath(),
148 handler=rift.tasklets.DTS.RegistrationHandler(
149 on_prepare=on_prepare
150 ),
151 flags=rwdts.Flag.PUBLISHER,
152 )
153
154 @asyncio.coroutine
155 def register(self):
Philip Josephb0a38052017-03-31 01:25:29 +0530156 self._log.debug("Register cloud account for project %s", self._project.name)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400157 yield from self._register_show_status()
158 yield from self._register_validate_rpc()
Philip Josephf4937572017-03-03 01:55:37 +0530159
160 def deregister(self):
Philip Josephb0a38052017-03-31 01:25:29 +0530161 self._log.debug("De-register cloud account for project %s", self._project.name)
162 self._rpc.deregister()
163 self._regh.deregister()