74e5ea282b56c552480ebeeac50ad4824bbc8659
[osm/SO.git] / common / python / rift / mano / ro_account / accounts.py
1
2 #
3 # Copyright 2017 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 import asyncio
18
19 from gi.repository import (
20 RwDts as rwdts,
21 RwRoAccountYang,
22 )
23
24 import rift.mano.dts as mano_dts
25 import rift.tasklets
26
27 from rift.tasklets.rwnsmtasklet import openmano_nsm
28 from rift.tasklets.rwnsmtasklet import rwnsmplugin
29
30 class ROAccount(object):
31 """
32 RO Account Model class
33 """
34 DEFAULT_PLUGIN = rwnsmplugin.RwNsPlugin
35
36 def __init__(self, dts=None, log=None, loop=None, project=None, records_publisher=None, account_msg=None):
37 self._dts = dts
38 self._log = log
39 self._loop = loop
40 self._project = project
41 self._records_publisher = records_publisher
42 self._account_msg = None
43 if account_msg is not None:
44 self._account_msg = account_msg.deep_copy()
45 self._name = self._account_msg.name
46
47 self._datacenters = []
48 self._status = RwRoAccountYang.YangData_RwProject_Project_RoAccountState_Account_ConnectionStatus(
49 status="unknown",
50 details="Connection status lookup not started"
51 )
52 self.live_instances = 0
53
54 if self._dts is None:
55 return
56
57 self._nsm_plugins = rwnsmplugin.NsmPlugins()
58 self._nsm_cls = self.DEFAULT_PLUGIN
59
60 try:
61 self._nsm_cls = self._nsm_plugins.class_by_plugin_name(
62 account_msg.ro_account_type
63 )
64 except KeyError as e:
65 self._log.warning(
66 "RO account nsm plugin not found: %s. Using standard rift nsm.",
67 account_msg.name
68 )
69
70 self._ro_plugin = self._create_plugin(self._nsm_cls, account_msg)
71
72 @property
73 def name(self):
74 return self._name
75
76 @property
77 def account_msg(self):
78 return self._account_msg
79
80 @property
81 def ro_acccount_type(self):
82 return self._account_msg.ro_account_type if self._account_msg else 'rift'
83
84 @property
85 def ro_plugin(self):
86 return self._ro_plugin
87
88 @property
89 def connection_status(self):
90 return self._status
91
92 def _create_plugin(self, nsm_cls, account_msg):
93 self._log.debug("Instantiating new RO account using class: %s", nsm_cls)
94 nsm_instance = nsm_cls(self._dts, self._log, self._loop,
95 self._records_publisher, account_msg, self._project)
96 return nsm_instance
97
98 def check_ro_account_status(self):
99 self._log.debug("Checking RO Account Status. Acct: %s",
100 self.name)
101 self._status = RwRoAccountYang.YangData_RwProject_Project_RoAccountState_Account_ConnectionStatus(
102 status="validating",
103 details="RO account connection status check in progress"
104 )
105 try:
106 self._datacenters = []
107 for uuid, name in self._ro_plugin._cli_api.datacenter_list():
108 self._datacenters.append({
109 'uuid':uuid,
110 'name':name
111 }
112 )
113 self._status = RwRoAccountYang.YangData_RwProject_Project_RoAccountState_Account_ConnectionStatus(
114 status="success",
115 details="RO account connection status success"
116 )
117 except:
118 self._status = RwRoAccountYang.YangData_RwProject_Project_RoAccountState_Account_ConnectionStatus(
119 status="failure",
120 details="RO account connection status failure"
121 )
122 self._log.warning("RO account connection status failure, Acct:%s, status:%s",
123 self.name, self._status)
124
125 def start_validate_ro_account(self, loop):
126 loop.run_in_executor(None, self.check_ro_account_status)