update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / cloud / operdata.py
1
2 #
3 # Copyright 2016-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 gi
20 import rift.tasklets
21
22 from gi.repository import(
23 RwCloudYang,
24 RwDts as rwdts,
25 RwTypes,
26 )
27 gi.require_version('RwKeyspec', '1.0')
28 from gi.repository.RwKeyspec import quoted_key
29
30 class CloudAccountNotFound(Exception):
31 pass
32
33
34 class CloudAccountDtsOperdataHandler(object):
35 def __init__(self, dts, log, loop, project):
36 self._dts = dts
37 self._log = log
38 self._loop = loop
39 self._project = project
40
41 self._regh = None
42 self._rpc = None
43 self.cloud_accounts = {}
44
45 def add_cloud_account(self, account):
46 self.cloud_accounts[account.name] = account
47 asyncio.ensure_future(
48 account.start_validate_credentials(self._loop),
49 loop=self._loop
50 )
51
52 def delete_cloud_account(self, account_name):
53 del self.cloud_accounts[account_name]
54
55 def get_saved_cloud_accounts(self, cloud_account_name):
56 ''' Get Cloud Account corresponding to passed name, or all saved accounts if name is None'''
57 saved_cloud_accounts = []
58
59 if cloud_account_name is None or cloud_account_name == "":
60 cloud_accounts = list(self.cloud_accounts.values())
61 saved_cloud_accounts.extend(cloud_accounts)
62 elif cloud_account_name in self.cloud_accounts:
63 account = self.cloud_accounts[cloud_account_name]
64 saved_cloud_accounts.append(account)
65 else:
66 errstr = "Cloud account {} does not exist".format(cloud_account_name)
67 raise KeyError(errstr)
68
69 return saved_cloud_accounts
70
71 @asyncio.coroutine
72 def create_notification(self, account):
73 xpath = "N,/rw-cloud:cloud-notif"
74 ac_status = RwCloudYang.YangNotif_RwCloud_CloudNotif()
75 ac_status.name = account.name
76 ac_status.message = account.connection_status.details
77
78 yield from self._dts.query_create(xpath, rwdts.XactFlag.ADVISE, ac_status)
79 self._log.info("Notification called by creating dts query: %s", ac_status)
80
81
82 @asyncio.coroutine
83 def _register_show_status(self):
84 def get_xpath(cloud_name=None):
85 return "D,/rw-cloud:cloud/account{}/connection-status".format(
86 "[name=%s]" % quoted_key(cloud_name) if cloud_name is not None else ''
87 )
88
89 @asyncio.coroutine
90 def on_prepare(xact_info, action, ks_path, msg):
91 path_entry = RwCloudYang.YangData_RwProject_Project_Cloud_Account.schema().keyspec_to_entry(ks_path)
92 cloud_account_name = path_entry.key00.name
93
94 try:
95 saved_accounts = self.get_saved_cloud_accounts(cloud_account_name)
96 for account in saved_accounts:
97 connection_status = account.connection_status
98 xpath = self._project.add_project(get_xpath(account.name))
99 xact_info.respond_xpath(
100 rwdts.XactRspCode.MORE,
101 xpath=xpath,
102 msg=account.connection_status,
103 )
104 except KeyError as e:
105 self._log.warning(str(e))
106 xact_info.respond_xpath(rwdts.XactRspCode.NA)
107 return
108
109 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
110
111 xpath = self._project.add_project(get_xpath())
112 self._regh = yield from self._dts.register(
113 xpath=xpath,
114 handler=rift.tasklets.DTS.RegistrationHandler(
115 on_prepare=on_prepare),
116 flags=rwdts.Flag.PUBLISHER,
117 )
118
119 @asyncio.coroutine
120 def _register_validate_rpc(self):
121 def get_xpath():
122 return "/rw-cloud:update-cloud-status"
123
124 @asyncio.coroutine
125 def on_prepare(xact_info, action, ks_path, msg):
126 if not msg.has_field("cloud_account"):
127 raise CloudAccountNotFound("Cloud account name not provided")
128 cloud_account_name = msg.cloud_account
129
130 if not self._project.rpc_check(msg, xact_info=xact_info):
131 return
132
133 try:
134 account = self.cloud_accounts[cloud_account_name]
135 except KeyError:
136 errmsg = "Cloud account name {} not found in project {}". \
137 format(cloud_account_name, self._project.name)
138 xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
139 get_xpath(),
140 errmsg)
141 raise CloudAccountNotFound(errmsg)
142
143 yield from account.start_validate_credentials(self._loop)
144
145 yield from self.create_notification(account)
146
147 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
148
149 self._rpc = yield from self._dts.register(
150 xpath=get_xpath(),
151 handler=rift.tasklets.DTS.RegistrationHandler(
152 on_prepare=on_prepare
153 ),
154 flags=rwdts.Flag.PUBLISHER,
155 )
156
157 @asyncio.coroutine
158 def register(self):
159 self._log.debug("Register cloud account for project %s", self._project.name)
160 yield from self._register_show_status()
161 yield from self._register_validate_rpc()
162
163 def deregister(self):
164 self._log.debug("De-register cloud account for project %s", self._project.name)
165 self._rpc.deregister()
166 self._regh.deregister()