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 create_nsr(self
, nsr_msg
, nsd
):
45 Create Network service record
50 def deploy(self
, nsr
):
54 def instantiate_ns(self
, nsr
, config_xact
):
56 Instantiate NSR with the passed nsr id
58 yield from nsr
.instantiate(config_xact
)
61 def instantiate_vnf(self
, nsr
, vnfr
):
63 Instantiate NSR with the passed nsr id
65 yield from vnfr
.instantiate(nsr
)
68 def instantiate_vl(self
, nsr
, vlr
):
70 Instantiate NSR with the passed nsr id
72 yield from vlr
.instantiate()
75 def terminate_ns(self
, nsr
):
77 Terminate the network service
82 def terminate_vnf(self
, vnfr
):
84 Terminate the network service
86 yield from vnfr
.terminate()
89 def terminate_vl(self
, vlr
):
91 Terminate the virtual link
93 yield from vlr
.terminate()
96 class NsmPlugins(object):
99 self
._plugin
_classes
= {
100 "openmano": openmano_nsm
.OpenmanoNsPlugin
,
106 return self
._plugin
_classes
108 def __getitem__(self
, name
):
110 print("%s", self
._plugin
_classes
)
111 return self
._plugin
_classes
[name
]
113 def register(self
, plugin_name
, plugin_class
, *args
):
114 """ Register a plugin to this Nsm"""
115 self
._plugin
_classes
[plugin_name
] = plugin_class
117 def deregister(self
, plugin_name
, plugin_class
, *args
):
118 """ Deregister a plugin to this Nsm"""
119 if plugin_name
in self
._plugin
_classes
:
120 del self
._plugin
_classes
[plugin_name
]
122 def class_by_plugin_name(self
, name
):
123 """ Get class by plugin name """
124 return self
._plugin
_classes
[name
]
127 class CloudAccountConfigSubscriber
:
128 def __init__(self
, log
, dts
, log_hdl
):
131 self
._log
_hdl
= log_hdl
133 self
._cloud
_sub
= rift
.mano
.cloud
.CloudAccountConfigSubscriber(
137 rift
.mano
.cloud
.CloudAccountConfigCallbacks())
139 def get_cloud_account_sdn_name(self
, account_name
):
140 if account_name
in self
._cloud
_sub
.accounts
:
141 self
._log
.debug("Cloud accnt msg is %s",self
._cloud
_sub
.accounts
[account_name
].account_msg
)
142 if self
._cloud
_sub
.accounts
[account_name
].account_msg
.has_field("sdn_account"):
143 sdn_account
= self
._cloud
_sub
.accounts
[account_name
].account_msg
.sdn_account
144 self
._log
.info("SDN associated with Cloud name %s is %s", account_name
, sdn_account
)
147 self
._log
.debug("No SDN Account associated with Cloud name %s", account_name
)
152 self
._cloud
_sub
.register()
155 class ROAccountPluginSelector(object):
157 Select the RO based on the config.
159 If no RO account is specified, then default to rift-ro.
162 Currently only one RO can be used (one-time global config.)
164 DEFAULT_PLUGIN
= RwNsPlugin
166 def __init__(self
, dts
, log
, loop
, records_publisher
):
170 self
._records
_publisher
= records_publisher
172 self
._nsm
_plugins
= NsmPlugins()
174 self
._ro
_sub
= mano_dts
.ROAccountConfigSubscriber(
178 callback
=self
.on_ro_account_change
181 # The default plugin will be RwNsPlugin
182 self
._plugin
_instances
= {}
183 self
._ro
_plugin
= self
._create
_plugin
(self
.DEFAULT_PLUGIN
, None)
187 return self
._ro
_plugin
189 def on_ro_account_change(self
, ro_account
, action
):
190 if action
== rwdts
.QueryAction
.CREATE
:
191 self
._on
_ro
_account
_added
(ro_account
)
192 elif action
== rwdts
.QueryAction
.DELETE
:
193 self
._on
_ro
_account
_deleted
(ro_account
)
195 def _on_ro_account_added(self
, ro_account
):
196 self
._log
.debug("Got nsm plugin RO account: %s", ro_account
)
198 nsm_cls
= self
._nsm
_plugins
.class_by_plugin_name(
199 ro_account
.account_type
201 except KeyError as e
:
203 "RO account nsm plugin not found: %s. Using standard rift nsm.",
206 nsm_cls
= self
.DEFAULT_PLUGIN
208 self
._ro
_plugin
= self
._create
_plugin
(nsm_cls
, ro_account
)
210 def _on_ro_account_deleted(self
, ro_account
):
211 self
._ro
_plugin
= None
213 def _create_plugin(self
, nsm_cls
, ro_account
):
214 # Check to see if the plugin was already instantiated
215 if nsm_cls
in self
._plugin
_instances
:
216 self
._log
.debug("RO account nsm plugin already instantiated. Using existing.")
217 return self
._plugin
_instances
[nsm_cls
]
219 # Otherwise, instantiate a new plugin using the cloud account
220 self
._log
.debug("Instantiating new RO account using class: %s", nsm_cls
)
221 nsm_instance
= nsm_cls(self
._dts
, self
._log
, self
._loop
,
222 self
._records
_publisher
, ro_account
)
224 self
._plugin
_instances
[nsm_cls
] = nsm_instance
229 yield from self
._ro
_sub
.register()