Revert "RIFT-14721: Added update mode for RO account, also enabled unit tests for...
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / cloud.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 from gi.repository import (
20 RwDts as rwdts,
21 RwcalYang as rwcal,
22 RwTypes,
23 ProtobufC,
24 )
25
26 import rift.mano.cloud
27 import rift.mano.dts as mano_dts
28 import rift.tasklets
29
30 from . import openmano_nsm
31 from . import rwnsmplugin
32
33
34 class RwNsPlugin(rwnsmplugin.NsmPluginBase):
35 """
36 RW Implentation of the NsmPluginBase
37 """
38 def __init__(self, dts, log, loop, publisher, ro_account):
39 self._dts = dts
40 self._log = log
41 self._loop = loop
42
43 def create_nsr(self, nsr_msg, nsd,key_pairs=None):
44 """
45 Create Network service record
46 """
47 pass
48
49 @asyncio.coroutine
50 def deploy(self, nsr):
51 pass
52
53 @asyncio.coroutine
54 def instantiate_ns(self, nsr, config_xact):
55 """
56 Instantiate NSR with the passed nsr id
57 """
58 yield from nsr.instantiate(config_xact)
59
60 @asyncio.coroutine
61 def instantiate_vnf(self, nsr, vnfr):
62 """
63 Instantiate NSR with the passed nsr id
64 """
65 yield from vnfr.instantiate(nsr)
66
67 @asyncio.coroutine
68 def instantiate_vl(self, nsr, vlr):
69 """
70 Instantiate NSR with the passed nsr id
71 """
72 yield from vlr.instantiate()
73
74 @asyncio.coroutine
75 def terminate_ns(self, nsr):
76 """
77 Terminate the network service
78 """
79 pass
80
81 @asyncio.coroutine
82 def terminate_vnf(self, vnfr):
83 """
84 Terminate the network service
85 """
86 yield from vnfr.terminate()
87
88 @asyncio.coroutine
89 def terminate_vl(self, vlr):
90 """
91 Terminate the virtual link
92 """
93 yield from vlr.terminate()
94
95
96 class NsmPlugins(object):
97 """ NSM Plugins """
98 def __init__(self):
99 self._plugin_classes = {
100 "openmano": openmano_nsm.OpenmanoNsPlugin,
101 }
102
103 @property
104 def plugins(self):
105 """ Plugin info """
106 return self._plugin_classes
107
108 def __getitem__(self, name):
109 """ Get item """
110 print("%s", self._plugin_classes)
111 return self._plugin_classes[name]
112
113 def register(self, plugin_name, plugin_class, *args):
114 """ Register a plugin to this Nsm"""
115 self._plugin_classes[plugin_name] = plugin_class
116
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]
121
122 def class_by_plugin_name(self, name):
123 """ Get class by plugin name """
124 return self._plugin_classes[name]
125
126
127 class CloudAccountConfigSubscriber:
128 def __init__(self, log, dts, log_hdl):
129 self._dts = dts
130 self._log = log
131 self._log_hdl = log_hdl
132
133 self._cloud_sub = rift.mano.cloud.CloudAccountConfigSubscriber(
134 self._dts,
135 self._log,
136 self._log_hdl,
137 rift.mano.cloud.CloudAccountConfigCallbacks())
138
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)
145 return sdn_account
146 else:
147 self._log.debug("No SDN Account associated with Cloud name %s", account_name)
148 return None
149
150 @asyncio.coroutine
151 def register(self):
152 self._cloud_sub.register()
153
154
155 class ROAccountPluginSelector(object):
156 """
157 Select the RO based on the config.
158
159 If no RO account is specified, then default to rift-ro.
160
161 Note:
162 Currently only one RO can be used (one-time global config.)
163 """
164 DEFAULT_PLUGIN = RwNsPlugin
165
166 def __init__(self, dts, log, loop, records_publisher):
167 self._dts = dts
168 self._log = log
169 self._loop = loop
170 self._records_publisher = records_publisher
171
172 self._nsm_plugins = NsmPlugins()
173
174 self._ro_sub = mano_dts.ROAccountConfigSubscriber(
175 self._log,
176 self._dts,
177 self._loop,
178 callback=self.on_ro_account_change
179 )
180
181 # The default plugin will be RwNsPlugin
182 self._plugin_instances = {}
183 self._ro_plugin = self._create_plugin(self.DEFAULT_PLUGIN, None)
184
185 @property
186 def ro_plugin(self):
187 return self._ro_plugin
188
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)
194
195 def _on_ro_account_added(self, ro_account):
196 self._log.debug("Got nsm plugin RO account: %s", ro_account)
197 try:
198 nsm_cls = self._nsm_plugins.class_by_plugin_name(
199 ro_account.account_type
200 )
201 except KeyError as e:
202 self._log.debug(
203 "RO account nsm plugin not found: %s. Using standard rift nsm.",
204 ro_account.name
205 )
206 nsm_cls = self.DEFAULT_PLUGIN
207
208 self._ro_plugin = self._create_plugin(nsm_cls, ro_account)
209
210 def _on_ro_account_deleted(self, ro_account):
211 self._ro_plugin = None
212
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]
218
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)
223
224 self._plugin_instances[nsm_cls] = nsm_instance
225 return nsm_instance
226
227 @asyncio.coroutine
228 def register(self):
229 yield from self._ro_sub.register()