3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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 (
37 from rift
.mano
.utils
.project
import (
42 from . import rwresmgr_core
as Core
43 from . import rwresmgr_config
as Config
44 from . import rwresmgr_events
as Event
47 class ResourceManager(object):
48 def __init__(self
, log
, log_hdl
, loop
, dts
, project
):
50 self
._log
_hdl
= log_hdl
53 self
._project
= project
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
)
61 yield from self
.config_handler
.register()
62 yield from self
.event_handler
.register()
65 self
.event_handler
.deregister()
66 self
.config_handler
.deregister()
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
)
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
)
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
)
81 def get_cloud_account_names(self
):
82 cloud_account_names
= self
.core
.get_cloud_account_names()
83 return cloud_account_names
85 def get_cloud_account_detail(self
, account_name
):
86 return self
.core
.get_cloud_account_detail(account_name
)
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
)
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
)
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
)
103 def get_pool_list(self
, cloud_account_name
):
104 return self
.core
.get_resource_pool_list(cloud_account_name
)
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
)
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
)
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
)
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')
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
)
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')
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')
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',
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
,
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')
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')
176 class ResMgrProject(ManoProject
):
178 def __init__(self
, name
, tasklet
, **kw
):
179 super(ResMgrProject
, self
).__init
__(tasklet
.log
, name
)
182 self
._resource
_manager
= None
186 self
._log
.debug("Initializing the Resource Manager tasklet for project {}".
188 self
._resource
_manager
= ResourceManager(self
._log
,
193 yield from self
._resource
_manager
.register()
195 def deregister(self
):
196 self
._log
.debug("De-registering project {}".format(self
.name
))
197 self
._resource
_manager
.deregister()
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")
205 self
._project
_handler
= None
213 super(ResMgrTasklet
, self
).start()
214 self
.log
.debug("Starting ResMgrTasklet")
216 self
.log
.debug("Registering with dts")
218 self
._dts
= rift
.tasklets
.DTS(self
.tasklet_info
,
219 RwResourceMgrYang
.get_schema(),
221 self
.on_dts_state_change
)
223 self
.log
.debug("Created DTS Api GI Object: %s", self
._dts
)
229 print("Caught Exception in RESMGR stop:", sys
.exc_info()[0])
232 def on_instance_started(self
):
233 self
.log
.debug("Got instance started callback")
237 self
.log
.debug("creating project handler")
238 self
.project_handler
= ProjectHandler(self
, ResMgrProject
)
239 self
.project_handler
.register()
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
251 state - current dts state
254 rwdts
.State
.INIT
: rwdts
.State
.REGN_COMPLETE
,
255 rwdts
.State
.CONFIG
: rwdts
.State
.RUN
,
259 rwdts
.State
.INIT
: self
.init
,
260 rwdts
.State
.RUN
: self
.run
,
263 # Transition application to next state
264 handler
= handlers
.get(state
, None)
265 if handler
is not None:
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
)