| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 1 | |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 2 | # |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 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 | |
| 18 | import sys |
| 19 | import asyncio |
| 20 | from gi import require_version |
| 21 | require_version('RwTypes', '1.0') |
| 22 | require_version('RwsdnalYang', '1.0') |
| 23 | require_version('RwSdnYang', '1.0') |
| 24 | |
| 25 | from gi.repository import ( |
| 26 | RwTypes, |
| 27 | RwsdnalYang, |
| 28 | RwSdnYang, |
| 29 | ) |
| 30 | import rw_peas |
| 31 | |
| 32 | if sys.version_info < (3, 4, 4): |
| 33 | asyncio.ensure_future = asyncio.async |
| 34 | |
| 35 | |
| 36 | class PluginLoadingError(Exception): |
| 37 | pass |
| 38 | |
| 39 | |
| 40 | class SDNAccountCalError(Exception): |
| 41 | pass |
| 42 | |
| 43 | |
| 44 | class SDNAccount(object): |
| 45 | def __init__(self, log, rwlog_hdl, account_msg): |
| 46 | self._log = log |
| 47 | self._account_msg = account_msg.deep_copy() |
| 48 | |
| 49 | self._sdn_plugin = None |
| 50 | self._engine = None |
| 51 | |
| 52 | self._sdn = self.plugin.get_interface("Topology") |
| 53 | self._sdn.init(rwlog_hdl) |
| 54 | |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 55 | self._status = RwSdnYang.YangData_RwProject_Project_Sdn_Account_ConnectionStatus( |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 56 | status="unknown", |
| 57 | details="Connection status lookup not started" |
| 58 | ) |
| 59 | |
| 60 | self._validate_task = None |
| 61 | |
| 62 | @property |
| 63 | def plugin(self): |
| 64 | if self._sdn_plugin is None: |
| 65 | try: |
| 66 | self._sdn_plugin = rw_peas.PeasPlugin( |
| 67 | getattr(self._account_msg, self.account_type).plugin_name, |
| 68 | 'RwSdn-1.0', |
| 69 | ) |
| 70 | |
| 71 | except AttributeError as e: |
| 72 | raise PluginLoadingError(str(e)) |
| 73 | |
| 74 | self._engine, _, _ = self._sdn_plugin() |
| 75 | |
| 76 | return self._sdn_plugin |
| 77 | |
| 78 | def _wrap_status_fn(self, fn, *args, **kwargs): |
| 79 | ret = fn(*args, **kwargs) |
| 80 | rw_status = ret[0] |
| 81 | if rw_status != RwTypes.RwStatus.SUCCESS: |
| 82 | msg = "%s returned %s" % (fn.__name__, str(rw_status)) |
| 83 | self._log.error(msg) |
| 84 | raise SDNAccountCalError(msg) |
| 85 | |
| 86 | # If there was only one other return value besides rw_status, then just |
| 87 | # return that element. Otherwise return the rest of the return values |
| 88 | # as a list. |
| 89 | return ret[1] if len(ret) == 2 else ret[1:] |
| 90 | |
| 91 | @property |
| 92 | def sdn(self): |
| 93 | return self._sdn |
| 94 | |
| 95 | @property |
| 96 | def name(self): |
| 97 | return self._account_msg.name |
| 98 | |
| 99 | @property |
| 100 | def account_msg(self): |
| 101 | return self._account_msg |
| 102 | |
| 103 | @property |
| 104 | def sdnal_account_msg(self): |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 105 | return RwsdnalYang.YangData_RwProject_Project_SdnAccounts_SdnAccountList.from_dict( |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 106 | self.account_msg.as_dict(), |
| 107 | ignore_missing_keys=True, |
| 108 | ) |
| 109 | |
| 110 | def sdn_account_msg(self, account_dict): |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 111 | self._account_msg = RwSdnYang.YangData_RwProject_Project_SdnAccounts_SdnAccountList.from_dict(account_dict) |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 112 | |
| 113 | @property |
| 114 | def account_type(self): |
| 115 | return self._account_msg.account_type |
| 116 | |
| 117 | @property |
| 118 | def connection_status(self): |
| 119 | return self._status |
| 120 | |
| 121 | def update_from_cfg(self, cfg): |
| 122 | self._log.debug("Updating parent SDN Account to %s", cfg) |
| 123 | |
| 124 | raise NotImplementedError("Update SDN account not yet supported") |
| 125 | |
| 126 | |
| 127 | @asyncio.coroutine |
| 128 | def validate_sdn_account_credentials(self, loop): |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 129 | self._log.debug("Validating SDN Account credentials %s", |
| 130 | self.name) |
| 131 | self._status = RwSdnYang.YangData_RwProject_Project_Sdn_Account_ConnectionStatus( |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 132 | status="validating", |
| 133 | details="SDN account connection validation in progress" |
| 134 | ) |
| 135 | rwstatus, status = yield from loop.run_in_executor( |
| 136 | None, |
| 137 | self._sdn.validate_sdn_creds, |
| 138 | self.sdnal_account_msg, |
| 139 | ) |
| 140 | if rwstatus == RwTypes.RwStatus.SUCCESS: |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 141 | self._status = RwSdnYang.YangData_RwProject_Project_Sdn_Account_ConnectionStatus.from_dict(status.as_dict()) |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 142 | else: |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 143 | self._status = RwSdnYang.YangData_RwProject_Project_Sdn_Account_ConnectionStatus( |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 144 | status="failure", |
| 145 | details="Error when calling SDNAL validate SDN creds" |
| 146 | ) |
| 147 | |
| Jeremy Mordkoff | 4870d0e | 2017-09-30 20:28:33 -0400 | [diff] [blame] | 148 | if self._status.status == 'failure': |
| 149 | self._log.error("SDN account validation failed; Acct: %s status: %s", |
| 150 | self.name, self._status) |
| Chamarty | 4bd9025 | 2017-04-03 17:26:43 -0400 | [diff] [blame] | 151 | |
| 152 | def start_validate_credentials(self, loop): |
| 153 | if self._validate_task is not None: |
| 154 | self._validate_task.cancel() |
| 155 | self._validate_task = None |
| 156 | |
| 157 | self._validate_task = asyncio.ensure_future( |
| 158 | self.validate_sdn_account_credentials(loop), |
| 159 | loop=loop |
| 160 | ) |