3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from gi
.repository
import(
26 class CloudAccountNotFound(Exception):
30 class CloudAccountDtsOperdataHandler(object):
31 def __init__(self
, dts
, log
, loop
):
36 self
.cloud_accounts
= {}
38 def add_cloud_account(self
, account
):
39 self
.cloud_accounts
[account
.name
] = account
40 account
.start_validate_credentials(self
._loop
)
42 def delete_cloud_account(self
, account_name
):
43 del self
.cloud_accounts
[account_name
]
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
= []
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
)
56 errstr
= "Cloud account {} does not exist".format(cloud_account_name
)
57 raise KeyError(errstr
)
59 return saved_cloud_accounts
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
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
)
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 ''
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())
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
,
95 self
._log
.warning(str(e
))
96 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
99 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
101 yield from self
._dts
.register(
103 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
104 on_prepare
=on_prepare
),
105 flags
=rwdts
.Flag
.PUBLISHER
,
108 def _register_validate_rpc(self
):
110 return "/rw-cloud:update-cloud-status"
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")
117 cloud_account_name
= msg
.cloud_account
119 account
= self
.cloud_accounts
[cloud_account_name
]
121 raise CloudAccountNotFound("Cloud account name %s not found" % cloud_account_name
)
123 account
.start_validate_credentials(self
._loop
)
125 yield from self
.create_notification(account
)
127 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
129 yield from self
._dts
.register(
131 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
132 on_prepare
=on_prepare
134 flags
=rwdts
.Flag
.PUBLISHER
,
139 yield from self
._register
_show
_status
()
140 yield from self
._register
_validate
_rpc
()