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.
19 from gi
.repository
import (
26 import rift
.mano
.cloud
27 import rift
.mano
.dts
as mano_dts
30 from . import openmano_nsm
31 from . import rwnsmplugin
34 class RwNsPlugin(rwnsmplugin
.NsmPluginBase
):
36 RW Implentation of the NsmPluginBase
38 def __init__(self
, dts
, log
, loop
, publisher
, ro_account
):
43 def set_state(self
, nsr_id
, state
):
46 def create_nsr(self
, nsr_msg
, nsd
,key_pairs
=None):
48 Create Network service record
53 def deploy(self
, nsr
):
57 def instantiate_ns(self
, nsr
, config_xact
):
59 Instantiate NSR with the passed nsr id
61 yield from nsr
.instantiate(config_xact
)
64 def instantiate_vnf(self
, nsr
, vnfr
):
66 Instantiate NSR with the passed nsr id
68 yield from vnfr
.instantiate(nsr
)
71 def instantiate_vl(self
, nsr
, vlr
):
73 Instantiate NSR with the passed nsr id
75 yield from vlr
.instantiate()
78 def terminate_ns(self
, nsr
):
80 Terminate the network service
85 def terminate_vnf(self
, vnfr
):
87 Terminate the network service
89 yield from vnfr
.terminate()
92 def terminate_vl(self
, vlr
):
94 Terminate the virtual link
96 yield from vlr
.terminate()
99 class NsmPlugins(object):
102 self
._plugin
_classes
= {
103 "openmano": openmano_nsm
.OpenmanoNsPlugin
,
109 return self
._plugin
_classes
111 def __getitem__(self
, name
):
113 print("%s", self
._plugin
_classes
)
114 return self
._plugin
_classes
[name
]
116 def register(self
, plugin_name
, plugin_class
, *args
):
117 """ Register a plugin to this Nsm"""
118 self
._plugin
_classes
[plugin_name
] = plugin_class
120 def deregister(self
, plugin_name
, plugin_class
, *args
):
121 """ Deregister a plugin to this Nsm"""
122 if plugin_name
in self
._plugin
_classes
:
123 del self
._plugin
_classes
[plugin_name
]
125 def class_by_plugin_name(self
, name
):
126 """ Get class by plugin name """
127 return self
._plugin
_classes
[name
]
130 class CloudAccountConfigSubscriber
:
131 def __init__(self
, log
, dts
, log_hdl
):
134 self
._log
_hdl
= log_hdl
136 self
._cloud
_sub
= rift
.mano
.cloud
.CloudAccountConfigSubscriber(
140 rift
.mano
.cloud
.CloudAccountConfigCallbacks())
142 def get_cloud_account_sdn_name(self
, account_name
):
143 if account_name
in self
._cloud
_sub
.accounts
:
144 self
._log
.debug("Cloud accnt msg is %s",self
._cloud
_sub
.accounts
[account_name
].account_msg
)
145 if self
._cloud
_sub
.accounts
[account_name
].account_msg
.has_field("sdn_account"):
146 sdn_account
= self
._cloud
_sub
.accounts
[account_name
].account_msg
.sdn_account
147 self
._log
.info("SDN associated with Cloud name %s is %s", account_name
, sdn_account
)
150 self
._log
.debug("No SDN Account associated with Cloud name %s", account_name
)
155 self
._cloud
_sub
.register()
158 class ROAccountPluginSelector(object):
160 Select the RO based on the config.
162 If no RO account is specified, then default to rift-ro.
165 Currently only one RO can be used (one-time global config.)
167 DEFAULT_PLUGIN
= RwNsPlugin
169 def __init__(self
, dts
, log
, loop
, records_publisher
):
173 self
._records
_publisher
= records_publisher
175 self
._nsm
_plugins
= NsmPlugins()
177 self
._ro
_sub
= mano_dts
.ROAccountConfigSubscriber(
181 callback
=self
.on_ro_account_change
183 self
._nsr
_sub
= mano_dts
.NsrCatalogSubscriber(
189 # The default plugin will be RwNsPlugin
190 self
._ro
_plugin
= self
._create
_plugin
(self
.DEFAULT_PLUGIN
, None)
191 self
.live_instances
= 0
195 return self
._ro
_plugin
197 def handle_nsr(self
, nsr
, action
):
198 if action
== rwdts
.QueryAction
.CREATE
:
199 self
.live_instances
+= 1
200 elif action
== rwdts
.QueryAction
.DELETE
:
201 self
.live_instances
-= 1
203 def on_ro_account_change(self
, ro_account
, action
):
204 if action
in [rwdts
.QueryAction
.CREATE
, rwdts
.QueryAction
.UPDATE
]:
205 self
._on
_ro
_account
_change
(ro_account
)
206 elif action
== rwdts
.QueryAction
.DELETE
:
207 self
._on
_ro
_account
_deleted
(ro_account
)
209 def _on_ro_account_change(self
, ro_account
):
210 self
._log
.debug("Got nsm plugin RO account: %s", ro_account
)
212 nsm_cls
= self
._nsm
_plugins
.class_by_plugin_name(
213 ro_account
.account_type
215 except KeyError as e
:
217 "RO account nsm plugin not found: %s. Using standard rift nsm.",
220 nsm_cls
= self
.DEFAULT_PLUGIN
222 ro_plugin
= self
._create
_plugin
(nsm_cls
, ro_account
)
223 if self
.live_instances
== 0:
224 self
._ro
_plugin
= ro_plugin
226 raise ValueError("Unable to change the plugin when live NS instances exists!")
228 def _on_ro_account_deleted(self
, ro_account
):
229 self
._ro
_plugin
= None
231 def _create_plugin(self
, nsm_cls
, ro_account
):
233 self
._log
.debug("Instantiating new RO account using class: %s", nsm_cls
)
234 nsm_instance
= nsm_cls(self
._dts
, self
._log
, self
._loop
,
235 self
._records
_publisher
, ro_account
)
241 yield from self
._ro
_sub
.register()
242 yield from self
._nsr
_sub
.register()