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
):
39 """Creates an instance of a DataCenterPublisher
42 tasklet - the tasklet that this publisher is registered for
45 super().__init
__(log
, dts
, loop
)
47 self
._ro
_sub
= mano_dts
.ROAccountConfigSubscriber(
51 callback
=self
.on_ro_account_change
55 def on_ro_account_change(self
, ro_account
, action
):
56 if action
in [ RwDts
.QueryAction
.CREATE
, RwDts
.QueryAction
.UPDATE
]:
57 self
.ro_accounts
[ro_account
.name
] = ro_account
58 elif action
== RwDts
.QueryAction
.DELETE
and ro_account
.name
in self
.ro_accounts
:
59 del self
.ro_accounts
[ro_account
.name
]
63 """Registers the publisher with DTS"""
64 yield from self
._ro
_sub
.register()
67 def on_prepare(xact_info
, action
, ks_path
, msg
):
69 # Create a datacenters instance to hold all of the cloud
71 datacenters
= RwLaunchpadYang
.DataCenters()
73 # Iterate over the known openmano accounts and populate cloud
74 # account instances with the corresponding data center info
75 for _
, account
in self
.ro_accounts
.items():
76 if account
.account_type
!= "openmano":
80 ro_account
= RwLaunchpadYang
.ROAccount()
81 ro_account
.name
= account
.name
83 # Create a client for this cloud account to query for
84 # the associated data centers
85 client
= openmano_client
.OpenmanoCliAPI(
87 account
.openmano
.host
,
88 account
.openmano
.port
,
89 account
.openmano
.tenant_id
,
92 # Populate the cloud account with the data center info
93 for uuid
, name
in client
.datacenter_list():
94 ro_account
.datacenters
.append(
95 RwLaunchpadYang
.DataCenter(
101 datacenters
.ro_accounts
.append(ro_account
)
103 except Exception as e
:
104 self
.log
.exception(e
)
106 xact_info
.respond_xpath(
107 RwDts
.XactRspCode
.MORE
,
108 'D,/rw-launchpad:datacenters',
112 xact_info
.respond_xpath(RwDts
.XactRspCode
.ACK
)
114 except Exception as e
:
115 self
.log
.exception(e
)
118 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
120 with self
.dts
.group_create() as group
:
121 self
.reg
= group
.register(
122 xpath
=DataCenterPublisher
.XPATH
,
124 flags
=RwDts
.Flag
.PUBLISHER
,