blob: 732c600fd8c6ed63feb0514513f33ad7234456dd [file] [log] [blame]
Chamarty7b759032017-04-03 17:26:43 -04001
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
18import asyncio
19import rift.tasklets
20
21from gi.repository import(
22 RwSdnYang,
23 RwDts as rwdts,
24 )
25
26
27class SDNAccountNotFound(Exception):
28 pass
29
30
31class SDNAccountDtsOperdataHandler(object):
Philip Joseph0ff08ee2017-04-07 15:36:32 +053032 def __init__(self, dts, log, loop, project):
Chamarty7b759032017-04-03 17:26:43 -040033 self._dts = dts
34 self._log = log
35 self._loop = loop
Philip Joseph0ff08ee2017-04-07 15:36:32 +053036 self._project = project
Chamarty7b759032017-04-03 17:26:43 -040037
38 self.sdn_accounts = {}
Philip Josepha3bb91f2017-04-07 09:53:26 +000039 self._oper = None
40 self._rpc = None
Chamarty7b759032017-04-03 17:26:43 -040041
42 def add_sdn_account(self, account):
43 self.sdn_accounts[account.name] = account
44 account.start_validate_credentials(self._loop)
45
46 def delete_sdn_account(self, account_name):
47 del self.sdn_accounts[account_name]
48
49 def get_saved_sdn_accounts(self, sdn_account_name):
50 ''' Get SDN Account corresponding to passed name, or all saved accounts if name is None'''
51 saved_sdn_accounts = []
52
53 if sdn_account_name is None or sdn_account_name == "":
54 sdn_accounts = list(self.sdn_accounts.values())
55 saved_sdn_accounts.extend(sdn_accounts)
56 elif sdn_account_name in self.sdn_accounts:
57 account = self.sdn_accounts[sdn_account_name]
58 saved_sdn_accounts.append(account)
59 else:
60 errstr = "SDN account {} does not exist".format(sdn_account_name)
61 raise KeyError(errstr)
62
63 return saved_sdn_accounts
64
65 def _register_show_status(self):
66 def get_xpath(sdn_name=None):
Philip Joseph0ff08ee2017-04-07 15:36:32 +053067 return self._project.add_project("D,/rw-sdn:sdn/account{}/connection-status".
68 format(
69 "[name='%s']" % sdn_name
70 if sdn_name is not None else ''))
Chamarty7b759032017-04-03 17:26:43 -040071
72 @asyncio.coroutine
73 def on_prepare(xact_info, action, ks_path, msg):
74 self._log.debug("Got show SDN connection status request: %s", ks_path.create_string())
75 path_entry = RwSdnYang.SDNAccount.schema().keyspec_to_entry(ks_path)
76 sdn_account_name = path_entry.key00.name
77
78 try:
79 saved_accounts = self.get_saved_sdn_accounts(sdn_account_name)
80 for account in saved_accounts:
81 connection_status = account.connection_status
82 self._log.debug("Responding to SDN connection status request: %s", connection_status)
83 xact_info.respond_xpath(
84 rwdts.XactRspCode.MORE,
85 xpath=get_xpath(account.name),
86 msg=account.connection_status,
87 )
88 except KeyError as e:
89 self._log.warning(str(e))
90 xact_info.respond_xpath(rwdts.XactRspCode.NA)
91 return
92
93 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
94
Philip Josepha3bb91f2017-04-07 09:53:26 +000095 self._oper = yield from self._dts.register(
Chamarty7b759032017-04-03 17:26:43 -040096 xpath=get_xpath(),
97 handler=rift.tasklets.DTS.RegistrationHandler(
98 on_prepare=on_prepare),
99 flags=rwdts.Flag.PUBLISHER,
100 )
101
102 def _register_validate_rpc(self):
103 def get_xpath():
104 return "/rw-sdn:update-sdn-status"
105
106 @asyncio.coroutine
107 def on_prepare(xact_info, action, ks_path, msg):
Philip Josepha3bb91f2017-04-07 09:53:26 +0000108 if self._project and not self._project.rpc_check(msg, xact_info=xact_info):
109 return
110
Chamarty7b759032017-04-03 17:26:43 -0400111 if not msg.has_field("sdn_account"):
112 raise SDNAccountNotFound("SDN account name not provided")
113
114 sdn_account_name = msg.sdn_account
115 try:
116 account = self.sdn_accounts[sdn_account_name]
117 except KeyError:
118 raise SDNAccountNotFound("SDN account name %s not found" % sdn_account_name)
119
120 account.start_validate_credentials(self._loop)
121
122 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
123
Philip Josepha3bb91f2017-04-07 09:53:26 +0000124 self._rpc = yield from self._dts.register(
125 xpath=get_xpath(),
126 handler=rift.tasklets.DTS.RegistrationHandler(
127 on_prepare=on_prepare
128 ),
129 flags=rwdts.Flag.PUBLISHER,
130 )
Chamarty7b759032017-04-03 17:26:43 -0400131
132 @asyncio.coroutine
133 def register(self):
134 yield from self._register_show_status()
135 yield from self._register_validate_rpc()
Philip Josepha3bb91f2017-04-07 09:53:26 +0000136
137 def deregister(self):
138 if self._oper:
139 self._oper.deregister()
140 self._oper = None
141
142 if self._rpc:
143 self._rpc.deregister()
144 self._rpc = None