3 # Copyright 2016 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 gi
.require_version('RwDts', '1.0')
22 gi
.require_version('RwcalYang', '1.0')
23 gi
.require_version('RwTypes', '1.0')
24 gi
.require_version('RwSdn', '1.0')
25 from gi
.repository
import (
28 IetfNetworkTopologyYang
,
35 from gi
.repository
.RwTypes
import RwStatus
39 class NwtopDiscoveryDtsHandler(object):
40 """ Handles DTS interactions for the Discovered Topology registration """
41 DISC_XPATH
= "D,/nd:network"
43 def __init__(self
, dts
, log
, loop
, acctstore
, nwdatastore
):
47 self
._acctstore
= acctstore
48 self
._nwdatastore
= nwdatastore
54 """ The registration handle associated with this Handler"""
59 """ Register for the Discovered Topology path """
62 def on_ready(regh
, status
):
63 """ On_ready for Discovered Topology registration """
64 self
._log
.debug("PUB reg ready for Discovered Topology handler regn_hdl(%s) status %s",
68 def on_prepare(xact_info
, action
, ks_path
, msg
):
69 """ prepare for Discovered Topology registration"""
71 "Got topology on_prepare callback (xact_info: %s, action: %s): %s",
72 xact_info
, action
, msg
75 if action
== rwdts
.QueryAction
.READ
:
77 for name
, sdnacct
in self
._acctstore
.items():
78 if sdnacct
.account_type
!= "odl":
82 rc
, nwtop
= sdnintf
.get_network_list(sdnacct
.sdnal_account_msg
)
83 #assert rc == RwStatus.SUCCESS
84 if rc
!= RwStatus
.SUCCESS
:
85 self
._log
.error("Fetching get network list for SDN Account %s failed", name
)
86 xact_info
.respond_xpath(rwdts
.XactRspCode
.NACK
)
89 self
._log
.debug("Topology: Retrieved network attributes ")
90 for nw
in nwtop
.network
:
91 # Add SDN account name
92 nw
.rw_network_attributes
.sdn_account_name
= name
93 nw
.server_provided
= False
94 nw
.network_id
= name
+ ':' + nw
.network_id
95 self
._log
.debug("...Network id %s", nw
.network_id
)
96 nw_xpath
= ("D,/nd:network[network-id=\'{}\']").format(nw
.network_id
)
97 xact_info
.respond_xpath(rwdts
.XactRspCode
.MORE
,
100 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
101 #err = "%s action on discovered Topology not supported" % action
102 #raise NotImplementedError(err)
104 self
._log
.debug("Registering for discovered topology using xpath %s", NwtopDiscoveryDtsHandler
.DISC_XPATH
)
106 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
108 on_prepare
=on_prepare
,
111 yield from self
._dts
.register(
112 NwtopDiscoveryDtsHandler
.DISC_XPATH
,
113 flags
=rwdts
.Flag
.PUBLISHER
,
118 class NwtopStaticDtsHandler(object):
119 """ Handles DTS interactions for the Static Topology registration """
120 STATIC_XPATH
= "C,/nd:network"
122 def __init__(self
, dts
, log
, loop
, acctstore
, nwdatastore
):
126 self
._acctstore
= acctstore
130 self
._nwdatastore
= nwdatastore
134 """ The registration handle associated with this Handler"""
140 """ Register for the Static Topology path """
143 def prepare_nw_cfg(dts
, acg
, xact
, xact_info
, ksp
, msg
, scratch
):
144 """Prepare for application configuration. Stash the pending
145 configuration object for subsequent transaction phases"""
146 self
._log
.debug("Prepare Network config received network id %s, msg %s",
148 self
.pending
[xact
.id] = msg
149 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
151 def apply_nw_config(dts
, acg
, xact
, action
, scratch
):
152 """Apply the pending configuration object"""
153 if action
== rwdts
.AppconfAction
.INSTALL
and xact
.id is None:
154 self
._log
.debug("No xact handle. Skipping apply config")
157 if xact
.id not in self
.pending
:
158 raise KeyError("No stashed configuration found with transaction id [{}]".format(xact
.id))
161 if action
== rwdts
.AppconfAction
.INSTALL
:
162 self
._nwdatastore
.create_network(self
.pending
[xact
.id].network_id
, self
.pending
[xact
.id])
163 elif action
== rwdts
.AppconfAction
.RECONCILE
:
164 self
._nwdatastore
.update_network(self
.pending
[xact
.id].network_id
, self
.pending
[xact
.id])
168 self
._log
.debug("Create network config done")
169 return RwTypes
.RwStatus
.SUCCESS
171 self
._log
.debug("Registering for static topology using xpath %s", NwtopStaticDtsHandler
.STATIC_XPATH
)
172 handler
=rift
.tasklets
.AppConfGroup
.Handler(
173 on_apply
=apply_nw_config
)
175 with self
._dts
.appconf_group_create(handler
=handler
) as acg
:
176 acg
.register(xpath
= NwtopStaticDtsHandler
.STATIC_XPATH
,
177 flags
= rwdts
.Flag
.SUBSCRIBER
,
178 on_prepare
=prepare_nw_cfg
)