SDN Accounts refactoring
[osm/SO.git] / rwlaunchpad / plugins / rwvns / rift / topmgr / rwtopmgr.py
1
2 #
3 # Copyright 2016 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 asyncio
19
20 import gi
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 (
26 RwDts as rwdts,
27 IetfNetworkYang,
28 IetfNetworkTopologyYang,
29 IetfL2TopologyYang,
30 RwTopologyYang,
31 RwsdnalYang,
32 RwTypes
33 )
34
35 from gi.repository.RwTypes import RwStatus
36 import rift.tasklets
37
38
39 class NwtopDiscoveryDtsHandler(object):
40 """ Handles DTS interactions for the Discovered Topology registration """
41 DISC_XPATH = "D,/nd:network"
42
43 def __init__(self, dts, log, loop, acctstore, nwdatastore):
44 self._dts = dts
45 self._log = log
46 self._loop = loop
47 self._acctstore = acctstore
48 self._nwdatastore = nwdatastore
49
50 self._regh = None
51
52 @property
53 def regh(self):
54 """ The registration handle associated with this Handler"""
55 return self._regh
56
57 @asyncio.coroutine
58 def register(self):
59 """ Register for the Discovered Topology path """
60
61 @asyncio.coroutine
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",
65 regh, status)
66
67 @asyncio.coroutine
68 def on_prepare(xact_info, action, ks_path, msg):
69 """ prepare for Discovered Topology registration"""
70 self._log.debug(
71 "Got topology on_prepare callback (xact_info: %s, action: %s): %s",
72 xact_info, action, msg
73 )
74
75 if action == rwdts.QueryAction.READ:
76
77 for name, sdnacct in self._acctstore.items():
78 if sdnacct.account_type != "odl":
79 continue
80 sdnintf = sdnacct.sdn
81
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)
87 return
88
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,
98 nw_xpath, nw)
99
100 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
101 #err = "%s action on discovered Topology not supported" % action
102 #raise NotImplementedError(err)
103
104 self._log.debug("Registering for discovered topology using xpath %s", NwtopDiscoveryDtsHandler.DISC_XPATH)
105
106 handler = rift.tasklets.DTS.RegistrationHandler(
107 on_ready=on_ready,
108 on_prepare=on_prepare,
109 )
110
111 yield from self._dts.register(
112 NwtopDiscoveryDtsHandler.DISC_XPATH,
113 flags=rwdts.Flag.PUBLISHER,
114 handler=handler
115 )
116
117
118 class NwtopStaticDtsHandler(object):
119 """ Handles DTS interactions for the Static Topology registration """
120 STATIC_XPATH = "C,/nd:network"
121
122 def __init__(self, dts, log, loop, acctstore, nwdatastore):
123 self._dts = dts
124 self._log = log
125 self._loop = loop
126 self._acctstore = acctstore
127
128 self._regh = None
129 self.pending = {}
130 self._nwdatastore = nwdatastore
131
132 @property
133 def regh(self):
134 """ The registration handle associated with this Handler"""
135 return self._regh
136
137
138 @asyncio.coroutine
139 def register(self):
140 """ Register for the Static Topology path """
141
142 @asyncio.coroutine
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",
147 msg.network_id, msg)
148 self.pending[xact.id] = msg
149 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
150
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")
155 return
156
157 if xact.id not in self.pending:
158 raise KeyError("No stashed configuration found with transaction id [{}]".format(xact.id))
159
160 try:
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])
165 except:
166 raise
167
168 self._log.debug("Create network config done")
169 return RwTypes.RwStatus.SUCCESS
170
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)
174
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)
179
180