640e4b596d9c93ecc9e02527597f0a4423accef0
[osm/SO.git] / rwcm / plugins / rwconman / rift / tasklets / rwconmantasklet / riftcm_config_plugin.py
1 #
2 # Copyright 2016 RIFT.IO Inc
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 import asyncio
18 import abc
19
20 # Default config agent plugin type
21 DEFAULT_CAP_TYPE = "riftca"
22
23 class RiftCMnsr(object):
24 '''
25 Agent class for NSR
26 created for Agents to use objects from NSR
27 '''
28 def __init__(self, nsr_dict, cfg):
29 self._nsr = nsr_dict
30 self._cfg = cfg
31 self._vnfrs = []
32 self._vnfrs_msg = []
33 self._vnfr_ids = {}
34 self._job_id = 0
35
36 @property
37 def name(self):
38 return self._nsr['name_ref']
39
40 @property
41 def nsd_name(self):
42 return self._nsr['nsd_name_ref']
43
44 @property
45 def nsd_id(self):
46 return self._nsr['nsd_ref']
47
48 @property
49 def id(self):
50 return self._nsr['ns_instance_config_ref']
51
52 @property
53 def nsr_dict(self):
54 return self._nsr
55
56 @property
57 def nsr_cfg_msg(self):
58 return self._cfg
59
60 @property
61 def job_id(self):
62 ''' Get a new job id for config primitive'''
63 self._job_id += 1
64 return self._job_id
65
66 @property
67 def vnfrs(self):
68 return self._vnfrs
69
70 @property
71 def member_vnf_index(self):
72 return self._vnfr['member_vnf_index_ref']
73
74 def add_vnfr(self, vnfr, vnfr_msg):
75 if vnfr['id'] in self._vnfr_ids.keys():
76 agent_vnfr = self._vnfr_ids[vnfr['id']]
77 else:
78 agent_vnfr = RiftCMvnfr(self.name, vnfr, vnfr_msg)
79 self._vnfrs.append(agent_vnfr)
80 self._vnfrs_msg.append(vnfr_msg)
81 self._vnfr_ids[agent_vnfr.id] = agent_vnfr
82 return agent_vnfr
83
84 @property
85 def vnfr_ids(self):
86 return self._vnfr_ids
87
88 class RiftCMvnfr(object):
89 '''
90 Agent base class for VNFR processing
91 '''
92 def __init__(self, nsr_name, vnfr_dict, vnfr_msg):
93 self._vnfr = vnfr_dict
94 self._vnfr_msg = vnfr_msg
95 self._nsr_name = nsr_name
96 self._configurable = False
97
98 @property
99 def nsr_name(self):
100 return self._nsr_name
101
102 @property
103 def vnfr(self):
104 return self._vnfr
105
106 @property
107 def vnfr_msg(self):
108 return self._vnfr_msg
109
110 @property
111 def name(self):
112 return self._vnfr['short_name']
113
114 @property
115 def tags(self):
116 try:
117 return self._vnfr['tags']
118 except KeyError:
119 return None
120
121 @property
122 def id(self):
123 return self._vnfr['id']
124
125 @property
126 def member_vnf_index(self):
127 return self._vnfr['member_vnf_index_ref']
128
129 @property
130 def vnf_configuration(self):
131 return self._vnfr['vnf_configuration']
132
133 @property
134 def xpath(self):
135 """ VNFR xpath """
136 return "D,/vnfr:vnfr-catalog/vnfr:vnfr[vnfr:id = '{}']".format(self.id)
137
138 def set_to_configurable(self):
139 self._configurable = True
140
141 @property
142 def is_configurable(self):
143 return self._configurable
144
145 @property
146 def vnf_cfg(self):
147 return self._vnfr['vnf_cfg']
148
149 class RiftCMConfigPluginBase(object):
150 """
151 Abstract base class for the NSM Configuration agent plugin.
152 There will be single instance of this plugin for each plugin type.
153 """
154
155 def __init__(self, dts, log, loop, config_agent):
156 self._dts = dts
157 self._log = log
158 self._loop = loop
159 self._config_agent = config_agent
160
161 @property
162 def agent_type(self):
163 raise NotImplementedError
164
165 @property
166 def name(self):
167 raise NotImplementedError
168
169 @property
170 def agent_data(self):
171 raise NotImplementedError
172
173 @property
174 def dts(self):
175 return self._dts
176
177 @property
178 def log(self):
179 return self._log
180
181 @property
182 def loop(self):
183 return self._loop
184
185 @property
186 def nsm(self):
187 return self._nsm
188
189
190 def vnfr(self, vnfr_id):
191 raise NotImplementedError
192
193 @abc.abstractmethod
194 def get_Service_name(self):
195 """ Get the service name specific to the plugin """
196 pass
197
198 @abc.abstractmethod
199 @asyncio.coroutine
200 def apply_config(self, agent_nsr, agent_vnfr, config, rpc_ip):
201 """ Notification on configuration of an NSR """
202 pass
203
204 @abc.abstractmethod
205 @asyncio.coroutine
206 def apply_ns_config(self, agent_nsr, agent_vnfrs, config, rpc_ip):
207 """ Notification on configuration of an NSR """
208 pass
209
210 @abc.abstractmethod
211 @asyncio.coroutine
212 def notify_create_vlr(self, agent_nsr, vld):
213 """ Notification on creation of an VL """
214 pass
215
216 @abc.abstractmethod
217 @asyncio.coroutine
218 def notify_create_vnfr(self, agent_nsr, agent_vnfr):
219 """ Notification on creation of an VNFR """
220 pass
221
222 @abc.abstractmethod
223 @asyncio.coroutine
224 def notify_instantiate_vnfr(self, agent_nsr, agent_vnfr):
225 """ Notify instantiation of the virtual network function """
226 pass
227
228 @abc.abstractmethod
229 @asyncio.coroutine
230 def notify_instantiate_vlr(self, agent_nsr, vl):
231 """ Notify instantiate of the virtual link"""
232 pass
233
234 @abc.abstractmethod
235 @asyncio.coroutine
236 def notify_terminate_vnfr(self, agent_nsr, agent_vnfr):
237 """Notify termination of the VNF """
238 pass
239
240 @abc.abstractmethod
241 @asyncio.coroutine
242 def notify_terminate_vlr(self, agent_nsr, vlr):
243 """Notify termination of the Virtual Link Record"""
244 pass
245
246 @abc.abstractmethod
247 @asyncio.coroutine
248 def apply_initial_config(self, vnfr_id, vnf):
249 """Apply initial configuration"""
250 pass
251
252 @abc.abstractmethod
253 @asyncio.coroutine
254 def get_config_status(self, vnfr_id):
255 """Get the status for the VNF"""
256 pass
257
258 @abc.abstractmethod
259 def get_action_status(self, execution_id):
260 """Get the action exection status"""
261 pass
262
263 @abc.abstractmethod
264 @asyncio.coroutine
265 def vnf_config_primitive(self, nsr_id, vnfr_id, primitive, output):
266 """Apply config primitive on a VNF"""
267 pass
268
269 @abc.abstractmethod
270 def is_vnfr_managed(self, vnfr_id):
271 """ Check if VNR is managed by config agent """
272 pass
273
274 @abc.abstractmethod
275 def add_vnfr_managed(self, agent_vnfr):
276 """ Add VNR to be managed by this config agent """
277 pass
278
279 def get_service_status(self, vnfr_id):
280 """Get the status of the service"""
281 return None
282
283 @asyncio.coroutine
284 def invoke(self, method, *args):
285 try:
286 rc = None
287 self._log.debug("Config agent plugin: method {} with args {}: {}".
288 format(method, args, self))
289
290 # TBD - Do a better way than string compare to find invoke the method
291 if method == 'notify_create_nsr':
292 rc = yield from self.notify_create_nsr(args[0], args[1])
293 elif method == 'notify_create_vlr':
294 rc = yield from self.notify_create_vlr(args[0], args[1], args[2])
295 elif method == 'notify_create_vnfr':
296 rc = yield from self.notify_create_vnfr(args[0], args[1])
297 elif method == 'notify_instantiate_nsr':
298 rc = yield from self.notify_instantiate_nsr(args[0])
299 elif method == 'notify_instantiate_vnfr':
300 rc = yield from self.notify_instantiate_vnfr(args[0], args[1])
301 elif method == 'notify_instantiate_vlr':
302 rc = yield from self.notify_instantiate_vlr(args[0], args[1])
303 elif method == 'notify_nsr_active':
304 rc = yield from self.notify_nsr_active(args[0], args[1])
305 elif method == 'notify_terminate_nsr':
306 rc = yield from self.notify_terminate_nsr(args[0])
307 elif method == 'notify_terminate_vnfr':
308 rc = yield from self.notify_terminate_vnfr(args[0], args[1])
309 elif method == 'notify_terminate_vlr':
310 rc = yield from self.notify_terminate_vlr(args[0], args[1])
311 elif method == 'apply_initial_config':
312 rc = yield from self.apply_initial_config(args[0], args[1])
313 elif method == 'apply_config':
314 rc = yield from self.apply_config(args[0], args[1], args[2])
315 elif method == 'get_config_status':
316 rc = yield from self.get_config_status(args[0], args[1])
317 else:
318 self._log.error("Unknown method %s invoked on config agent plugin",
319 method)
320 except Exception as e:
321 self._log.error("Caught exception while invoking method: %s, Exception: %s", method, str(e))
322 raise
323 return rc