05731a6153227a3863a1e6ebebe4bc27f3fe1547
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / datacenters.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
20 from gi.repository import (
21 RwDts,
22 RwLaunchpadYang,
23 )
24
25 import rift.mano.dts as mano_dts
26 import rift.openmano.openmano_client as openmano_client
27 import rift.tasklets
28
29
30 class DataCenterPublisher(mano_dts.DtsHandler):
31 """
32 This class is reponsible for exposing the data centers associated with an
33 openmano cloud account.
34 """
35
36 XPATH = "D,/rw-launchpad:datacenters"
37
38 def __init__(self, log, dts, loop):
39 """Creates an instance of a DataCenterPublisher
40
41 Arguments:
42 tasklet - the tasklet that this publisher is registered for
43
44 """
45 super().__init__(log, dts, loop)
46
47 self._ro_sub = mano_dts.ROAccountConfigSubscriber(
48 self.log,
49 self.dts,
50 self.loop,
51 callback=self.on_ro_account_change
52 )
53 self.ro_accounts = {}
54
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]
60
61 @asyncio.coroutine
62 def register(self):
63 """Registers the publisher with DTS"""
64 yield from self._ro_sub.register()
65
66 @asyncio.coroutine
67 def on_prepare(xact_info, action, ks_path, msg):
68 try:
69 # Create a datacenters instance to hold all of the cloud
70 # account data.
71 datacenters = RwLaunchpadYang.DataCenters()
72
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":
77 continue
78
79 try:
80 ro_account = RwLaunchpadYang.ROAccount()
81 ro_account.name = account.name
82
83 # Create a client for this cloud account to query for
84 # the associated data centers
85 client = openmano_client.OpenmanoCliAPI(
86 self.log,
87 account.openmano.host,
88 account.openmano.port,
89 account.openmano.tenant_id,
90 )
91
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(
96 uuid=uuid,
97 name=name,
98 )
99 )
100
101 datacenters.ro_accounts.append(ro_account)
102
103 except Exception as e:
104 self.log.exception(e)
105
106 xact_info.respond_xpath(
107 RwDts.XactRspCode.MORE,
108 'D,/rw-launchpad:datacenters',
109 datacenters,
110 )
111
112 xact_info.respond_xpath(RwDts.XactRspCode.ACK)
113
114 except Exception as e:
115 self.log.exception(e)
116 raise
117
118 handler = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
119
120 with self.dts.group_create() as group:
121 self.reg = group.register(
122 xpath=DataCenterPublisher.XPATH,
123 handler=handler,
124 flags=RwDts.Flag.PUBLISHER,
125 )