Code Coverage

Cobertura Coverage Report > osm_pla.server >

server.py

Trend

Classes100%
 
Lines82%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
server.py
100%
1/1
82%
109/133
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
server.py
82%
109/133
N/A

Source

osm_pla/server/server.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright 2020 ArctosLabs Scandinavia AB
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #    http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 # implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 1 import asyncio
20 1 import logging
21 1 import itertools
22 1 from pathlib import Path
23
24 1 import yaml
25 1 from osm_common import dbmemory, dbmongo, msglocal, msgkafka
26
27 1 from osm_pla.config.config import Config
28 1 from osm_pla.placement.mznplacement import MznPlacementConductor
29 1 from osm_pla.placement.mznplacement import NsPlacementDataFactory
30
31
32 1 class Server:
33 1     pil_price_list_file = Path('/placement/pil_price_list.yaml')
34 1     vnf_price_list_file = Path('/placement/vnf_price_list.yaml')
35
36 1     def __init__(self, config: Config, loop=None):
37 1         self.log = logging.getLogger("pla.server")
38 1         self.db = None
39 1         self.msgBus = None
40 1         self.config = config
41 1         self.loop = loop or asyncio.get_event_loop()
42
43 1         try:
44 1             if config.get('database', 'driver') == "mongo":
45 0                 self.db = dbmongo.DbMongo()
46 0                 self.db.db_connect(config.get('database'))
47 1             elif config.get('database', 'driver') == "memory":
48 1                 self.db = dbmemory.DbMemory()
49 1                 self.db.db_connect(config.get('database'))
50             else:
51 0                 raise Exception("Invalid configuration param '{}' at '[database]':'driver'".format(
52                     config.get('database', 'driver')))
53
54 1             if config.get('message', 'driver') == "local":
55 1                 self.msgBus = msglocal.MsgLocal()
56 0             elif config.get('message', 'driver') == "kafka":
57 0                 self.msgBus = msgkafka.MsgKafka()
58             else:
59 0                 raise Exception("Invalid message bus driver {}".format(
60                     config.get('message', 'driver')))
61 1             self.msgBus.loop = loop
62 1             self.msgBus.connect(config.get('message'))
63
64 0         except Exception as e:
65 0             self.log.exception("kafka setup error. Exception: {}".format(e))
66
67 1     def _get_nslcmop(self, nsdlcmop_id):
68         """
69         :param nsdlcmop_id:
70         :return: nslcmop from database corresponding to nslcmop_id
71         """
72 1         db_filter = {"_id": nsdlcmop_id}
73 1         nslcmop = self.db.get_one("nslcmops", db_filter)
74 1         return nslcmop
75
76 1     def _get_projects(self):
77         """
78         :return: project name to project id mapping
79         """
80 0         projects = self.db.get_list("projects")
81 0         return {project['_id']: project['name'] for project in projects}
82
83 1     def _get_nsd(self, nsd_id):
84         """
85         :param nsd_id:
86         :return: nsd from database corresponding to nsd_id
87         """
88 1         db_filter = {"_id": nsd_id}
89 1         return self.db.get_one("nsds", db_filter)
90
91 1     def _get_vim_accounts(self, vim_account_ids):
92         """
93         :param vim_account_ids: list of VIM account ids
94         :return: list of vim account entries from database corresponding to list in vim_accounts_id
95         """
96 1         db_filter = {"_id": vim_account_ids}
97 1         return self.db.get_list("vim_accounts", db_filter)
98
99 1     def _read_vnf_price_list(self, price_list_file_path):
100         """
101         read vnf price list configuration file
102         :param price_list_file_path:
103         :return:
104         """
105 1         with open(str(price_list_file_path)) as pl_fd:
106 1             price_list = yaml.safe_load_all(pl_fd)
107 1             return next(price_list)
108
109 1     def _price_list_with_project(self, price_list):
110         """
111         Figure out if this price list is with  project or not.
112         Note: to handle the unlikely event that a project is called 'prices' we do not simply check if 'prices'
113         is in the dict keys for a price list sequence but rather go down one step in the nesting
114         in which we either have
115         1) 'prices:{vim_url:...}' if prices are also per project, or
116         2) '{vim_url:...}' if prices are only per vim
117
118         :param price_list:
119         :return: True if project part of price list, else False
120         """
121 1         price_list_entry_keys = set(price_list[0].keys())
122 1         price_list_entry_keys.remove('vnfd')
123 1         pl_key = price_list_entry_keys.pop()
124 1         entry_to_check = price_list[0][pl_key][0].keys()
125 1         return True if 'prices' in entry_to_check else False
126
127 1     def _get_vnf_price_list(self, price_list_file_path, project_name=None):
128         """
129         read vnf price list configuration file, determine its type and reformat content accordingly
130
131         :param price_list_file_path:
132         :param project_name:
133         :return: dictionary formatted as {'<vnfd>': {'<vim-url>':'<price>'}}
134         """
135 1         price_list_data = self._read_vnf_price_list(price_list_file_path)
136 1         if self._price_list_with_project(price_list_data):
137 1             res = {}
138 1             for i in price_list_data:
139 1                 price_data = i[project_name] if type(i[project_name]) is dict else i[project_name][0]
140 1                 res_component = {i['vim_name']: i['price'] for i in price_data['prices']}
141 1                 res.update({i['vnfd']: res_component})
142 1             return res
143         else:
144 1             return {i['vnfd']: {i1['vim_name']: i1['price'] for i1 in i['prices']} for i in price_list_data}
145
146 1     def _get_pil_info(self, pil_info_file_path):
147         """
148         read and return pil information from file
149         :param pil_info_file_path: Path to pil_info file
150         :return pil configuration file content as Python object
151         """
152 1         with open(str(pil_info_file_path)) as pil_fd:
153 1             data = yaml.safe_load_all(pil_fd)
154 1             return next(data)
155
156 1     def _create_vnf_id_maps(self, nsd):
157         """
158         map identifier for 'member-vnf-index' in nsd to syntax that is safe for mzn
159
160          return tuples with mappings {<adjusted id>: <original id>} and {<original id>: <adjusted id>}
161         """
162         # TODO: Change for multiple DF support
163 1         ns_df = nsd.get('df', [{}])[0]
164 1         next_idx = itertools.count()
165 1         member_vnf_index2mzn = {e['id']: 'VNF' + str(next(next_idx)) for e in
166                                 ns_df.get('vnf-profile', [])}
167
168         # reverse the name map dictionary, used when the placement result is remapped
169 1         mzn_name2member_vnf_index = {v: k for k, v in member_vnf_index2mzn.items()}
170
171 1         return member_vnf_index2mzn, mzn_name2member_vnf_index
172
173 1     async def get_placement(self, nslcmop_id):
174         """
175         - Collects and prepares placement information.
176         - Request placement computation.
177         - Formats and distribute placement result
178
179         Note: exceptions result in empty response message
180
181         :param nslcmop_id:
182         :return:
183         """
184 1         try:
185 1             nslcmop = self._get_nslcmop(nslcmop_id)
186 1             nsd = self._get_nsd(nslcmop['operationParams']['nsdId'])
187 1             member_vnf_index2mzn, mzn2member_vnf_index = self._create_vnf_id_maps(nsd)
188             # adjust vnf identifiers
189             # TODO: Change for multiple DF support
190 1             ns_df = nsd.get('df', [{}])[0]
191 1             for vnf_profile in ns_df.get('vnf-profile', []):
192 1                 vnf_profile['id'] = member_vnf_index2mzn[vnf_profile['id']]
193 1                 for vlc in vnf_profile.get('virtual-link-connectivity', []):
194 1                     for ccpd in vlc.get('constituent-cpd-id', []):
195 1                         ccpd['constituent-base-element-id'] = member_vnf_index2mzn[ccpd['constituent-base-element-id']]
196 1             self.log.info("adjusted nsd: {}".format(nsd))
197 1             projects = self._get_projects()
198 1             self.log.info("projects: {}".format(projects))
199 1             nslcmop_project = nslcmop['_admin']['projects_read'][0]
200 1             self.log.info("nslcmop_project: {}".format(nslcmop_project))
201 1             valid_vim_accounts = nslcmop['operationParams']['validVimAccounts']
202 1             vim_accounts_data = self._get_vim_accounts(valid_vim_accounts)
203 1             vims_information = {_['name']: _['_id'] for _ in vim_accounts_data}
204 1             price_list = self._get_vnf_price_list(Server.vnf_price_list_file, projects[nslcmop_project])
205 1             pil_info = self._get_pil_info(Server.pil_price_list_file)
206 1             pinnings = nslcmop['operationParams'].get('vnf', [])
207             # remap member-vnf-index values according to id map
208 1             for pinning in pinnings:
209 0                 pinning['member-vnf-index'] = member_vnf_index2mzn[pinning['member-vnf-index']]
210 1             self.log.info("pinnings: {}".format(pinnings))
211 1             order_constraints = nslcmop['operationParams'].get('placement-constraints')
212 1             self.log.info("order constraints: {}".format(order_constraints))
213
214 1             nspd = NsPlacementDataFactory(vims_information,
215                                           price_list,
216                                           nsd,
217                                           pil_info,
218                                           pinnings, order_constraints).create_ns_placement_data()
219
220 1             vnf_placement = MznPlacementConductor(self.log).do_placement_computation(nspd)
221
222 1         except Exception as e:
223             # Note: there is no cure for failure so we have a catch-all clause here
224 1             self.log.exception("PLA fault. Exception: {}".format(e))
225 1             vnf_placement = []
226         finally:
227             # remap names in vnf_placement
228 1             for e in vnf_placement:
229 1                 e['member-vnf-index'] = mzn2member_vnf_index[e['member-vnf-index']]
230 1             await self.msgBus.aiowrite("pla", "placement",
231                                        {'placement': {'vnf': vnf_placement, 'nslcmopId': nslcmop_id}})
232
233 1     def handle_kafka_command(self, topic, command, params):
234 1         self.log.info("Kafka msg arrived: {} {} {}".format(topic, command, params))
235 1         if topic == "pla" and command == "get_placement":
236 1             nslcmop_id = params.get('nslcmopId')
237 1             self.loop.create_task(self.get_placement(nslcmop_id))
238
239 1     async def kafka_read(self):
240 0         self.log.info("Task kafka_read start")
241 0         while True:
242 0             try:
243 0                 topics = "pla"
244 0                 await self.msgBus.aioread(topics, self.loop, self.handle_kafka_command)
245 0             except Exception as e:
246 0                 self.log.error("kafka read error. Exception: {}".format(e))
247 0                 await asyncio.sleep(5, loop=self.loop)
248
249 1     def run(self):
250 0         self.loop.run_until_complete(self.kafka_read())
251 0         self.loop.close()
252 0         self.loop = None
253 0         if self.msgBus:
254 0             self.msgBus.disconnect()