update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwresmgr / rift / tasklets / rwresmgrtasklet / rwresmgrtasklet.py
1
2 #
3 # Copyright 2016 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 sys
21
22 import gi
23 gi.require_version('RwDts', '1.0')
24 gi.require_version('RwYang', '1.0')
25 gi.require_version('RwResourceMgrYang', '1.0')
26 gi.require_version('RwLaunchpadYang', '1.0')
27 gi.require_version('RwcalYang', '1.0')
28 from gi.repository import (
29 RwDts as rwdts,
30 RwYang,
31 RwResourceMgrYang,
32 RwLaunchpadYang,
33 RwcalYang,
34 )
35
36 import rift.tasklets
37 from rift.mano.utils.project import (
38 ManoProject,
39 ProjectHandler,
40 )
41
42 from . import rwresmgr_core as Core
43 from . import rwresmgr_config as Config
44 from . import rwresmgr_events as Event
45
46
47 class ResourceManager(object):
48 def __init__(self, log, log_hdl, loop, dts, project):
49 self._log = log
50 self._log_hdl = log_hdl
51 self._loop = loop
52 self._dts = dts
53 self._project = project
54
55 self.config_handler = Config.ResourceMgrConfig(self._dts, self._log, self._log_hdl, self._loop, self)
56 self.event_handler = Event.ResourceMgrEvent(self._dts, self._log, self._loop, self)
57 self.core = Core.ResourceMgrCore(self._dts, self._log, self._log_hdl, self._loop, self)
58
59 @asyncio.coroutine
60 def register(self):
61 yield from self.config_handler.register()
62 yield from self.event_handler.register()
63
64 def deregister(self):
65 self.event_handler.deregister()
66 self.config_handler.deregister()
67
68 def add_cloud_account_config(self, account):
69 self._log.debug("Received Cloud-Account add config event for account: %s", account.name)
70 self.core.add_cloud_account(account)
71
72 def update_cloud_account_config(self, account):
73 self._log.debug("Received Cloud-Account update config event for account: %s", account.name)
74 self.core.update_cloud_account(account)
75
76 def delete_cloud_account_config(self, account_name, dry_run=False):
77 self._log.debug("Received Cloud-Account delete event for account (dry_run: %s): %s",
78 dry_run, account_name)
79 self.core.delete_cloud_account(account_name, dry_run)
80
81 def get_cloud_account_names(self):
82 cloud_account_names = self.core.get_cloud_account_names()
83 return cloud_account_names
84
85 def get_cloud_account_detail(self, account_name):
86 return self.core.get_cloud_account_detail(account_name)
87
88 def pool_add(self, cloud_account_name, pool):
89 self._log.debug("Received Pool add event for cloud account %s pool: %s",
90 cloud_account_name, pool.name)
91 self.core.add_resource_pool(cloud_account_name, pool)
92
93 def pool_modify(self, cloud_account_name, pool):
94 self._log.debug("Received Pool modify event for cloud account %s pool: %s",
95 cloud_account_name, pool.name)
96 self.core.modify_resource_pool(cloud_account_name, pool)
97
98 def pool_delete(self, cloud_account_name, pool_name):
99 self._log.debug("Received Pool delete event for cloud account %s pool: %s",
100 cloud_account_name, pool_name)
101 self.core.delete_resource_pool(cloud_account_name, pool_name)
102
103 def get_pool_list(self, cloud_account_name):
104 return self.core.get_resource_pool_list(cloud_account_name)
105
106 def get_pool_info(self, cloud_account_name, pool_name):
107 self._log.debug("Received get-pool-info event for cloud account %s pool: %s",
108 cloud_account_name, pool_name)
109 return self.core.get_resource_pool_info(cloud_account_name, pool_name)
110
111 def lock_pool(self, cloud_account_name, pool_name):
112 self._log.debug("Received pool unlock event for pool: %s",
113 cloud_account_name, pool_name)
114 self.core.lock_resource_pool(cloud_account_name, pool_name)
115
116 def unlock_pool(self, cloud_account_name, pool_name):
117 self._log.debug("Received pool unlock event for pool: %s",
118 cloud_account_name, pool_name)
119 self.core.unlock_resource_pool(cloud_account_name, pool_name)
120
121 @asyncio.coroutine
122 def allocate_virtual_network(self, event_id, cloud_account_name, request):
123 self._log.info("Received network resource allocation request with event-id: %s", event_id)
124 resource = yield from self.core.allocate_virtual_resource(event_id, cloud_account_name, request, 'network')
125 return resource
126
127 @asyncio.coroutine
128 def reallocate_virtual_network(self, event_id, cloud_account_name, request, resource):
129 self._log.info("Received network resource reallocation request with event-id: %s", event_id)
130 resource = yield from self.core.reallocate_virtual_resource(event_id, cloud_account_name, request, 'network', resource)
131 return resource
132
133 @asyncio.coroutine
134 def release_virtual_network(self, event_id):
135 self._log.info("Received network resource release request with event-id: %s", event_id)
136 yield from self.core.release_virtual_resource(event_id, 'network')
137
138 @asyncio.coroutine
139 def read_virtual_network_info(self, event_id):
140 self._log.info("Received network resource read request with event-id: %s", event_id)
141 info = yield from self.core.read_virtual_resource(event_id, 'network')
142 return info
143
144 @asyncio.coroutine
145 def allocate_virtual_compute(self, event_id, cloud_account_name, request):
146 self._log.info("Received compute resource allocation request "
147 "(cloud account: %s) with event-id: %s",
148 cloud_account_name, event_id)
149 resource = yield from self.core.allocate_virtual_resource(
150 event_id, cloud_account_name, request, 'compute',
151 )
152 return resource
153
154 @asyncio.coroutine
155 def reallocate_virtual_compute(self, event_id, cloud_account_name, request, resource):
156 self._log.info("Received compute resource reallocation request "
157 "(cloud account: %s) with event-id: %s",
158 cloud_account_name, event_id)
159 resource = yield from self.core.reallocate_virtual_resource(
160 event_id, cloud_account_name, request, 'compute', resource,
161 )
162 return resource
163
164 @asyncio.coroutine
165 def release_virtual_compute(self, event_id):
166 self._log.info("Received compute resource release request with event-id: %s", event_id)
167 yield from self.core.release_virtual_resource(event_id, 'compute')
168
169 @asyncio.coroutine
170 def read_virtual_compute_info(self, event_id):
171 self._log.info("Received compute resource read request with event-id: %s", event_id)
172 info = yield from self.core.read_virtual_resource(event_id, 'compute')
173 return info
174
175
176 class ResMgrProject(ManoProject):
177
178 def __init__(self, name, tasklet, **kw):
179 super(ResMgrProject, self).__init__(tasklet.log, name)
180 self.update(tasklet)
181
182 self._resource_manager = None
183
184 @asyncio.coroutine
185 def register (self):
186 self._log.debug("Initializing the Resource Manager tasklet for project {}".
187 format(self.name))
188 self._resource_manager = ResourceManager(self._log,
189 self._log_hdl,
190 self._loop,
191 self._dts,
192 self,)
193 yield from self._resource_manager.register()
194
195 def deregister(self):
196 self._log.debug("De-registering project {}".format(self.name))
197 self._resource_manager.deregister()
198
199
200 class ResMgrTasklet(rift.tasklets.Tasklet):
201 def __init__(self, *args, **kwargs):
202 super(ResMgrTasklet, self).__init__(*args, **kwargs)
203 self.rwlog.set_category("rw-resource-mgr-log")
204 self._dts = None
205 self._project_handler = None
206 self.projects = {}
207
208 @property
209 def dts(self):
210 return self._dts
211
212 def start(self):
213 super(ResMgrTasklet, self).start()
214 self.log.debug("Starting ResMgrTasklet")
215
216 self.log.debug("Registering with dts")
217
218 self._dts = rift.tasklets.DTS(self.tasklet_info,
219 RwResourceMgrYang.get_schema(),
220 self.loop,
221 self.on_dts_state_change)
222
223 self.log.debug("Created DTS Api GI Object: %s", self._dts)
224
225 def stop(self):
226 try:
227 self._dts.deinit()
228 except Exception:
229 print("Caught Exception in RESMGR stop:", sys.exc_info()[0])
230 raise
231
232 def on_instance_started(self):
233 self.log.debug("Got instance started callback")
234
235 @asyncio.coroutine
236 def init(self):
237 self.log.debug("creating project handler")
238 self.project_handler = ProjectHandler(self, ResMgrProject)
239 self.project_handler.register()
240
241 @asyncio.coroutine
242 def run(self):
243 pass
244
245 @asyncio.coroutine
246 def on_dts_state_change(self, state):
247 """Take action according to current dts state to transition
248 application into the corresponding application state
249
250 Arguments
251 state - current dts state
252 """
253 switch = {
254 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
255 rwdts.State.CONFIG: rwdts.State.RUN,
256 }
257
258 handlers = {
259 rwdts.State.INIT: self.init,
260 rwdts.State.RUN: self.run,
261 }
262
263 # Transition application to next state
264 handler = handlers.get(state, None)
265 if handler is not None:
266 yield from handler()
267
268 # Transition dts to next state
269 next_state = switch.get(state, None)
270 if next_state is not None:
271 self._dts.handle.set_state(next_state)