1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
|
#
# -------------------------------------------------------------------------
# Copyright (c) 2015-2017 AT&T Intellectual Property
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -------------------------------------------------------------------------
#
import re
import time
import uuid
import copy
import json
from oslo_config import cfg
from oslo_log import log
from conductor.common import rest
from conductor.data.plugins import constants
from conductor.data.plugins.inventory_provider import base
from conductor.data.plugins.inventory_provider import hpa_utils
from conductor.data.plugins.triage_translator.triage_translator import TraigeTranslator
from conductor.i18n import _LE, _LI
from oslo_config import cfg
from oslo_log import log
LOG = log.getLogger(__name__)
CONF = cfg.CONF
AAI_OPTS = [
cfg.IntOpt('cache_refresh_interval',
default=1440,
help='Interval with which to refresh the local cache, '
'in minutes.'),
cfg.IntOpt('complex_cache_refresh_interval',
default=1440,
help='Interval with which to refresh the local complex cache, '
'in minutes.'),
cfg.StrOpt('table_prefix',
default='aai',
help='Data Store table prefix.'),
cfg.StrOpt('server_url',
default='https://controller:8443/aai',
help='Base URL for A&AI, up to and not including '
'the version, and without a trailing slash.'),
cfg.StrOpt('aai_rest_timeout',
default='30',
help='Timeout for A&AI Rest Call'),
cfg.StrOpt('aai_retries',
default='3',
help='Number of retry for A&AI Rest Call'),
cfg.StrOpt('server_url_version',
default='v10',
help='The version of A&AI in v# format.'),
cfg.StrOpt('certificate_file',
default='certificate.pem',
help='SSL/TLS certificate file in pem format. '
'This certificate must be registered with the A&AI '
'endpoint.'),
cfg.StrOpt('certificate_key_file',
default='certificate_key.pem',
help='Private Certificate Key file in pem format.'),
cfg.StrOpt('certificate_authority_bundle_file',
default='certificate_authority_bundle.pem',
help='Certificate Authority Bundle file in pem format. '
'Must contain the appropriate trust chain for the '
'Certificate file.'),
#TODO(larry): follow-up with ONAP people on this (AA&I basic auth username and password?)
cfg.StrOpt('username',
default='',
help='Username for AAI.'),
cfg.StrOpt('password',
default='',
help='Password for AAI.'),
]
CONF.register_opts(AAI_OPTS, group='aai')
class AAI(base.InventoryProviderBase):
"""Active and Available Inventory Provider"""
def __init__(self):
"""Initializer"""
# FIXME(jdandrea): Pass this in to init.
self.conf = CONF
self.base = self.conf.aai.server_url.rstrip('/')
self.version = self.conf.aai.server_url_version.rstrip('/')
self.cert = self.conf.aai.certificate_file
self.key = self.conf.aai.certificate_key_file
self.verify = self.conf.aai.certificate_authority_bundle_file
self.cache_refresh_interval = self.conf.aai.cache_refresh_interval
self.last_refresh_time = None
self.complex_cache_refresh_interval = \
self.conf.aai.complex_cache_refresh_interval
self.complex_last_refresh_time = None
self.timeout = self.conf.aai.aai_rest_timeout
self.retries = self.conf.aai.aai_retries
self.username = self.conf.aai.username
self.password = self.conf.aai.password
self.triage_translator=TraigeTranslator()
# Cache is initially empty
self._aai_cache = {}
self._aai_complex_cache = {}
def initialize(self):
"""Perform any late initialization."""
# Initialize the Python requests
self._init_python_request()
# Refresh the cache once for now
self._refresh_cache()
# TODO(jdandrea): Make this periodic, and without a True condition!
# executor = futurist.ThreadPoolExecutor()
# while True:
# fut = executor.submit(self.refresh_cache)
# fut.result()
#
# # Now wait for the next time.
# # FIXME(jdandrea): Put inside refresh_cache()?
# refresh_interval = self.conf.aai.cache_refresh_interval
# time.sleep(refresh_interval)
# executor.shutdown()
def name(self):
"""Return human-readable name."""
return "A&AI"
def _get_version_from_string(self, string):
"""Extract version number from string"""
return re.sub("[^0-9.]", "", string)
def _aai_versioned_path(self, path):
"""Return a URL path with the A&AI version prepended"""
return '/{}/{}'.format(self.version, path.lstrip('/'))
def _request(self, method='get', path='/', data=None,
context=None, value=None):
"""Performs HTTP request."""
headers = {
'X-FromAppId': 'CONDUCTOR',
'X-TransactionId': str(uuid.uuid4()),
}
kwargs = {
"method": method,
"path": path,
"headers": headers,
"data": data,
}
# TODO(jdandrea): Move timing/response logging into the rest helper?
start_time = time.time()
response = self.rest.request(**kwargs)
elapsed = time.time() - start_time
LOG.debug("Total time for A&AI request "
"({0:}: {1:}): {2:.3f} sec".format(context, value, elapsed))
if response is None:
LOG.error(_LE("No response from A&AI ({}: {})").
format(context, value))
elif response.status_code != 200:
LOG.error(_LE("A&AI request ({}: {}) returned HTTP "
"status {} {}, link: {}{}").
format(context, value,
response.status_code, response.reason,
self.base, path))
return response
def _init_python_request(self):
kwargs = {
"server_url": self.base,
"retries": self.retries,
"username": self.username,
"password": self.password,
"cert_file": self.cert,
"cert_key_file": self.key,
"ca_bundle_file": self.verify,
"log_debug": self.conf.debug,
"read_timeout": self.timeout,
}
self.rest = rest.REST(**kwargs)
def _refresh_cache(self):
"""Refresh the A&AI cache."""
if not self.last_refresh_time or \
(time.time() - self.last_refresh_time) > \
self.cache_refresh_interval * 60:
# TODO(jdandrea): This is presently brute force.
# It does not persist to Music. A general purpose ORM caching
# object likely needs to be made, with a key (hopefully we
# can use one that is not just a UUID), a value, and a
# timestamp. The other alternative is to not use the ORM
# layer and call the API directly, but that is
# also trading one set of todos for another ...
# Get all A&AI sites
LOG.info(_LI("**** Refreshing A&AI cache *****"))
path = self._aai_versioned_path(
'/cloud-infrastructure/cloud-regions/?depth=0')
response = self._request(
path=path, context="cloud regions", value="all")
if response is None:
return
regions = {}
if response.status_code == 200:
body = response.json()
regions = body.get('cloud-region', {})
if not regions:
# Nothing to update the cache with
LOG.error(_LE("A&AI returned no regions, link: {}{}").
format(self.base, path))
return
cache = {
'cloud_region': {},
'service': {},
}
for region in regions:
cloud_region_id = region.get('cloud-region-id')
LOG.debug("Working on region '{}' ".format(cloud_region_id))
cloud_region_version = region.get('cloud-region-version')
cloud_owner = region.get('cloud-owner')
cloud_type = region.get('cloud-type')
cloud_zone = region.get('cloud-zone')
physical_location_list = self._get_aai_rel_link_data(data = region, related_to = 'complex', search_key = 'complex.physical-location-id')
if len(physical_location_list) > 0:
physical_location_id = physical_location_list[0].get('d_value')
if not (cloud_region_version and
cloud_region_id):
continue
rel_link_data_list = \
self._get_aai_rel_link_data(
data=region,
related_to='complex',
search_key='complex.physical-location-id')
if len(rel_link_data_list) > 1:
LOG.error(_LE("Region {} has more than one complex").
format(cloud_region_id))
LOG.debug("Region {}: {}".format(cloud_region_id, region))
continue
rel_link_data = rel_link_data_list[0]
complex_id = rel_link_data.get("d_value")
complex_link = rel_link_data.get("link")
if complex_id and complex_link:
complex_info = self._get_complex(
complex_link=complex_link,
complex_id=complex_id)
else: # no complex information
LOG.error(_LE("Region {} does not reference a complex").
format(cloud_region_id))
continue
if not complex_info:
LOG.error(_LE("Region {}, complex {} info not found, "
"link {}").format(cloud_region_id,
complex_id, complex_link))
continue
latitude = complex_info.get('latitude')
longitude = complex_info.get('longitude')
city = complex_info.get('city')
state = complex_info.get('state')
region = complex_info.get('region')
country = complex_info.get('country')
complex_name = complex_info.get('complex-name')
if not (latitude and longitude and city and country
and complex_name):
keys = ('latitude', 'longitude', 'city', 'country',
'complex_name')
missing_keys = \
list(set(keys).difference(complex_info.keys()))
LOG.error(_LE("Complex {} is missing {}, link: {}").
format(complex_id, missing_keys, complex_link))
LOG.debug("Complex {}: {}".
format(complex_id, complex_info))
continue
cache['cloud_region'][cloud_region_id] = {
'cloud_region_version': cloud_region_version,
'cloud_owner': cloud_owner,
'cloud_type': cloud_type,
'cloud_zone': cloud_zone,
'complex_name': complex_name,
'physical_location_id': physical_location_id,
'complex': {
'complex_id': complex_id,
'complex_name': complex_name,
'latitude': latitude,
'longitude': longitude,
'city': city,
'state': state,
'region': region,
'country': country,
}
}
# Added for HPA support
if self.conf.HPA_enabled:
flavors = self._get_flavors(cloud_owner, cloud_region_id)
cache['cloud_region'][cloud_region_id]['flavors'] = flavors
LOG.debug("Candidate with cloud_region_id '{}' selected "
"as a potential candidate - ".format(cloud_region_id))
LOG.debug("Done with region '{}' ".format(cloud_region_id))
self._aai_cache = cache
self.last_refresh_time = time.time()
LOG.info(_LI("**** A&AI cache refresh complete *****"))
@staticmethod
def _get_aai_rel_link(data, related_to):
"""Given an A&AI data structure, return the related-to link"""
rel_dict = data.get('relationship-list')
if rel_dict:
for key, rel_list in rel_dict.items():
for rel in rel_list:
if related_to == rel.get('related-to'):
return rel.get('related-link')
@staticmethod
def _get_aai_rel_link_data(data, related_to, search_key=None,
match_dict=None):
# some strings that we will encounter frequently
rel_lst = "relationship-list"
rkey = "relationship-key"
rval = "relationship-value"
rdata = "relationship-data"
response = list()
if match_dict:
m_key = match_dict.get('key')
m_value = match_dict.get('value')
else:
m_key = None
m_value = None
rel_dict = data.get(rel_lst)
if rel_dict: # check if data has relationship lists
for key, rel_list in rel_dict.items():
for rel in rel_list:
if rel.get("related-to") == related_to:
dval = None
matched = False
link = rel.get("related-link")
r_data = rel.get(rdata, [])
if search_key:
for rd in r_data:
if rd.get(rkey) == search_key:
dval = rd.get(rval)
if not match_dict: # return first match
response.append(
{"link": link, "d_value": dval}
)
break # go to next relation
if rd.get(rkey) == m_key \
and rd.get(rval) == m_value:
matched = True
if match_dict and matched: # if matching required
response.append(
{"link": link, "d_value": dval}
)
# matched, return search value corresponding
# to the matched r_data group
else: # no search key; just return the link
response.append(
{"link": link, "d_value": dval}
)
if len(response) == 0:
response.append(
{"link": None, "d_value": None}
)
return response
def _get_complex(self, complex_link, complex_id=None):
if not self.complex_last_refresh_time or \
(time.time() - self.complex_last_refresh_time) > \
self.complex_cache_refresh_interval * 60:
self._aai_complex_cache.clear()
if complex_id and complex_id in self._aai_complex_cache:
return self._aai_complex_cache[complex_id]
else:
path = self._aai_versioned_path(
self._get_aai_path_from_link(complex_link))
response = self._request(
path=path, context="complex", value=complex_id)
if response is None:
return
if response.status_code == 200:
complex_info = response.json()
if 'complex' in complex_info:
complex_info = complex_info.get('complex')
latitude = complex_info.get('latitude')
longitude = complex_info.get('longitude')
city = complex_info.get('city')
country = complex_info.get('country')
# removed the state check for countries in Europe
# that do not always enter states
if not (latitude and longitude and city and country):
keys = ('latitude', 'longitude', 'city', 'country')
missing_keys = \
list(set(keys).difference(complex_info.keys()))
LOG.error(_LE("Complex {} is missing {}, link: {}").
format(complex_id, missing_keys, complex_link))
LOG.debug("Complex {}: {}".
format(complex_id, complex_info))
return
if complex_id: # cache only if complex_id is given
self._aai_complex_cache[complex_id] = response.json()
self.complex_last_refresh_time = time.time()
return complex_info
def _get_regions(self):
self._refresh_cache()
regions = self._aai_cache.get('cloud_region', {})
return regions
def _get_flavors(self, cloud_owner, cloud_region_id):
'''
Fetch all flavors of a given cloud regions specified using
{cloud-owner}/{cloud-region-id} composite key
:return flavors_info json object which list of flavor nodes and
its children - HPACapabilities:
'''
LOG.debug("Fetch all flavors and its child nodes HPACapabilities")
flavor_path = constants.FLAVORS_URI % (cloud_owner, cloud_region_id)
path = self._aai_versioned_path(flavor_path)
LOG.debug("Flavors path '{}' ".format(path))
response = self._request(path=path, context="flavors", value="all")
if response is None:
return
if response.status_code == 200:
flavors_info = response.json()
if not flavors_info or not flavors_info["flavor"] or \
len(flavors_info["flavor"]) == 0:
LOG.error(_LE("Flavor is missing in Cloud-Region {}/{}").
format(cloud_owner, cloud_region_id))
return
LOG.debug(flavors_info)
# Remove extraneous flavor information
return flavors_info
else:
LOG.error(_LE("Received Error while fetching flavors from" \
"Cloud-region {}/{}").format(cloud_owner,
cloud_region_id))
return
def _get_aai_path_from_link(self, link):
path = link.split(self.version, 1)
if not path or len(path) <= 1:
# TODO(shankar): Treat this as a critical error?
LOG.error(_LE("A&AI version {} not found in link {}").
format(self.version, link))
else:
return "{}".format(path[1])
def check_candidate_role(self, host_id=None):
vnf_name_uri = '/network/generic-vnfs/?vnf-name=' + host_id + '&depth=0'
path = self._aai_versioned_path(vnf_name_uri)
response = self._request('get', path=path, data=None,
context="vnf name")
if response is None or not response.ok:
return None
body = response.json()
generic_vnf = body.get("generic-vnf", [])
for vnf in generic_vnf:
related_to = "service-instance"
search_key = "customer.global-customer-id"
match_key = "customer.global-customer-id"
rl_data_list = self._get_aai_rel_link_data(
data=vnf,
related_to=related_to,
search_key=search_key,
)
if len(rl_data_list) != 1:
return None
rl_data = rl_data_list[0]
candidate_role_link = rl_data.get("link")
if not candidate_role_link:
LOG.error(_LE("Unable to get candidate role link for host id {} ").format(host_id))
return None
candidate_role_path = self._get_aai_path_from_link(candidate_role_link) + '/allotted-resources?depth=all'
path = self._aai_versioned_path(candidate_role_path)
response = self._request('get', path=path, data=None,
context="candidate role")
if response is None or not response.ok:
return None
body = response.json()
response_items = body.get('allotted-resource')
if len(response_items) > 0:
role = response_items[0].get('role')
return role
def check_network_roles(self, network_role_id=None):
# the network role query from A&AI is not using
# the version number in the query
network_role_uri = \
'/network/l3-networks?network-role=' + network_role_id
path = self._aai_versioned_path(network_role_uri)
# This UUID is reserved by A&AI for a Conductor-specific named query.
named_query_uid = "role-UUID"
data = {
"query-parameters": {
"named-query": {
"named-query-uuid": named_query_uid
}
},
"instance-filters": {
"instance-filter": [
{
"l3-network": {
"network-role": network_role_id
}
}
]
}
}
region_ids = set()
response = self._request('get', path=path, data=data,
context="role", value=network_role_id)
if response is None:
return None
body = response.json()
response_items = body.get('l3-network', [])
for item in response_items:
cloud_region_instances = self._get_aai_rel_link_data(
data=item,
related_to='cloud-region',
search_key='cloud-region.cloud-region-id'
)
if len(cloud_region_instances) > 0:
for r_instance in cloud_region_instances:
region_id = r_instance.get('d_value')
if region_id is not None:
region_ids.add(region_id)
# return region ids that fit the role
return region_ids
def resolve_host_location(self, host_name):
path = self._aai_versioned_path('/query?format=id')
data = {"start": ["network/pnfs/pnf/" + host_name,
"cloud-infrastructure/pservers/pserver/" + host_name],
"query": "query/ucpe-instance"
}
response = self._request('put', path=path, data=data,
context="host name", value=host_name)
if response is None or response.status_code != 200:
return None
body = response.json()
results = body.get('results', [])
complex_link = None
for result in results:
if "resource-type" in result and \
"resource-link" in result and \
result["resource-type"] == "complex":
complex_link = result["resource-link"]
if not complex_link:
LOG.error(_LE("Unable to get a complex link for hostname {} "
" in response {}").format(host_name, response))
return None
complex_info = self._get_complex(
complex_link=complex_link,
complex_id=None
)
if complex_info:
lat = complex_info.get('latitude')
lon = complex_info.get('longitude')
country = complex_info.get('country')
if lat and lon:
location = {"latitude": lat, "longitude": lon}
location["country"] = country if country else None
return location
else:
LOG.error(_LE("Unable to get a latitude and longitude "
"information for hostname {} from complex "
" link {}").format(host_name, complex_link))
return None
else:
LOG.error(_LE("Unable to get a complex information for "
" hostname {} from complex "
" link {}").format(host_name, complex_link))
return None
def resolve_clli_location(self, clli_name):
clli_uri = '/cloud-infrastructure/complexes/complex/' + clli_name
path = self._aai_versioned_path(clli_uri)
response = self._request('get', path=path, data=None,
context="clli name", value=clli_name)
if response is None or response.status_code != 200:
return None
body = response.json()
if body:
lat = body.get('latitude')
lon = body.get('longitude')
country = body.get('country')
if lat and lon:
location = {"latitude": lat, "longitude": lon}
location["country"] = country if country else None
return location
else:
LOG.error(_LE("Unable to get a latitude and longitude "
"information for CLLI code {} from complex").
format(clli_name))
return None
else:
LOG.error(_LE("Unable to get a complex information for "
" clli {} from complex "
" link {}").format(clli_name, clli_uri))
return None
def get_inventory_group_pairs(self, service_description):
pairs = list()
path = self._aai_versioned_path(
'/network/instance-groups/?description={}&depth=0'.format(
service_description))
response = self._request(path=path, context="inventory group",
value=service_description)
if response is None or response.status_code != 200:
return
body = response.json()
if "instance-group" not in body:
LOG.error(_LE("Unable to get instance groups from inventory "
" in response {}").format(response))
return
for instance_groups in body["instance-group"]:
s_instances = self._get_aai_rel_link_data(
data=instance_groups,
related_to='service-instance',
search_key='service-instance.service-instance-id'
)
if s_instances and len(s_instances) == 2:
pair = list()
for s_inst in s_instances:
pair.append(s_inst.get('d_value'))
pairs.append(pair)
else:
LOG.error(_LE("Number of instance pairs not found to "
"be two: {}").format(instance_groups))
return pairs
def _log_multiple_item_error(self, name, service_type,
related_to, search_key='',
context=None, value=None):
"""Helper method to log multiple-item errors
Used by resolve_demands
"""
LOG.error(_LE("Demand {}, role {} has more than one {} ({})").
format(name, service_type, related_to, search_key))
if context and value:
LOG.debug("{} details: {}".format(context, value))
def check_sriov_automation(self, aic_version, demand_name, candidate_name):
"""Check if specific candidate has SRIOV automation available or not
Used by resolve_demands
"""
if aic_version:
LOG.debug(_LI("Demand {}, candidate {} has an AIC version "
"number {}").format(demand_name, candidate_name,
aic_version)
)
if aic_version == "X.Y":
return True
return False
def match_candidate_attribute(self, candidate, attribute_name,
restricted_value, demand_name,
inventory_type):
"""Check if specific candidate attribute matches the restricted value
Used by resolve_demands
"""
if restricted_value and \
restricted_value is not '' and \
candidate[attribute_name] != restricted_value:
LOG.info(_LI("Demand: {} "
"Discarded {} candidate as "
"it doesn't match the "
"{} attribute "
"{} ").format(demand_name,
inventory_type,
attribute_name,
restricted_value
)
)
return True
return False
def match_vserver_attribute(self, vserver_list):
value = None
for i in range(0, len(vserver_list)):
if value and \
value != vserver_list[i].get('d_value'):
return False
value = vserver_list[i].get('d_value')
return True
def match_inventory_attributes(self, template_attributes,
inventory_attributes, candidate_id):
for attribute_key, attribute_values in template_attributes.items():
if attribute_key and (attribute_key == 'service-type' or
attribute_key == 'equipment-role' or
attribute_key == 'model-invariant-id' or
attribute_key == 'model-version-id'):
continue
match_type = 'any'
if type(attribute_values) is dict:
if 'any' in attribute_values:
attribute_values = attribute_values['any']
elif 'not' in attribute_values:
match_type = 'not'
attribute_values = attribute_values['not']
if match_type == 'any':
if attribute_key not in inventory_attributes or \
(len(attribute_values) > 0 and
inventory_attributes[attribute_key] not in attribute_values):
return False
elif match_type == 'not':
# drop the candidate when
# 1). field exists in AAI and 2). value is not null or empty 3). value is one of those in the 'not' list
# Remember, this means if the property is not returned at all from AAI, that still can be a candidate.
if attribute_key in inventory_attributes and \
inventory_attributes[attribute_key] and \
inventory_attributes[attribute_key] in attribute_values:
return False
return True
def first_level_service_call(self, path, name, service_type):
response = self._request(
path=path, context="demand, GENERIC-VNF role",
value="{}, {}".format(name, service_type))
if response is None or response.status_code != 200:
return list() # move ahead with next requirement
body = response.json()
return body.get("generic-vnf", [])
def resolove_v_server_for_candidate(self, candidate, vs_link, add_interfaces, demand_name, triage_translator_data):
if not vs_link:
LOG.error(_LE("{} VSERVER link information not "
"available from A&AI").format(demand_name))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="VSERVER link information not")
return None # move ahead with the next vnf
if add_interfaces:
vs_link = vs_link + '?depth=2'
vs_path = self._get_aai_path_from_link(vs_link)
if not vs_path:
LOG.error(_LE("{} VSERVER path information not "
"available from A&AI - {}").
format(demand_name, vs_path))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="VSERVER path information not available from A&AI")
return None # move ahead with the next vnf
path = self._aai_versioned_path(vs_path)
response = self._request(
path=path, context="demand, VSERVER",
value="{}, {}".format(demand_name, vs_path))
if response is None or response.status_code != 200:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason=response.status_code)
return None
return response.json()
def resolve_vf_modules_for_generic_vnf(self, candidate, vnf, demand_name, triage_translator_data):
raw_path = '/network/generic-vnfs/generic-vnf/{}?depth=1'.format(vnf.get("vnf-id"))
path = self._aai_versioned_path(raw_path)
response = self._request('get', path=path, data=None)
if response is None or response.status_code != 200:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason=response)
return None
generic_vnf_details = response.json()
if generic_vnf_details is None or not generic_vnf_details.get('vf-modules') \
or not generic_vnf_details.get('vf-modules').get('vf-module'):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Generic-VNF No detailed data for VF-modules")
return None
else:
return generic_vnf_details.get('vf-modules').get('vf-module')
def resolve_cloud_regions_by_cloud_region_id(self, cloud_region_id):
cloud_region_uri = '/cloud-infrastructure/cloud-regions' \
'/?cloud-region-id=' \
+ cloud_region_id
path = self._aai_versioned_path(cloud_region_uri)
response = self._request('get',
path=path,
data=None)
if response is None or response.status_code != 200:
return None
body = response.json()
return body.get('cloud-region', [])
def assign_candidate_existing_placement(self, candidate, existing_placement):
"""Assign existing_placement and cost parameters to candidate
Used by resolve_demands
"""
candidate['existing_placement'] = 'false'
if existing_placement:
if existing_placement.get('candidate_id') == candidate['candidate_id']:
candidate['cost'] = self.conf.data.existing_placement_cost
candidate['existing_placement'] = 'true'
def resovle_conflict_id(self, conflict_identifier, candidate):
# Initialize the conflict_id_list
conflict_id_list = list()
# conflict_id is separated by pipe (|)
separator = '|'
for conflict_element in conflict_identifier:
# if the conflict_element is a dictionary with key = 'get_candidate_attribute',
# then add candidate's coressponding value to conflict_id string
if isinstance(conflict_element, dict) and 'get_candidate_attribute' in conflict_element:
attribute_name = conflict_element.get('get_candidate_attribute')
conflict_id_list.append(candidate[attribute_name] + separator)
elif isinstance(conflict_element, unicode):
conflict_id_list.append(conflict_element + separator)
return ''.join(conflict_id_list)
def resolve_v_server_links_for_vnf(self, vnf):
related_to = "vserver"
search_key = "cloud-region.cloud-owner"
rl_data_list = self._get_aai_rel_link_data(
data=vnf, related_to=related_to,
search_key=search_key)
vs_link_list = list()
for i in range(0, len(rl_data_list)):
vs_link_list.append(rl_data_list[i].get('link'))
return vs_link_list
def resolve_complex_info_link_for_v_server(self, candidate, v_server, cloud_owner, cloud_region_id, service_type,
demand_name, triage_translator_data):
related_to = "pserver"
rl_data_list = self._get_aai_rel_link_data(
data=v_server,
related_to=related_to,
search_key=None
)
if len(rl_data_list) > 1:
self._log_multiple_item_error(
demand_name, service_type, related_to, "item",
"VSERVER", v_server)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data, reason="item VSERVER")
return None
rl_data = rl_data_list[0]
ps_link = rl_data.get('link')
# Third level query to get cloud region from pserver
if not ps_link:
LOG.error(_LE("{} pserver related link "
"not found in A&AI: {}").
format(demand_name, rl_data))
# if HPA_feature is disabled
if not self.conf.HPA_enabled:
# Triage Tool Feature Changes
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="ps link not found")
return None
else:
if not (cloud_owner and cloud_region_id):
LOG.error("{} cloud-owner or cloud-region not "
"available from A&AI".
format(demand_name))
# Triage Tool Feature Changes
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Cloud owner and cloud region "
"id not found")
return None # move ahead with the next vnf
cloud_region_uri = \
'/cloud-infrastructure/cloud-regions/cloud-region' \
'/?cloud-owner=' + cloud_owner \
+ '&cloud-region-id=' + cloud_region_id
path = self._aai_versioned_path(cloud_region_uri)
response = self._request('get',
path=path,
data=None)
if response is None or response.status_code != 200:
# Triage Tool Feature Changes
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason=response)
return None
body = response.json()
else:
ps_path = self._get_aai_path_from_link(ps_link)
if not ps_path:
LOG.error(_LE("{} pserver path information "
"not found in A&AI: {}").
format(demand_name, ps_link))
# Triage Tool Feature Changes
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="ps path not found")
return None # move ahead with the next vnf
path = self._aai_versioned_path(ps_path)
response = self._request(
path=path, context="PSERVER", value=ps_path)
if response is None or response.status_code != 200:
# Triage Tool Feature Changes
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason=response)
return None
body = response.json()
related_to = "complex"
search_key = "complex.physical-location-id"
rl_data_list = self._get_aai_rel_link_data(
data=body,
related_to=related_to,
search_key=search_key
)
if len(rl_data_list) > 1:
if not self.match_vserver_attribute(rl_data_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key, "PSERVER", body)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason="PSERVER error")
return None
return rl_data_list[0]
def resolve_cloud_region_id_and_version_for_vnf(self, candidate, vnf, service_type, demand_name,
triage_translator_data):
related_to = "vserver"
search_key = "cloud-region.cloud-region-id"
rl_data_list = self._get_aai_rel_link_data(
data=vnf,
related_to=related_to,
search_key=search_key
)
if len(rl_data_list) > 1:
if not self.match_vserver_attribute(rl_data_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key,
"VNF", vnf)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="VNF error")
return None, None
cloud_region_rl_data = rl_data_list[0]
cloud_region_id = cloud_region_rl_data.get('d_value')
# get version for service candidate
cloud_region_version_rl_data = {'d_value': ''}
if cloud_region_id:
regions = self.resolve_cloud_regions_by_cloud_region_id(cloud_region_id)
if regions is None:
return cloud_region_rl_data, None
for region in regions:
if "cloud-region-version" in region:
cloud_region_version_rl_data['d_value'] = self._get_version_from_string(region["cloud-region-version"])
return cloud_region_rl_data, cloud_region_version_rl_data
def resolve_cloud_owner_for_vnf(self, candidate, vnf, service_type, demand_name, triage_translator_data):
related_to = "vserver"
search_key = "cloud-region.cloud-owner"
rl_data_list = self._get_aai_rel_link_data(
data=vnf, related_to=related_to,
search_key=search_key)
if len(rl_data_list) > 1:
if not self.match_vserver_attribute(rl_data_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key,
"VNF", vnf)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="VNF error")
return None
return rl_data_list[0]
def resolve_global_customer_id_for_vnf(self, candidate, vnf, customer_id, service_type, demand_name,
triage_translator_data):
related_to = "service-instance"
search_key = "customer.global-customer-id"
match_key = "customer.global-customer-id"
rl_data_list = self._get_aai_rel_link_data(
data=vnf,
related_to=related_to,
search_key=search_key,
match_dict={'key': match_key,
'value': customer_id}
)
if len(rl_data_list) > 1:
if not self.match_vserver_attribute(rl_data_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key, "VNF", vnf)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason=" match_vserver_attribute generic-vnf")
return None
return rl_data_list[0]
def resolve_service_instance_id_for_vnf(self, candidate, vnf, customer_id, service_type, demand_name,
triage_translator_data):
related_to = "service-instance"
search_key = "service-instance.service-instance-id"
match_key = "customer.global-customer-id"
rl_data_list = self._get_aai_rel_link_data(
data=vnf,
related_to=related_to,
search_key=search_key,
match_dict={'key': match_key,
'value': customer_id}
)
if len(rl_data_list) > 1:
if not self.match_vserver_attribute(rl_data_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key, "VNF", vnf)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason="multiple_item_error generic-vnf")
return None
return rl_data_list[0]
def build_complex_info_for_candidate(self, candidate, vnf, complex_list, service_type, demand_name,
triage_translator_data):
if not complex_list or \
len(complex_list) < 1:
LOG.error("Complex information not "
"available from A&AI")
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Complex information not available from A&AI")
return
# In the scenario where no pserver information is available
# assumption here is that cloud-region does not span across
# multiple complexes
if len(complex_list) > 1:
related_to = "complex"
search_key = "complex.physical-location-id"
if not self.match_vserver_attribute(complex_list):
self._log_multiple_item_error(
demand_name, service_type, related_to, search_key,
"VNF", vnf)
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Generic-vnf error")
return
rl_data = complex_list[0]
complex_link = rl_data.get('link')
complex_id = rl_data.get('d_value')
# Final query for the complex information
if not (complex_link and complex_id):
LOG.debug("{} complex information not "
"available from A&AI - {}".
format(demand_name, complex_link))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Complex information not available from A&AI")
return # move ahead with the next vnf
else:
complex_info = self._get_complex(
complex_link=complex_link,
complex_id=complex_id
)
if not complex_info:
LOG.debug("{} complex information not "
"available from A&AI - {}".
format(demand_name, complex_link))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], demand_name,
triage_translator_data,
reason="Complex information not available from A&AI")
return # move ahead with the next vnf
candidate['physical_location_id'] = \
complex_id
candidate['complex_name'] = \
complex_info.get('complex-name')
candidate['latitude'] = \
complex_info.get('latitude')
candidate['longitude'] = \
complex_info.get('longitude')
candidate['state'] = \
complex_info.get('state')
candidate['country'] = \
complex_info.get('country')
candidate['city'] = \
complex_info.get('city')
candidate['region'] = \
complex_info.get('region')
def resolve_demands(self, demands, plan_info, triage_translator_data):
"""Resolve demands into inventory candidate lists"""
self.triage_translator.getPlanIdNAme(plan_info['plan_name'], plan_info['plan_id'],triage_translator_data)
resolved_demands = {}
for name, requirements in demands.items():
self.triage_translator.addDemandsTriageTranslator(name, triage_translator_data)
resolved_demands[name] = []
for requirement in requirements:
inventory_type = requirement.get('inventory_type').lower()
service_subscription = requirement.get('service_subscription')
candidate_uniqueness = requirement.get('unique', 'true')
filtering_attributes = requirement.get('filtering_attributes')
passthrough_attributes = requirement.get('passthrough_attributes')
#TODO: may need to support multiple service_type and customer_id in the futrue
#TODO: make it consistent for dash and underscore
if filtering_attributes:
# catch equipment-role and service-type from template
equipment_role = filtering_attributes.get('equipment-role')
service_type = filtering_attributes.get('service-type')
if equipment_role:
service_type = equipment_role
# catch global-customer-id and customer-id from template
global_customer_id = filtering_attributes.get('global-customer-id')
customer_id = filtering_attributes.get('customer-id')
if global_customer_id:
customer_id = global_customer_id
model_invariant_id = filtering_attributes.get('model-invariant-id')
model_version_id = filtering_attributes.get('model-version-id')
service_role = filtering_attributes.get('service-role')
# For earlier
else:
service_type = equipment_role = requirement.get('service_type')
customer_id = global_customer_id = requirement.get('customer_id')
# region_id is OPTIONAL. This will restrict the initial
# candidate set to come from the given region id
restricted_region_id = requirement.get('region')
restricted_complex_id = requirement.get('complex')
# Used for order locking feature
# by defaut, conflict id is the combination of candidate id, service type and vnf-e2e-key
conflict_identifier = requirement.get('conflict_identifier')
# VLAN fields
vlan_key = requirement.get('vlan_key')
port_key = requirement.get('port_key')
# get required candidates from the demand
required_candidates = requirement.get("required_candidates")
# get existing_placement from the demand
existing_placement = requirement.get("existing_placement")
if required_candidates:
resolved_demands['required_candidates'] = \
required_candidates
# get excluded candidate from the demand
excluded_candidates = requirement.get("excluded_candidates")
# service_resource_id is OPTIONAL and is
# transparent to Conductor
service_resource_id = requirement.get('service_resource_id') \
if requirement.get('service_resource_id') else ''
# add all the candidates of cloud type
if inventory_type == 'cloud':
# load region candidates from cache
regions = self._get_regions()
if not regions or len(regions) < 1:
LOG.debug("Region information is not "
"available in cache")
for region_id, region in regions.items():
# Pick only candidates from the restricted_region
candidate = dict()
candidate['inventory_provider'] = 'aai'
candidate['service_resource_id'] = service_resource_id
candidate['inventory_type'] = 'cloud'
candidate['candidate_id'] = region_id
candidate['location_id'] = region_id
candidate['location_type'] = 'att_aic'
candidate['cost'] = self.conf.data.cloud_candidate_cost
candidate['cloud_region_version'] = \
self._get_version_from_string(
region['cloud_region_version'])
candidate['cloud_owner'] = \
region['cloud_owner']
candidate['physical_location_id'] = \
region['complex']['complex_id']
candidate['complex_name'] = \
region['complex']['complex_name']
candidate['latitude'] = \
region['complex']['latitude']
candidate['longitude'] = \
region['complex']['longitude']
candidate['city'] = \
region['complex']['city']
candidate['state'] = \
region['complex']['state']
candidate['region'] = \
region['complex']['region']
candidate['country'] = \
region['complex']['country']
candidate['uniqueness'] = candidate_uniqueness
# Added vim-id for short-term workaround
if self.conf.HPA_enabled:
candidate['vim-id'] = \
region['cloud_owner'] + '_' + region_id
# Added for HPA
candidate['flavors'] = \
region['flavors']
if self.check_sriov_automation(
candidate['cloud_region_version'], name,
candidate['candidate_id']):
candidate['sriov_automation'] = 'true'
else:
candidate['sriov_automation'] = 'false'
cloud_region_attr = dict()
cloud_region_attr['cloud-owner'] = region['cloud_owner']
cloud_region_attr['cloud-region-version'] = region['cloud_region_version']
cloud_region_attr['cloud-type'] = region['cloud_type']
cloud_region_attr['cloud-zone'] = region['cloud_zone']
cloud_region_attr['complex-name'] = region['complex_name']
cloud_region_attr['physical-location-id'] = region['physical_location_id']
if filtering_attributes and (not self.match_inventory_attributes(filtering_attributes, cloud_region_attr, candidate['candidate_id'])):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason='attributes and match invetory attributes')
continue
if conflict_identifier:
candidate['conflict_id'] = self.resovle_conflict_id(conflict_identifier, candidate)
if not self.match_region(candidate, restricted_region_id, restricted_complex_id, name,
triage_translator_data):
continue
self.assign_candidate_existing_placement(candidate, existing_placement)
# Pick only candidates not in the excluded list
# if excluded candidate list is provided
if excluded_candidates and self.match_candidate_by_list(candidate, excluded_candidates, True, name, triage_translator_data):
continue
# Pick only candidates in the required list
# if required candidate list is provided
if required_candidates and not self.match_candidate_by_list(candidate, required_candidates, False, name, triage_translator_data):
continue
self.add_passthrough_attributes(candidate, passthrough_attributes, name, triage_translator_data)
# add candidate to demand candidates
resolved_demands[name].append(candidate)
LOG.debug(">>>>>>> Candidate <<<<<<<")
LOG.debug(json.dumps(candidate, indent=4))
elif (inventory_type == 'service') \
and customer_id:
# First level query to get the list of generic vnfs
vnf_by_model_invariant = list()
if filtering_attributes and model_invariant_id:
raw_path = '/network/generic-vnfs/' \
'?model-invariant-id={}&depth=0'.format(model_invariant_id)
if model_version_id:
raw_path = '/network/generic-vnfs/' \
'?model-invariant-id={}&model-version-id={}&depth=0'.format(model_invariant_id, model_version_id)
path = self._aai_versioned_path(raw_path)
vnf_by_model_invariant = self.first_level_service_call(path, name, service_type)
vnf_by_service_type = list()
if service_type or equipment_role:
path = self._aai_versioned_path(
'/network/generic-vnfs/'
'?equipment-role={}&depth=0'.format(service_type))
vnf_by_service_type = self.first_level_service_call(path, name, service_type)
generic_vnf = vnf_by_model_invariant + vnf_by_service_type
vnf_dict = dict()
for vnf in generic_vnf:
# if this vnf already appears, skip it
vnf_id = vnf.get('vnf-id')
if vnf_id in vnf_dict:
continue
# add vnf (with vnf_id as key) to the dictionary
vnf_dict[vnf_id] = vnf
# create a default candidate
candidate = dict()
candidate['inventory_provider'] = 'aai'
candidate['service_resource_id'] = service_resource_id
candidate['inventory_type'] = inventory_type
candidate['candidate_id'] = ''
candidate['location_id'] = ''
candidate['location_type'] = 'att_aic'
candidate['host_id'] = ''
candidate['cost'] = self.conf.data.service_candidate_cost
candidate['cloud_owner'] = ''
candidate['cloud_region_version'] = ''
candidate['vlan_key'] = vlan_key
candidate['port_key'] = port_key
candidate['uniqueness'] = candidate_uniqueness
# start populating the candidate
candidate['host_id'] = vnf.get("vnf-name")
rl_data = self.resolve_cloud_owner_for_vnf(candidate, vnf, service_type, name,
triage_translator_data)
if rl_data is None:
continue
else:
cloud_owner = rl_data.get('d_value')
candidate['cloud_owner'] = cloud_owner
cloud_region_id_rl_data, cloud_region_version_rl_data = self.resolve_cloud_region_id_and_version_for_vnf(
candidate, vnf, service_type, name, triage_translator_data)
if cloud_region_id_rl_data is None or cloud_region_version_rl_data is None:
continue
cloud_region_id = cloud_region_id_rl_data.get('d_value')
cloud_region_version = cloud_region_version_rl_data.get('d_value')
candidate['location_id'] = cloud_region_id
candidate['cloud_region_version'] = cloud_region_version
# Added vim-id for short-term workaround
if self.conf.HPA_enabled:
if not cloud_owner:
continue
candidate['vim-id'] = \
candidate['cloud_owner'] + '_' + cloud_region_id
if self.check_sriov_automation(
candidate['cloud_region_version'], name,
candidate['host_id']):
candidate['sriov_automation'] = 'true'
else:
candidate['sriov_automation'] = 'false'
rl_data = self.resolve_global_customer_id_for_vnf(candidate, vnf, customer_id, service_type,
name, triage_translator_data)
if rl_data is None:
continue
else:
vs_cust_id = rl_data.get('d_value')
rl_data = self.resolve_service_instance_id_for_vnf(candidate, vnf, customer_id,
service_type, name, triage_translator_data)
if rl_data is None:
continue
else:
vs_service_instance_id = rl_data.get('d_value')
if vs_cust_id and vs_cust_id == customer_id:
candidate['candidate_id'] = \
vs_service_instance_id
else: # vserver is for a different customer
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], name,
triage_translator_data,
reason= "vserver is for a different customer")
continue
# Second level query to get the pserver from vserver
complex_list = list()
vs_link_list = self.resolve_v_server_links_for_vnf(vnf)
for vs_link in vs_link_list:
body = self.resolove_v_server_for_candidate(candidate, vs_link, True, name,
triage_translator_data)
if body is None:
continue
rl_data = self.resolve_complex_info_link_for_v_server(candidate, body, cloud_owner,
cloud_region_id, service_type,
name, triage_translator_data)
if rl_data is None:
continue
complex_list.append(rl_data)
self.build_complex_info_for_candidate(candidate, vnf, complex_list, service_type, name,
triage_translator_data)
if "complex_name" not in candidate:
continue
# add specifal parameters for comparsion
vnf['global-customer-id'] = customer_id
vnf['customer-id'] = customer_id
vnf['cloud-region-id'] = cloud_region_id
vnf['physical-location-id'] = candidate.get('physical_location_id')
if filtering_attributes and not self.match_inventory_attributes(filtering_attributes, vnf, candidate['candidate_id']):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason="attibute check error")
continue
self.assign_candidate_existing_placement(candidate, existing_placement)
# Pick only candidates not in the excluded list
# if excluded candidate list is provided
if excluded_candidates and self.match_candidate_by_list(candidate, excluded_candidates, True,
name, triage_translator_data):
continue
# Pick only candidates in the required list
# if required candidate list is provided
if required_candidates and not self.match_candidate_by_list(candidate, required_candidates,
False, name, triage_translator_data):
continue
# add the candidate to the demand
# Pick only candidates from the restricted_region
# or restricted_complex
if not self.match_region(candidate, restricted_region_id, restricted_complex_id, name,
triage_translator_data):
continue
else:
self.add_passthrough_attributes(candidate, passthrough_attributes, name, triage_translator_data)
resolved_demands[name].append(candidate)
LOG.debug(">>>>>>> Candidate <<<<<<<")
LOG.debug(json.dumps(candidate, indent=4))
elif (inventory_type == 'vfmodule') \
and customer_id:
# First level query to get the list of generic vnfs
vnf_by_model_invariant = list()
if filtering_attributes and model_invariant_id:
raw_path = '/network/generic-vnfs/' \
'?model-invariant-id={}&depth=0'.format(model_invariant_id)
if model_version_id:
raw_path = '/network/generic-vnfs/' \
'?model-invariant-id={}&model-version-id={}&depth=0'.format(model_invariant_id,
model_version_id)
path = self._aai_versioned_path(raw_path)
vnf_by_model_invariant = self.first_level_service_call(path, name, service_type)
vnf_by_service_type = list()
if service_type or equipment_role:
path = self._aai_versioned_path(
'/network/generic-vnfs/'
'?equipment-role={}&depth=0'.format(service_type))
vnf_by_service_type = self.first_level_service_call(path, name, service_type)
generic_vnf = vnf_by_model_invariant + vnf_by_service_type
vnf_dict = dict()
for vnf in generic_vnf:
# if this vnf already appears, skip it
vnf_id = vnf.get('vnf-id')
if vnf_id in vnf_dict:
continue
# add vnf (with vnf_id as key) to the dictionary
vnf_dict[vnf_id] = vnf
# create a default candidate
candidate = dict()
candidate['inventory_provider'] = 'aai'
candidate['service_resource_id'] = service_resource_id
candidate['inventory_type'] = inventory_type
candidate['candidate_id'] = ''
candidate['location_id'] = ''
candidate['location_type'] = 'att_aic'
candidate['host_id'] = ''
candidate['cost'] = self.conf.data.service_candidate_cost
candidate['cloud_owner'] = ''
candidate['cloud_region_version'] = ''
candidate['vlan_key'] = vlan_key
candidate['port_key'] = port_key
candidate['uniqueness'] = candidate_uniqueness
# start populating the candidate
candidate['host_id'] = vnf.get("vnf-name")
candidate['nf-name'] = vnf.get("vnf-name")
candidate['nf-id'] = vnf.get("vnf-id")
candidate['nf-type'] = 'vnf'
candidate['vnf-type'] = vnf.get("vnf-type")
candidate['ipv4-oam-address'] = ''
candidate['ipv6-oam-address'] = ''
if vnf.get("ipv4-oam-address"):
candidate['ipv4-oam-address'] = vnf.get("ipv4-oam-address")
if vnf.get("ipv6-oam-address"):
candidate['ipv6-oam-address'] = vnf.get("ipv6-oam-address")
rl_data = self.resolve_global_customer_id_for_vnf(candidate, vnf, customer_id, service_type,
name, triage_translator_data)
if rl_data is None:
continue
else:
vs_cust_id = rl_data.get('d_value')
rl_data = self.resolve_service_instance_id_for_vnf(candidate, vnf, customer_id,
service_type, name, triage_translator_data)
if rl_data is None:
continue
else:
vs_service_instance_id = rl_data.get('d_value')
if vs_cust_id and vs_cust_id == customer_id:
candidate['service_instance_id'] = vs_service_instance_id
else: # vserver is for a different customer
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], name,
triage_translator_data,
reason="candidate is for a different customer")
continue
vf_modules_list = self.resolve_vf_modules_for_generic_vnf(candidate, vnf, name,
triage_translator_data)
if vf_modules_list is None:
continue
candidate_base = candidate
for vf_module in vf_modules_list:
# for vfmodule demands we allow to have vfmodules from different cloud regions
candidate = copy.deepcopy(candidate_base)
candidate['candidate_id'] = vf_module.get("vf-module-id")
candidate['vf-module-name'] = vf_module.get("vf-module-name")
candidate['vf-module-id'] = vf_module.get("vf-module-id")
rl_data = self.resolve_cloud_owner_for_vnf(candidate, vf_module, service_type, name,
triage_translator_data)
if rl_data is None:
continue
else:
cloud_owner = rl_data.get('d_value')
candidate['cloud_owner'] = cloud_owner
cloud_region_id_rl_data, cloud_region_version_rl_data = self.resolve_cloud_region_id_and_version_for_vnf(
candidate, vf_module, service_type, name, triage_translator_data)
if cloud_region_id_rl_data is None or cloud_region_version_rl_data is None:
continue
cloud_region_id = cloud_region_id_rl_data.get('d_value')
cloud_region_version = cloud_region_version_rl_data.get('d_value')
candidate['location_id'] = cloud_region_id
candidate['cloud_region_version'] = cloud_region_version
# Added vim-id for short-term workaround
if self.conf.HPA_enabled:
if not cloud_owner:
continue
candidate['vim-id'] = \
candidate['cloud_owner'] + '_' + cloud_region_id
if self.check_sriov_automation(
candidate['cloud_region_version'], name,
candidate['host_id']):
candidate['sriov_automation'] = 'true'
else:
candidate['sriov_automation'] = 'false'
# Second level query to get the pserver from vserver
candidate['vservers'] = list()
complex_list = list()
vs_link_list = self.resolve_v_server_links_for_vnf(vf_module)
for vs_link in vs_link_list:
body = self.resolove_v_server_for_candidate(candidate, vs_link, True, name,
triage_translator_data)
if body is None:
continue
candidate_vserver = dict()
candidate_vserver['vserver-id'] = body.get('vserver-id')
candidate_vserver['vserver-name'] = body.get('vserver-name')
rl_data = self.resolve_complex_info_link_for_v_server(candidate, body, cloud_owner,
cloud_region_id, service_type,
name, triage_translator_data)
if rl_data is None:
continue
#Interfaces info
if not body.get('l-interfaces') or not body.get('l-interfaces').get('l-interface'):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], name,
triage_translator_data,
reason="VF-server interfaces error")
continue
else:
l_interfaces = body.get('l-interfaces').get('l-interface')
candidate_vserver['l-interfaces'] = list()
for l_interface in l_interfaces:
vserver_interface = dict()
vserver_interface['interface-id'] = l_interface.get('interface-id')
vserver_interface['interface-name'] = l_interface.get('interface-name')
vserver_interface['macaddr'] = l_interface.get('macaddr')
vserver_interface['network-id'] = l_interface.get('network-name')
vserver_interface['network-name'] = ''
vserver_interface['ipv4-addresses'] = list()
vserver_interface['ipv6-addresses'] = list()
if l_interface.get('l3-interface-ipv4-address-list'):
for ip_address_info in l_interface.get('l3-interface-ipv4-address-list'):
vserver_interface['ipv4-addresses'].\
append(ip_address_info.get('l3-interface-ipv4-address'))
if l_interface.get('l3-interface-ipv6-address-list'):
for ip_address_info in l_interface.get('l3-interface-ipv6-address-list'):
vserver_interface['ipv6-addresses'].\
append(ip_address_info.get('l3-interface-ipv6-address'))
candidate_vserver['l-interfaces'].append(vserver_interface)
complex_list.append(rl_data)
candidate['vservers'].append(candidate_vserver)
self.build_complex_info_for_candidate(candidate, vnf, complex_list, service_type, name,
triage_translator_data)
if candidate.get("complex_name") is None:
continue
##add vf-module parameters for filtering
vnf_vf_module_inventory = copy.deepcopy(vnf)
vnf_vf_module_inventory.update(vf_module)
# add specifal parameters for comparsion
vnf_vf_module_inventory['global-customer-id'] = customer_id
vnf_vf_module_inventory['customer-id'] = customer_id
vnf_vf_module_inventory['cloud-region-id'] = cloud_region_id
vnf_vf_module_inventory['physical-location-id'] = candidate.get('physical_location_id')
vnf_vf_module_inventory['service_instance_id'] = vs_service_instance_id
if filtering_attributes and not self.match_inventory_attributes(filtering_attributes, vnf_vf_module_inventory,
candidate['candidate_id']):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'],
candidate['location_id'], name,
triage_translator_data,
reason="attibute check error")
continue
self.assign_candidate_existing_placement(candidate, existing_placement)
# Pick only candidates not in the excluded list
# if excluded candidate list is provided
if excluded_candidates and self.match_candidate_by_list(candidate, excluded_candidates, True,
name, triage_translator_data):
continue
# Pick only candidates in the required list
# if required candidate list is provided
if required_candidates and not self.match_candidate_by_list(candidate, required_candidates,
False, name,
triage_translator_data):
continue
# add the candidate to the demand
# Pick only candidates from the restricted_region
# or restricted_complex
if not self.match_region(candidate, restricted_region_id, restricted_complex_id, name,
triage_translator_data):
continue
else:
self.add_passthrough_attributes(candidate, passthrough_attributes, name, triage_translator_data)
resolved_demands[name].append(candidate)
LOG.debug(">>>>>>> Candidate <<<<<<<")
LOG.debug(json.dumps(candidate, indent=4))
elif inventory_type == 'transport' \
and customer_id and service_type and \
service_subscription and service_role:
path = self._aai_versioned_path('business/customers/customer/{}/service-subscriptions/'
'service-subscription/{}/service-instances'
'?service-type={}&service-role={}'.format(customer_id,
service_subscription,
service_type,
service_role))
response = self._request('get', path=path, data=None)
if response is None or response.status_code != 200:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason=response.status_code)
continue
body = response.json()
transport_vnfs = body.get('service-instance', [])
for vnf in transport_vnfs:
# create a default candidate
candidate = dict()
candidate['inventory_provider'] = 'aai'
candidate['service_resource_id'] = service_resource_id
candidate['inventory_type'] = 'transport'
candidate['candidate_id'] = ''
candidate['location_id'] = ''
candidate['location_type'] = 'att_aic'
candidate['uniqueness'] = candidate_uniqueness
candidate['cost'] = self.conf.data.transport_candidate_cost
vnf_service_instance_id = vnf.get('service-instance-id')
if vnf_service_instance_id:
candidate['candidate_id'] = vnf_service_instance_id
else:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name,triage_translator_data,
reason="service-instance-id error ")
continue
related_to = "zone"
zone_link = self._get_aai_rel_link(
data=vnf, related_to=related_to)
if not zone_link:
LOG.error("Zone information not available"
"from A&AI for transport candidates")
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason="Zone information not available from A&AI for transport candidates")
continue
zone_aai_path = self._get_aai_path_from_link(zone_link)
response = self._request('get', path=zone_aai_path, data=None)
if response is None or response.status_code != 200:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason=response.status_code)
continue
body = response.json()
candidate['zone_id'] = body.get('zone-id')
candidate['zone_name'] = body.get('zone-name')
related_to = "complex"
search_key = "complex.physical-location-id"
rel_link_data_list = self._get_aai_rel_link_data(
data=body,
related_to=related_to,
search_key=search_key
)
if len(rel_link_data_list) > 1:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason="rel_link_data_list error")
continue
rel_link_data = rel_link_data_list[0]
complex_id = rel_link_data.get("d_value")
complex_link = rel_link_data.get('link')
if not (complex_link and complex_id):
LOG.debug("{} complex information not "
"available from A&AI - {}".
format(name, complex_link))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason="complex information not available from A&AI")
continue
else:
complex_info = self._get_complex(
complex_link=complex_link,
complex_id=complex_id
)
if not complex_info:
LOG.debug("{} complex information not "
"available from A&AI - {}".
format(name, complex_link))
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'], name, triage_translator_data,
reason="complex information not available from A&AI")
continue # move ahead with the next vnf
candidate['physical_location_id'] = \
complex_id
candidate['complex_name'] = \
complex_info.get('complex-name')
candidate['latitude'] = \
complex_info.get('latitude')
candidate['longitude'] = \
complex_info.get('longitude')
candidate['state'] = \
complex_info.get('state')
candidate['country'] = \
complex_info.get('country')
candidate['city'] = \
complex_info.get('city')
candidate['region'] = \
complex_info.get('region')
self.add_passthrough_attributes(candidate, passthrough_attributes, name, triage_translator_data)
# add candidate to demand candidates
resolved_demands[name].append(candidate)
else:
LOG.error("Unknown inventory_type "
" {}".format(inventory_type))
return resolved_demands
def add_passthrough_attributes(self, candidate, passthrough_attributes, demand_name, triage_translator_data):
if passthrough_attributes is None:
return
if len(passthrough_attributes.items()) > 0:
candidate['passthrough_attributes'] = dict()
for key, value in passthrough_attributes.items():
candidate['passthrough_attributes'][key] = value
def match_region(self, candidate, restricted_region_id, restricted_complex_id, demand_name, triage_translator_data):
if self.match_candidate_attribute(
candidate,
"location_id",
restricted_region_id,
demand_name,
candidate.get('inventory_type')) or \
self.match_candidate_attribute(
candidate,
"physical_location_id",
restricted_complex_id,
demand_name,
candidate.get('inventory_type')):
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason="candidate region does not match")
return False
else:
return True
def match_candidate_by_list(self, candidate, candidates_list, exclude, demand_name, triage_translator_data):
has_candidate = False
if candidates_list:
for list_candidate in candidates_list:
if list_candidate \
and list_candidate.get('inventory_type') \
== candidate.get('inventory_type'):
if isinstance(list_candidate.get('candidate_id'), list):
for candidate_id in list_candidate.get('candidate_id'):
if candidate_id == candidate.get('candidate_id'):
has_candidate = True
break
else:
raise Exception("Invalid candidate id list format")
if has_candidate:
break
if not exclude:
if not has_candidate:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason="has_required_candidate candidate")
elif has_candidate:
self.triage_translator.collectDroppedCandiate(candidate['candidate_id'], candidate['location_id'],
demand_name, triage_translator_data,
reason="excluded candidate")
return has_candidate
def match_hpa(self, candidate, features):
"""Match HPA features requirement with the candidate flavors """
hpa_provider = hpa_utils.HpaMatchProvider(candidate, features)
if hpa_provider.init_verify():
directives = hpa_provider.match_flavor()
else:
directives = None
return directives
|