New feature: Code changes for project support
[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 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 def register_cloud_account_config(self):
75 def on_add_cloud_account_apply(account):
76 self._log.debug("Received on_add_cloud_account: %s", account)
77 self._parent.add_cloud_account_config(account)
78
79 def on_delete_cloud_account_apply(account_name):
80 self._log.debug("Received on_delete_cloud_account_apply: %s", account_name)
81 self._parent.delete_cloud_account_config(account_name)
82
83 @asyncio.coroutine
84 def on_delete_cloud_account_prepare(account_name):
85 self._log.debug("Received on_delete_cloud_account_prepare: %s", account_name)
86 self._parent.delete_cloud_account_config(account_name, dry_run=True)
87
88 cloud_callbacks = rift.mano.cloud.CloudAccountConfigCallbacks(
89 on_add_apply=on_add_cloud_account_apply,
90 on_delete_apply=on_delete_cloud_account_apply,
91 on_delete_prepare=on_delete_cloud_account_prepare,
92 )
93
94 self._cloud_sub = rift.mano.cloud.CloudAccountConfigSubscriber(
95 self._dts, self._log, self._rwlog_hdl,
96 self._project, cloud_callbacks
97 )
98 self._cloud_sub.register()
99
100 @asyncio.coroutine
101 def register_resource_pool_operational_data(self):
102 @asyncio.coroutine
103 def on_prepare(xact_info, action, ks_path, msg):
104 self._log.debug("ResourceMgr providing resource-pool information")
105 msg = RwResourceMgrYang.ResourcePoolRecords()
106
107 cloud_accounts = self._parent.get_cloud_account_names()
108 for cloud_account_name in cloud_accounts:
109 pools = self._parent.get_pool_list(cloud_account_name)
110 self._log.debug("Publishing information about cloud account %s %d resource pools",
111 cloud_account_name, len(pools))
112
113 cloud_account_msg = msg.cloud_account.add()
114 cloud_account_msg.name = cloud_account_name
115 for pool in pools:
116 pool_info = self._parent.get_pool_info(cloud_account_name, pool.name)
117 cloud_account_msg.records.append(pool_info)
118
119 xact_info.respond_xpath(rwdts.XactRspCode.ACK,
120 self._project.add_project(ResourceMgrConfig.XPATH_POOL_OPER_DATA),
121 msg=msg,)
122
123 xpath = self._project.add_project(ResourceMgrConfig.XPATH_POOL_OPER_DATA)
124 self._log.debug("Registering for Resource Mgr resource-pool-record using xpath: {}".
125 format(xpath))
126
127 handler=rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
128 self._res_sub = yield from self._dts.register(xpath=xpath,
129 handler=handler,
130 flags=rwdts.Flag.PUBLISHER)