RIFT OSM R1 Initial Submission
[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.openmano.openmano_client as openmano_client
26 import rift.tasklets
27
28
29 class DataCenterPublisher(object):
30 """
31 This class is reponsible for exposing the data centers associated with an
32 openmano cloud account.
33 """
34
35 XPATH = "D,/rw-launchpad:datacenters"
36
37 def __init__(self, tasklet):
38 """Creates an instance of a DataCenterPublisher
39
40 Arguments:
41 tasklet - the tasklet that this publisher is registered for
42
43 """
44 self.tasklet = tasklet
45 self.reg = None
46
47 @property
48 def dts(self):
49 """The DTS instance used by this tasklet"""
50 return self.tasklet.dts
51
52 @property
53 def log(self):
54 """The logger used by this tasklet"""
55 return self.tasklet.log
56
57 @property
58 def loop(self):
59 """The event loop used by this tasklet"""
60 return self.tasklet.loop
61
62 @property
63 def accounts(self):
64 """The known openmano cloud accounts"""
65 accounts = list()
66 for acc in self.tasklet.cloud_accounts:
67 if acc.account_type == "openmano":
68 accounts.append(acc.account_msg)
69
70 return accounts
71
72 @asyncio.coroutine
73 def register(self):
74 """Registers the publisher with DTS"""
75
76 @asyncio.coroutine
77 def on_prepare(xact_info, action, ks_path, msg):
78 try:
79 # Create a datacenters instance to hold all of the cloud
80 # account data.
81 datacenters = RwLaunchpadYang.DataCenters()
82
83 # Iterate over the known openmano accounts and populate cloud
84 # account instances with the corresponding data center info
85 for account in self.accounts:
86 try:
87 cloud_account = RwLaunchpadYang.CloudAccount()
88 cloud_account.name = account.name
89
90 # Create a client for this cloud account to query for
91 # the associated data centers
92 client = openmano_client.OpenmanoCliAPI(
93 self.log,
94 account.openmano.host,
95 account.openmano.port,
96 account.openmano.tenant_id,
97 )
98
99 # Populate the cloud account with the data center info
100 for uuid, name in client.datacenter_list():
101 cloud_account.datacenters.append(
102 RwLaunchpadYang.DataCenter(
103 uuid=uuid,
104 name=name,
105 )
106 )
107
108 datacenters.cloud_accounts.append(cloud_account)
109
110 except Exception as e:
111 self.log.exception(e)
112
113 xact_info.respond_xpath(
114 RwDts.XactRspCode.MORE,
115 'D,/rw-launchpad:datacenters',
116 datacenters,
117 )
118
119 xact_info.respond_xpath(RwDts.XactRspCode.ACK)
120
121 except Exception as e:
122 self.log.exception(e)
123 raise
124
125 handler = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
126
127 with self.dts.group_create() as group:
128 self.reg = group.register(
129 xpath=DataCenterPublisher.XPATH,
130 handler=handler,
131 flags=RwDts.Flag.PUBLISHER,
132 )