3 # Copyright 2017 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from gi
.repository
import(
27 class SDNAccountNotFound(Exception):
31 class SDNAccountDtsOperdataHandler(object):
32 def __init__(self
, dts
, log
, loop
):
37 self
.sdn_accounts
= {}
39 def add_sdn_account(self
, account
):
40 self
.sdn_accounts
[account
.name
] = account
41 account
.start_validate_credentials(self
._loop
)
43 def delete_sdn_account(self
, account_name
):
44 del self
.sdn_accounts
[account_name
]
46 def get_saved_sdn_accounts(self
, sdn_account_name
):
47 ''' Get SDN Account corresponding to passed name, or all saved accounts if name is None'''
48 saved_sdn_accounts
= []
50 if sdn_account_name
is None or sdn_account_name
== "":
51 sdn_accounts
= list(self
.sdn_accounts
.values())
52 saved_sdn_accounts
.extend(sdn_accounts
)
53 elif sdn_account_name
in self
.sdn_accounts
:
54 account
= self
.sdn_accounts
[sdn_account_name
]
55 saved_sdn_accounts
.append(account
)
57 errstr
= "SDN account {} does not exist".format(sdn_account_name
)
58 raise KeyError(errstr
)
60 return saved_sdn_accounts
62 def _register_show_status(self
):
63 def get_xpath(sdn_name
=None):
64 return "D,/rw-sdn:sdn/account{}/connection-status".format(
65 "[name='%s']" % sdn_name
if sdn_name
is not None else ''
69 def on_prepare(xact_info
, action
, ks_path
, msg
):
70 self
._log
.debug("Got show SDN connection status request: %s", ks_path
.create_string())
71 path_entry
= RwSdnYang
.SDNAccount
.schema().keyspec_to_entry(ks_path
)
72 sdn_account_name
= path_entry
.key00
.name
75 saved_accounts
= self
.get_saved_sdn_accounts(sdn_account_name
)
76 for account
in saved_accounts
:
77 connection_status
= account
.connection_status
78 self
._log
.debug("Responding to SDN connection status request: %s", connection_status
)
79 xact_info
.respond_xpath(
80 rwdts
.XactRspCode
.MORE
,
81 xpath
=get_xpath(account
.name
),
82 msg
=account
.connection_status
,
85 self
._log
.warning(str(e
))
86 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
89 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
91 yield from self
._dts
.register(
93 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
94 on_prepare
=on_prepare
),
95 flags
=rwdts
.Flag
.PUBLISHER
,
98 def _register_validate_rpc(self
):
100 return "/rw-sdn:update-sdn-status"
103 def on_prepare(xact_info
, action
, ks_path
, msg
):
104 if not msg
.has_field("sdn_account"):
105 raise SDNAccountNotFound("SDN account name not provided")
107 sdn_account_name
= msg
.sdn_account
109 account
= self
.sdn_accounts
[sdn_account_name
]
111 raise SDNAccountNotFound("SDN account name %s not found" % sdn_account_name
)
113 account
.start_validate_credentials(self
._loop
)
115 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
117 yield from self
._dts
.register(
119 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
120 on_prepare
=on_prepare
122 flags
=rwdts
.Flag
.PUBLISHER
,
127 yield from self
._register
_show
_status
()
128 yield from self
._register
_validate
_rpc
()