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