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 (
38 from . import rwresmgr_core
as Core
39 from . import rwresmgr_config
as Config
40 from . import rwresmgr_events
as Event
43 class ResourceManager(object):
44 def __init__(self
, log
, log_hdl
, loop
, dts
):
46 self
._log
_hdl
= log_hdl
49 self
.config_handler
= Config
.ResourceMgrConfig(self
._dts
, self
._log
, self
._log
_hdl
, self
._loop
, self
)
50 self
.event_handler
= Event
.ResourceMgrEvent(self
._dts
, self
._log
, self
._loop
, self
)
51 self
.core
= Core
.ResourceMgrCore(self
._dts
, self
._log
, self
._log
_hdl
, self
._loop
, self
)
55 yield from self
.config_handler
.register()
56 yield from self
.event_handler
.register()
58 def add_cloud_account_config(self
, account
):
59 self
._log
.debug("Received Cloud-Account add config event for account: %s", account
.name
)
60 self
.core
.add_cloud_account(account
)
62 def update_cloud_account_config(self
, account
):
63 self
._log
.debug("Received Cloud-Account update config event for account: %s", account
.name
)
64 self
.core
.update_cloud_account(account
)
66 def delete_cloud_account_config(self
, account_name
, dry_run
=False):
67 self
._log
.debug("Received Cloud-Account delete event for account (dry_run: %s): %s",
68 dry_run
, account_name
)
69 self
.core
.delete_cloud_account(account_name
, dry_run
)
71 def get_cloud_account_names(self
):
72 cloud_account_names
= self
.core
.get_cloud_account_names()
73 return cloud_account_names
75 def pool_add(self
, cloud_account_name
, pool
):
76 self
._log
.debug("Received Pool add event for cloud account %s pool: %s",
77 cloud_account_name
, pool
.name
)
78 self
.core
.add_resource_pool(cloud_account_name
, pool
)
80 def pool_modify(self
, cloud_account_name
, pool
):
81 self
._log
.debug("Received Pool modify event for cloud account %s pool: %s",
82 cloud_account_name
, pool
.name
)
83 self
.core
.modify_resource_pool(cloud_account_name
, pool
)
85 def pool_delete(self
, cloud_account_name
, pool_name
):
86 self
._log
.debug("Received Pool delete event for cloud account %s pool: %s",
87 cloud_account_name
, pool_name
)
88 self
.core
.delete_resource_pool(cloud_account_name
, pool_name
)
90 def get_pool_list(self
, cloud_account_name
):
91 return self
.core
.get_resource_pool_list(cloud_account_name
)
93 def get_pool_info(self
, cloud_account_name
, pool_name
):
94 self
._log
.debug("Received get-pool-info event for cloud account %s pool: %s",
95 cloud_account_name
, pool_name
)
96 return self
.core
.get_resource_pool_info(cloud_account_name
, pool_name
)
98 def lock_pool(self
, cloud_account_name
, pool_name
):
99 self
._log
.debug("Received pool unlock event for pool: %s",
100 cloud_account_name
, pool_name
)
101 self
.core
.lock_resource_pool(cloud_account_name
, pool_name
)
103 def unlock_pool(self
, cloud_account_name
, pool_name
):
104 self
._log
.debug("Received pool unlock event for pool: %s",
105 cloud_account_name
, pool_name
)
106 self
.core
.unlock_resource_pool(cloud_account_name
, pool_name
)
109 def allocate_virtual_network(self
, event_id
, cloud_account_name
, request
):
110 self
._log
.info("Received network resource allocation request with event-id: %s", event_id
)
111 resource
= yield from self
.core
.allocate_virtual_resource(event_id
, cloud_account_name
, request
, 'network')
115 def reallocate_virtual_network(self
, event_id
, cloud_account_name
, request
, resource
):
116 self
._log
.info("Received network resource allocation request with event-id: %s", event_id
)
117 resource
= yield from self
.core
.reallocate_virtual_resource(event_id
, cloud_account_name
, request
, 'network', resource
)
121 def release_virtual_network(self
, event_id
):
122 self
._log
.info("Received network resource release request with event-id: %s", event_id
)
123 yield from self
.core
.release_virtual_resource(event_id
, 'network')
126 def read_virtual_network_info(self
, event_id
):
127 self
._log
.info("Received network resource read request with event-id: %s", event_id
)
128 info
= yield from self
.core
.read_virtual_resource(event_id
, 'network')
132 def allocate_virtual_compute(self
, event_id
, cloud_account_name
, request
):
133 self
._log
.info("Received compute resource allocation request "
134 "(cloud account: %s) with event-id: %s",
135 cloud_account_name
, event_id
)
136 resource
= yield from self
.core
.allocate_virtual_resource(
137 event_id
, cloud_account_name
, request
, 'compute',
142 def reallocate_virtual_compute(self
, event_id
, cloud_account_name
, request
, resource
):
143 self
._log
.info("Received compute resource allocation request "
144 "(cloud account: %s) with event-id: %s",
145 cloud_account_name
, event_id
)
146 resource
= yield from self
.core
.reallocate_virtual_resource(
147 event_id
, cloud_account_name
, request
, 'compute', resource
,
152 def release_virtual_compute(self
, event_id
):
153 self
._log
.info("Received compute resource release request with event-id: %s", event_id
)
154 yield from self
.core
.release_virtual_resource(event_id
, 'compute')
157 def read_virtual_compute_info(self
, event_id
):
158 self
._log
.info("Received compute resource read request with event-id: %s", event_id
)
159 info
= yield from self
.core
.read_virtual_resource(event_id
, 'compute')
163 class ResMgrTasklet(rift
.tasklets
.Tasklet
):
164 def __init__(self
, *args
, **kwargs
):
165 super(ResMgrTasklet
, self
).__init
__(*args
, **kwargs
)
166 self
.rwlog
.set_category("rw-resource-mgr-log")
168 self
._resource
_manager
= None
171 super(ResMgrTasklet
, self
).start()
172 self
.log
.info("Starting ResMgrTasklet")
174 self
.log
.debug("Registering with dts")
176 self
._dts
= rift
.tasklets
.DTS(self
.tasklet_info
,
177 RwResourceMgrYang
.get_schema(),
179 self
.on_dts_state_change
)
181 self
.log
.debug("Created DTS Api GI Object: %s", self
._dts
)
187 print("Caught Exception in RESMGR stop:", sys
.exc_info()[0])
190 def on_instance_started(self
):
191 self
.log
.debug("Got instance started callback")
195 self
._log
.info("Initializing the Resource Manager tasklet")
196 self
._resource
_manager
= ResourceManager(self
.log
,
200 yield from self
._resource
_manager
.register()
207 def on_dts_state_change(self
, state
):
208 """Take action according to current dts state to transition
209 application into the corresponding application state
212 state - current dts state
215 rwdts
.State
.INIT
: rwdts
.State
.REGN_COMPLETE
,
216 rwdts
.State
.CONFIG
: rwdts
.State
.RUN
,
220 rwdts
.State
.INIT
: self
.init
,
221 rwdts
.State
.RUN
: self
.run
,
224 # Transition application to next state
225 handler
= handlers
.get(state
, None)
226 if handler
is not None:
229 # Transition dts to next state
230 next_state
= switch
.get(state
, None)
231 if next_state
is not None:
232 self
._dts
.handle
.set_state(next_state
)