Update to use rw-project definition from platform
[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 rift.tasklets
20
21 from gi.repository import(
22 RwCloudYang,
23 RwDts as rwdts,
24 RwTypes,
25 )
26
27 class CloudAccountNotFound(Exception):
28 pass
29
30
31 class CloudAccountDtsOperdataHandler(object):
32 def __init__(self, dts, log, loop, project):
33 self._dts = dts
34 self._log = log
35 self._loop = loop
36 self._project = project
37
38 self._regh = None
39 self._rpc = None
40 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
76 def _register_show_status(self):
77 def get_xpath(cloud_name=None):
78 return "D,/rw-cloud:cloud/account{}/connection-status".format(
79 "[name='%s']" % cloud_name if cloud_name is not None else ''
80 )
81
82 @asyncio.coroutine
83 def on_prepare(xact_info, action, ks_path, msg):
84 path_entry = RwCloudYang.CloudAcc.schema().keyspec_to_entry(ks_path)
85 cloud_account_name = path_entry.key00.name
86 self._log.debug("Got show cloud connection status request: %s", ks_path.create_string())
87
88 try:
89 saved_accounts = self.get_saved_cloud_accounts(cloud_account_name)
90 for account in saved_accounts:
91 connection_status = account.connection_status
92 self._log.debug("Responding to cloud connection status request: %s", connection_status)
93 xpath = self._project.add_project(get_xpath(account.name))
94 xact_info.respond_xpath(
95 rwdts.XactRspCode.MORE,
96 xpath=xpath,
97 msg=account.connection_status,
98 )
99 except KeyError as e:
100 self._log.warning(str(e))
101 xact_info.respond_xpath(rwdts.XactRspCode.NA)
102 return
103
104 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
105
106 xpath = self._project.add_project(get_xpath())
107 self._regh = yield from self._dts.register(
108 xpath=xpath,
109 handler=rift.tasklets.DTS.RegistrationHandler(
110 on_prepare=on_prepare),
111 flags=rwdts.Flag.PUBLISHER,
112 )
113
114 def _register_validate_rpc(self):
115 def get_xpath():
116 return "/rw-cloud:update-cloud-status"
117
118 @asyncio.coroutine
119 def on_prepare(xact_info, action, ks_path, msg):
120 if not msg.has_field("cloud_account"):
121 raise CloudAccountNotFound("Cloud account name not provided")
122 cloud_account_name = msg.cloud_account
123
124 if not self._project.rpc_check(msg, xact_info=xact_info):
125 return
126
127 try:
128 account = self.cloud_accounts[cloud_account_name]
129 except KeyError:
130 errmsg = "Cloud account name {} not found in project {}". \
131 format(cloud_account_name, self._project.name)
132 xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
133 get_xpath(),
134 errmsg)
135 raise CloudAccountNotFound(errmsg)
136
137 account.start_validate_credentials(self._loop)
138
139 yield from self.create_notification(account)
140
141 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
142
143 self._rpc = yield from self._dts.register(
144 xpath=get_xpath(),
145 handler=rift.tasklets.DTS.RegistrationHandler(
146 on_prepare=on_prepare
147 ),
148 flags=rwdts.Flag.PUBLISHER,
149 )
150
151 @asyncio.coroutine
152 def register(self):
153 yield from self._register_show_status()
154 yield from self._register_validate_rpc()
155
156 def deregister(self):
157 yield from self._rpc.deregister()
158 yield from self._regh.deregister()