update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwresmgr / rift / tasklets / rwresmgrtasklet / rwresmgr_config.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 logging
20 import time
21 import uuid
22 from enum import Enum
23
24 import gi
25 gi.require_version('RwDts', '1.0')
26 gi.require_version('RwYang', '1.0')
27 gi.require_version('RwResourceMgrYang', '1.0')
28 gi.require_version('RwLaunchpadYang', '1.0')
29 gi.require_version('RwcalYang', '1.0')
30
31 from gi.repository import (
32 RwDts as rwdts,
33 RwYang,
34 RwResourceMgrYang,
35 RwLaunchpadYang,
36 RwcalYang,
37 )
38
39 from gi.repository.RwTypes import RwStatus
40 import rift.tasklets
41 import rift.mano.cloud
42
43
44 class ResourceMgrConfig(object):
45 XPATH_POOL_OPER_DATA = "D,/rw-resource-mgr:resource-pool-records"
46 def __init__(self, dts, log, rwlog_hdl, loop, parent):
47 self._dts = dts
48 self._log = log
49 self._rwlog_hdl = rwlog_hdl
50 self._loop = loop
51 self._parent = parent
52
53 self._cloud_sub = None
54 self._res_sub = None
55 self._project = parent._project
56
57 @asyncio.coroutine
58 def register(self):
59 yield from self.register_resource_pool_operational_data()
60 yield from self.register_cloud_account_config()
61
62 def deregister(self):
63 self._log.debug("De-register for project {}".format(self._project.name))
64 if self._cloud_sub:
65 self._cloud_sub.deregister()
66 self._cloud_sub = None
67
68 if self._res_sub:
69 self._res_sub.delete_element(
70 self._project.add_project(ResourceMgrConfig.XPATH_POOL_OPER_DATA))
71 self._res_sub.deregister()
72 self._res_sub = None
73
74 @asyncio.coroutine
75 def register_cloud_account_config(self):
76 def on_add_cloud_account_apply(account):
77 self._log.debug("Received on_add_cloud_account: %s", account)
78 self._parent.add_cloud_account_config(account)
79
80 def on_delete_cloud_account_apply(account_name):
81 self._log.debug("Received on_delete_cloud_account_apply: %s", account_name)
82 self._parent.delete_cloud_account_config(account_name)
83
84 @asyncio.coroutine
85 def on_delete_cloud_account_prepare(account_name):
86 self._log.debug("Received on_delete_cloud_account_prepare: %s", account_name)
87 self._parent.delete_cloud_account_config(account_name, dry_run=True)
88
89 cloud_callbacks = rift.mano.cloud.CloudAccountConfigCallbacks(
90 on_add_apply=on_add_cloud_account_apply,
91 on_delete_apply=on_delete_cloud_account_apply,
92 on_delete_prepare=on_delete_cloud_account_prepare,
93 )
94
95 self._cloud_sub = rift.mano.cloud.CloudAccountConfigSubscriber(
96 self._dts, self._log, self._rwlog_hdl,
97 self._project, cloud_callbacks
98 )
99 yield from self._cloud_sub.register()
100
101 @asyncio.coroutine
102 def register_resource_pool_operational_data(self):
103 @asyncio.coroutine
104 def on_prepare(xact_info, action, ks_path, msg):
105 self._log.debug("ResourceMgr providing resource-pool information")
106 msg = RwResourceMgrYang.YangData_RwProject_Project_ResourcePoolRecords()
107
108 cloud_accounts = self._parent.get_cloud_account_names()
109 for cloud_account_name in cloud_accounts:
110 pools = self._parent.get_pool_list(cloud_account_name)
111 self._log.debug("Publishing information about cloud account %s %d resource pools",
112 cloud_account_name, len(pools))
113
114 cloud_account_msg = msg.cloud_account.add()
115 cloud_account_msg.name = cloud_account_name
116 for pool in pools:
117 pool_info = self._parent.get_pool_info(cloud_account_name, pool.name)
118 cloud_account_msg.records.append(pool_info)
119
120 xact_info.respond_xpath(rwdts.XactRspCode.ACK,
121 self._project.add_project(ResourceMgrConfig.XPATH_POOL_OPER_DATA),
122 msg=msg,)
123
124 xpath = self._project.add_project(ResourceMgrConfig.XPATH_POOL_OPER_DATA)
125 self._log.debug("Registering for Resource Mgr resource-pool-record using xpath: {}".
126 format(xpath))
127
128 handler=rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
129 self._res_sub = yield from self._dts.register(xpath=xpath,
130 handler=handler,
131 flags=rwdts.Flag.PUBLISHER)