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.
20 from gi
.repository
import (
25 import rift
.mano
.dts
as mano_dts
26 import rift
.openmano
.openmano_client
as openmano_client
30 class DataCenterPublisher(mano_dts
.DtsHandler
):
32 This class is reponsible for exposing the data centers associated with an
33 openmano cloud account.
36 XPATH
= "D,/rw-launchpad:datacenters"
38 def __init__(self
, log
, dts
, loop
, project
):
39 """Creates an instance of a DataCenterPublisher
42 tasklet - the tasklet that this publisher is registered for
45 super().__init
__(log
, dts
, loop
, project
)
47 self
._ro
_sub
= mano_dts
.ROAccountConfigSubscriber(
52 callback
=self
.on_ro_account_change
56 def on_ro_account_change(self
, ro_account
, action
):
57 if action
in [ RwDts
.QueryAction
.CREATE
, RwDts
.QueryAction
.UPDATE
]:
58 self
.ro_accounts
[ro_account
.name
] = ro_account
59 elif action
== RwDts
.QueryAction
.DELETE
and ro_account
.name
in self
.ro_accounts
:
60 del self
.ro_accounts
[ro_account
.name
]
63 self
._log
.debug("De-register datacenter handler for project {}".
64 format(self
.project
.name
))
69 self
._ro
_sub
.deregister()
73 """Registers the publisher with DTS"""
74 yield from self
._ro
_sub
.register()
77 def on_prepare(xact_info
, action
, ks_path
, msg
):
79 # Create a datacenters instance to hold all of the cloud
81 datacenters
= RwLaunchpadYang
.DataCenters()
83 # Iterate over the known openmano accounts and populate cloud
84 # account instances with the corresponding data center info
85 for _
, account
in self
.ro_accounts
.items():
86 if account
.account_type
!= "openmano":
90 ro_account
= RwLaunchpadYang
.ROAccount()
91 ro_account
.name
= account
.name
93 # Create a client for this cloud account to query for
94 # the associated data centers
95 client
= openmano_client
.OpenmanoCliAPI(
97 account
.openmano
.host
,
98 account
.openmano
.port
,
99 account
.openmano
.tenant_id
,
102 # Populate the cloud account with the data center info
103 for uuid
, name
in client
.datacenter_list():
104 ro_account
.datacenters
.append(
105 RwLaunchpadYang
.DataCenter(
111 datacenters
.ro_accounts
.append(ro_account
)
113 except Exception as e
:
114 self
.log
.exception(e
)
116 xact_info
.respond_xpath(
117 RwDts
.XactRspCode
.MORE
,
118 self
.project
.add_project(DataCenterPublisher
.XPATH
),
122 xact_info
.respond_xpath(RwDts
.XactRspCode
.ACK
)
124 except Exception as e
:
125 self
.log
.exception(e
)
128 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
130 with self
.dts
.group_create() as group
:
131 self
.reg
= group
.register(
132 xpath
=self
.project
.add_project(DataCenterPublisher
.XPATH
),
134 flags
=RwDts
.Flag
.PUBLISHER
,