RIFT OSM R1 Initial Submission
[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):
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 ROAccountConfigSubscriber(mano_dts.AbstractConfigSubscriber):
128
129 def key_name(self):
130 return "name"
131
132 def get_xpath(self):
133 return("C,/rw-launchpad:resource-orchestrator")
134
135
136 class CloudAccountConfigSubscriber:
137 def __init__(self, log, dts, log_hdl):
138 self._dts = dts
139 self._log = log
140 self._log_hdl = log_hdl
141
142 self._cloud_sub = rift.mano.cloud.CloudAccountConfigSubscriber(
143 self._dts,
144 self._log,
145 self._log_hdl,
146 rift.mano.cloud.CloudAccountConfigCallbacks())
147
148 def get_cloud_account_sdn_name(self, account_name):
149 if account_name in self._cloud_sub.accounts:
150 self._log.debug("Cloud accnt msg is %s",self._cloud_sub.accounts[account_name].account_msg)
151 if self._cloud_sub.accounts[account_name].account_msg.has_field("sdn_account"):
152 sdn_account = self._cloud_sub.accounts[account_name].account_msg.sdn_account
153 self._log.info("SDN associated with Cloud name %s is %s", account_name, sdn_account)
154 return sdn_account
155 else:
156 self._log.debug("No SDN Account associated with Cloud name %s", account_name)
157 return None
158
159 @asyncio.coroutine
160 def register(self):
161 self._cloud_sub.register()
162
163
164 class ROAccountPluginSelector(object):
165 """
166 Select the RO based on the config.
167
168 If no RO account is specified, then default to rift-ro.
169
170 Note:
171 Currently only one RO can be used (one-time global config.)
172 """
173 DEFAULT_PLUGIN = RwNsPlugin
174
175 def __init__(self, dts, log, loop, records_publisher):
176 self._dts = dts
177 self._log = log
178 self._loop = loop
179 self._records_publisher = records_publisher
180
181 self._nsm_plugins = NsmPlugins()
182
183 self._ro_sub = ROAccountConfigSubscriber(
184 self._log,
185 self._dts,
186 self._loop,
187 callback=self.on_ro_account_change
188 )
189
190 # The default plugin will be RwNsPlugin
191 self._plugin_instances = {}
192 self._ro_plugin = self._create_plugin(self.DEFAULT_PLUGIN, None)
193
194 @property
195 def ro_plugin(self):
196 return self._ro_plugin
197
198 def on_ro_account_change(self, ro_account, action):
199 if action == rwdts.QueryAction.CREATE:
200 self._on_ro_account_added(ro_account)
201 elif action == rwdts.QueryAction.DELETE:
202 self._on_ro_account_deleted(ro_account)
203
204 def _on_ro_account_added(self, ro_account):
205 self._log.debug("Got nsm plugin RO account: %s", ro_account)
206 try:
207 nsm_cls = self._nsm_plugins.class_by_plugin_name(
208 ro_account.account_type
209 )
210 except KeyError as e:
211 self._log.debug(
212 "RO account nsm plugin not found: %s. Using standard rift nsm.",
213 ro_account.name
214 )
215 nsm_cls = self.DEFAULT_PLUGIN
216
217 self._ro_plugin = self._create_plugin(nsm_cls, ro_account)
218
219 def _on_ro_account_deleted(self, ro_account):
220 self._ro_plugin = None
221
222 def _create_plugin(self, nsm_cls, ro_account):
223 # Check to see if the plugin was already instantiated
224 if nsm_cls in self._plugin_instances:
225 self._log.debug("RO account nsm plugin already instantiated. Using existing.")
226 return self._plugin_instances[nsm_cls]
227
228 # Otherwise, instantiate a new plugin using the cloud account
229 self._log.debug("Instantiating new RO account using class: %s", nsm_cls)
230 nsm_instance = nsm_cls(self._dts, self._log, self._loop,
231 self._records_publisher, ro_account)
232
233 self._plugin_instances[nsm_cls] = nsm_instance
234 return nsm_instance
235
236 @asyncio.coroutine
237 def register(self):
238 yield from self._ro_sub.register()