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
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
|
/*-
* ============LICENSE_START=======================================================
* OPENECOMP - MSO
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
* ============LICENSE_END=========================================================
*/
package org.openecomp.mso.adapters.network;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Holder;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.openecomp.mso.adapters.network.exceptions.NetworkException;
import org.openecomp.mso.cloud.CloudConfig;
import org.openecomp.mso.cloud.CloudConfigFactory;
import org.openecomp.mso.cloud.CloudSite;
import org.openecomp.mso.db.catalog.CatalogDatabase;
import org.openecomp.mso.db.catalog.beans.HeatTemplate;
import org.openecomp.mso.db.catalog.beans.NetworkResource;
import org.openecomp.mso.db.catalog.utils.MavenLikeVersioning;
import org.openecomp.mso.entity.MsoRequest;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoAlarmLogger;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.openstack.beans.HeatStatus;
import org.openecomp.mso.openstack.beans.NetworkInfo;
import org.openecomp.mso.openstack.beans.NetworkRollback;
import org.openecomp.mso.openstack.beans.NetworkStatus;
import org.openecomp.mso.openstack.beans.Pool;
import org.openecomp.mso.openstack.beans.StackInfo;
import org.openecomp.mso.openstack.beans.Subnet;
import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
import org.openecomp.mso.openstack.exceptions.MsoException;
import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
import org.openecomp.mso.openstack.utils.MsoCommonUtils;
import org.openecomp.mso.openstack.utils.MsoHeatUtils;
import org.openecomp.mso.openstack.utils.MsoHeatUtilsWithUpdate;
import org.openecomp.mso.openstack.utils.MsoNeutronUtils;
import org.openecomp.mso.openstack.utils.MsoNeutronUtils.NetworkType;
import org.openecomp.mso.properties.MsoPropertiesException;
import org.openecomp.mso.properties.MsoPropertiesFactory;
import static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;
@WebService(serviceName = "NetworkAdapter", endpointInterface = "org.openecomp.mso.adapters.network.MsoNetworkAdapter", targetNamespace = "http://org.openecomp.mso/network")
public class MsoNetworkAdapterImpl implements MsoNetworkAdapter {
MsoPropertiesFactory msoPropertiesFactory=new MsoPropertiesFactory();
CloudConfigFactory cloudConfigFactory=new CloudConfigFactory();
private static final String AIC3_NW_PROPERTY= "org.openecomp.mso.adapters.network.aic3nw";
private static final String AIC3_NW="OS::ContrailV2::VirtualNetwork";
public static final String MSO_PROP_NETWORK_ADAPTER="MSO_PROP_NETWORK_ADAPTER";
private static final String VLANS = "vlans";
private static final String PHYSICAL_NETWORK = "physical_network";
private static final String UPDATE_NETWORK_CONTEXT = "UpdateNetwork";
private static final String NETWORK_ID = "network_id";
private static final String NETWORK_FQDN = "network_fqdn";
private static final String CREATE_NETWORK_CONTEXT = "CreateNetwork";
private static final String MSO_CONFIGURATION_ERROR = "MsoConfigurationError";
private static final String NEUTRON_MODE = "NEUTRON";
private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
protected CloudConfig cloudConfig;
/**
* Health Check web method. Does nothing but return to show the adapter is deployed.
*/
@Override
public void healthCheck () {
LOGGER.debug ("Health check call in Network Adapter");
}
/**
* Do not use this constructor or the msoPropertiesFactory will be NULL.
*
* @see MsoNetworkAdapterImpl#MsoNetworkAdapterImpl(MsoPropertiesFactory)
*/
public MsoNetworkAdapterImpl() {
}
/**
* This constructor MUST be used if this class if called with the new operator.
* @param msoPropFactory
*/
public MsoNetworkAdapterImpl(MsoPropertiesFactory msoPropFactory,CloudConfigFactory cloudConfigFact) {
this.msoPropertiesFactory = msoPropFactory;
this.cloudConfigFactory=cloudConfigFact;
cloudConfig = cloudConfigFactory.getCloudConfig ();
}
@Override
public void createNetwork (String cloudSiteId,
String tenantId,
String networkType,
String networkName,
String physicalNetworkName,
List <Integer> vlans,
Boolean failIfExists,
Boolean backout,
List <Subnet> subnets,
MsoRequest msoRequest,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
Holder <String> networkFqdn = new Holder <String> ();
createNetwork (cloudSiteId,
tenantId,
networkType,
networkName,
physicalNetworkName,
vlans,
null,
null,
null,
failIfExists,
backout,
subnets,
null,
null,
msoRequest,
networkId,
neutronNetworkId,
networkFqdn,
subnetIdMap,
rollback);
}
@Override
public void createNetworkContrail (String cloudSiteId,
String tenantId,
String networkType,
String networkName,
List <String> routeTargets,
String shared,
String external,
Boolean failIfExists,
Boolean backout,
List <Subnet> subnets,
List <String> policyFqdns,
List<String> routeTableFqdns,
MsoRequest msoRequest,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <String> networkFqdn,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
createNetwork (cloudSiteId,
tenantId,
networkType,
networkName,
null,
null,
routeTargets,
shared,
external,
failIfExists,
backout,
subnets,
policyFqdns,
routeTableFqdns,
msoRequest,
networkId,
neutronNetworkId,
networkFqdn,
subnetIdMap,
rollback);
}
/**
* This is the "Create Network" web service implementation.
* It will create a new Network of the requested type in the specified cloud
* and tenant. The tenant must exist at the time this service is called.
*
* If a network with the same name already exists, this can be considered a
* success or failure, depending on the value of the 'failIfExists' parameter.
*
* There will be a pre-defined set of network types defined in the MSO Catalog.
* All such networks will have a similar configuration, based on the allowable
* Openstack networking definitions. This includes basic networks, provider
* networks (with a single VLAN), and multi-provider networks (one or more VLANs)
*
* Initially, all provider networks must be "vlan" type, and multiple segments in
* a multi-provider network must be multiple VLANs on the same physical network.
*
* This service supports two modes of Network creation/update:
* - via Heat Templates
* - via Neutron API
* The network orchestration mode for each network type is declared in its
* catalog definition. All Heat-based templates must support some subset of
* the same input parameters: network_name, physical_network, vlan(s).
*
* The method returns the network ID and a NetworkRollback object. This latter
* object can be passed as-is to the rollbackNetwork operation to undo everything
* that was created. This is useful if a network is successfully created but
* the orchestration fails on a subsequent operation.
*/
private void createNetwork (String cloudSiteId,
String tenantId,
String networkType,
String networkName,
String physicalNetworkName,
List <Integer> vlans,
List <String> routeTargets,
String shared,
String external,
Boolean failIfExists,
Boolean backout,
List <Subnet> subnets,
List <String> policyFqdns,
List <String> routeTableFqdns,
MsoRequest msoRequest,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <String> networkFqdn,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
MsoLogger.setLogContext (msoRequest);
MsoLogger.setServiceName ("CreateNetwork");
LOGGER.debug ("*** CREATE Network: " + networkName
+ " of type "
+ networkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId);
// Will capture execution time for metrics
long startTime = System.currentTimeMillis ();
// Build a default rollback object (no actions performed)
NetworkRollback networkRollback = new NetworkRollback ();
networkRollback.setCloudId (cloudSiteId);
networkRollback.setTenantId (tenantId);
networkRollback.setMsoRequest (msoRequest);
// tenant query is not required here.
// If the tenant doesn’t exist, the Heat calls will fail anyway (when the HeatUtils try to obtain a token).
// So this is just catching that error in a bit more obvious way up front.
cloudConfig = cloudConfigFactory.getCloudConfig ();
CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
if (cloudSite == null)
{
String error = "Configuration Error. Stack " + networkName + " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ " CloudSite does not exist in MSO Configuration";
LOGGER.error (MessageEnum.RA_CONFIG_EXC, error, "", "", MsoLogger.ErrorCode.DataError, "Configuration Error");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
// Set the detailed error as the Exception 'message'
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
// Get a handle to the Catalog Database
CatalogDatabase db = getCatalogDB ();
// Make sure DB connection is always closed
try {
NetworkResource networkResource = networkCheck (db,
startTime,
networkType,
networkName,
physicalNetworkName,
vlans,
routeTargets,
cloudSite);
String mode = networkResource.getOrchestrationMode ();
NetworkType neutronNetworkType = NetworkType.valueOf (networkResource.getNeutronNetworkType ());
if (NEUTRON_MODE.equals (mode)) {
// Use an MsoNeutronUtils for all neutron commands
MsoNeutronUtils neutron = new MsoNeutronUtils (MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
// See if the Network already exists (by name)
NetworkInfo netInfo = null;
long queryNetworkStarttime = System.currentTimeMillis ();
try {
netInfo = neutron.queryNetwork (networkName, tenantId, cloudSiteId);
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Response successfully received from OpenStack", "OpenStack", "QueryNetwork", null);
} catch (MsoException me) {
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while querying network from OpenStack", "OpenStack", "QueryNetwork", null);
String error = "Create Network (neutron): query network " + networkName
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_QUERY_NETWORK_EXC, networkName, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception while querying network from OpenStack", me);
me.addContext (CREATE_NETWORK_CONTEXT);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while querying network from OpenStack");
throw new NetworkException (me);
}
if (netInfo != null) {
// Exists. If that's OK, return success with the network ID.
// Otherwise, return an exception.
if (failIfExists != null && failIfExists) {
String error = "Create Nework: Network " + networkName
+ " already exists in "
+ cloudSiteId
+ "/"
+ tenantId
+ " with ID " + netInfo.getId();
LOGGER.error (MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Network already exists");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.Conflict, error);
throw new NetworkException(error, MsoExceptionCategory.USERDATA);
} else {
// Populate the outputs from the existing network.
networkId.value = netInfo.getId ();
neutronNetworkId.value = netInfo.getId ();
rollback.value = networkRollback; // Default rollback - no updates performed
String msg = "Found Existing network, status=" + netInfo.getStatus () + " for Neutron mode";
LOGGER.warn (MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, "", "", MsoLogger.ErrorCode.DataError, "Found Existing network, status=" + netInfo.getStatus ());
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, msg);
}
return;
}
long createNetworkStarttime = System.currentTimeMillis ();
try {
netInfo = neutron.createNetwork (cloudSiteId,
tenantId,
neutronNetworkType,
networkName,
physicalNetworkName,
vlans);
LOGGER.recordMetricEvent (createNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Response successfully received from OpenStack", "OpenStack", "CreateNetwork", null);
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
LOGGER.recordMetricEvent (createNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with OpenStack", "OpenStack", "CreateNetwork", null);
String error = "Create Network: type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_CREATE_NETWORK_EXC, networkName, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception while communicate with OpenStack", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
// Note: ignoring MsoNetworkAlreadyExists because we already checked.
// If reach this point, network creation is successful.
// Since directly created via Neutron, networkId tracked by MSO is the same
// as the neutron network ID.
networkId.value = netInfo.getId ();
neutronNetworkId.value = netInfo.getId ();
networkRollback.setNetworkCreated (true);
networkRollback.setNetworkId (netInfo.getId ());
networkRollback.setNeutronNetworkId (netInfo.getId ());
networkRollback.setNetworkType (networkType);
LOGGER.debug ("Network " + networkName + " created, id = " + netInfo.getId ());
} else if ("HEAT".equals (mode)) {
// Use an MsoHeatUtils for all Heat commands
MsoHeatUtils heat = new MsoHeatUtils (MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory,cloudConfigFactory);
HeatTemplate heatTemplate = db.getHeatTemplate (networkResource.getTemplateId ());
if (heatTemplate == null) {
String error = "Network error - undefined Heat Template. Network Type = " + networkType;
LOGGER.error (MessageEnum.RA_PARAM_NOT_FOUND, "Heat Template", "Network Type", networkType, "Openstack", "", MsoLogger.ErrorCode.DataError, "Network error - undefined Heat Template. Network Type = " + networkType);
alarmLogger.sendAlarm (MSO_CONFIGURATION_ERROR, MsoAlarmLogger.CRITICAL, error); // Alarm on this
// error,
// configuration
// must be fixed
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, error);
throw new NetworkException (error, MsoExceptionCategory.INTERNAL);
}
LOGGER.debug ("Got HEAT Template from DB: " + heatTemplate.toString ());
// "Fix" the template if it has CR/LF (getting this from Oracle)
String template = heatTemplate.getHeatTemplate ();
template = template.replaceAll ("\r\n", "\n");
boolean aic3template=false;
String aic3nw = AIC3_NW;
try {
aic3nw = msoPropertiesFactory.getMsoJavaProperties(MSO_PROP_NETWORK_ADAPTER).getProperty(AIC3_NW_PROPERTY, AIC3_NW);
} catch (MsoPropertiesException e) {
String error = "Unable to get properties:" + MSO_PROP_NETWORK_ADAPTER;
LOGGER.error (MessageEnum.RA_CONFIG_EXC, error, "", "", MsoLogger.ErrorCode.DataError, "Exception - Unable to get properties", e);
}
if (template.contains(aic3nw))
aic3template = true;
// First, look up to see if the Network already exists (by name).
// For HEAT orchestration of networks, the stack name will always match the network name
StackInfo heatStack = null;
long queryNetworkStarttime = System.currentTimeMillis ();
try {
heatStack = heat.queryStack (cloudSiteId, tenantId, networkName);
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Response successfully received from OpenStack", "OpenStack", "QueryNetwork", null);
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while querying stack from OpenStack", "OpenStack", "QueryNetwork", null);
String error = "Create Network (heat): query network " + networkName
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_QUERY_NETWORK_EXC, networkName, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception while querying stack from OpenStack", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
if (heatStack != null && !(heatStack.getStatus () == HeatStatus.NOTFOUND)) {
// Stack exists. Return success or error depending on input directive
if (failIfExists != null && failIfExists) {
String error = "CreateNetwork: Stack " + networkName
+ " already exists in "
+ cloudSiteId
+ "/"
+ tenantId
+ " as " + heatStack.getCanonicalName();
LOGGER.error (MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, "", "", MsoLogger.ErrorCode.DataError, "Network already exists");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.Conflict, error);
throw new NetworkException(error, MsoExceptionCategory.USERDATA);
} else {
// Populate the outputs from the existing stack.
networkId.value = heatStack.getCanonicalName ();
neutronNetworkId.value = (String) heatStack.getOutputs ().get (NETWORK_ID);
rollback.value = networkRollback; // Default rollback - no updates performed
if (aic3template)
{
networkFqdn.value = (String) heatStack.getOutputs().get(NETWORK_FQDN);
}
Map <String, Object> outputs = heatStack.getOutputs ();
Map <String, String> sMap = new HashMap <String, String> ();
if (outputs != null) {
for (String key : outputs.keySet ()) {
if (key != null && key.startsWith ("subnet")) {
if (aic3template) //one subnet_id output
{
Map <String, String> map = getSubnetUUId(key, outputs, subnets);
sMap.putAll(map);
}
else //multiples subnet_%aaid% outputs
{
String subnetUUId = (String) outputs.get(key);
sMap.put (key.substring("subnet_id_".length()), subnetUUId);
}
}
}
}
subnetIdMap.value = sMap;
String msg = "Found Existing network stack, status=" + heatStack.getStatus () + " for Heat mode";
LOGGER.warn (MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, "", "", MsoLogger.ErrorCode.DataError, "Found Existing network stack, status=" + heatStack.getStatus ());
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.Suc, "Found Existing network stack");
}
return;
}
// Ready to deploy the new Network
// Build the common set of HEAT template parameters
Map <String, Object> stackParams = populateNetworkParams (neutronNetworkType,
networkName,
physicalNetworkName,
vlans,
routeTargets,
shared,
external,
aic3template);
// Validate (and update) the input parameters against the DB definition
// Shouldn't happen unless DB config is wrong, since all networks use same inputs
// and inputs were already validated.
try {
stackParams = heat.validateStackParams (stackParams, heatTemplate);
} catch (IllegalArgumentException e) {
String error = "Create Network: Configuration Error: " + e.getMessage ();
LOGGER.error (MessageEnum.RA_CONFIG_EXC, e.getMessage(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception - Create Network, Configuration Error", e);
alarmLogger.sendAlarm (MSO_CONFIGURATION_ERROR, MsoAlarmLogger.CRITICAL, error); // Alarm on this
// error,
// configuration
// must be fixed
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
// Input parameters were not valid
throw new NetworkException (error, MsoExceptionCategory.INTERNAL);
}
if (subnets != null) {
try {
if (aic3template)
{
template = mergeSubnetsAIC3 (template, subnets, stackParams);
}
else
{
template = mergeSubnets (template, subnets);
}
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
String error = "Create Network (heat): type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_CREATE_NETWORK_EXC, neutronNetworkType.toString(), cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception Create Network, merging subnets", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException (me);
}
}
if (policyFqdns != null && !policyFqdns.isEmpty() && aic3template) {
try {
mergePolicyRefs (policyFqdns, stackParams);
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
String error = "Create Network (heat) mergePolicyRefs type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_CREATE_NETWORK_EXC, neutronNetworkType.toString(), cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception Create Network, merging policyRefs", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException (me);
}
}
if (routeTableFqdns != null && !routeTableFqdns.isEmpty() && aic3template) {
try {
mergeRouteTableRefs (routeTableFqdns, stackParams);
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
String error = "Create Network (heat) mergeRouteTableRefs type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_CREATE_NETWORK_EXC, neutronNetworkType.toString(), cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception Create Network, merging routeTableRefs", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException (me);
}
}
// Deploy the network stack
// Ignore MsoStackAlreadyExists exception because we already checked.
long createStackStartTime = System.currentTimeMillis ();
try {
if (backout == null)
backout = true;
heatStack = heat.createStack (cloudSiteId,
tenantId,
networkName,
template,
stackParams,
true,
heatTemplate.getTimeoutMinutes (),
null,
null,
null,
backout.booleanValue());
} catch (MsoException me) {
me.addContext (CREATE_NETWORK_CONTEXT);
String error = "Create Network (heat): type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error (MessageEnum.RA_CREATE_NETWORK_EXC, networkName, cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception creating network", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
// Reach this point if createStack is successful.
// For Heat-based orchestration, the MSO-tracked network ID is the heat stack,
// and the neutronNetworkId is the network UUID returned in stack outputs.
networkId.value = heatStack.getCanonicalName ();
neutronNetworkId.value = (String) heatStack.getOutputs ().get (NETWORK_ID);
if (aic3template)
{
networkFqdn.value = (String) heatStack.getOutputs().get(NETWORK_FQDN);
}
Map <String, Object> outputs = heatStack.getOutputs ();
Map <String, String> sMap = new HashMap <String, String> ();
if (outputs != null) {
for (String key : outputs.keySet ()) {
if (key != null && key.startsWith ("subnet")) {
if (aic3template) //one subnet output expected
{
Map <String, String> map = getSubnetUUId(key, outputs, subnets);
sMap.putAll(map);
}
else //multiples subnet_%aaid% outputs allowed
{
String subnetUUId = (String) outputs.get(key);
sMap.put (key.substring("subnet_id_".length()), subnetUUId);
}
}
}
}
subnetIdMap.value = sMap;
rollback.value = networkRollback;
// Populate remaining rollback info and response parameters.
networkRollback.setNetworkStackId (heatStack.getCanonicalName ());
networkRollback.setNeutronNetworkId ((String) heatStack.getOutputs ().get (NETWORK_ID));
networkRollback.setNetworkCreated (true);
networkRollback.setNetworkType (networkType);
LOGGER.debug ("Network " + networkName + " successfully created via HEAT");
}
} finally {
db.close ();
}
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.Suc, "Successfully created network");
return;
}
@Override
public void updateNetwork (String cloudSiteId,
String tenantId,
String networkType,
String networkId,
String networkName,
String physicalNetworkName,
List <Integer> vlans,
List <Subnet> subnets,
MsoRequest msoRequest,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
updateNetwork (cloudSiteId,
tenantId,
networkType,
networkId,
networkName,
physicalNetworkName,
vlans,
null,
null,
null,
subnets,
null,
null,
msoRequest,
subnetIdMap,
rollback);
}
@Override
public void updateNetworkContrail (String cloudSiteId,
String tenantId,
String networkType,
String networkId,
String networkName,
List <String> routeTargets,
String shared,
String external,
List <Subnet> subnets,
List <String> policyFqdns,
List<String> routeTableFqdns,
MsoRequest msoRequest,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
updateNetwork (cloudSiteId,
tenantId,
networkType,
networkId,
networkName,
null,
null,
routeTargets,
shared,
external,
subnets,
policyFqdns,
routeTableFqdns,
msoRequest,
subnetIdMap,
rollback);
}
/**
* This is the "Update Network" web service implementation.
* It will update an existing Network of the requested type in the specified cloud
* and tenant. The typical use will be to replace the VLANs with the supplied
* list (to add or remove a VLAN), but other properties may be updated as well.
*
* There will be a pre-defined set of network types defined in the MSO Catalog.
* All such networks will have a similar configuration, based on the allowable
* Openstack networking definitions. This includes basic networks, provider
* networks (with a single VLAN), and multi-provider networks (one or more VLANs).
*
* Initially, all provider networks must currently be "vlan" type, and multi-provider
* networks must be multiple VLANs on the same physical network.
*
* This service supports two modes of Network update:
* - via Heat Templates
* - via Neutron API
* The network orchestration mode for each network type is declared in its
* catalog definition. All Heat-based templates must support some subset of
* the same input parameters: network_name, physical_network, vlan, segments.
*
* The method returns a NetworkRollback object. This object can be passed
* as-is to the rollbackNetwork operation to undo everything that was updated.
* This is useful if a network is successfully updated but orchestration
* fails on a subsequent operation.
*/
private void updateNetwork (String cloudSiteId,
String tenantId,
String networkType,
String networkId,
String networkName,
String physicalNetworkName,
List <Integer> vlans,
List <String> routeTargets,
String shared,
String external,
List <Subnet> subnets,
List <String> policyFqdns,
List<String> routeTableFqdns,
MsoRequest msoRequest,
Holder <Map <String, String>> subnetIdMap,
Holder <NetworkRollback> rollback) throws NetworkException {
MsoLogger.setLogContext (msoRequest);
MsoLogger.setServiceName ("UpdateNetwork");
LOGGER.debug ("***UPDATE Network adapter with Network: " + networkName
+ " of type "
+ networkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId);
// Will capture execution time for metrics
long startTime = System.currentTimeMillis ();
// Build a default rollback object (no actions performed)
NetworkRollback networkRollback = new NetworkRollback ();
networkRollback.setCloudId (cloudSiteId);
networkRollback.setTenantId (tenantId);
networkRollback.setMsoRequest (msoRequest);
cloudConfig = cloudConfigFactory.getCloudConfig ();
CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
if (cloudSite == null) {
String error = "UpdateNetwork: Configuration Error. Stack " + networkName + " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ " CloudSite does not exist in MSO Configuration";
LOGGER.error (MessageEnum.RA_CONFIG_EXC, error, "Openstack", "", MsoLogger.ErrorCode.DataError, "CloudSite does not exist in MSO Configuration");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
// Set the detailed error as the Exception 'message'
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
// Get a handle to the Catalog Database
CatalogDatabase db = getCatalogDB ();
// Make sure DB connection is always closed
try {
NetworkResource networkResource = networkCheck(db,
startTime,
networkType,
networkName,
physicalNetworkName,
vlans,
routeTargets,
cloudSite);
String mode = networkResource.getOrchestrationMode();
NetworkType neutronNetworkType = NetworkType.valueOf(networkResource.getNeutronNetworkType());
// Use an MsoNeutronUtils for all Neutron commands
MsoNeutronUtils neutron = new MsoNeutronUtils(MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
if (NEUTRON_MODE.equals(mode)) {
// Verify that the Network exists
// For Neutron-based orchestration, the networkId is the Neutron Network UUID.
NetworkInfo netInfo = null;
long queryNetworkStarttime = System.currentTimeMillis();
try {
netInfo = neutron.queryNetwork(networkId, tenantId, cloudSiteId);
LOGGER.recordMetricEvent(queryNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryNetwork", null);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "Update Network (neutron): query " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent(queryNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "QueryNetwork", null);
LOGGER.error(MessageEnum.RA_QUERY_NETWORK_EXC, networkId, cloudSiteId, tenantId, "OpenStack", "QueryNetwork", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryNetwork", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException(me);
}
if (netInfo == null) {
String error = "Update Nework: Network " + networkId
+ " does not exist in "
+ cloudSiteId
+ "/"
+ tenantId;
LOGGER.error(MessageEnum.RA_NETWORK_NOT_FOUND, networkId, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.BusinessProcesssError, "Network not found");
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, error);
// Does not exist. Throw an exception (can't update a non-existent network)
throw new NetworkException(error, MsoExceptionCategory.USERDATA);
}
long updateNetworkStarttime = System.currentTimeMillis();
try {
netInfo = neutron.updateNetwork(cloudSiteId,
tenantId,
networkId,
neutronNetworkType,
physicalNetworkName,
vlans);
LOGGER.recordMetricEvent(updateNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "UpdateNetwork", null);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "Update Network (neutron): " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error(MessageEnum.RA_UPDATE_NETWORK_ERR, networkId, cloudSiteId, tenantId, "Openstack", "updateNetwork", MsoLogger.ErrorCode.DataError, "Exception - updateNetwork", me);
LOGGER.recordMetricEvent(updateNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "UpdateNetwork", null);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException(me);
}
// Add the network ID and previously queried vlans to the rollback object
networkRollback.setNetworkId(netInfo.getId());
networkRollback.setNeutronNetworkId(netInfo.getId());
networkRollback.setNetworkType(networkType);
// Save previous parameters
networkRollback.setNetworkName(netInfo.getName());
networkRollback.setPhysicalNetwork(netInfo.getProvider());
networkRollback.setVlans(netInfo.getVlans());
LOGGER.debug("Network " + networkId + " updated, id = " + netInfo.getId());
} else if ("HEAT".equals(mode)) {
// Use an MsoHeatUtils for all Heat commands
MsoHeatUtilsWithUpdate heat = new MsoHeatUtilsWithUpdate(MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory, cloudConfigFactory);
// First, look up to see that the Network already exists.
// For Heat-based orchestration, the networkId is the network Stack ID.
StackInfo heatStack = null;
long queryStackStarttime = System.currentTimeMillis();
try {
heatStack = heat.queryStack(cloudSiteId, tenantId, networkName);
LOGGER.recordMetricEvent(queryStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryStack", null);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "UpdateNetwork (heat): query " + networkName
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent(queryStackStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "QueryStack", null);
LOGGER.error(MessageEnum.RA_QUERY_NETWORK_EXC, networkId, cloudSiteId, tenantId, "OpenStack", "queryStack", MsoLogger.ErrorCode.DataError, "Exception - QueryStack", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException(me);
}
if (heatStack == null || (heatStack.getStatus() == HeatStatus.NOTFOUND)) {
String error = "UpdateNetwork: Stack " + networkName
+ " does not exist in "
+ cloudSiteId
+ "/"
+ tenantId;
LOGGER.error(MessageEnum.RA_NETWORK_NOT_FOUND, networkId, cloudSiteId, tenantId, "OpenStack", "queryStack", MsoLogger.ErrorCode.DataError, "Network not found");
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, error);
// Network stack does not exist. Return an error
throw new NetworkException(error, MsoExceptionCategory.USERDATA);
}
// Get the previous parameters for rollback
Map<String, Object> heatParams = heatStack.getParameters();
String previousNetworkName = (String) heatParams.get("network_name");
String previousPhysicalNetwork = (String) heatParams.get(PHYSICAL_NETWORK);
List<Integer> previousVlans = new ArrayList<Integer>();
String vlansParam = (String) heatParams.get(VLANS);
if (vlansParam != null) {
for (String vlan : vlansParam.split(",")) {
try {
previousVlans.add(Integer.parseInt(vlan));
} catch (NumberFormatException e) {
LOGGER.warn(MessageEnum.RA_VLAN_PARSE, networkId, vlansParam, "", "", MsoLogger.ErrorCode.DataError, "Exception - VLAN parse", e);
}
}
}
LOGGER.debug("Update Stack: Previous VLANS: " + previousVlans);
// Ready to deploy the updated Network via Heat
HeatTemplate heatTemplate = db.getHeatTemplate(networkResource.getTemplateId());
if (heatTemplate == null) {
String error = "Network error - undefined Heat Template. Network Type=" + networkType;
LOGGER.error(MessageEnum.RA_PARAM_NOT_FOUND, "Heat Template", "Network Type", networkType, "OpenStack", "getHeatTemplate", MsoLogger.ErrorCode.DataError, "Network error - undefined Heat Template. Network Type=" + networkType);
alarmLogger.sendAlarm(MSO_CONFIGURATION_ERROR, MsoAlarmLogger.CRITICAL, error);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, error);
throw new NetworkException(error, MsoExceptionCategory.INTERNAL);
}
LOGGER.debug("Got HEAT Template from DB: " + heatTemplate.toString());
// "Fix" the template if it has CR/LF (getting this from Oracle)
String template = heatTemplate.getHeatTemplate();
template = template.replaceAll("\r\n", "\n");
boolean aic3template = false;
String aic3nw = AIC3_NW;
try {
aic3nw = msoPropertiesFactory.getMsoJavaProperties(MSO_PROP_NETWORK_ADAPTER).getProperty(AIC3_NW_PROPERTY, AIC3_NW);
} catch (MsoPropertiesException e) {
String error = "Unable to get properties:" + MSO_PROP_NETWORK_ADAPTER;
LOGGER.error(MessageEnum.RA_CONFIG_EXC, error, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - Unable to get properties", e);
}
if (template.contains(aic3nw))
aic3template = true;
// Build the common set of HEAT template parameters
Map<String, Object> stackParams = populateNetworkParams(neutronNetworkType,
networkName,
physicalNetworkName,
vlans,
routeTargets,
shared,
external,
aic3template);
// Validate (and update) the input parameters against the DB definition
// Shouldn't happen unless DB config is wrong, since all networks use same inputs
try {
stackParams = heat.validateStackParams(stackParams, heatTemplate);
} catch (IllegalArgumentException e) {
String error = "UpdateNetwork: Configuration Error: Network Type=" + networkType;
LOGGER.error(MessageEnum.RA_CONFIG_EXC, "Network Type=" + networkType, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - UpdateNetwork: Configuration Error");
alarmLogger.sendAlarm(MSO_CONFIGURATION_ERROR, MsoAlarmLogger.CRITICAL, error);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, error);
throw new NetworkException(error, MsoExceptionCategory.INTERNAL, e);
}
if (subnets != null) {
try {
if (aic3template) {
template = mergeSubnetsAIC3(template, subnets, stackParams);
} else {
template = mergeSubnets(template, subnets);
}
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "Update Network (heat): type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error(MessageEnum.RA_UPDATE_NETWORK_ERR, neutronNetworkType.toString(), cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - UpdateNetwork mergeSubnets ", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException(me);
}
}
if (policyFqdns != null && aic3template) {
try {
mergePolicyRefs(policyFqdns, stackParams);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "UpdateNetwork (heat) mergePolicyRefs type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error(MessageEnum.RA_UPDATE_NETWORK_ERR, neutronNetworkType.toString(), cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - UpdateNetwork mergePolicyRefs", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException(me);
}
}
if (routeTableFqdns != null && !routeTableFqdns.isEmpty() && aic3template) {
try {
mergeRouteTableRefs(routeTableFqdns, stackParams);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "UpdateNetwork (heat) mergeRouteTableRefs type " + neutronNetworkType
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.error(MessageEnum.RA_UPDATE_NETWORK_ERR, neutronNetworkType.toString(), cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception - UpdateNetwork mergeRouteTableRefs", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, error);
throw new NetworkException(me);
}
}
// Update the network stack
// Ignore MsoStackNotFound exception because we already checked.
long updateStackStarttime = System.currentTimeMillis();
try {
heatStack = heat.updateStack(cloudSiteId,
tenantId,
networkId,
template,
stackParams,
true,
heatTemplate.getTimeoutMinutes());
LOGGER.recordMetricEvent(updateStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "UpdateStack", null);
} catch (MsoException me) {
me.addContext(UPDATE_NETWORK_CONTEXT);
String error = "Update Network: " + networkId + " in " + cloudSiteId + "/" + tenantId + ": " + me;
LOGGER.recordMetricEvent(updateStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "UpdateStack", null);
LOGGER.error(MessageEnum.RA_UPDATE_NETWORK_ERR, networkId, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - update network", me);
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException(me);
}
Map<String, Object> outputs = heatStack.getOutputs();
Map<String, String> sMap = new HashMap<String, String>();
if (outputs != null) {
for (String key : outputs.keySet()) {
if (key != null && key.startsWith("subnet")) {
if (aic3template) //one subnet output expected
{
Map<String, String> map = getSubnetUUId(key, outputs, subnets);
sMap.putAll(map);
} else //multiples subnet_%aaid% outputs allowed
{
String subnetUUId = (String) outputs.get(key);
sMap.put(key.substring("subnet_id_".length()), subnetUUId);
}
}
}
}
subnetIdMap.value = sMap;
// Reach this point if createStack is successful.
// Populate remaining rollback info and response parameters.
networkRollback.setNetworkStackId(heatStack.getCanonicalName());
networkRollback.setNeutronNetworkId((String) outputs.get(NETWORK_ID));
networkRollback.setNetworkType(networkType);
// Save previous parameters
networkRollback.setNetworkName(previousNetworkName);
networkRollback.setPhysicalNetwork(previousPhysicalNetwork);
networkRollback.setVlans(previousVlans);
rollback.value = networkRollback;
LOGGER.debug("Network " + networkId + " successfully updated via HEAT");
}
} finally {
db.close ();
}
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully updated network");
return;
}
private NetworkResource networkCheck (CatalogDatabase db,
long startTime,
String networkType,
String networkName,
String physicalNetworkName,
List <Integer> vlans,
List <String> routeTargets,
CloudSite cloudSite) throws NetworkException {
// Retrieve the Network Resource definition
NetworkResource networkResource = db.getNetworkResource (networkType);
if (networkResource == null) {
String error = "CreateNetwork: Unknown Network Type: " + networkType;
LOGGER.error (MessageEnum.RA_UNKOWN_PARAM, "Network Type", networkType, "OpenStack", "", MsoLogger.ErrorCode.DataError, "CreateNetwork: Unknown Network Type");
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
LOGGER.debug ("Got Network definition from Catalog: " + networkResource.toString ());
String mode = networkResource.getOrchestrationMode ();
NetworkType neutronNetworkType = NetworkType.valueOf (networkResource.getNeutronNetworkType ());
// All Networks are orchestrated via HEAT or Neutron
if (!("HEAT".equals (mode) || NEUTRON_MODE.equals (mode))) {
String error = "CreateNetwork: Configuration Error: Network Type = " + networkType;
LOGGER.error (MessageEnum.RA_NETWORK_ORCHE_MODE_NOT_SUPPORT, mode, "OpenStack", "", MsoLogger.ErrorCode.DataError, "CreateNetwork: Configuration Error");
// Alarm on this error, configuration must be fixed
alarmLogger.sendAlarm (MSO_CONFIGURATION_ERROR, MsoAlarmLogger.CRITICAL, error);
throw new NetworkException (error, MsoExceptionCategory.INTERNAL);
}
MavenLikeVersioning aicV = new MavenLikeVersioning();
aicV.setVersion(cloudSite.getAic_version());
try {
if ((aicV.isMoreRecentThan(networkResource.getAicVersionMin()) || aicV.isTheSameVersion(networkResource.getAicVersionMin())) // aic >= min
&& (aicV.isTheSameVersion(networkResource.getAicVersionMax()) || !(aicV.isMoreRecentThan(networkResource.getAicVersionMax())))) //aic <= max
{
LOGGER.debug("Network Type:" + networkType
+ " VersionMin:" + networkResource.getAicVersionMin()
+ " VersionMax:" + networkResource.getAicVersionMax()
+ " supported on Cloud:" + cloudSite.getId()
+ " with AIC_Version:" + cloudSite.getAic_version());
} else {
String error = "Network Type:" + networkType
+ " Version_Min:" + networkResource.getAicVersionMin()
+ " Version_Max:" + networkResource.getAicVersionMax()
+ " not supported on Cloud:" + cloudSite.getId()
+ " with AIC_Version:" + cloudSite.getAic_version();
LOGGER.error(MessageEnum.RA_CONFIG_EXC, error, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Network Type not supported on Cloud");
LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
throw new NetworkException(error, MsoExceptionCategory.USERDATA);
}
} catch (Exception e) {
String error = "Exception during Network version check";
LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION_ARG, e.getMessage(), "", "", MsoLogger.ErrorCode.DataError, error);
throw new NetworkException (error, MsoExceptionCategory.INTERNAL);
}
// Validate the Network parameters.
String missing = validateNetworkParams (neutronNetworkType,
networkName,
physicalNetworkName,
vlans,
routeTargets);
if (!missing.isEmpty ()) {
String error = "Create Network: Missing parameters: " + missing;
LOGGER.error (MessageEnum.RA_MISSING_PARAM, missing, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Create Network: Missing parameters");
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
return networkResource;
}
@Override
public void queryNetwork (String cloudSiteId,
String tenantId,
String networkNameOrId,
MsoRequest msoRequest,
Holder <Boolean> networkExists,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <NetworkStatus> status,
Holder <List <Integer>> vlans,
Holder <Map <String, String>> subnetIdMap) throws NetworkException {
queryNetwork (cloudSiteId,
tenantId,
networkNameOrId,
msoRequest,
networkExists,
networkId,
neutronNetworkId,
status,
vlans,
null,
subnetIdMap);
}
@Override
public void queryNetworkContrail (String cloudSiteId,
String tenantId,
String networkNameOrId,
MsoRequest msoRequest,
Holder <Boolean> networkExists,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <NetworkStatus> status,
Holder <List <String>> routeTargets,
Holder <Map <String, String>> subnetIdMap) throws NetworkException {
queryNetwork (cloudSiteId,
tenantId,
networkNameOrId,
msoRequest,
networkExists,
networkId,
neutronNetworkId,
status,
null,
routeTargets,
subnetIdMap);
}
/**
* This is the queryNetwork method. It returns the existence and status of
* the specified network, along with its Neutron UUID and list of VLANs.
* This method attempts to find the network using both Heat and Neutron.
* Heat stacks are first searched based on the provided network name/id.
* If none is found, the Neutron is directly queried.
*/
private void queryNetwork (String cloudSiteId,
String tenantId,
String networkNameOrId,
MsoRequest msoRequest,
Holder <Boolean> networkExists,
Holder <String> networkId,
Holder <String> neutronNetworkId,
Holder <NetworkStatus> status,
Holder <List <Integer>> vlans,
Holder <List <String>> routeTargets,
Holder <Map <String, String>> subnetIdMap) throws NetworkException {
MsoLogger.setLogContext (msoRequest);
MsoLogger.setServiceName ("QueryNetwork");
LOGGER.debug ("*** QUERY Network with Network: " + networkNameOrId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId);
// Will capture execution time for metrics
long startTime = System.currentTimeMillis ();
if (isNullOrEmpty (cloudSiteId)
|| isNullOrEmpty(tenantId)
|| isNullOrEmpty(networkNameOrId)) {
String error = "Missing mandatory parameter cloudSiteId, tenantId or networkId";
LOGGER.error (MessageEnum.RA_MISSING_PARAM, "cloudSiteId or tenantId or networkNameOrId", "OpenStack", "", MsoLogger.ErrorCode.DataError, "Missing mandatory parameter cloudSiteId, tenantId or networkId");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, error);
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
cloudConfig = cloudConfigFactory.getCloudConfig ();
CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
if (cloudSite == null)
{
String error = "Configuration Error. Stack " + networkNameOrId + " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ " CloudSite does not exist in MSO Configuration";
LOGGER.error (MessageEnum.RA_CONFIG_EXC, error, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Configuration Error");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
// Set the detailed error as the Exception 'message'
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
// Use MsoNeutronUtils for all NEUTRON commands
MsoHeatUtils heat = new MsoHeatUtils (MSO_PROP_NETWORK_ADAPTER,msoPropertiesFactory,cloudConfigFactory);
MsoNeutronUtils neutron = new MsoNeutronUtils (MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
String mode = null;
String neutronId = null;
// Try Heat first, since networks may be named the same as the Heat stack
StackInfo heatStack = null;
long queryStackStarttime = System.currentTimeMillis ();
try {
heatStack = heat.queryStack (cloudSiteId, tenantId, networkNameOrId);
LOGGER.recordMetricEvent (queryStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryStack", null);
} catch (MsoException me) {
me.addContext ("QueryNetwork");
String error = "Query Network (heat): " + networkNameOrId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (queryStackStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "BPMN", "QueryStack", null);
LOGGER.error (MessageEnum.RA_QUERY_NETWORK_EXC, networkNameOrId, cloudSiteId, tenantId, "OpenStack", "queryStack", MsoLogger.ErrorCode.DataError, "Exception - Query Network (heat)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
// Populate the outputs based on the returned Stack information
if (heatStack != null && heatStack.getStatus () != HeatStatus.NOTFOUND) {
// Found it. Get the neutronNetworkId for further query
Map <String, Object> outputs = heatStack.getOutputs ();
neutronId = (String) outputs.get (NETWORK_ID);
mode = "HEAT";
Map <String, String> sMap = new HashMap <String, String> ();
if (outputs != null) {
for (String key : outputs.keySet ()) {
if (key != null && key.startsWith ("subnet_id_")) //multiples subnet_%aaid% outputs
{
String subnetUUId = (String) outputs.get(key);
sMap.put (key.substring("subnet_id_".length()), subnetUUId);
}
else if (key != null && key.startsWith ("subnet")) //one subnet output expected
{
Map <String, String> map = getSubnetUUId(key, outputs, null);
sMap.putAll(map);
}
}
}
subnetIdMap.value = sMap;
} else {
// Input ID was not a Heat stack ID. Try it directly in Neutron
neutronId = networkNameOrId;
mode = NEUTRON_MODE;
}
// Query directly against the Neutron Network for the details
// no RouteTargets available for ContrailV2 in neutron net-show
// networkId is heatStackId
long queryNetworkStarttime = System.currentTimeMillis ();
try {
NetworkInfo netInfo = neutron.queryNetwork (neutronId, tenantId, cloudSiteId);
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryNetwork", null);
if (netInfo != null) {
// Found. Populate the output elements
networkExists.value = Boolean.TRUE;
if ("HEAT".equals (mode)) {
networkId.value = heatStack.getCanonicalName ();
} else {
networkId.value = netInfo.getId ();
}
neutronNetworkId.value = netInfo.getId ();
status.value = netInfo.getStatus ();
if (vlans != null)
vlans.value = netInfo.getVlans ();
LOGGER.debug ("Network " + networkNameOrId
+ " found ("
+ mode
+ "), ID = "
+ networkId.value
+ ("HEAT".equals (mode) ? ",NeutronId = " + neutronNetworkId.value : ""));
} else {
// Not found. Populate the status fields, leave the rest null
networkExists.value = Boolean.FALSE;
status.value = NetworkStatus.NOTFOUND;
neutronNetworkId.value = null;
if (vlans != null)
vlans.value = new ArrayList <Integer> ();
LOGGER.debug ("Network " + networkNameOrId + " not found");
}
} catch (MsoException me) {
me.addContext ("QueryNetwork");
String error = "Query Network (neutron): " + networkNameOrId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (queryNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "QueryNetwork", null);
LOGGER.error (MessageEnum.RA_QUERY_NETWORK_EXC, networkNameOrId, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - Query Network (neutron)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully queried network");
return;
}
/**
* This is the "Delete Network" web service implementation.
* It will delete a Network in the specified cloud and tenant.
*
* If the network is not found, it is treated as a success.
*
* This service supports two modes of Network creation/update/delete:
* - via Heat Templates
* - via Neutron API
* The network orchestration mode for each network type is declared in its
* catalog definition.
*
* For Heat-based orchestration, the networkId should be the stack ID.
* For Neutron-based orchestration, the networkId should be the Neutron network UUID.
*
* The method returns nothing on success. Rollback is not possible for delete
* commands, so any failure on delete will require manual fallout in the client.
*/
@Override
public void deleteNetwork (String cloudSiteId,
String tenantId,
String networkType,
String networkId,
MsoRequest msoRequest,
Holder <Boolean> networkDeleted) throws NetworkException {
MsoLogger.setLogContext (msoRequest);
MsoLogger.setServiceName ("DeleteNetwork");
LOGGER.debug ("*** DELETE Network adapter with Network: " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId);
// Will capture execution time for metrics
long startTime = System.currentTimeMillis ();
// Get a handle to the Catalog Database
CatalogDatabase db = getCatalogDB ();
// Make sure DB connection is always closed
try {
if (isNullOrEmpty (cloudSiteId)
|| isNullOrEmpty(tenantId)
|| isNullOrEmpty(networkId)) {
String error = "Missing mandatory parameter cloudSiteId, tenantId or networkId";
LOGGER.error (MessageEnum.RA_MISSING_PARAM, "cloudSiteId or tenantId or networkId", "Openstack", "", MsoLogger.ErrorCode.DataError, "Missing mandatory parameter cloudSiteId, tenantId or networkId");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, error);
throw new NetworkException (error, MsoExceptionCategory.USERDATA);
}
// Retrieve the Network Resource definition
NetworkResource networkResource = db.getNetworkResource (networkType);
String mode = "";
if (networkResource != null) {
LOGGER.debug ("Got Network definition from Catalog: " + networkResource.toString ());
mode = networkResource.getOrchestrationMode ();
}
if (NEUTRON_MODE.equals (mode)) {
// Use MsoNeutronUtils for all NEUTRON commands
MsoNeutronUtils neutron = new MsoNeutronUtils (MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
long deleteNetworkStarttime = System.currentTimeMillis ();
try {
// The deleteNetwork function in MsoNeutronUtils returns success if the network
// was not found. So don't bother to query first.
boolean deleted = neutron.deleteNetwork (networkId, tenantId, cloudSiteId);
LOGGER.recordMetricEvent (deleteNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "DeleteNetwork", null);
networkDeleted.value = deleted;
} catch (MsoException me) {
me.addContext ("DeleteNetwork");
String error = "Delete Network (neutron): " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (deleteNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "DeleteNetwork", null);
LOGGER.error (MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Delete Network (neutron)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
} else { // DEFAULT to ("HEAT".equals (mode))
long deleteStackStarttime = System.currentTimeMillis ();
// Use MsoHeatUtils for all HEAT commands
MsoHeatUtils heat = new MsoHeatUtils (MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory,cloudConfigFactory);
try {
// The deleteStack function in MsoHeatUtils returns NOTFOUND if the stack was not found or if the stack was deleted.
// So query first to report back if stack WAS deleted or just NOTOFUND
StackInfo heatStack = null;
heatStack = heat.queryStack(cloudSiteId, tenantId, networkId);
if (heatStack != null && heatStack.getStatus() != HeatStatus.NOTFOUND)
{
heat.deleteStack (tenantId, cloudSiteId, networkId, true);
LOGGER.recordMetricEvent (deleteStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "DeleteStack", null);
networkDeleted.value = true;
}
else
{
networkDeleted.value = false;
}
} catch (MsoException me) {
me.addContext ("DeleteNetwork");
String error = "Delete Network (heat): " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (deleteStackStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "DeleteStack", null);
LOGGER.error (MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Delete Network (heat)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
}
} finally {
db.close ();
}
// On success, nothing is returned.
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully deleted network");
return;
}
public CatalogDatabase getCatalogDB() {
return new CatalogDatabase();
}
/**
* This web service endpoint will rollback a previous Create VNF operation.
* A rollback object is returned to the client in a successful creation
* response. The client can pass that object as-is back to the rollbackVnf
* operation to undo the creation.
*
* The rollback includes removing the VNF and deleting the tenant if the
* tenant did not exist prior to the VNF creation.
*/
@Override
public void rollbackNetwork (NetworkRollback rollback) throws NetworkException {
MsoLogger.setServiceName ("RollbackNetwork");
// Will capture execution time for metrics
long startTime = System.currentTimeMillis ();
if (rollback == null) {
LOGGER.error (MessageEnum.RA_ROLLBACK_NULL, "Openstack", "", MsoLogger.ErrorCode.DataError, "rollback is null");
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "No action to perform");
return;
}
MsoLogger.setLogContext (rollback.getMsoRequest());
// Get the elements of the VnfRollback object for easier access
String cloudSiteId = rollback.getCloudId ();
String tenantId = rollback.getTenantId ();
String networkId = rollback.getNetworkStackId ();
String networkType = rollback.getNetworkType ();
LOGGER.debug ("*** ROLLBACK Network " + networkId + " in " + cloudSiteId + "/" + tenantId);
// rollback may be null (e.g. if network already existed when Create was called)
// Get a handle to the Catalog Database
CatalogDatabase db = getCatalogDB ();
// Make sure DB connection is always closed
try {
// Retrieve the Network Resource definition
NetworkResource networkResource = db.getNetworkResource (networkType);
String mode = "";
if (networkResource != null) {
LOGGER.debug ("Got Network definition from Catalog: " + networkResource.toString ());
mode = networkResource.getOrchestrationMode ();
}
if (rollback.getNetworkCreated ()) {
// Rolling back a newly created network, so delete it.
if (NEUTRON_MODE.equals (mode)) {
// Use MsoNeutronUtils for all NEUTRON commands
MsoNeutronUtils neutron = new MsoNeutronUtils (MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
long deleteNetworkStarttime = System.currentTimeMillis ();
try {
// The deleteNetwork function in MsoNeutronUtils returns success if the network
// was not found. So don't bother to query first.
neutron.deleteNetwork (networkId, tenantId, cloudSiteId);
LOGGER.recordMetricEvent (deleteNetworkStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "DeleteNetwork", null);
} catch (MsoException me) {
me.addContext ("RollbackNetwork");
String error = "Rollback Network (neutron): " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (deleteNetworkStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "DeleteNetwork", null);
LOGGER.error (MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - Rollback Network (neutron)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
} else { // DEFAULT to if ("HEAT".equals (mode))
// Use MsoHeatUtils for all HEAT commands
MsoHeatUtils heat = new MsoHeatUtils (MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory,cloudConfigFactory);
long deleteStackStarttime = System.currentTimeMillis ();
try {
// The deleteStack function in MsoHeatUtils returns success if the stack
// was not found. So don't bother to query first.
heat.deleteStack (tenantId, cloudSiteId, networkId, true);
LOGGER.recordMetricEvent (deleteStackStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "DeleteStack", null);
} catch (MsoException me) {
me.addContext ("RollbackNetwork");
String error = "Rollback Network (heat): " + networkId
+ " in "
+ cloudSiteId
+ "/"
+ tenantId
+ ": "
+ me;
LOGGER.recordMetricEvent (deleteStackStarttime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "DeleteStack", null);
LOGGER.error (MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId, "OpenStack", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - Rollback Network (heat)", me);
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
throw new NetworkException (me);
}
}
}
} finally {
db.close ();
}
LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully rolled back network");
return;
}
private String validateNetworkParams (NetworkType neutronNetworkType,
String networkName,
String physicalNetwork,
List <Integer> vlans,
List <String> routeTargets) {
String sep = "";
StringBuilder missing = new StringBuilder ();
if (isNullOrEmpty(networkName)) {
missing.append ("networkName");
sep = ",";
}
if (neutronNetworkType == NetworkType.PROVIDER || neutronNetworkType == NetworkType.MULTI_PROVIDER) {
if (isNullOrEmpty(physicalNetwork)) {
missing.append (sep).append ("physicalNetworkName");
sep = ",";
}
if (vlans == null || vlans.isEmpty ()) {
missing.append (sep).append (VLANS);
}
}
return missing.toString ();
}
private Map <String, Object> populateNetworkParams (NetworkType neutronNetworkType,
String networkName,
String physicalNetwork,
List <Integer> vlans,
List <String> routeTargets,
String shared,
String external,
boolean aic3template) {
// Build the common set of HEAT template parameters
Map <String, Object> stackParams = new HashMap <String, Object> ();
stackParams.put ("network_name", networkName);
if (neutronNetworkType == NetworkType.PROVIDER) {
// For Provider type
stackParams.put (PHYSICAL_NETWORK, physicalNetwork);
stackParams.put ("vlan", vlans.get (0).toString ());
} else if (neutronNetworkType == NetworkType.MULTI_PROVIDER) {
// For Multi-provider, PO supports a custom resource extension of ProviderNet.
// It supports all ProviderNet properties except segmentation_id, and adds a
// comma-separated-list of VLANs as a "segments" property.
// Note that this does not match the Neutron definition of Multi-Provider network,
// which contains a list of 'segments', each having physical_network, network_type,
// and segmentation_id.
StringBuilder buf = new StringBuilder ();
String sep = "";
for (Integer vlan : vlans) {
buf.append (sep).append (vlan.toString ());
sep = ",";
}
String csl = buf.toString ();
stackParams.put (PHYSICAL_NETWORK, physicalNetwork);
stackParams.put (VLANS, csl);
}
if (routeTargets != null && !routeTargets.isEmpty()) {
StringBuilder buf = new StringBuilder ();
String sep = "";
for (String rt : routeTargets) {
if (!isNullOrEmpty(rt))
{
if (aic3template)
buf.append (sep).append ("target:" + rt.toString ());
else
buf.append (sep).append (rt.toString ());
sep = ",";
}
}
String csl = buf.toString ();
stackParams.put ("route_targets", csl);
}
if (isNullOrEmpty(shared)) {
stackParams.put ("shared", "False");
} else {
stackParams.put ("shared", shared);
}
if (isNullOrEmpty(external)) {
stackParams.put ("external", "False");
} else {
stackParams.put ("external", external);
}
return stackParams;
}
/** policyRef_list structure in stackParams
[
{
"network_policy_refs_data_sequence": {
"network_policy_refs_data_sequence_major": "1",
"network_policy_refs_data_sequence_minor": "0"
}
},
{
"network_policy_refs_data_sequence": {
"network_policy_refs_data_sequence_major": "2",
"network_policy_refs_data_sequence_minor": "0"
}
}
]
**/
private void mergePolicyRefs(List <String> pFqdns, Map <String, Object> stackParams) throws MsoException {
//Resource Property
List<ContrailPolicyRef> prlist = new ArrayList <ContrailPolicyRef> ();
int index = 1;
for (String pf : pFqdns) {
if (!isNullOrEmpty(pf))
{
ContrailPolicyRef pr = new ContrailPolicyRef();
pr.populate(String.valueOf(index), "0");
index++;
LOGGER.debug("Contrail PolicyRefs Data:" + pr.toString());
prlist.add(pr);
}
}
JsonNode node = null;
try
{
ObjectMapper mapper = new ObjectMapper();
node = mapper.convertValue(prlist, JsonNode.class);
String jsonString = mapper.writeValueAsString(prlist);
LOGGER.debug("Json PolicyRefs Data:" + jsonString);
}
catch (Exception e)
{
String error = "Error creating JsonNode for policyRefs Data";
LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "Openstack", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception creating JsonNode for policyRefs Data", e);
throw new MsoAdapterException (error);
}
//update parameters
if (pFqdns != null && node != null)
{
StringBuilder buf = new StringBuilder ();
String sep = "";
for (String pf : pFqdns) {
if (!isNullOrEmpty(pf))
{
buf.append (sep).append (pf.toString ());
sep = ",";
}
}
String csl = buf.toString ();
stackParams.put ("policy_refs", csl);
stackParams.put ("policy_refsdata", node);
}
LOGGER.debug ("StackParams updated with policy refs");
return;
}
private void mergeRouteTableRefs(List <String> rtFqdns, Map <String, Object> stackParams) throws MsoException {
//update parameters
if (rtFqdns != null)
{
StringBuilder buf = new StringBuilder ();
String sep = "";
for (String rtf : rtFqdns) {
if (!isNullOrEmpty(rtf))
{
buf.append (sep).append (rtf.toString ());
sep = ",";
}
}
String csl = buf.toString ();
stackParams.put ("route_table_refs", csl);
}
LOGGER.debug ("StackParams updated with route_table refs");
return;
}
/*** Subnet Output structure from Juniper
{
"ipam_subnets": [
{
"subnet": {
"ip_prefix": "10.100.1.0",
"ip_prefix_len": 28
},
"addr_from_start": null,
"enable_dhcp": false,
"default_gateway": "10.100.1.1",
"dns_nameservers": [],
"dhcp_option_list": null,
"subnet_uuid": "10391fbf-6b9c-4160-825d-2d018b7649cf",
"allocation_pools": [
{
"start": "10.100.1.3",
"end": "10.100.1.5"
},
{
"start": "10.100.1.6",
"end": "10.100.1.9"
}
],
"host_routes": null,
"dns_server_address": "10.100.1.13",
"subnet_name": "subnet_MsoNW1_692c9032-e1a2-4d64-828c-7b9a4fcc05b0"
},
{
"subnet": {
"ip_prefix": "10.100.2.16",
"ip_prefix_len": 28
},
"addr_from_start": null,
"enable_dhcp": true,
"default_gateway": "10.100.2.17",
"dns_nameservers": [],
"dhcp_option_list": null,
"subnet_uuid": "c7aac5ea-66fe-443a-85f9-9c38a608c0f6",
"allocation_pools": [
{
"start": "10.100.2.18",
"end": "10.100.2.20"
}
],
"host_routes": null,
"dns_server_address": "10.100.2.29",
"subnet_name": "subnet_MsoNW1_692c9032-e1a2-4d64-828c-7b9a4fcc05b1"
}
],
"host_routes": null
}
***/
private String mergeSubnetsAIC3 (String heatTemplate, List <Subnet> subnets, Map <String, Object> stackParams) throws MsoException {
//Resource Property
List<ContrailSubnet> cslist = new ArrayList <ContrailSubnet> ();
for (Subnet subnet : subnets) {
ContrailSubnet cs = new ContrailSubnet();
LOGGER.debug("Input Subnet:" + subnet.toString());
cs.populateWith(subnet);
LOGGER.debug("Contrail Subnet:" + cs.toString());
cslist.add(cs);
}
JsonNode node = null;
try
{
ObjectMapper mapper = new ObjectMapper();
node = mapper.convertValue(cslist, JsonNode.class);
String jsonString = mapper.writeValueAsString(cslist);
LOGGER.debug("Json Subnet List:" + jsonString);
}
catch (Exception e)
{
String error = "Error creating JsonNode from input subnets";
LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.DataError, "Exception creating JsonNode from input subnets", e);
throw new MsoAdapterException (error);
}
//update parameters
if (node != null)
{
stackParams.put ("subnet_list", node);
}
//Outputs - All subnets are in one ipam_subnets structure
String outputTempl = " subnet:\n" + " description: Openstack subnet identifier\n"
+ " value: { get_attr: [network, network_ipam_refs, 0, attr]}\n";
// append outputs in heatTemplate
int outputsIdx = heatTemplate.indexOf ("outputs:");
heatTemplate = insertStr (heatTemplate, outputTempl, outputsIdx + 8);
LOGGER.debug ("Template updated with all AIC3.0 subnets:" + heatTemplate);
return heatTemplate;
}
private String mergeSubnets (String heatTemplate, List <Subnet> subnets) throws MsoException {
String resourceTempl = " subnet_%subnetId%:\n" + " type: OS::Neutron::Subnet\n"
+ " properties:\n"
+ " name: %name%\n"
+ " network_id: { get_resource: network }\n"
+ " cidr: %cidr%\n";
/* make these optional
+ " ip_version: %ipversion%\n"
+ " enable_dhcp: %enabledhcp%\n"
+ " gateway_ip: %gatewayip%\n"
+ " allocation_pools:\n"
+ " - start: %poolstart%\n"
+ " end: %poolend%\n";
*/
String outputTempl = " subnet_id_%subnetId%:\n" + " description: Openstack subnet identifier\n"
+ " value: {get_resource: subnet_%subnetId%}\n";
String curR = "";
String curO = "";
StringBuilder resourcesBuf = new StringBuilder ();
StringBuilder outputsBuf = new StringBuilder ();
for (Subnet subnet : subnets) {
// build template for each subnet
curR = resourceTempl;
if (subnet.getSubnetId () != null) {
curR = curR.replace ("%subnetId%", subnet.getSubnetId ());
} else {
String error = "Missing Required AAI SubnetId for subnet in HEAT Template";
LOGGER.error (MessageEnum.RA_MISSING_PARAM, error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Missing Required AAI ID for subnet in HEAT Template");
throw new MsoAdapterException (error);
}
if (subnet.getSubnetName () != null) {
curR = curR.replace ("%name%", subnet.getSubnetName ());
} else {
curR = curR.replace ("%name%", subnet.getSubnetId ());
}
if (subnet.getCidr () != null) {
curR = curR.replace ("%cidr%", subnet.getCidr ());
} else {
String error = "Missing Required cidr for subnet in HEAT Template";
LOGGER.error (MessageEnum.RA_MISSING_PARAM, error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Missing Required cidr for subnet in HEAT Template");
throw new MsoAdapterException (error);
}
if (subnet.getIpVersion () != null) {
curR = curR + " ip_version: " + subnet.getIpVersion () + "\n";
}
if (subnet.getEnableDHCP () != null) {
curR = curR + " enable_dhcp: " + Boolean.toString (subnet.getEnableDHCP ()) + "\n";
}
if (subnet.getGatewayIp () != null && !subnet.getGatewayIp ().isEmpty() ) {
curR = curR + " gateway_ip: " + subnet.getGatewayIp () + "\n";
}
if (subnet.getAllocationPools() != null) {
curR = curR + " allocation_pools:\n";
for (Pool pool : subnet.getAllocationPools())
{
if (!isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()))
{
curR = curR + " - start: " + pool.getStart () + "\n";
curR = curR + " end: " + pool.getEnd () + "\n";
}
}
}
resourcesBuf.append (curR);
curO = outputTempl;
curO = curO.replace ("%subnetId%", subnet.getSubnetId ());
outputsBuf.append (curO);
}
// append resources and outputs in heatTemplate
LOGGER.debug ("Tempate initial:" + heatTemplate);
int outputsIdx = heatTemplate.indexOf ("outputs:");
heatTemplate = insertStr (heatTemplate, outputsBuf.toString (), outputsIdx + 8);
int resourcesIdx = heatTemplate.indexOf ("resources:");
heatTemplate = insertStr (heatTemplate, resourcesBuf.toString (), resourcesIdx + 10);
LOGGER.debug ("Template updated with all subnets:" + heatTemplate);
return heatTemplate;
}
private Map <String, String> getSubnetUUId(String key, Map <String, Object> outputs, List <Subnet> subnets) {
Map <String, String> sMap = new HashMap <String, String> ();
try{
Object obj = outputs.get(key);
ObjectMapper mapper = new ObjectMapper();
String jStr = mapper.writeValueAsString(obj);
LOGGER.debug ("Subnet_Ipam Output JSON String:" + obj.getClass() + " " + jStr);
JsonNode rootNode = mapper.readTree(jStr);
for (JsonNode sNode : rootNode.path("ipam_subnets"))
{
LOGGER.debug("Output Subnet Node" + sNode.toString());
String name = sNode.path("subnet_name").getTextValue();
String uuid = sNode.path("subnet_uuid").getTextValue();
String aaiId = name; // default
// try to find aaiId for name in input subnetList
if (subnets != null)
{
for (Subnet subnet : subnets)
{
if ( subnet != null && !isNullOrEmpty(subnet.getSubnetName()))
{
if (subnet.getSubnetName().equals(name))
{
aaiId = subnet.getSubnetId();
break;
}
}
}
}
sMap.put(aaiId, uuid); //bpmn needs aaid to uuid map
}
}
catch (Exception e)
{
LOGGER.error (MessageEnum.RA_MARSHING_ERROR, "error getting subnet-uuids", "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception getting subnet-uuids", e);
}
LOGGER.debug ("Return sMap" + sMap.toString());
return sMap;
}
private static String insertStr (String template, String snippet, int index) {
String updatedTemplate = "";
LOGGER.debug ("Index:" + index + " Snippet:" + snippet);
String templateBeg = template.substring (0, index);
String templateEnd = template.substring (index);
updatedTemplate = templateBeg + "\n" + snippet + templateEnd;
LOGGER.debug ("Template updated with a subnet:" + updatedTemplate);
return updatedTemplate;
}
}
|