aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/MsoVnfAdapterImpl.java
blob: 9adcdcb47acadda36f596bc907dfd596d437b9b8 (plain)
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
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * Modifications Copyright (c) 2019 IBM
 * ================================================================================
 * 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.onap.so.adapters.vnf;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.jws.WebService;
import javax.xml.ws.Holder;
import org.apache.commons.collections.CollectionUtils;
import org.onap.so.adapters.valet.GenericValetResponse;
import org.onap.so.adapters.valet.ValetClient;
import org.onap.so.adapters.valet.beans.HeatRequest;
import org.onap.so.adapters.valet.beans.ValetConfirmResponse;
import org.onap.so.adapters.valet.beans.ValetCreateResponse;
import org.onap.so.adapters.valet.beans.ValetDeleteResponse;
import org.onap.so.adapters.valet.beans.ValetRollbackResponse;
import org.onap.so.adapters.valet.beans.ValetStatus;
import org.onap.so.adapters.valet.beans.ValetUpdateResponse;
import org.onap.so.adapters.vnf.exceptions.VnfException;
import org.onap.so.adapters.vnf.exceptions.VnfNotFound;
import org.onap.so.client.aai.AAIResourcesClient;
import org.onap.so.cloud.CloudConfig;
import org.onap.so.db.catalog.beans.CloudIdentity;
import org.onap.so.db.catalog.beans.CloudSite;
import org.onap.so.db.catalog.beans.HeatEnvironment;
import org.onap.so.db.catalog.beans.HeatFiles;
import org.onap.so.db.catalog.beans.HeatTemplate;
import org.onap.so.db.catalog.beans.HeatTemplateParam;
import org.onap.so.db.catalog.beans.VfModule;
import org.onap.so.db.catalog.beans.VfModuleCustomization;
import org.onap.so.db.catalog.beans.VnfResource;
import org.onap.so.db.catalog.data.repository.VFModuleCustomizationRepository;
import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
import org.onap.so.db.catalog.utils.MavenLikeVersioning;
import org.onap.so.entity.MsoRequest;
import org.onap.so.heatbridge.HeatBridgeApi;
import org.onap.so.heatbridge.HeatBridgeImpl;
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.LoggingAnchor;
import org.onap.so.logger.MessageEnum;
import org.onap.so.openstack.beans.HeatStatus;
import org.onap.so.openstack.beans.StackInfo;
import org.onap.so.openstack.beans.VnfRollback;
import org.onap.so.openstack.beans.VnfStatus;
import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
import org.onap.so.openstack.exceptions.MsoException;
import org.onap.so.openstack.exceptions.MsoExceptionCategory;
import org.onap.so.openstack.exceptions.MsoHeatNotFoundException;
import org.onap.so.openstack.utils.MsoHeatEnvironmentEntry;
import org.onap.so.openstack.utils.MsoHeatUtils;
import org.onap.so.openstack.utils.MsoHeatUtilsWithUpdate;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.Image;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.heat.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebService(serviceName = "VnfAdapter", endpointInterface = "org.onap.so.adapters.vnf.MsoVnfAdapter",
        targetNamespace = "http://org.onap.so/vnf")
@Component
@Transactional
public class MsoVnfAdapterImpl implements MsoVnfAdapter {

    @Autowired
    private CloudConfig cloudConfig;

    @Autowired
    private Environment environment;

    private static final Logger logger = LoggerFactory.getLogger(MsoVnfAdapterImpl.class);


    private static final String VNF_ADAPTER_SERVICE_NAME = "MSO-BPMN:MSO-VnfAdapter.";
    private static final String CHECK_REQD_PARAMS = "org.onap.so.adapters.vnf.checkRequiredParameters";
    private static final String ADD_GET_FILES_ON_VOLUME_REQ = "org.onap.so.adapters.vnf.addGetFilesOnVolumeReq";
    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
    private static final String VALET_ENABLED = "org.onap.so.adapters.vnf.valet_enabled";
    private static final String FAIL_REQUESTS_ON_VALET_FAILURE =
            "org.onap.so.adapters.vnf.fail_requests_on_valet_failure";
    private static final String OPENSTACK = "OpenStack";
    private static final String DELETE_VNF = "DeleteVNF";
    private static final String QUERY_STACK = "QueryStack";
    private static final String CREATE_VFM_MODULE = "CreateVFModule";
    private static final String CREATE_VF_STACK = "Create VF: Stack";
    private static final String STACK = "Stack";
    private static final String USER_ERROR = "USER ERROR";
    private static final String VERSION_MIN = "VersionMin";
    private static final String VERSION_MAX = "VersionMax";

    @Autowired
    private VFModuleCustomizationRepository vfModuleCustomRepo;
    @Autowired
    private VnfResourceRepository vnfResourceRepo;
    @Autowired
    private MsoHeatUtilsWithUpdate heatU;
    @Autowired
    private MsoHeatUtils msoHeatUtils;
    @Autowired
    private ValetClient vci;

    /**
     * DO NOT use that constructor to instantiate this class, the msoPropertiesfactory will be NULL.
     *
     * @see MsoVnfAdapterImpl#MsoVnfAdapterImpl(MsoPropertiesFactory, CloudConfigFactory)
     */
    public MsoVnfAdapterImpl() {
        // Do nothing
        // DO NOT use that constructor to instantiate this class, the msoPropertiesfactory will be NULL.
    }

    /**
     * Health Check web method. Does nothing but return to show the adapter is deployed.
     */
    @Override
    public void healthCheck() {
        logger.debug("Health check call in VNF Adapter");
    }

    /**
     * This is the "Create VNF" web service implementation. It will create a new VNF of the requested type in the
     * specified cloud and tenant. The tenant must exist before this service is called.
     *
     * If a VNF with the same name already exists, this can be considered a success or failure, depending on the value
     * of the 'failIfExists' parameter.
     *
     * All VNF types will be defined in the MSO catalog. The caller must request one of these pre-defined types or an
     * error will be returned. Within the catalog, each VNF type references (among other things) a Heat template which
     * is used to deploy the required VNF artifacts (VMs, networks, etc.) to the cloud.
     *
     * Depending on the Heat template, a variable set of input parameters will be defined, some of which are required.
     * The caller is responsible to pass the necessary input data for the VNF or an error will be thrown.
     *
     * The method returns the vnfId (the canonical name), a Map of VNF output attributes, and a VnfRollback object. This
     * last object can be passed as-is to the rollbackVnf operation to undo everything that was created for the VNF.
     * This is useful if a VNF is successfully created but the orchestrator fails on a subsequent operation.
     *
     * @param cloudSiteId CLLI code of the cloud site in which to create the VNF
     * @param cloudOwner cloud owner of the cloud region in which to create the VNF
     * @param tenantId Openstack tenant identifier
     * @param vnfType VNF type key, should match a VNF definition in catalog DB
     * @param vnfVersion VNF version key, should match a VNF definition in catalog DB
     * @param vnfName Name to be assigned to the new VNF
     * @param inputs Map of key=value inputs for VNF stack creation
     * @param failIfExists Flag whether already existing VNF should be considered a success or failure
     * @param msoRequest Request tracking information for logs
     * @param vnfId Holder for output VNF Openstack ID
     * @param outputs Holder for Map of VNF outputs from heat (assigned IPs, etc)
     * @param rollback Holder for returning VnfRollback object
     */
    @Override
    public void createVnf(String cloudSiteId, String cloudOwner, String tenantId, String vnfType, String vnfVersion,
            String vnfName, String requestType, String volumeGroupHeatStackId, Map<String, Object> inputs,
            Boolean failIfExists, Boolean backout, Boolean enableBridge, MsoRequest msoRequest, Holder<String> vnfId,
            Holder<Map<String, String>> outputs, Holder<VnfRollback> rollback) throws VnfException {
        // parameters used for multicloud adapter
        String genericVnfId = "";
        String vfModuleId = "";
        // Create a hook here to catch shortcut createVf requests:
        if (requestType != null && requestType.startsWith("VFMOD")) {
            logger.debug("Calling createVfModule from createVnf -- requestType={}", requestType);
            String newRequestType = requestType.substring(5);
            String vfVolGroupHeatStackId = "";
            String vfBaseHeatStackId = "";
            try {
                if (volumeGroupHeatStackId != null) {
                    vfVolGroupHeatStackId =
                            volumeGroupHeatStackId.substring(0, volumeGroupHeatStackId.lastIndexOf('|'));
                    vfBaseHeatStackId = volumeGroupHeatStackId.substring(volumeGroupHeatStackId.lastIndexOf('|') + 1);
                }
            } catch (Exception e) {
                // might be ok - both are just blank
                logger.debug("ERROR trying to parse the volumeGroupHeatStackId {}", volumeGroupHeatStackId, e);
            }
            this.createVfModule(cloudSiteId, cloudOwner, tenantId, vnfType, vnfVersion, genericVnfId, vnfName,
                    vfModuleId, newRequestType, vfVolGroupHeatStackId, vfBaseHeatStackId, null, inputs, failIfExists,
                    backout, enableBridge, msoRequest, vnfId, outputs, rollback);
            return;
        }
        // createVf will know if the requestType starts with "X" that it's the "old" way
        StringBuilder newRequestTypeSb = new StringBuilder("X");
        String vfVolGroupHeatStackId = "";
        String vfBaseHeatStackId = "";
        if (requestType != null) {
            newRequestTypeSb.append(requestType);
        }
        this.createVfModule(cloudSiteId, cloudOwner, tenantId, vnfType, vnfVersion, genericVnfId, vnfName, vfModuleId,
                newRequestTypeSb.toString(), vfVolGroupHeatStackId, vfBaseHeatStackId, null, inputs, failIfExists,
                backout, enableBridge, msoRequest, vnfId, outputs, rollback);
        return;
        // End createVf shortcut
    }

    @Override
    public void updateVnf(String cloudSiteId, String cloudOwner, String tenantId, String vnfType, String vnfVersion,
            String vnfName, String requestType, String volumeGroupHeatStackId, Map<String, Object> inputs,
            MsoRequest msoRequest, Holder<Map<String, String>> outputs, Holder<VnfRollback> rollback)
            throws VnfException {
        // As of 1707 - this method should no longer be called
        logger.debug("UpdateVnf called?? This should not be called any longer - update vfModule");
    }

    /**
     * This is the "Query VNF" web service implementation. It will look up a VNF by name or ID in the specified cloud
     * and tenant.
     *
     * The method returns an indicator that the VNF exists, its Openstack internal ID, its status, and the set of
     * outputs (from when the stack was created).
     *
     * @param cloudSiteId CLLI code of the cloud site in which to query
     * @param tenantId Openstack tenant identifier
     * @param vnfName VNF Name or Openstack ID
     * @param msoRequest Request tracking information for logs
     * @param vnfExists Flag reporting the result of the query
     * @param vnfId Holder for output VNF Openstack ID
     * @param outputs Holder for Map of VNF outputs from heat (assigned IPs, etc)
     */
    @Override
    public void queryVnf(String cloudSiteId, String cloudOwner, String tenantId, String vnfName, MsoRequest msoRequest,
            Holder<Boolean> vnfExists, Holder<String> vnfId, Holder<VnfStatus> status,
            Holder<Map<String, String>> outputs) throws VnfException {

        logger.debug("Querying VNF {} in {}/{}", vnfName, cloudSiteId, tenantId);

        // Will capture execution time for metrics

        StackInfo heatStack;
        try {
            heatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, vnfName);
        } catch (MsoException me) {
            me.addContext("QueryVNF");
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            String error =
                    "Query VNF: " + vnfName + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.EIGHT, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudSiteId, tenantId,
                    OPENSTACK, "QueryVNF", ErrorCode.DataError.getValue(), "Exception - " + QUERY_STACK, me);
            logger.debug(error);
            throw new VnfException(me);
        }

        // Populate the outputs based on the returned Stack information
        //
        if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
            // Not Found
            vnfExists.value = Boolean.FALSE;
            status.value = VnfStatus.NOTFOUND;
            vnfId.value = null;
            outputs.value = new HashMap<>(); // Return as an empty map

            logger.debug("VNF {} not found", vnfName);
        } else {
            vnfExists.value = Boolean.TRUE;
            status.value = stackStatusToVnfStatus(heatStack.getStatus());
            vnfId.value = heatStack.getCanonicalName();
            outputs.value = copyStringOutputs(heatStack.getOutputs());

            logger.debug("VNF {} found, ID = {}", vnfName, vnfId.value);
        }
        return;
    }

    /**
     * This is the "Delete VNF" web service implementation. It will delete a VNF by name or ID in the specified cloud
     * and tenant.
     *
     * The method has no outputs.
     *
     * @param cloudSiteId CLLI code of the cloud site in which to delete
     * @param cloudOwner cloud owner of the cloud region in which to delete
     * @param tenantId Openstack tenant identifier
     * @param vnfName VNF Name or Openstack ID
     * @param msoRequest Request tracking information for logs
     */
    @Override
    public void deleteVnf(String cloudSiteId, String cloudOwner, String tenantId, String vnfName, MsoRequest msoRequest)
            throws VnfException {

        logger.debug("Deleting VNF {} in {}", vnfName, cloudSiteId + "/" + tenantId);

        try {
            msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfName, true, 118);
        } catch (MsoException me) {
            me.addContext(DELETE_VNF);
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            String error =
                    "Delete VNF: " + vnfName + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_DELETE_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId,
                    tenantId, OPENSTACK, DELETE_VNF, ErrorCode.DataError.getValue(), "Exception - " + DELETE_VNF, me);
            logger.debug(error);
            throw new VnfException(me);
        }

        // On success, nothing is returned.
        return;
    }

    /**
     * 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.
     */
    @Override
    public void rollbackVnf(VnfRollback rollback) throws VnfException {
        // rollback may be null (e.g. if stack already existed when Create was called)
        if (rollback == null) {
            logger.info(MessageEnum.RA_ROLLBACK_NULL.toString(), OPENSTACK, "rollbackVnf");
            return;
        }

        // Get the elements of the VnfRollback object for easier access
        String cloudSiteId = rollback.getCloudSiteId();
        String cloudOwner = rollback.getCloudOwner();
        String tenantId = rollback.getTenantId();
        String vnfId = rollback.getVnfId();

        logger.debug("Rolling Back VNF {} in {}", vnfId, cloudOwner + "/" + cloudSiteId + "/" + tenantId);

        // Use the MsoHeatUtils to delete the stack. Set the polling flag to true.
        // The possible outcomes of deleteStack are a StackInfo object with status
        // of NOTFOUND (on success) or FAILED (on error). Also, MsoOpenstackException
        // could be thrown.
        try {
            msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfId, true, 118);
        } catch (MsoException me) {
            // Failed to rollback the Stack due to an openstack exception.
            // Convert to a generic VnfException
            me.addContext("RollbackVNF");
            String error =
                    "Rollback VNF: " + vnfId + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_DELETE_VNF_ERR.toString(), vnfId, cloudOwner, cloudSiteId,
                    tenantId, OPENSTACK, "DeleteStack", ErrorCode.DataError.getValue(), "Exception - DeleteStack", me);
            logger.debug(error);
            throw new VnfException(me);
        }
        return;
    }

    private VnfStatus stackStatusToVnfStatus(HeatStatus stackStatus) {
        switch (stackStatus) {
            case CREATED:
                return VnfStatus.ACTIVE;
            case UPDATED:
                return VnfStatus.ACTIVE;
            case FAILED:
                return VnfStatus.FAILED;
            default:
                return VnfStatus.UNKNOWN;
        }
    }

    private Map<String, String> copyStringOutputs(Map<String, Object> stackOutputs) {
        Map<String, String> stringOutputs = new HashMap<>();
        for (Map.Entry<String, Object> entry : stackOutputs.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            try {
                stringOutputs.put(key, value.toString());
            } catch (Exception e) {
                StringBuilder msg = new StringBuilder("Unable to add " + key + " to outputs");
                if (value instanceof Integer) { // nothing to add to the message
                } else if (value instanceof JsonNode) {
                    msg.append(" - exception converting JsonNode");
                } else if (value instanceof java.util.LinkedHashMap) {
                    msg.append(" exception converting LinkedHashMap");
                } else {
                    msg.append(" - unable to call .toString() " + e.getMessage());
                }
                logger.debug(msg.toString(), e);
            }
        }
        return stringOutputs;
    }

    private Map<String, Object> copyStringInputs(Map<String, Object> stringInputs) {
        return new HashMap<>(stringInputs);
    }

    private void heatbridge(StackInfo heatStack, String cloudOwner, String cloudSiteId, String tenantId,
            String genericVnfName, String vfModuleId) {
        try {
            CloudSite cloudSite =
                    cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
            CloudIdentity cloudIdentity = cloudSite.getIdentityService();
            String heatStackId = heatStack.getCanonicalName().split("/")[1];

            List<String> oobMgtNetNames = new ArrayList<>();

            HeatBridgeApi heatBridgeClient =
                    new HeatBridgeImpl(new AAIResourcesClient(), cloudIdentity, cloudOwner, cloudSiteId, tenantId);

            List<Resource> stackResources = heatBridgeClient.queryNestedHeatStackResources(heatStackId);

            List<Server> osServers = heatBridgeClient.getAllOpenstackServers(stackResources);

            List<Image> osImages = heatBridgeClient.extractOpenstackImagesFromServers(osServers);

            List<Flavor> osFlavors = heatBridgeClient.extractOpenstackFlavorsFromServers(osServers);

            logger.debug("Successfully queried heat stack{} for resources.", heatStackId);
            // os images
            if (osImages != null && !osImages.isEmpty()) {
                heatBridgeClient.buildAddImagesToAaiAction(osImages);
                logger.debug("Successfully built AAI actions to add images.");
            } else {
                logger.debug("No images to update to AAI.");
            }
            // flavors
            if (osFlavors != null && !osFlavors.isEmpty()) {
                heatBridgeClient.buildAddFlavorsToAaiAction(osFlavors);
                logger.debug("Successfully built AAI actions to add flavors.");
            } else {
                logger.debug("No flavors to update to AAI.");
            }

            // compute resources
            heatBridgeClient.buildAddVserversToAaiAction(genericVnfName, vfModuleId, osServers);
            logger.debug("Successfully queried compute resources and built AAI vserver actions.");

            // neutron resources
            List<String> oobMgtNetIds = new ArrayList<>();

            // if no network-id list is provided, however network-name list is
            if (!CollectionUtils.isEmpty(oobMgtNetNames)) {
                oobMgtNetIds = heatBridgeClient.extractNetworkIds(oobMgtNetNames);
            }
            heatBridgeClient.buildAddVserverLInterfacesToAaiAction(stackResources, oobMgtNetIds);
            logger.debug(
                    "Successfully queried neutron resources and built AAI actions to add l-interfaces to vservers.");

            // Update AAI
            heatBridgeClient.submitToAai();
        } catch (Exception ex) {
            logger.debug("Heatbrige failed for stackId: " + heatStack.getCanonicalName(), ex);
        }
    }

    private String convertNode(final JsonNode node) {
        try {
            final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
            return JSON_MAPPER.writeValueAsString(obj);
        } catch (JsonParseException jpe) {
            logger.debug("Error converting json to string: {}", jpe.getMessage(), jpe);
        } catch (Exception e) {
            logger.debug("Error converting json to string: {}", e.getMessage(), e);
        }
        return "[Error converting json to string]";
    }

    private Map<String, String> convertMapStringObjectToStringString(Map<String, Object> objectMap) {
        if (objectMap == null) {
            return null;
        }
        Map<String, String> stringMap = new HashMap<>();
        for (String key : objectMap.keySet()) {
            if (!stringMap.containsKey(key)) {
                Object obj = objectMap.get(key);
                if (obj instanceof String) {
                    stringMap.put(key, (String) objectMap.get(key));
                } else if (obj instanceof JsonNode) {
                    // This is a bit of mess - but I think it's the least impacting
                    // let's convert it BACK to a string - then it will get converted back later
                    try {
                        String str = this.convertNode((JsonNode) obj);
                        stringMap.put(key, str);
                    } catch (Exception e) {
                        logger.debug("DANGER WILL ROBINSON: unable to convert value for JsonNode " + key, e);
                        // okay in this instance - only string values (fqdn) are expected to be needed
                    }
                } else if (obj instanceof java.util.LinkedHashMap) {
                    logger.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
                    try {
                        String str = JSON_MAPPER.writeValueAsString(obj);
                        stringMap.put(key, str);
                    } catch (Exception e) {
                        logger.debug("DANGER WILL ROBINSON: unable to convert value for LinkedHashMap " + key, e);
                    }
                } else if (obj instanceof Integer) {
                    try {
                        String str = "" + obj;
                        stringMap.put(key, str);
                    } catch (Exception e) {
                        logger.debug("DANGER WILL ROBINSON: unable to convert value for Integer " + key, e);
                    }
                } else {
                    try {
                        String str = obj.toString();
                        stringMap.put(key, str);
                    } catch (Exception e) {
                        logger.debug(
                                "DANGER WILL ROBINSON: unable to convert value " + key + " (" + e.getMessage() + ")",
                                e);
                    }
                }
            }
        }

        return stringMap;
    }

    @Override
    public void createVfModule(String cloudSiteId, String cloudOwner, String tenantId, String vnfType,
            String vnfVersion, String genericVnfName, String vnfName, String vfModuleId, String requestType,
            String volumeGroupHeatStackId, String baseVfHeatStackId, String modelCustomizationUuid,
            Map<String, Object> inputs, Boolean failIfExists, Boolean backout, Boolean enableBridge,
            MsoRequest msoRequest, Holder<String> vnfId, Holder<Map<String, String>> outputs,
            Holder<VnfRollback> rollback) throws VnfException {
        String vfModuleName = vnfName;
        String vfModuleType = vnfType;
        String vfVersion = vnfVersion;
        String mcu = modelCustomizationUuid;
        boolean useMCUuid = false;
        if (mcu != null && !mcu.isEmpty()) {
            if ("null".equalsIgnoreCase(mcu)) {
                logger.debug("modelCustomizationUuid: passed in as the string 'null' - will ignore: "
                        + modelCustomizationUuid);
                useMCUuid = false;
                mcu = "";
            } else {
                logger.debug("Found modelCustomizationUuid! Will use that: {}", mcu);
                useMCUuid = true;
            }
        }

        String requestTypeString = "";
        if (requestType != null && !"".equals(requestType)) {
            requestTypeString = requestType;
        }
        String nestedStackId = null;
        if (volumeGroupHeatStackId != null && !"".equals(volumeGroupHeatStackId)
                && !"null".equalsIgnoreCase(volumeGroupHeatStackId)) {
            nestedStackId = volumeGroupHeatStackId;
        }
        String nestedBaseStackId = null;
        if (baseVfHeatStackId != null && !"".equals(baseVfHeatStackId) && !"null".equalsIgnoreCase(baseVfHeatStackId)) {
            nestedBaseStackId = baseVfHeatStackId;
        }

        // This method will also handle doing things the "old" way - i.e., just orchestrate a VNF
        boolean oldWay = false;
        if (requestTypeString.startsWith("X")) {
            oldWay = true;
            logger.debug("orchestrating a VNF - *NOT* a module!");
            requestTypeString = requestTypeString.substring(1);
        }

        // let's parse out the request type we're being sent
        boolean isBaseRequest = false;
        boolean isVolumeRequest = false;
        if (requestTypeString.startsWith("VOLUME")) {
            isVolumeRequest = true;
        }

        logger.debug("requestTypeString = " + requestTypeString + ", nestedStackId = " + nestedStackId
                + ", nestedBaseStackId = " + nestedBaseStackId);

        // Build a default rollback object (no actions performed)
        VnfRollback vfRollback = new VnfRollback();
        vfRollback.setCloudSiteId(cloudSiteId);
        vfRollback.setCloudOwner(cloudOwner);
        vfRollback.setTenantId(tenantId);
        vfRollback.setMsoRequest(msoRequest);
        vfRollback.setRequestType(requestTypeString);
        vfRollback.setVolumeGroupHeatStackId(volumeGroupHeatStackId);
        vfRollback.setBaseGroupHeatStackId(baseVfHeatStackId);
        vfRollback.setIsBase(isBaseRequest);
        vfRollback.setModelCustomizationUuid(mcu);

        // handle a nestedStackId if sent- this one would be for the volume - so applies to both Vf and Vnf
        StackInfo nestedHeatStack = null;
        Map<String, Object> nestedVolumeOutputs = null;
        if (nestedStackId != null) {
            try {
                logger.debug("Querying for nestedStackId = {}", nestedStackId);
                nestedHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedStackId);
            } catch (MsoException me) {
                // Failed to query the Stack due to an openstack exception.
                // Convert to a generic VnfException
                me.addContext(CREATE_VFM_MODULE);
                String error = "Create VFModule: Attached heatStack ID Query " + nestedStackId + " in " + cloudOwner
                        + "/" + cloudSiteId + "/" + tenantId + ": " + me;
                logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.BusinessProcesssError.getValue(),
                        "MsoException trying to query nested stack", me);
                logger.debug("ERROR trying to query nested stack= {}", error);
                throw new VnfException(me);
            }
            if (nestedHeatStack == null || nestedHeatStack.getStatus() == HeatStatus.NOTFOUND) {
                String error = "Create VFModule: Attached heatStack ID DOES NOT EXIST " + nestedStackId + " in "
                        + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
                logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK,
                        ErrorCode.BusinessProcesssError.getValue(),
                        "Create VFModule: Attached heatStack ID " + "DOES NOT EXIST");
                logger.debug(error);
                throw new VnfException(error, MsoExceptionCategory.USERDATA);
            } else {
                logger.debug("Found nested volume heat stack - copying values to inputs *later*");
                nestedVolumeOutputs = nestedHeatStack.getOutputs();
            }
        }

        // handle a nestedBaseStackId if sent- this is the stack ID of the base. Should be null for VNF requests
        StackInfo nestedBaseHeatStack = null;
        Map<String, Object> baseStackOutputs = null;
        if (nestedBaseStackId != null) {
            try {
                logger.debug("Querying for nestedBaseStackId = {}", nestedBaseStackId);
                nestedBaseHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedBaseStackId);
            } catch (MsoException me) {
                // Failed to query the Stack due to an openstack exception.
                // Convert to a generic VnfException
                me.addContext(CREATE_VFM_MODULE);
                String error = "Create VFModule: Attached baseHeatStack ID Query " + nestedBaseStackId + " in "
                        + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
                logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.BusinessProcesssError.getValue(),
                        "MsoException trying to query nested base stack", me);
                logger.debug("ERROR trying to query nested base stack= {}", error);
                throw new VnfException(me);
            }
            if (nestedBaseHeatStack == null || nestedBaseHeatStack.getStatus() == HeatStatus.NOTFOUND) {
                String error = "Create VFModule: Attached base heatStack ID DOES NOT EXIST " + nestedBaseStackId
                        + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
                logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK,
                        ErrorCode.BusinessProcesssError.getValue(),
                        "Create VFModule: Attached base heatStack ID DOES NOT EXIST");
                logger.debug("Exception occurred", error);
                throw new VnfException(error, MsoExceptionCategory.USERDATA);
            } else {
                logger.debug("Found nested base heat stack - these values will be copied to inputs *later*");
                baseStackOutputs = nestedBaseHeatStack.getOutputs();
            }
        }

        try {
            VfModule vf = null;
            VnfResource vnfResource = null;
            VfModuleCustomization vfmc = null;
            if (useMCUuid) {
                vfmc = vfModuleCustomRepo.findFirstByModelCustomizationUUIDOrderByCreatedDesc(mcu);
                if (vfmc != null)
                    vf = vfmc.getVfModule();
                else
                    vf = null;

                // this will be the new way going forward. We find the vf by mcu - otherwise, code is the same.
                if (vf == null) {
                    logger.debug("Unable to find vfModuleCust with modelCustomizationUuid={}", mcu);
                    String error =
                            "Create vfModule error: Unable to find vfModuleCust with modelCustomizationUuid=" + mcu;
                    logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(),
                            "VF Module ModelCustomizationUuid", modelCustomizationUuid, OPENSTACK,
                            ErrorCode.DataError.getValue(),
                            "Create VF Module: Unable to find vfModule with " + "modelCustomizationUuid=" + mcu);
                    logger.debug(error);
                    throw new VnfException(error, MsoExceptionCategory.USERDATA);
                } else {
                    logger.trace("Found vfModuleCust entry {}", vfmc.toString());
                }
                if (vf.getIsBase()) {
                    isBaseRequest = true;
                    logger.debug("This is a BASE VF request!");
                } else {
                    logger.debug("This is *not* a BASE VF request!");
                    if (!isVolumeRequest && nestedBaseStackId == null) {
                        logger.debug(
                                "DANGER WILL ROBINSON! This is unexpected - no nestedBaseStackId with this non-base request");
                    }
                }
            }

            else { // This is to support gamma only - get info from vnf_resource table
                if (vfVersion != null && !vfVersion.isEmpty()) {
                    vnfResource = vnfResourceRepo.findByModelNameAndModelVersion(vnfType, vnfVersion);
                } else {
                    vnfResource = vnfResourceRepo.findByModelName(vnfType);
                }
                if (vnfResource == null) {
                    String error = "Create VNF: Unknown VNF Type: " + vnfType;
                    logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "VNF Type", vnfType,
                            OPENSTACK, ErrorCode.DataError.getValue(), "Create VNF: Unknown VNF Type");
                    logger.debug(error);
                    throw new VnfException(error, MsoExceptionCategory.USERDATA);
                }
                logger.debug("Got VNF module definition from Catalog: {}", vnfResource.toString());
            }
            // By here - we have either a vf or vnfResource

            // Add version check
            // First - see if it's in the VnfResource record
            // if we have a vf Module - then we have to query to get the VnfResource record.
            if (!oldWay) {
                if (vf != null) {
                    vnfResource = vf.getVnfResources();
                }
                if (vnfResource == null) {
                    logger.debug("Unable to find vnfResource will not error for now...");
                }
            }
            String minVersionVnf = null;
            String maxVersionVnf = null;
            if (vnfResource != null) {
                try {
                    minVersionVnf = vnfResource.getAicVersionMin();
                    maxVersionVnf = vnfResource.getAicVersionMax();
                } catch (Exception e) {
                    logger.debug("Unable to pull min/max version for this VNF Resource entry", e);
                    minVersionVnf = null;
                    maxVersionVnf = null;
                }
                if (minVersionVnf != null && "".equals(minVersionVnf)) {
                    minVersionVnf = null;
                }
                if (maxVersionVnf != null && "".equals(maxVersionVnf)) {
                    maxVersionVnf = null;
                }
            }
            if (minVersionVnf != null && maxVersionVnf != null) {
                MavenLikeVersioning aicV = new MavenLikeVersioning();

                // double check
                if (this.cloudConfig != null) {
                    Optional<CloudSite> cloudSiteOpt = this.cloudConfig.getCloudSite(cloudSiteId);
                    if (cloudSiteOpt.isPresent()) {
                        aicV.setVersion(cloudSiteOpt.get().getCloudVersion());
                        // Add code to handle unexpected values in here
                        boolean moreThanMin = true;
                        boolean equalToMin = true;
                        boolean moreThanMax = true;
                        boolean equalToMax = true;
                        boolean doNotTest = false;
                        try {
                            moreThanMin = aicV.isMoreRecentThan(minVersionVnf);
                            equalToMin = aicV.isTheSameVersion(minVersionVnf);
                            moreThanMax = aicV.isMoreRecentThan(maxVersionVnf);
                            equalToMax = aicV.isTheSameVersion(maxVersionVnf);
                        } catch (Exception e) {
                            logger.debug(
                                    "An exception occurred while trying to test Cloud Version {} - will default to not check",
                                    e.getMessage(), e);
                            doNotTest = true;
                        }
                        if (!doNotTest) {
                            if ((moreThanMin || equalToMin) // aic >= min
                                    && (equalToMax || !(moreThanMax))) { // aic <= max
                                logger.debug("VNF Resource " + vnfResource.getModelName() + ", ModelUuid="
                                        + vnfResource.getModelUUID() + " " + VERSION_MIN + " =" + minVersionVnf + " "
                                        + VERSION_MAX + " :" + maxVersionVnf + " supported on Cloud: " + cloudSiteId
                                        + " with AIC_Version:" + cloudSiteOpt.get().getCloudVersion());
                            } else {
                                // ERROR
                                String error = "VNF Resource type: " + vnfResource.getModelName() + ", ModelUuid="
                                        + vnfResource.getModelUUID() + " " + VERSION_MIN + " =" + minVersionVnf + " "
                                        + VERSION_MAX + " :" + maxVersionVnf + " NOT supported on Cloud: " + cloudSiteId
                                        + " with AIC_Version:" + cloudSiteOpt.get().getCloudVersion();
                                logger.error(LoggingAnchor.FIVE, MessageEnum.RA_CONFIG_EXC.toString(), error, OPENSTACK,
                                        ErrorCode.BusinessProcesssError.getValue(), "Exception - setVersion");
                                logger.debug(error);
                                throw new VnfException(error, MsoExceptionCategory.USERDATA);
                            }
                        } else {
                            logger.debug("bypassing testing Cloud version...");
                        }
                    } // let this error out downstream to avoid introducing uncertainty at this stage
                } else {
                    logger.debug("cloudConfig is NULL - cannot check cloud site version");
                }
            }

            // By the time we get here - heatTemplateId and heatEnvtId should be populated (or null)
            HeatTemplate heatTemplate = null;
            HeatEnvironment heatEnvironment = null;
            if (oldWay) {
                // This will handle old Gamma BrocadeVCE VNF
                heatTemplate = vnfResource.getHeatTemplates();
            } else {
                if (vf != null) {
                    if (isVolumeRequest) {
                        heatTemplate = vf.getVolumeHeatTemplate();
                        heatEnvironment = vfmc.getVolumeHeatEnv();
                    } else {
                        heatTemplate = vf.getModuleHeatTemplate();
                        heatEnvironment = vfmc.getHeatEnvironment();
                    }
                }
            }

            if (heatTemplate == null) {
                String error = "UpdateVF: No Heat Template ID defined in catalog database for " + vfModuleType
                        + ", modelCustomizationUuid=" + mcu + ", vfModuleUuid="
                        + (vf != null ? vf.getModelUUID() : "null") + ", vnfResourceModelUuid="
                        + vnfResource.getModelUUID() + ", reqType=" + requestTypeString;
                logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "Heat Template " + "ID",
                        vfModuleType, OPENSTACK, ErrorCode.DataError.getValue(), error);
                logger.debug(error);
                throw new VnfException(error, MsoExceptionCategory.INTERNAL);
            } else {
                logger.debug("Got HEAT Template from DB: {}", heatTemplate.getHeatTemplate());
            }

            if (oldWay) {
                // This will handle old Gamma BrocadeVCE VNF
                logger.debug("No environment parameter found for this Type " + vfModuleType);
            } else {
                if (heatEnvironment == null) {
                    String error = "Update VNF: undefined Heat Environment. VF=" + vfModuleType
                            + ", modelCustomizationUuid=" + mcu + ", vfModuleUuid="
                            + (vf != null ? vf.getModelUUID() : "null") + ", vnfResourceModelUuid="
                            + vnfResource.getModelUUID() + ", reqType=" + requestTypeString;
                    logger.error(LoggingAnchor.FIVE, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(),
                            "Heat " + "Environment ID", OPENSTACK, ErrorCode.DataError.getValue(), error);
                    logger.debug(error);
                    throw new VnfException(error, MsoExceptionCategory.INTERNAL);
                } else {
                    logger.debug("Got Heat Environment from DB: {}", heatEnvironment.getEnvironment());
                }
            }

            logger.debug("In MsoVnfAdapterImpl, about to call db.getNestedTemplates avec templateId="
                    + heatTemplate.getArtifactUuid());


            List<HeatTemplate> nestedTemplates = heatTemplate.getChildTemplates();
            Map<String, Object> nestedTemplatesChecked = new HashMap<>();
            if (nestedTemplates != null && !nestedTemplates.isEmpty()) {
                // for debugging print them out
                logger.debug("Contents of nestedTemplates - to be added to files: on stack:");
                for (HeatTemplate entry : nestedTemplates) {
                    nestedTemplatesChecked.put(entry.getTemplateName(), entry.getTemplateBody());
                    logger.debug("Adding Nested Template", entry.getTemplateName());
                }
            } else {
                logger.debug("No nested templates found - nothing to do here");
                nestedTemplatesChecked = null;
            }

            // Also add the files: for any get_files associated with this vnf_resource_id
            // *if* there are any
            List<HeatFiles> heatFiles = null;

            Map<String, Object> heatFilesObjects = new HashMap<>();

            // Add ability to turn on adding get_files with volume requests (by property).
            boolean addGetFilesOnVolumeReq = false;
            try {
                String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ);
                if ("true".equalsIgnoreCase(propertyString) || "y".equalsIgnoreCase(propertyString)) {
                    addGetFilesOnVolumeReq = true;
                    logger.debug("AddGetFilesOnVolumeReq - setting to true! {}", propertyString);
                }
            } catch (Exception e) {
                logger.debug("An error occured trying to get property " + MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ
                        + " - default to false", e);
            }

            if (!isVolumeRequest || addGetFilesOnVolumeReq) {
                if (oldWay) {
                    logger.debug("In MsoVnfAdapterImpl createVfModule, this should not happen, no heat files!");
                } else {
                    // now use VF_MODULE_TO_HEAT_FILES table
                    logger.debug(
                            "In MsoVnfAdapterImpl createVfModule, about to call db.getHeatFilesForVfModule avec vfModuleId="
                                    + vf.getModelUUID());
                    heatFiles = vf.getHeatFiles();
                }
                if (heatFiles != null && !heatFiles.isEmpty()) {
                    // add these to stack - to be done in createStack
                    // here, we will map them to Map<String, Object> from
                    // Map<String, HeatFiles>
                    // this will match the nested templates format
                    logger.debug("Contents of heatFiles - to be added to files: on stack");

                    for (HeatFiles heatfile : heatFiles) {
                        logger.debug(heatfile.getFileName() + " -> " + heatfile.getFileBody());
                        heatFilesObjects.put(heatfile.getFileName(), heatfile.getFileBody());
                    }
                } else {
                    logger.debug("No heat files found -nothing to do here");
                    heatFilesObjects = null;
                }
            }

            // Check that required parameters have been supplied
            String missingParams = null;
            List<String> paramList = new ArrayList<>();

            // consult the PARAM_ALIAS field to see if we've been
            // supplied an alias. Only check if we don't find it initially.
            // don't flag missing parameters if there's an environment - because they might be there.
            // And also new - add parameter to turn off checking all together if we find we're blocking orders we
            // shouldn't
            boolean checkRequiredParameters = true;
            try {
                String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
                if ("false".equalsIgnoreCase(propertyString) || "n".equalsIgnoreCase(propertyString)) {
                    checkRequiredParameters = false;
                    logger.debug("CheckRequiredParameters is FALSE. Will still check but then skip blocking..."
                            + MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
                }
            } catch (Exception e) {
                // No problem - default is true
                logger.debug("An exception occured trying to get property {}", MsoVnfAdapterImpl.CHECK_REQD_PARAMS, e);
            }
            // Part 1: parse envt entries to see if reqd parameter is there (before used a simple grep
            // Part 2: only submit to openstack the parameters in the envt that are in the heat template
            // Note this also removes any comments
            MsoHeatEnvironmentEntry mhee = null;
            if (heatEnvironment != null && heatEnvironment.getEnvironment() != null
                    && heatEnvironment.getEnvironment().contains("parameters:")) {

                StringBuilder sb = new StringBuilder(heatEnvironment.getEnvironment());

                mhee = new MsoHeatEnvironmentEntry(sb);
                StringBuilder sb2 = new StringBuilder("\nHeat Template Parameters:\n");
                for (HeatTemplateParam parm : heatTemplate.getParameters()) {
                    sb2.append("\t" + parm.getParamName() + ", required=" + parm.isRequired());
                }
                if (!mhee.isValid()) {
                    sb2.append("Environment says it's not valid! " + mhee.getErrorString());
                } else {
                    sb2.append("\nEnvironment:");
                    sb2.append(mhee.toFullString());
                }
                logger.debug(sb2.toString());
            } else {
                logger.debug("NO ENVIRONMENT for this entry");
            }
            // all variables converted to their native object types
            Map<String, Object> goldenInputs = null;

            ArrayList<String> parameterNames = new ArrayList<>();
            HashMap<String, String> aliasToParam = new HashMap<>();
            StringBuilder sb = new StringBuilder("\nTemplate Parameters:\n");
            int cntr = 0;
            try {
                for (HeatTemplateParam htp : heatTemplate.getParameters()) {
                    sb.append("param[" + cntr++ + "]=" + htp.getParamName());
                    parameterNames.add(htp.getParamName());
                    if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
                        aliasToParam.put(htp.getParamAlias(), htp.getParamName());
                        sb.append(" ** (alias=" + htp.getParamAlias() + ")");
                    }
                    sb.append("\n");
                }
                logger.debug(sb.toString());
            } catch (Exception e) {
                logger.debug("??An exception occurred trying to go through Parameter Names {}", e.getMessage(), e);
            }
            // Step 1 - convert what we got as inputs (Map<String, String>) to a
            // Map<String, Object> - where the object matches the param type identified in the template
            // This will also not copy over params that aren't identified in the template
            goldenInputs = msoHeatUtils.convertInputMap(inputs, heatTemplate);
            // Step 2 - now simply add the outputs as we received them - no need to convert to string
            logger.debug("Now add in the base stack outputs if applicable");
            msoHeatUtils.copyBaseOutputsToInputs(goldenInputs, baseStackOutputs, parameterNames, aliasToParam);
            // Step 3 - add the volume inputs if any
            logger.debug("Now add in the volume stack outputs if applicable");
            msoHeatUtils.copyBaseOutputsToInputs(goldenInputs, nestedVolumeOutputs, parameterNames, aliasToParam);

            for (HeatTemplateParam parm : heatTemplate.getParameters()) {
                logger.debug("Parameter:'" + parm.getParamName() + "', isRequired=" + parm.isRequired() + ", alias="
                        + parm.getParamAlias());

                if (parm.isRequired() && (goldenInputs == null || !goldenInputs.containsKey(parm.getParamName()))) {
                    // The check for an alias was moved to the method in MsoHeatUtils - when we converted the
                    // Map<String, String> to Map<String, Object>
                    logger.debug("**Parameter " + parm.getParamName() + " is required and not in the inputs...check "
                            + "environment");
                    if (mhee != null && mhee.containsParameter(parm.getParamName())) {
                        logger.debug("Required parameter {} appears to be in environment - do not count as missing",
                                parm.getParamName());
                    } else {
                        logger.debug("adding to missing parameters list: {}", parm.getParamName());
                        if (missingParams == null) {
                            missingParams = parm.getParamName();
                        } else {
                            missingParams += "," + parm.getParamName();
                        }
                    }
                }
                paramList.add(parm.getParamName());
            }
            if (missingParams != null) {
                if (checkRequiredParameters) {
                    // Problem - missing one or more required parameters
                    String error = "Create VFModule: Missing Required inputs: " + missingParams;
                    logger.error(LoggingAnchor.FIVE, MessageEnum.RA_MISSING_PARAM.toString(), missingParams, OPENSTACK,
                            ErrorCode.DataError.getValue(), "Create VFModule: Missing Required inputs");
                    logger.debug(error);
                    throw new VnfException(error, MsoExceptionCategory.USERDATA);
                } else {
                    logger.debug("found missing parameters - but checkRequiredParameters is false - will not block");
                }
            } else {
                logger.debug("No missing parameters found - ok to proceed");
            }
            // We can now remove the recreating of the ENV with only legit params - that check is done for us,
            // and it causes problems with json that has arrays
            String newEnvironmentString = null;
            if (mhee != null) {
                newEnvironmentString = mhee.getRawEntry().toString();
            }

            // "Fix" the template if it has CR/LF (getting this from Oracle)
            String template = heatTemplate.getHeatTemplate();
            template = template.replaceAll("\r\n", "\n");

            // Valet - 1806
            boolean isValetEnabled = this.checkBooleanProperty(MsoVnfAdapterImpl.VALET_ENABLED, false);
            boolean failRequestOnValetFailure =
                    this.checkBooleanProperty(MsoVnfAdapterImpl.FAIL_REQUESTS_ON_VALET_FAILURE, false);
            logger.debug("isValetEnabled={}, failRequestsOnValetFailure={}", isValetEnabled, failRequestOnValetFailure);
            if (oldWay || isVolumeRequest) {
                isValetEnabled = false;
                logger.debug("do not send to valet for volume requests or brocade");
            }
            boolean sendResponseToValet = false;
            if (isValetEnabled) {
                Holder<Map<String, Object>> valetModifiedParamsHolder = new Holder<>();
                sendResponseToValet = this.valetCreateRequest(cloudSiteId, cloudOwner, tenantId, heatFilesObjects,
                        nestedTemplatesChecked, vfModuleName, backout, heatTemplate, newEnvironmentString, goldenInputs,
                        msoRequest, inputs, failRequestOnValetFailure, valetModifiedParamsHolder);
                if (sendResponseToValet) {
                    goldenInputs = valetModifiedParamsHolder.value;
                }
            }

            // Have the tenant. Now deploy the stack itself
            // Ignore MsoTenantNotFound and MsoStackAlreadyExists exceptions
            // because we already checked for those.

            StackInfo heatStack = null;
            try {
                if (backout == null) {
                    backout = true;
                }
                if (failIfExists == null) {
                    failIfExists = false;
                }
                if (msoHeatUtils != null) {
                    heatStack = msoHeatUtils.createStack(cloudSiteId, cloudOwner, tenantId, vfModuleName, null,
                            template, goldenInputs, true, heatTemplate.getTimeoutMinutes(), newEnvironmentString,
                            nestedTemplatesChecked, heatFilesObjects, backout.booleanValue(), failIfExists);
                } else {
                    throw new MsoHeatNotFoundException();
                }
            } catch (MsoException me) {
                me.addContext(CREATE_VFM_MODULE);
                logger.error("Error creating Stack", me);
                if (isValetEnabled && sendResponseToValet) {
                    logger.debug("valet is enabled, the orchestration failed - now sending rollback to valet");
                    try {
                        GenericValetResponse<ValetRollbackResponse> gvr = this.vci
                                .callValetRollbackRequest(msoRequest.getRequestId(), null, backout, me.getMessage());
                        // Nothing to really do here whether it succeeded or not other than log it.
                        logger.debug("Return code from Rollback response is {}", gvr.getStatusCode());
                    } catch (Exception e) {
                        logger.error("Exception encountered while sending Rollback to Valet ", e);
                    }
                }
                throw new VnfException(me);
            } catch (NullPointerException npe) {
                logger.error("Error creating Stack", npe);
                throw new VnfException("NullPointerException during heat.createStack");
            } catch (Exception e) {
                logger.error("Error creating Stack", e);
                throw new VnfException("Exception during heat.createStack! " + e.getMessage());
            }
            // Reach this point if createStack is successful.
            // Populate remaining rollback info and response parameters.
            vfRollback.setVnfId(heatStack.getCanonicalName());
            vfRollback.setVnfCreated(true);

            vnfId.value = heatStack.getCanonicalName();
            outputs.value = copyStringOutputs(heatStack.getOutputs());
            rollback.value = vfRollback;
            if (isValetEnabled && sendResponseToValet) {
                logger.debug("valet is enabled, the orchestration succeeded - now send confirm to valet with stack id");
                try {
                    GenericValetResponse<ValetConfirmResponse> gvr =
                            this.vci.callValetConfirmRequest(msoRequest.getRequestId(), heatStack.getCanonicalName());
                    // Nothing to really do here whether it succeeded or not other than log it.
                    logger.debug("Return code from Confirm response is {}", gvr.getStatusCode());
                } catch (Exception e) {
                    logger.error("Exception encountered while sending Confirm to Valet ", e);
                }
            }
            logger.debug("VF Module {} successfully created", vfModuleName);
            if (enableBridge != null && enableBridge) {
                // call heatbridge
                heatbridge(heatStack, cloudOwner, cloudSiteId, tenantId, genericVnfName, vfModuleId);
            }
        } catch (Exception e) {
            logger.debug("unhandled exception in create VF", e);
            throw new VnfException("Exception during create VF " + e.getMessage());
        }
    }

    @Override
    public void deleteVfModule(String cloudSiteId, String cloudOwner, String tenantId, String vnfName,
            MsoRequest msoRequest, Holder<Map<String, String>> outputs) throws VnfException {
        Map<String, Object> stackOutputs;
        try {
            stackOutputs = msoHeatUtils.queryStackForOutputs(cloudSiteId, cloudOwner, tenantId, vnfName);
        } catch (MsoException me) {
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            me.addContext("DeleteVFModule");
            String error = "Delete VFModule: Query to get outputs: " + vnfName + " in " + cloudOwner + "/" + cloudSiteId
                    + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId,
                    tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), "Exception - " + QUERY_STACK, me);
            logger.debug(error);
            throw new VnfException(me);
        }
        // call method which handles the conversion from Map<String,Object> to Map<String,String> for our expected
        // Object types
        outputs.value = this.convertMapStringObjectToStringString(stackOutputs);

        boolean isValetEnabled = this.checkBooleanProperty(MsoVnfAdapterImpl.VALET_ENABLED, false);
        boolean failRequestOnValetFailure =
                this.checkBooleanProperty(MsoVnfAdapterImpl.FAIL_REQUESTS_ON_VALET_FAILURE, false);
        logger.debug("isValetEnabled={}, failRequestsOnValetFailure={}", isValetEnabled, failRequestOnValetFailure);
        boolean valetDeleteRequestSucceeded = false;
        if (isValetEnabled) {
            valetDeleteRequestSucceeded = this.valetDeleteRequest(cloudSiteId, cloudOwner, tenantId, vnfName,
                    msoRequest, failRequestOnValetFailure);
        }

        try {
            msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfName, true, 118);
        } catch (MsoException me) {
            me.addContext(DELETE_VNF);
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            String error =
                    "Delete VF: " + vnfName + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_DELETE_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId,
                    tenantId, OPENSTACK, "DeleteStack", ErrorCode.DataError.getValue(), "Exception - deleteStack", me);
            logger.debug(error);
            if (isValetEnabled && valetDeleteRequestSucceeded) {
                logger.debug("valet is enabled, the orchestration failed - now sending rollback to valet");
                try {
                    GenericValetResponse<ValetRollbackResponse> gvr = this.vci
                            .callValetRollbackRequest(msoRequest.getRequestId(), vnfName, false, me.getMessage());
                    // Nothing to really do here whether it succeeded or not other than log it.
                    logger.debug("Return code from Rollback response is {}", gvr.getStatusCode());
                } catch (Exception e) {
                    logger.error("Exception encountered while sending Rollback to Valet ", e);
                }
            }
            throw new VnfException(me);
        }
        if (isValetEnabled && valetDeleteRequestSucceeded) {
            // only if the original request succeeded do we send a confirm
            logger.debug("valet is enabled, the delete succeeded - now send confirm to valet");
            try {
                GenericValetResponse<ValetConfirmResponse> gvr =
                        this.vci.callValetConfirmRequest(msoRequest.getRequestId(), vnfName);
                // Nothing to really do here whether it succeeded or not other than log it.
                logger.debug("Return code from Confirm response is {}", gvr.getStatusCode());
            } catch (Exception e) {
                logger.error("Exception encountered while sending Confirm to Valet ", e);
            }
        }
    }

    @Override
    public void updateVfModule(String cloudSiteId, String cloudOwner, String tenantId, String vnfType,
            String vnfVersion, String vnfName, String requestType, String volumeGroupHeatStackId,
            String baseVfHeatStackId, String vfModuleStackId, String modelCustomizationUuid, Map<String, Object> inputs,
            MsoRequest msoRequest, Holder<Map<String, String>> outputs, Holder<VnfRollback> rollback)
            throws VnfException {
        String vfModuleName = vnfName;
        String vfModuleType = vnfType;
        String methodName = "updateVfModule";
        String serviceName = VNF_ADAPTER_SERVICE_NAME + methodName;

        StringBuilder sbInit = new StringBuilder();
        sbInit.append("updateVfModule: \n");
        sbInit.append("cloudOwner=" + cloudOwner + "\n");
        sbInit.append("cloudSiteId=" + cloudSiteId + "\n");
        sbInit.append("tenantId=" + tenantId + "\n");
        sbInit.append("vnfType=" + vnfType + "\n");
        sbInit.append("vnfVersion=" + vnfVersion + "\n");
        sbInit.append("vnfName=" + vnfName + "\n");
        sbInit.append("requestType=" + requestType + "\n");
        sbInit.append("volumeGroupHeatStackId=" + volumeGroupHeatStackId + "\n");
        sbInit.append("baseVfHeatStackId=" + baseVfHeatStackId + "\n");
        sbInit.append("vfModuleStackId=" + vfModuleStackId + "\n");
        sbInit.append("modelCustomizationUuid=" + modelCustomizationUuid + "\n");
        logger.debug(sbInit.toString());

        String mcu = modelCustomizationUuid;
        boolean useMCUuid = false;
        if (mcu != null && !mcu.isEmpty()) {
            if ("null".equalsIgnoreCase(mcu)) {
                logger.debug("modelCustomizationUuid: passed in as the string 'null' - will ignore: {}",
                        modelCustomizationUuid);
                useMCUuid = false;
                mcu = "";
            } else {
                logger.debug("Found modelCustomizationUuid! Will use that: {}", mcu);
                useMCUuid = true;
            }
        }

        String requestTypeString = "";
        if (requestType != null && !"".equals(requestType)) {
            requestTypeString = requestType;
        }

        String nestedStackId = null;
        if (volumeGroupHeatStackId != null && !"".equals(volumeGroupHeatStackId)
                && !"null".equalsIgnoreCase(volumeGroupHeatStackId)) {
            nestedStackId = volumeGroupHeatStackId;
        }
        String nestedBaseStackId = null;
        if (baseVfHeatStackId != null && !"".equals(baseVfHeatStackId) && !"null".equalsIgnoreCase(baseVfHeatStackId)) {
            nestedBaseStackId = baseVfHeatStackId;
        }

        if (inputs == null) {
            // Create an empty set of inputs
            inputs = new HashMap<>();
            logger.debug("inputs == null - setting to empty");
        }

        boolean isBaseRequest = false;
        boolean isVolumeRequest = false;
        if (requestTypeString.startsWith("VOLUME")) {
            isVolumeRequest = true;
        }
        if ((vfModuleName == null || "".equals(vfModuleName.trim())) && vfModuleStackId != null) {
            vfModuleName = this.getVfModuleNameFromModuleStackId(vfModuleStackId);
        }

        logger.debug("Updating VFModule: " + vfModuleName + " of type " + vfModuleType + "in " + cloudOwner + "/"
                + cloudSiteId + "/" + tenantId);
        logger.debug("requestTypeString = " + requestTypeString + ", nestedVolumeStackId = " + nestedStackId
                + ", nestedBaseStackId = " + nestedBaseStackId);

        // Build a default rollback object (no actions performed)
        VnfRollback vfRollback = new VnfRollback();
        vfRollback.setCloudSiteId(cloudSiteId);
        vfRollback.setCloudOwner(cloudOwner);
        vfRollback.setTenantId(tenantId);
        vfRollback.setMsoRequest(msoRequest);
        vfRollback.setRequestType(requestTypeString);
        vfRollback.setVolumeGroupHeatStackId(volumeGroupHeatStackId);
        vfRollback.setBaseGroupHeatStackId(baseVfHeatStackId);
        vfRollback.setIsBase(isBaseRequest);
        vfRollback.setVfModuleStackId(vfModuleStackId);
        vfRollback.setModelCustomizationUuid(mcu);

        StackInfo heatStack;
        logger.debug("UpdateVfModule - querying for {}", vfModuleName);
        try {
            heatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, vfModuleName);
        } catch (MsoException me) {
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            me.addContext("UpdateVFModule");
            String error = "Update VFModule: Query " + vfModuleName + " in " + cloudOwner + "/" + cloudSiteId + "/"
                    + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                    cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(),
                    "Exception - " + QUERY_STACK, me);
            logger.debug(error);
            throw new VnfException(me);
        }

        // TODO - do we need to check for the other status possibilities?
        if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
            // Not Found
            String error = "Update VF: Stack " + vfModuleName + " does not exist in " + cloudOwner + "/" + cloudSiteId
                    + "/" + tenantId;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_VNF_NOT_EXIST.toString(), vfModuleName, cloudOwner,
                    cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
            throw new VnfNotFound(cloudSiteId, cloudOwner, tenantId, vfModuleName);
        } else {
            logger.debug("Found Existing stack, status={}", heatStack.getStatus());
            // Populate the outputs from the existing stack.
            outputs.value = copyStringOutputs(heatStack.getOutputs());
            rollback.value = vfRollback; // Default rollback - no updates performed
        }

        // 1604 Cinder Volume support - handle a nestedStackId if sent (volumeGroupHeatStackId):
        StackInfo nestedHeatStack = null;
        Map<String, Object> nestedVolumeOutputs = null;
        if (nestedStackId != null) {
            try {
                logger.debug("Querying for nestedStackId = {}", nestedStackId);
                nestedHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedStackId);
            } catch (MsoException me) {
                // Failed to query the Stack due to an openstack exception.
                // Convert to a generic VnfException
                me.addContext("UpdateVFModule");
                String error = "Update VF: Attached heatStack ID Query " + nestedStackId + " in " + cloudOwner + "/"
                        + cloudSiteId + "/" + tenantId + ": " + me;
                logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudOwner,
                        cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(),
                        "Exception - " + error, me);
                logger.debug("ERROR trying to query nested stack= {}", error);
                throw new VnfException(me);
            }
            if (nestedHeatStack == null || nestedHeatStack.getStatus() == HeatStatus.NOTFOUND) {
                String error = "Update VFModule: Attached volume heatStack ID DOES NOT EXIST " + nestedStackId + " in "
                        + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
                logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudOwner,
                        cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
                logger.debug(error);
                throw new VnfException(error, MsoExceptionCategory.USERDATA);
            } else {
                logger.debug("Found nested heat stack - copying values to inputs *later*");
                nestedVolumeOutputs = nestedHeatStack.getOutputs();
                msoHeatUtils.copyStringOutputsToInputs(inputs, nestedHeatStack.getOutputs(), false);
            }
        }
        // handle a nestedBaseStackId if sent - this is the stack ID of the base.
        StackInfo nestedBaseHeatStack = null;
        Map<String, Object> baseStackOutputs = null;
        if (nestedBaseStackId != null) {
            try {
                logger.debug("Querying for nestedBaseStackId = {}", nestedBaseStackId);
                nestedBaseHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedBaseStackId);
            } catch (MsoException me) {
                // Failed to query the Stack due to an openstack exception.
                // Convert to a generic VnfException
                me.addContext("UpdateVfModule");
                String error = "Update VFModule: Attached baseHeatStack ID Query " + nestedBaseStackId + " in "
                        + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
                logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(),
                        "Exception - " + error, me);
                logger.debug("ERROR trying to query nested base stack= {}", error);
                throw new VnfException(me);
            }
            if (nestedBaseHeatStack == null || nestedBaseHeatStack.getStatus() == HeatStatus.NOTFOUND) {
                String error = "Update VFModule: Attached base heatStack ID DOES NOT EXIST " + nestedBaseStackId
                        + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
                logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner,
                        cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
                logger.debug(error);
                throw new VnfException(error, MsoExceptionCategory.USERDATA);
            } else {
                logger.debug("Found nested base heat stack - copying values to inputs *later*");
                baseStackOutputs = nestedBaseHeatStack.getOutputs();
                msoHeatUtils.copyStringOutputsToInputs(inputs, nestedBaseHeatStack.getOutputs(), false);
            }
        }

        // Retrieve the VF definition
        VnfResource vnfResource = null;
        VfModule vf = null;
        VfModuleCustomization vfmc = null;
        if (useMCUuid) {
            vfmc = vfModuleCustomRepo.findFirstByModelCustomizationUUIDOrderByCreatedDesc(modelCustomizationUuid);
            vf = vfmc != null ? vfmc.getVfModule() : null;
            if (vf == null) {
                logger.debug("Unable to find a vfModule matching modelCustomizationUuid={}", mcu);
            }
        } else {
            logger.debug("1707 and later - MUST PROVIDE Model Customization UUID!");
        }
        if (vf == null) {
            String error = "Update VfModule: unable to find vfModule with modelCustomizationUuid=" + mcu;
            logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "VF Module Type", vfModuleType,
                    OPENSTACK, ErrorCode.DataError.getValue(), error);
            throw new VnfException(error, MsoExceptionCategory.USERDATA);
        }
        logger.debug("Got VF module definition from Catalog: {}", vf.toString());
        if (vf.getIsBase()) {
            isBaseRequest = true;
            logger.debug("This a BASE update request");
        } else {
            logger.debug("This is *not* a BASE VF update request");
            if (!isVolumeRequest && nestedBaseStackId == null) {
                logger.debug(
                        "DANGER WILL ROBINSON! This is unexpected - no nestedBaseStackId with this non-base request");
            }
        }

        // 1607 - Add version check
        // First - see if it's in the VnfResource record
        // if we have a vf Module - then we have to query to get the VnfResource record.
        if (vf.getModelUUID() != null) {
            String vnfResourceModelUuid = vf.getModelUUID();

            vnfResource = vf.getVnfResources();
            if (vnfResource == null) {
                logger.debug("Unable to find vnfResource at ? will not error for now...", vnfResourceModelUuid);
            }
        }

        String minVersionVnf = null;
        String maxVersionVnf = null;
        if (vnfResource != null) {
            try {
                minVersionVnf = vnfResource.getAicVersionMin();
                maxVersionVnf = vnfResource.getAicVersionMax();
            } catch (Exception e) {
                logger.debug("Unable to pull min/max version for this VNF Resource entry", e);
                minVersionVnf = null;
                maxVersionVnf = null;
            }
            if (minVersionVnf != null && "".equals(minVersionVnf)) {
                minVersionVnf = null;
            }
            if (maxVersionVnf != null && "".equals(maxVersionVnf)) {
                maxVersionVnf = null;
            }
        }
        if (minVersionVnf != null && maxVersionVnf != null) {
            MavenLikeVersioning aicV = new MavenLikeVersioning();

            // double check
            if (this.cloudConfig != null) {
                Optional<CloudSite> cloudSiteOpt = this.cloudConfig.getCloudSite(cloudSiteId);
                if (cloudSiteOpt.isPresent()) {
                    aicV.setVersion(cloudSiteOpt.get().getCloudVersion());
                    boolean moreThanMin = true;
                    boolean equalToMin = true;
                    boolean moreThanMax = true;
                    boolean equalToMax = true;
                    boolean doNotTest = false;
                    try {
                        moreThanMin = aicV.isMoreRecentThan(minVersionVnf);
                        equalToMin = aicV.isTheSameVersion(minVersionVnf);
                        moreThanMax = aicV.isMoreRecentThan(maxVersionVnf);
                        equalToMax = aicV.isTheSameVersion(maxVersionVnf);
                    } catch (Exception e) {
                        logger.debug(
                                "An exception occured while trying to test AIC Version {} - will default to not check",
                                e.getMessage(), e);
                        doNotTest = true;
                    }
                    if (!doNotTest) {
                        if ((moreThanMin || equalToMin) // aic >= min
                                && ((equalToMax) || !(moreThanMax))) { // aic <= max
                            logger.debug("VNF Resource " + vnfResource.getModelName() + " " + VERSION_MIN + " ="
                                    + minVersionVnf + " " + VERSION_MAX + " :" + maxVersionVnf + " supported on Cloud: "
                                    + cloudSiteId + " with AIC_Version:" + aicV);
                        } else {
                            // ERROR
                            String error = "VNF Resource type: " + vnfResource.getModelName() + " " + VERSION_MIN + " ="
                                    + minVersionVnf + " " + VERSION_MAX + " :" + maxVersionVnf
                                    + " NOT supported on Cloud: " + cloudSiteId + " with AIC_Version:" + aicV;
                            logger.error(LoggingAnchor.FIVE, MessageEnum.RA_CONFIG_EXC.toString(), error, OPENSTACK,
                                    ErrorCode.BusinessProcesssError.getValue(), "Exception - setVersion");
                            logger.debug(error);
                            throw new VnfException(error, MsoExceptionCategory.USERDATA);
                        }
                    } else {
                        logger.debug("bypassing testing AIC version...");
                    }
                } // let this error out downstream to avoid introducing uncertainty at this stage
            } else {
                logger.debug("cloudConfig is NULL - cannot check cloud site version");
            }

        } else {
            logger.debug("AIC Version not set in VNF_Resource - do not error for now - not checked.");
        }
        // End Version check 1607

        HeatTemplate heatTemplate = null;
        HeatEnvironment heatEnvironment = null;
        if (isVolumeRequest) {
            heatTemplate = vf.getVolumeHeatTemplate();
            heatEnvironment = vfmc.getVolumeHeatEnv();
        } else {
            heatTemplate = vf.getModuleHeatTemplate();
            heatEnvironment = vfmc.getHeatEnvironment();
        }

        if (heatTemplate == null) {
            String error = "UpdateVF: No Heat Template ID defined in catalog database for " + vfModuleType
                    + ", modelCustomizationUuid=" + mcu + ", vfModuleUuid=" + vf.getModelUUID() + ", reqType="
                    + requestTypeString;
            logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "Heat Template ID",
                    vfModuleType, OPENSTACK, ErrorCode.DataError.getValue(), error);
            throw new VnfException(error, MsoExceptionCategory.INTERNAL);
        } else {
            logger.debug("Got HEAT Template from DB: {}", heatTemplate.getHeatTemplate());
        }

        if (heatEnvironment == null) {
            String error = "Update VNF: undefined Heat Environment. VF=" + vfModuleType + ", modelCustomizationUuid="
                    + mcu + ", vfModuleUuid=" + vf.getModelUUID() + ", reqType=" + requestTypeString;
            logger.error(LoggingAnchor.FIVE, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "Heat Environment ID",
                    OPENSTACK, ErrorCode.DataError.getValue(), error);
            throw new VnfException(error, MsoExceptionCategory.INTERNAL);
        } else {
            logger.debug("Got Heat Environment from DB: {}", heatEnvironment.getEnvironment());
        }

        logger.debug("In MsoVnfAdapterImpl, about to call db.getNestedTemplates avec templateId={}",
                heatTemplate.getArtifactUuid());


        List<HeatTemplate> nestedTemplates = heatTemplate.getChildTemplates();
        Map<String, Object> nestedTemplatesChecked = new HashMap<>();
        if (nestedTemplates != null && !nestedTemplates.isEmpty()) {
            // for debugging print them out
            logger.debug("Contents of nestedTemplates - to be added to files: on stack:");
            for (HeatTemplate entry : nestedTemplates) {

                nestedTemplatesChecked.put(entry.getTemplateName(), entry.getTemplateBody());
                logger.debug(entry.getTemplateName() + " -> " + entry.getTemplateBody());
            }
        } else {
            logger.debug("No nested templates found - nothing to do here");
            nestedTemplatesChecked = null;
        }

        // Also add the files: for any get_files associated with this VfModule
        // *if* there are any
        logger.debug("In MsoVnfAdapterImpl.updateVfModule, about to call db.getHeatFiles avec vfModuleId={}",
                vf.getModelUUID());

        List<HeatFiles> heatFiles = null;
        Map<String, Object> heatFilesObjects = new HashMap<>();

        // Add ability to turn on adding get_files with volume requests (by property).
        boolean addGetFilesOnVolumeReq = false;
        try {
            String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ);
            if ("true".equalsIgnoreCase(propertyString) || "y".equalsIgnoreCase(propertyString)) {
                addGetFilesOnVolumeReq = true;
                logger.debug("AddGetFilesOnVolumeReq - setting to true! {}", propertyString);
            }
        } catch (Exception e) {
            logger.debug("An error occured trying to get property {} - default to false",
                    MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ, e);
        }
        if (!isVolumeRequest || addGetFilesOnVolumeReq) {
            logger.debug("In MsoVnfAdapterImpl updateVfModule, about to call db.getHeatFilesForVfModule avec "
                    + "vfModuleId={}", vf.getModelUUID());

            heatFiles = vf.getHeatFiles();
            if (heatFiles != null && !heatFiles.isEmpty()) {
                // add these to stack - to be done in createStack
                // here, we will map them to Map<String, Object> from Map<String, HeatFiles>
                // this will match the nested templates format
                logger.debug("Contents of heatFiles - to be added to files: on stack:");
                for (HeatFiles heatfile : heatFiles) {
                    logger.debug(heatfile.getFileName() + " -> " + heatfile.getFileBody());
                    heatFilesObjects.put(heatfile.getFileName(), heatfile.getFileBody());
                }
            } else {
                logger.debug("No heat files found -nothing to do here");
                heatFilesObjects = null;
            }
        }

        // Check that required parameters have been supplied
        String missingParams = null;
        List<String> paramList = new ArrayList<>();

        // New for 1510 - consult the PARAM_ALIAS field to see if we've been
        // supplied an alias. Only check if we don't find it initially.
        // Also new in 1510 - don't flag missing parameters if there's an environment - because they might be there.
        // And also new - add parameter to turn off checking all together if we find we're blocking orders we
        // shouldn't
        boolean checkRequiredParameters = true;
        try {
            String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
            if ("false".equalsIgnoreCase(propertyString) || "n".equalsIgnoreCase(propertyString)) {
                checkRequiredParameters = false;
                logger.debug("CheckRequiredParameters is FALSE. Will still check but then skip blocking...",
                        MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
            }
        } catch (Exception e) {
            // No problem - default is true
            logger.debug("An exception occured trying to get property {}", MsoVnfAdapterImpl.CHECK_REQD_PARAMS, e);
        }
        // 1604 - Add enhanced environment & parameter checking
        // Part 1: parse envt entries to see if reqd parameter is there (before used a simple grep
        // Part 2: only submit to openstack the parameters in the envt that are in the heat template
        // Note this also removes any comments
        MsoHeatEnvironmentEntry mhee = null;
        if (heatEnvironment != null && heatEnvironment.getEnvironment().toLowerCase().contains("parameters:")) {
            logger.debug("Enhanced environment checking enabled - 1604");
            StringBuilder sb = new StringBuilder(heatEnvironment.getEnvironment());
            mhee = new MsoHeatEnvironmentEntry(sb);
            StringBuilder sb2 = new StringBuilder("\nHeat Template Parameters:\n");
            for (HeatTemplateParam parm : heatTemplate.getParameters()) {
                sb2.append("\t" + parm.getParamName() + ", required=" + parm.isRequired());
            }
            if (!mhee.isValid()) {
                sb2.append("Environment says it's not valid! " + mhee.getErrorString());
            } else {
                sb2.append("\nEnvironment:");
                sb2.append(mhee.toFullString());
            }
            logger.debug(sb2.toString());
        } else {
            logger.debug("NO ENVIRONMENT for this entry");
        }
        // New for 1607 - support params of json type
        HashMap<String, JsonNode> jsonParams = new HashMap<>();
        boolean hasJson = false;

        for (HeatTemplateParam parm : heatTemplate.getParameters()) {
            logger.debug("Parameter:'" + parm.getParamName() + "', isRequired=" + parm.isRequired() + ", alias="
                    + parm.getParamAlias());
            // handle json
            String parameterType = parm.getParamType();
            if (parameterType == null || "".equals(parameterType.trim())) {
                parameterType = "String";
            }
            JsonNode jsonNode = null;
            if ("json".equalsIgnoreCase(parameterType) && inputs != null) {
                if (inputs.containsKey(parm.getParamName())) {
                    hasJson = true;
                    String jsonString = null;
                    try {
                        jsonString = JSON_MAPPER.writeValueAsString(inputs.get(parm.getParamName()));
                        jsonNode = JSON_MAPPER.readTree(jsonString);
                    } catch (JsonParseException jpe) {
                        // TODO - what to do here?
                        // for now - send the error to debug
                        logger.debug("Json Error Converting {} - {}", parm.getParamName(), jpe.getMessage(), jpe);
                        hasJson = false;
                        jsonNode = null;
                    } catch (Exception e) {
                        // or here?
                        logger.debug("Json Error Converting {} {}", parm.getParamName(), e.getMessage(), e);
                        hasJson = false;
                        jsonNode = null;
                    }
                    if (jsonNode != null) {
                        jsonParams.put(parm.getParamName(), jsonNode);
                    }
                } else if (inputs.containsKey(parm.getParamAlias())) {
                    hasJson = true;
                    String jsonString = null;
                    try {
                        jsonString = (String) inputs.get(parm.getParamAlias());
                        jsonNode = JSON_MAPPER.readTree(jsonString);
                    } catch (JsonParseException jpe) {
                        // TODO - what to do here?
                        // for now - send the error to debug, but just leave it as a String
                        String errorMessage = jpe.getMessage();
                        logger.debug("Json Error Converting " + parm.getParamName() + " - " + errorMessage, jpe);
                        hasJson = false;
                        jsonNode = null;
                    } catch (Exception e) {
                        // or here?
                        logger.debug("Json Error Converting " + parm.getParamName() + " " + e.getMessage(), e);
                        hasJson = false;
                        jsonNode = null;
                    }
                    if (jsonNode != null) {
                        // Notice here - we add it to the jsonParams hashMap with the actual name -
                        // then manipulate the inputs so when we check for aliases below - it will not
                        // get flagged.
                        jsonParams.put(parm.getParamName(), jsonNode);
                        inputs.remove(parm.getParamAlias());
                        inputs.put(parm.getParamName(), jsonString);
                    }
                } // TODO add a check for the parameter in the env file
            }

            if (parm.isRequired() && (inputs == null || !inputs.containsKey(parm.getParamName()))) {
                if (inputs.containsKey(parm.getParamAlias())) {
                    // They've submitted using an alias name. Remove that from inputs, and add back using real name.
                    String realParamName = parm.getParamName();
                    String alias = parm.getParamAlias();
                    Object value = inputs.get(alias);
                    logger.debug("*Found an Alias: paramName=" + realParamName + ",alias=" + alias + ",value=" + value);
                    inputs.remove(alias);
                    inputs.put(realParamName, value);
                    logger.debug("{} entry removed from inputs, added back using {}", alias, realParamName);
                }
                // enhanced - check if it's in the Environment (note: that method
                else if (mhee != null && mhee.containsParameter(parm.getParamName())) {

                    logger.debug("Required parameter {} appears to be in environment - do not count as missing",
                            parm.getParamName());
                } else {
                    logger.debug("adding to missing parameters list: {}", parm.getParamName());
                    if (missingParams == null) {
                        missingParams = parm.getParamName();
                    } else {
                        missingParams += "," + parm.getParamName();
                    }
                }
            }
            paramList.add(parm.getParamName());
        }


        if (missingParams != null) {
            // Problem - missing one or more required parameters
            if (checkRequiredParameters) {
                String error = "Update VNF: Missing Required inputs: " + missingParams;
                logger.error(LoggingAnchor.FIVE, MessageEnum.RA_MISSING_PARAM.toString(), missingParams, OPENSTACK,
                        ErrorCode.DataError.getValue(), error);
                throw new VnfException(error, MsoExceptionCategory.USERDATA);
            } else {
                logger.debug("found missing parameters - but checkRequiredParameters is false - will not block");
            }
        }

        // Just submit the envt entry as received from the database
        String newEnvironmentString = null;
        if (mhee != null) {
            newEnvironmentString = mhee.getRawEntry().toString();
        }
        // Remove any extraneous parameters (don't throw an error)
        if (inputs != null) {
            List<String> extraParams = new ArrayList<>();
            extraParams.addAll(inputs.keySet());
            // This is not a valid parameter for this template
            extraParams.removeAll(paramList);
            if (!extraParams.isEmpty()) {
                logger.warn(LoggingAnchor.SIX, MessageEnum.RA_VNF_EXTRA_PARAM.toString(), vnfType,
                        extraParams.toString(), OPENSTACK, ErrorCode.DataError.getValue(), "Extra params");
                inputs.keySet().removeAll(extraParams);
            }
        }
        Map<String, Object> goldenInputs = copyStringInputs(inputs);
        // 1607 - when we get here - we have clean inputs. Create inputsTwo in case we have json
        Map<String, Object> inputsTwo = null;
        if (hasJson && jsonParams.size() > 0) {
            inputsTwo = new HashMap<>();
            for (Map.Entry<String, Object> entry : inputs.entrySet()) {
                String keyParamName = entry.getKey();
                Object value = entry.getValue();
                if (jsonParams.containsKey(keyParamName)) {
                    inputsTwo.put(keyParamName, jsonParams.get(keyParamName));
                } else {
                    inputsTwo.put(keyParamName, value);
                }
            }
            goldenInputs = inputsTwo;
        }

        // "Fix" the template if it has CR/LF (getting this from Oracle)
        String template = heatTemplate.getHeatTemplate();
        template = template.replaceAll("\r\n", "\n");

        boolean isValetEnabled = this.checkBooleanProperty(MsoVnfAdapterImpl.VALET_ENABLED, false);
        boolean failRequestOnValetFailure =
                this.checkBooleanProperty(MsoVnfAdapterImpl.FAIL_REQUESTS_ON_VALET_FAILURE, false);
        logger.debug("isValetEnabled={}, failRequestsOnValetFailure={}", isValetEnabled, failRequestOnValetFailure);
        if (isVolumeRequest) {
            isValetEnabled = false;
            logger.debug("never send a volume request to valet");
        }
        boolean sendResponseToValet = false;
        if (isValetEnabled) {
            Holder<Map<String, Object>> valetModifiedParamsHolder = new Holder<>();
            String parsedVfModuleName = this.getVfModuleNameFromModuleStackId(vfModuleStackId);
            // Make sure it is set to something.
            if (parsedVfModuleName == null || parsedVfModuleName.isEmpty()) {
                parsedVfModuleName = "unknown";
            }
            sendResponseToValet = this.valetUpdateRequest(cloudSiteId, cloudOwner, tenantId, heatFilesObjects,
                    nestedTemplatesChecked, parsedVfModuleName, false, heatTemplate, newEnvironmentString, goldenInputs,
                    msoRequest, inputs, failRequestOnValetFailure, valetModifiedParamsHolder);
            if (sendResponseToValet) {
                goldenInputs = valetModifiedParamsHolder.value;
            }
        }

        // Have the tenant. Now deploy the stack itself
        // Ignore MsoTenantNotFound and MsoStackAlreadyExists exceptions
        // because we already checked for those.
        try {
            heatStack = heatU.updateStack(cloudSiteId, cloudOwner, tenantId, vfModuleName, template, goldenInputs, true,
                    heatTemplate.getTimeoutMinutes(), newEnvironmentString,
                    // heatEnvironmentString,
                    nestedTemplatesChecked, heatFilesObjects);
        } catch (MsoException me) {
            me.addContext("UpdateVFModule");
            String error = "Update VFModule " + vfModuleType + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId
                    + ": " + me;
            logger.error(LoggingAnchor.EIGHT, MessageEnum.RA_UPDATE_VNF_ERR.toString(), vfModuleType, cloudOwner,
                    cloudSiteId, tenantId, OPENSTACK, ErrorCode.DataError.getValue(), "Exception - " + error, me);
            if (isValetEnabled && sendResponseToValet) {
                logger.debug("valet is enabled, the orchestration failed - now sending rollback to valet");
                try {
                    GenericValetResponse<ValetRollbackResponse> gvr =
                            this.vci.callValetRollbackRequest(msoRequest.getRequestId(), null, false, me.getMessage());
                    // Nothing to really do here whether it succeeded or not other than log it.
                    logger.debug("Return code from Rollback response is {}", gvr.getStatusCode());
                } catch (Exception e) {
                    logger.error("Exception encountered while sending Rollback to Valet ", e);
                }
            }
            throw new VnfException(me);
        }


        // Reach this point if updateStack is successful.
        // Populate remaining rollback info and response parameters.
        vfRollback.setVnfId(heatStack.getCanonicalName());
        vfRollback.setVnfCreated(true);

        if (isValetEnabled && sendResponseToValet) {
            logger.debug("valet is enabled, the update succeeded - now send confirm to valet with stack id");
            try {
                GenericValetResponse<ValetConfirmResponse> gvr =
                        this.vci.callValetConfirmRequest(msoRequest.getRequestId(), heatStack.getCanonicalName());
                // Nothing to really do here whether it succeeded or not other than log it.
                logger.debug("Return code from Confirm response is {}", gvr.getStatusCode());
            } catch (Exception e) {
                logger.error("Exception encountered while sending Confirm to Valet ", e);
            }
        }

        outputs.value = copyStringOutputs(heatStack.getOutputs());
        rollback.value = vfRollback;
    }

    private String getVfModuleNameFromModuleStackId(String vfModuleStackId) {
        // expected format of vfModuleStackId is "MSOTEST51-vSAMP3_base_module-0/1fc1f86c-7b35-447f-99a6-c23ec176ae24"
        // before the "/" is the vfModuleName and after the "/" is the heat stack id in Openstack
        if (vfModuleStackId == null)
            return null;
        int index = vfModuleStackId.lastIndexOf('/');
        if (index <= 0)
            return null;
        String vfModuleName = null;
        try {
            vfModuleName = vfModuleStackId.substring(0, index);
        } catch (Exception e) {
            logger.debug("Exception", e);
            vfModuleName = null;
        }
        return vfModuleName;
    }

    /*
     * Helper method to check a boolean property value - on error return provided default
     */
    private boolean checkBooleanProperty(String propertyName, boolean defaultValue) {
        boolean property = defaultValue;
        try {
            String propertyString = this.environment.getProperty(propertyName);
            if ("true".equalsIgnoreCase(propertyString) || "y".equalsIgnoreCase(propertyString)) {
                property = true;
            } else if ("false".equalsIgnoreCase(propertyString) || "n".equalsIgnoreCase(propertyString)) {
                property = false;
            }
        } catch (Exception e) {
            logger.debug("An exception occured trying to get property {} - defaulting to ", propertyName, defaultValue,
                    e);
            property = defaultValue;
        }
        return property;
    }

    /*
     * Helper method to combine getFiles and nestedTemplates in to a single Map
     */
    private Map<String, Object> combineGetFilesAndNestedTemplates(Map<String, Object> getFiles,
            Map<String, Object> nestedTemplates) {
        boolean haveGetFiles = true;
        boolean haveNestedTemplates = true;
        Map<String, Object> files = new HashMap<>();
        if (getFiles == null || getFiles.isEmpty()) {
            haveGetFiles = false;
        }
        if (nestedTemplates == null || nestedTemplates.isEmpty()) {
            haveNestedTemplates = false;
        }
        if (haveGetFiles && haveNestedTemplates) {
            for (String keyString : getFiles.keySet()) {
                files.put(keyString, getFiles.get(keyString));
            }
            for (String keyString : nestedTemplates.keySet()) {
                files.put(keyString, nestedTemplates.get(keyString));
            }
        } else {
            // Handle if we only have one or neither:
            if (haveGetFiles) {
                files = getFiles;
            }
            if (haveNestedTemplates) {
                files = nestedTemplates;
            }
        }
        return files;
    }

    /*
     * Valet Create request
     */
    private boolean valetCreateRequest(String cloudSiteId, String cloudOwner, String tenantId,
            Map<String, Object> heatFilesObjects, Map<String, Object> nestedTemplatesChecked, String vfModuleName,
            boolean backout, HeatTemplate heatTemplate, String newEnvironmentString, Map<String, Object> goldenInputs,
            MsoRequest msoRequest, Map<String, Object> inputs, boolean failRequestOnValetFailure,
            Holder<Map<String, Object>> valetModifiedParamsHolder) throws VnfException {
        boolean valetSucceeded = false;
        String valetErrorMessage = "more detail not available";
        try {
            String keystoneUrl = msoHeatUtils.getCloudSiteKeystoneUrl(cloudSiteId);
            Map<String, Object> files =
                    this.combineGetFilesAndNestedTemplates(heatFilesObjects, nestedTemplatesChecked);
            HeatRequest heatRequest = new HeatRequest(vfModuleName, backout, heatTemplate.getTimeoutMinutes(),
                    heatTemplate.getTemplateBody(), newEnvironmentString, files, goldenInputs);
            GenericValetResponse<ValetCreateResponse> createReq = this.vci.callValetCreateRequest(
                    msoRequest.getRequestId(), cloudSiteId, cloudOwner, tenantId, msoRequest.getServiceInstanceId(),
                    (String) inputs.get("vnf_id"), (String) inputs.get("vnf_name"), (String) inputs.get("vf_module_id"),
                    (String) inputs.get("vf_module_name"), keystoneUrl, heatRequest);
            ValetCreateResponse vcr = createReq.getReturnObject();
            if (vcr != null && createReq.getStatusCode() == 200) {
                ValetStatus status = vcr.getStatus();
                if (status != null) {
                    String statusCode = status.getStatus(); // "ok" or "failed"
                    if ("ok".equalsIgnoreCase(statusCode)) {
                        Map<String, Object> newInputs = vcr.getParameters();
                        if (newInputs != null) {
                            Map<String, Object> oldGold = goldenInputs;
                            logger.debug("parameters before being modified by valet:{}", oldGold.toString());
                            goldenInputs = new HashMap<>();
                            for (String key : newInputs.keySet()) {
                                goldenInputs.put(key, newInputs.get(key));
                            }
                            valetModifiedParamsHolder.value = goldenInputs;
                            logger.debug("parameters after being modified by valet:{}", goldenInputs.toString());
                            valetSucceeded = true;
                        }
                    } else {
                        valetErrorMessage = status.getMessage();
                    }
                }
            } else {
                logger.debug("Got a bad response back from valet");
                valetErrorMessage = "Bad response back from Valet";
                valetSucceeded = false;
            }
        } catch (Exception e) {
            logger.error("An exception occurred trying to call valet ...", e);
            valetSucceeded = false;
            valetErrorMessage = e.getMessage();
        }
        if (failRequestOnValetFailure && !valetSucceeded) {
            // The valet request failed - and property says to fail the request
            // TODO Create a new exception class for valet?
            throw new VnfException("A failure occurred with Valet: " + valetErrorMessage);
        }
        return valetSucceeded;
    }

    /*
     * Valet update request
     */

    private boolean valetUpdateRequest(String cloudSiteId, String cloudOwnerId, String tenantId,
            Map<String, Object> heatFilesObjects, Map<String, Object> nestedTemplatesChecked, String vfModuleName,
            boolean backout, HeatTemplate heatTemplate, String newEnvironmentString, Map<String, Object> goldenInputs,
            MsoRequest msoRequest, Map<String, Object> inputs, boolean failRequestOnValetFailure,
            Holder<Map<String, Object>> valetModifiedParamsHolder) throws VnfException {

        boolean valetSucceeded = false;
        String valetErrorMessage = "more detail not available";
        try {
            String keystoneUrl = msoHeatUtils.getCloudSiteKeystoneUrl(cloudSiteId);
            Map<String, Object> files =
                    this.combineGetFilesAndNestedTemplates(heatFilesObjects, nestedTemplatesChecked);
            HeatRequest heatRequest = new HeatRequest(vfModuleName, false, heatTemplate.getTimeoutMinutes(),
                    heatTemplate.getTemplateBody(), newEnvironmentString, files, goldenInputs);
            // vnf name is not sent to MSO on update requests - so we will set it to the vf module name for now
            GenericValetResponse<ValetUpdateResponse> updateReq =
                    this.vci.callValetUpdateRequest(msoRequest.getRequestId(), cloudSiteId, cloudOwnerId, tenantId,
                            msoRequest.getServiceInstanceId(), (String) inputs.get("vnf_id"), vfModuleName,
                            (String) inputs.get("vf_module_id"), vfModuleName, keystoneUrl, heatRequest);
            ValetUpdateResponse vur = updateReq.getReturnObject();
            if (vur != null && updateReq.getStatusCode() == 200) {
                ValetStatus status = vur.getStatus();
                if (status != null) {
                    String statusCode = status.getStatus(); // "ok" or "failed"
                    if ("ok".equalsIgnoreCase(statusCode)) {
                        Map<String, Object> newInputs = vur.getParameters();
                        if (newInputs != null) {
                            Map<String, Object> oldGold = goldenInputs;
                            logger.debug("parameters before being modified by valet:{}", oldGold);
                            goldenInputs = new HashMap<>();
                            for (String key : newInputs.keySet()) {
                                goldenInputs.put(key, newInputs.get(key));
                            }
                            valetModifiedParamsHolder.value = goldenInputs;
                            logger.debug("parameters after being modified by valet:{}", goldenInputs);
                            valetSucceeded = true;
                        }
                    } else {
                        valetErrorMessage = status.getMessage();
                    }
                }
            } else {
                logger.debug("Got a bad response back from valet");
                valetErrorMessage = "Got a bad response back from valet";
                valetSucceeded = false;
            }
        } catch (Exception e) {
            logger.error("An exception occurred trying to call valet - will continue processing for now...", e);
            valetErrorMessage = e.getMessage();
            valetSucceeded = false;
        }
        if (failRequestOnValetFailure && !valetSucceeded) {
            // The valet request failed - and property says to fail the request
            // TODO Create a new exception class for valet?
            throw new VnfException("A failure occurred with Valet: " + valetErrorMessage);
        }
        return valetSucceeded;
    }

    /*
     * Valet delete request
     */
    private boolean valetDeleteRequest(String cloudSiteId, String cloudOwnerId, String tenantId, String vnfName,
            MsoRequest msoRequest, boolean failRequestOnValetFailure) {
        boolean valetDeleteRequestSucceeded = false;
        String valetErrorMessage = "more detail not available";
        try {
            String vfModuleId = vnfName;
            String vfModuleName = vnfName;
            try {
                vfModuleName = vnfName.substring(0, vnfName.indexOf('/'));
                vfModuleId = vnfName.substring(vnfName.indexOf('/') + 1);
            } catch (Exception e) {
                // do nothing - send what we got for vnfName for both to valet
                logger.error("An exception occurred trying to call MsoVnfAdapterImpl.valetDeleteRequest() method", e);
            }
            GenericValetResponse<ValetDeleteResponse> deleteReq = this.vci.callValetDeleteRequest(
                    msoRequest.getRequestId(), cloudSiteId, cloudOwnerId, tenantId, vfModuleId, vfModuleName);
            ValetDeleteResponse vdr = deleteReq.getReturnObject();
            if (vdr != null && deleteReq.getStatusCode() == 200) {
                ValetStatus status = vdr.getStatus();
                if (status != null) {
                    String statusCode = status.getStatus(); // "ok" or "failed"
                    if ("ok".equalsIgnoreCase(statusCode)) {
                        logger.debug("delete request to valet returned success");
                        valetDeleteRequestSucceeded = true;
                    } else {
                        logger.debug("delete request to valet returned failure");
                        valetDeleteRequestSucceeded = false;
                        valetErrorMessage = status.getMessage();
                    }
                }
            } else {
                logger.debug("Got a bad response back from valet - delete request failed");
                valetDeleteRequestSucceeded = false;
                valetErrorMessage = "Got a bad response back from valet - delete request failed";
            }
        } catch (Exception e) {
            logger.error("An exception occurred trying to call valet - valetDeleteRequest failed", e);
            valetDeleteRequestSucceeded = false;
            valetErrorMessage = e.getMessage();
        }
        if (!valetDeleteRequestSucceeded && failRequestOnValetFailure) {
            logger.error("ValetDeleteRequestFailed - del req still will be sent to openstack",
                    new VnfException("ValetDeleteRequestFailedError"));
        }
        return valetDeleteRequestSucceeded;
    }
}