aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java
blob: 492ebcf0e40173d5b61ef68a7eb1d973cd64a911 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.tosca;


import fj.F;
import fj.data.Either;
import io.vavr.Tuple2;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.onap.sdc.tosca.services.YamlUtil;
import org.openecomp.sdc.be.components.impl.ImportUtils;
import org.openecomp.sdc.be.components.impl.ImportUtils.Constants;
import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
import org.openecomp.sdc.be.config.ArtifactConfigManager;
import org.openecomp.sdc.be.config.ArtifactConfiguration;
import org.openecomp.sdc.be.config.ComponentType;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
import org.openecomp.sdc.be.plugins.CsarEntryGenerator;
import org.openecomp.sdc.be.resources.data.DAOArtifactData;
import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil;
import org.openecomp.sdc.be.utils.CommonBeUtils;
import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.log.elements.LoggerSupportability;
import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
import org.openecomp.sdc.common.log.enums.StatusCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.openecomp.sdc.common.util.ValidationUtils;
import org.openecomp.sdc.common.zip.ZipUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.yaml.snakeyaml.Yaml;

/**
 * @author tg851x
 *
 */
@org.springframework.stereotype.Component("csar-utils")
public class CsarUtils {
    private static final Logger log = Logger.getLogger(CsarUtils.class);
    private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(CsarUtils.class.getName());
    private static final String PATH_DELIMITER = "/";
    @Autowired
    private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
    @Autowired
    private ArtifactCassandraDao artifactCassandraDao;
    @Autowired
    private ComponentsUtils componentsUtils;
    @Autowired
    private ToscaExportHandler toscaExportUtils;
    @Autowired
    protected ToscaOperationFacade toscaOperationFacade;

    @Autowired(required = false)
    private List<CsarEntryGenerator> generators;

    private static final String CONFORMANCE_LEVEL = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel();
    private static final String SDC_VERSION = ExternalConfiguration.getAppVersion();
    public static final String ARTIFACTS_PATH = "Artifacts/";
    private static final String RESOURCES_PATH = "Resources/";
    private static final String DEFINITIONS_PATH = "Definitions/";
    public static final String WORKFLOW_ARTIFACT_DIR = "Workflows"+File.separator+"BPMN"+File.separator;
    public static final String DEPLOYMENT_ARTIFACTS_DIR = "Deployment"+File.separator;
    private static final String CSAR_META_VERSION = "1.0";
    private static final String CSAR_META_PATH_FILE_NAME = "csar.meta";
    private static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
    private static final String TOSCA_META_VERSION = "1.0";
    private static final String CSAR_VERSION = "1.1";
    public static final String ARTIFACTS = "Artifacts";
    private static final String DEFINITION = "Definitions";
    private static final String DEL_PATTERN = "([/\\\\]+)";
    private static final String WORD_PATTERN = "\\w\\_\\-\\.\\s]+)";
    public static final String VALID_ENGLISH_ARTIFACT_NAME = "([" + WORD_PATTERN;
    private static final String VALID_ENGLISH_ARTIFACT_NAME_WITH_DIGITS = "([\\d" + WORD_PATTERN;
    private static final String ARTIFACT_NAME_UNIQUE_ID = "ArtifactName {}, unique ID {}";

    private static final String VFC_NODE_TYPE_ARTIFACTS_PATH_PATTERN = ARTIFACTS + DEL_PATTERN +
                                                                              ImportUtils.Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX +
                                                                              VALID_ENGLISH_ARTIFACT_NAME_WITH_DIGITS + DEL_PATTERN +
                                                                              VALID_ENGLISH_ARTIFACT_NAME_WITH_DIGITS + DEL_PATTERN +
                                                                              VALID_ENGLISH_ARTIFACT_NAME_WITH_DIGITS + DEL_PATTERN +
                                                                              VALID_ENGLISH_ARTIFACT_NAME_WITH_DIGITS;

    public static final String VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN = ARTIFACTS + DEL_PATTERN+
                                                                             // Artifact Group (i.e Deployment/Informational)
                                                                             VALID_ENGLISH_ARTIFACT_NAME + DEL_PATTERN +
                                                                             // Artifact Type
                                                                             VALID_ENGLISH_ARTIFACT_NAME  + DEL_PATTERN +
                                                                             // Artifact Any File Name
                                                                             ".+";

    public static final String SERVICE_TEMPLATE_PATH_PATTERN = DEFINITION + DEL_PATTERN+
                                                                       // Service Template File Name
                                                                       VALID_ENGLISH_ARTIFACT_NAME;

    public static final String ARTIFACT_CREATED_FROM_CSAR = "Artifact created from csar";
    private static final String BLOCK_0_TEMPLATE = "SDC-TOSCA-Meta-File-Version: %s\nSDC-TOSCA-Definitions-Version: %s\n";

    private String versionFirstThreeOctets;

    public CsarUtils() {
        if(SDC_VERSION != null && !SDC_VERSION.isEmpty()){
            Matcher matcher = Pattern.compile("(?!\\.)(\\d+(\\.\\d+)+)(?![\\d\\.])").matcher(SDC_VERSION);
            matcher.find();
            setVersionFirstThreeOctets(matcher.group(0));
        } else {
            setVersionFirstThreeOctets("");
        }
    }

    /**
     *
     * @param component
     * @param getFromCS
     * @param isInCertificationRequest
     * @return
     */
    public Either<byte[], ResponseFormat> createCsar(Component component, boolean getFromCS, boolean isInCertificationRequest) {
        loggerSupportability.log(LoggerSupportabilityActions.GENERATE_CSAR, StatusCode.STARTED,"Starting to create Csar for component {} ",component.getName());
        final String createdBy = component.getCreatorFullName();
        String fileName;
        Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
        ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
        fileName = artifactDefinition.getArtifactName();

        String toscaConformanceLevel = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel();
        String csarBlock0 = createCsarBlock0(CSAR_META_VERSION, toscaConformanceLevel);
        byte[] csarBlock0Byte = csarBlock0.getBytes();

        final String toscaBlock0 = createToscaBlock0(TOSCA_META_VERSION, CSAR_VERSION, createdBy, fileName);
        byte[] toscaBlock0Byte = toscaBlock0.getBytes();

        return generateCsarZip(csarBlock0Byte, toscaBlock0Byte, component, getFromCS, isInCertificationRequest)
            .left().map(responseFormat -> {
            loggerSupportability.log(LoggerSupportabilityActions.GENERATE_CSAR, StatusCode.COMPLETE,
                "Ended create Csar for component {} ", component.getName());
            return responseFormat;
        });
    }

    private Either<byte[], ResponseFormat> generateCsarZip(byte[] csarBlock0Byte, byte[] toscaBlock0Byte, Component component, boolean getFromCS, boolean isInCertificationRequest) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
             ZipOutputStream zip = new ZipOutputStream(out)) {
            zip.putNextEntry(new ZipEntry(CSAR_META_PATH_FILE_NAME));
            zip.write(csarBlock0Byte);
            zip.putNextEntry(new ZipEntry(TOSCA_META_PATH_FILE_NAME));
            zip.write(toscaBlock0Byte);
            Either<ZipOutputStream, ResponseFormat> populateZip = populateZip(component, getFromCS, zip, isInCertificationRequest);
            if (populateZip.isRight()) {
                log.debug("Failed to populate CSAR zip file {}. Please fix DB table accordingly ", populateZip.right().value());
                return Either.right(populateZip.right().value());
            }

            zip.finish();
            byte[] byteArray = out.toByteArray();

            return Either.left(byteArray);
        } catch (IOException e) {
            log.debug("Failed with IOexception to create CSAR zip for component {}. Please fix DB table accordingly ", component.getUniqueId(), e);

            ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
            return Either.right(responseFormat);
        }
}

    private Either<ZipOutputStream, ResponseFormat> populateZip(Component component, boolean getFromCS, ZipOutputStream zip, boolean isInCertificationRequest) throws IOException {

        ArtifactDefinition artifactDef = component
            .getToscaArtifacts()
            .get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);

        Either<MainYamlWithDependencies, ResponseFormat> toscaRepresentation =
            fetchToscaRepresentation(component, getFromCS, artifactDef);

        // This should not be done but in order to keep the refactoring small enough we stop here.
        // TODO: Refactor the rest of this function
        byte[] mainYaml;
        List<Triple<String, String, Component>> dependencies;
        if(toscaRepresentation.isLeft()) {
            mainYaml = toscaRepresentation.left().value().mainYaml;
            dependencies = toscaRepresentation.left().value().dependencies.orElse(null);
        } else {
            return Either.right(toscaRepresentation.right().value());
        }

        String fileName = artifactDef.getArtifactName();
        zip.putNextEntry(new ZipEntry(DEFINITIONS_PATH + fileName));
        zip.write(mainYaml);

        //US798487 - Abstraction of complex types
        if (!ModelConverter.isAtomicComponent(component)){
            log.debug("Component {} is complex - generating abstract type for it..", component.getName());
			      writeComponentInterface(component, zip, fileName, false);
        }

        //UID <cassandraId,filename,component>
        Map<String, ImmutableTriple<String,String, Component>> innerComponentsCache = new HashMap<>();

        Either<ZipOutputStream, ResponseFormat> responseFormat = getZipOutputStreamResponseFormatEither(zip, dependencies, innerComponentsCache);
        if (responseFormat != null) return responseFormat;

        //retrieve SDC.zip from Cassandra
        Either<byte[], ResponseFormat> latestSchemaFilesFromCassandra = getLatestSchemaFilesFromCassandra();

        if(latestSchemaFilesFromCassandra.isRight()){
            log.error("Error retrieving SDC Schema files from cassandra");
            return Either.right(latestSchemaFilesFromCassandra.right().value());
        }

        final byte[] schemaFileZip = latestSchemaFilesFromCassandra.left().value();

        final List<String> nodesFromPackage = findNonRootNodesFromPackage(dependencies);

        //add files from retrieved SDC.zip to Definitions folder in CSAR
        addSchemaFilesFromCassandra(zip, schemaFileZip, nodesFromPackage);

        Either<CsarDefinition, ResponseFormat> collectedComponentCsarDefinition = collectComponentCsarDefinition(component);

        if (collectedComponentCsarDefinition.isRight()) {
            return Either.right(collectedComponentCsarDefinition.right().value());
        }

        if (generators != null) {
	        for (CsarEntryGenerator generator: generators) {
	            log.debug("Invoking CsarEntryGenerator: {}", generator.getClass().getName());
		        for (Entry<String, byte[]> pluginGeneratedFile : generator.generateCsarEntries(component).entrySet()) {
                    zip.putNextEntry(new ZipEntry(pluginGeneratedFile.getKey()));
                    zip.write(pluginGeneratedFile.getValue());
		        }
	        }
        }

        return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest);
    }

    private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaRepresentation(
        Component component,
        boolean getFromCS,
        ArtifactDefinition artifactDef
    ) {
        LifecycleStateEnum lifecycleState = component.getLifecycleState();

        boolean shouldBeFetchedFromCassandra = getFromCS ||
            !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN ||
                lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);

        Either<MainYamlWithDependencies, ResponseFormat> toscaRepresentation =
            shouldBeFetchedFromCassandra ?
                fetchToscaRepresentation(artifactDef) :
                generateToscaRepresentation(component);

        return toscaRepresentation.left().bind(iff(
            myd -> !myd.dependencies.isPresent(),
            myd -> fetchToscaTemplateDependencies(myd.mainYaml, component)
        ));
    }

    private static <L, R> F<L, Either<L, R>> iff(Predicate<L> p, Function<L, Either<L, R>> ifTrue) {
        return l -> p.test(l) ? ifTrue.apply(l) : Either.left(l);
    }

    private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaTemplateDependencies(
        byte[] mainYml,
        Component component
    ) {
        return toscaExportUtils.getDependencies(component).right().map(toscaError -> {
            log.debug("Failed to retrieve dependencies for component {}, error {}",
                component.getUniqueId(), toscaError);
            return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError));
        }).left().map(tt -> MainYamlWithDependencies.make(mainYml, tt));
    }

    private Either<MainYamlWithDependencies, ResponseFormat> generateToscaRepresentation(Component component) {
        return toscaExportUtils.exportComponent(component).right().map(toscaError -> {
            log.debug("exportComponent failed", toscaError);
            return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError));
        }).left().map(MainYamlWithDependencies::make);
    }

    private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaRepresentation(ArtifactDefinition artifactDef) {
        return getFromCassandra(artifactDef.getEsId()).right().map(as -> {
            log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDef.getArtifactName(), artifactDef.getUniqueId());
            return componentsUtils.getResponseFormat(as);
        }).left().map(MainYamlWithDependencies::make);
    }

    // TODO: Refactor the ToscaRepresentation class in order to remove the following one
    // This will be done in a separate change
    private static class MainYamlWithDependencies {

        private final byte[] mainYaml;
        private final Optional<List<Triple<String, String, Component>>> dependencies;

        private MainYamlWithDependencies(byte[] mainYaml,
            Optional<List<Triple<String, String, Component>>> dependencies) {
            this.mainYaml = mainYaml;
            this.dependencies = dependencies;
        }

        public static MainYamlWithDependencies make(byte[] mainYaml) {
            return new MainYamlWithDependencies(mainYaml, Optional.empty());
        }

        public static MainYamlWithDependencies make(ToscaRepresentation tr) {
            return new MainYamlWithDependencies(tr.getMainYaml().getBytes(), Optional.ofNullable(tr.getDependencies()));
        }

        public static MainYamlWithDependencies make(byte[] mainYaml, ToscaTemplate tt) {
            return new MainYamlWithDependencies(mainYaml, Optional.ofNullable(tt.getDependencies()));
        }
    }

    /**
     * Create a list of all derived nodes found on the package
     *
     * @param dependencies all node dependencies
     * @return a list of nodes
     */
    private List<String> findNonRootNodesFromPackage(final List<Triple<String, String, Component>> dependencies) {
        final List<String> nodes = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dependencies)) {
            final String NATIVE_ROOT = "tosca.nodes.Root";
            dependencies.forEach(dependency -> {
                if (dependency.getRight() instanceof Resource) {
                    final Resource resource = (Resource) dependency.getRight();
                    if (CollectionUtils.isNotEmpty(resource.getDerivedList())) {
                        resource.getDerivedList().stream()
                            .filter(node -> !nodes.contains(node) && !NATIVE_ROOT.equalsIgnoreCase(node))
                            .forEach(node -> nodes.add(node));
                    }
                }
            });
        }
        return nodes;
    }

    /**
     * Writes a new zip entry
     *
     * @param zipInputStream the zip entry to be read
     * @return a map of the given zip entry
     */
    private Map<String, Object> readYamlZipEntry(final ZipInputStream zipInputStream) throws IOException {
        final int initSize = 2048;
        final StringBuilder zipEntry = new StringBuilder();
        final byte[] buffer = new byte[initSize];
        int read = 0;
        while ((read = zipInputStream.read(buffer, 0, initSize)) >= 0) {
            zipEntry.append(new String(buffer, 0, read));
        }

        return (Map<String, Object>) new Yaml().load(zipEntry.toString());
    }

    /**
     * Filters and removes all duplicated nodes found
     *
     * @param nodesFromPackage a List of all derived nodes found on the given package
     * @param nodesFromArtifactFile represents the nodes.yml file stored in Cassandra
     * @return a nodes Map updated
     */
    private Map<String, Object> updateNodeYml(final List<String> nodesFromPackage,
                                              final Map<String, Object> nodesFromArtifactFile) {

        if (MapUtils.isNotEmpty(nodesFromArtifactFile)) {
            final String nodeTypeBlock = ToscaTagNamesEnum.NODE_TYPES.getElementName();
            final Map<String, Object> nodeTypes = (Map<String, Object>) nodesFromArtifactFile.get(nodeTypeBlock);
            nodesFromPackage.stream()
                .filter(nodeTypes::containsKey)
                .forEach(nodeTypes::remove);

            nodesFromArtifactFile.replace(nodeTypeBlock, nodeTypes);
        }

        return nodesFromArtifactFile;
    }

    /**
     * Updates the zip entry from the given parameters
     *
     * @param byteArrayOutputStream an output stream in which the data is written into a byte array.
     * @param nodesYaml a Map of nodes to be written
     */
    private void updateZipEntry(final ByteArrayOutputStream byteArrayOutputStream,
                                final Map<String, Object> nodesYaml) throws IOException {
        if (MapUtils.isNotEmpty(nodesYaml)) {
            byteArrayOutputStream.write(new YamlUtil().objectToYaml(nodesYaml).getBytes());
        }
    }

    private Either<ZipOutputStream, ResponseFormat> getZipOutputStreamResponseFormatEither(
        ZipOutputStream zip,
        List<Triple<String, String, Component>> dependencies,
        Map<String, ImmutableTriple<String, String, Component>> innerComponentsCache
    ) throws IOException {
        if (dependencies != null && !dependencies.isEmpty()) {
            for (Triple<String, String, Component> d : dependencies) {
                String cassandraId = d.getMiddle();
                Component childComponent = d.getRight();

                Either<byte[], ResponseFormat> entryData = getEntryData(cassandraId, childComponent)
                    .right().map(x -> componentsUtils.getResponseFormat(x));

                if (entryData.isRight()) {
                    return Either.right(entryData.right().value());
                }

                //fill innerComponentsCache
                String fileName = d.getLeft();
                // TODO: Extract the cache related functions to their own class
                addComponentToCache(innerComponentsCache, cassandraId, fileName, childComponent);
                addInnerComponentsToCache(innerComponentsCache, childComponent);
            }

            //add inner components to CSAR
            Either<ZipOutputStream, ResponseFormat> responseFormat = addInnerComponentsToCSAR(zip,
                innerComponentsCache);
            if (responseFormat != null) {
                return responseFormat;
            }
        }
        return null;
    }

    private Either<ZipOutputStream, ResponseFormat> addInnerComponentsToCSAR(
        ZipOutputStream zip,
        Map<String, ImmutableTriple<String, String, Component>> innerComponentsCache
    ) throws IOException {
        for (Entry<String, ImmutableTriple<String, String, Component>> entry : innerComponentsCache.entrySet()) {
            ImmutableTriple<String, String, Component> ict = entry.getValue();
            Component innerComponent = ict.getRight();

            String icFileName = ict.getMiddle();
            // add component to zip
            Either<Tuple2<byte[], ZipEntry>, ResponseFormat> zipEntry = toZipEntry(ict);
            // TODO: this should not be done, we should instead compose this either further,
            // but in order to keep this refactoring small, we'll stop here.
            if (zipEntry.isRight()) {
                return Either.right(zipEntry.right().value());
            }
            Tuple2<byte[], ZipEntry> value = zipEntry.left().value();
            zip.putNextEntry(value._2);
            zip.write(value._1);
            // add component interface to zip
            if (!ModelConverter.isAtomicComponent(innerComponent)) {
                writeComponentInterface(innerComponent, zip, icFileName, true);
            }
        }
        return null;
    }

    private Either<Tuple2<byte[], ZipEntry>, ResponseFormat> toZipEntry(
        ImmutableTriple<String, String, Component> cachedEntry
    ) {
        String cassandraId = cachedEntry.getLeft();
        String fileName = cachedEntry.getMiddle();
        Component innerComponent = cachedEntry.getRight();
        return getEntryData(cassandraId, innerComponent)
            .right().map(status -> {
                log.debug("Failed adding to zip component {}, error {}", cassandraId, status);
                return componentsUtils.getResponseFormat(status);
            }).left().map(content -> new Tuple2<>(content, new ZipEntry(DEFINITIONS_PATH + fileName)));
    }

    private void addSchemaFilesFromCassandra(final ZipOutputStream zip,
                                             final byte[] schemaFileZip,
                                             final List<String> nodesFromPackage) {
        final int initSize = 2048;
        log.debug("Starting copy from Schema file zip to CSAR zip");
        try (final ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(schemaFileZip));
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            final BufferedOutputStream bos = new BufferedOutputStream(out, initSize)) {

            ZipEntry entry;
            while ((entry = zipStream.getNextEntry()) != null) {
                ZipUtils.checkForZipSlipInRead(entry);
                final String entryName = entry.getName();
                int readSize = initSize;
                final byte[] entryData = new byte[initSize];
                if (entryName.equalsIgnoreCase("nodes.yml")) {
                    handleNode(zipStream, out, nodesFromPackage);
                } else {
                    while ((readSize = zipStream.read(entryData, 0, readSize)) != -1) {
                        bos.write(entryData, 0, readSize);
                    }
                    bos.flush();
                }
                out.flush();
                zip.putNextEntry(new ZipEntry(DEFINITIONS_PATH + entryName));
                zip.write(out.toByteArray());
                zip.flush();
                out.reset();
            }
        } catch (final Exception e) {
            log.error("Error while writing the SDC schema file to the CSAR", e);
            throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
        }
        log.debug("Finished copy from Schema file zip to CSAR zip");
    }

    /**
     * Handles the nodes.yml zip entry, updating the nodes.yml to avoid duplicated nodes on it.
     *
     * @param zipInputStream the zip entry to be read
     * @param byteArrayOutputStream an output stream in which the data is written into a byte array.
     * @param nodesFromPackage list of all nodes found on the onboarded package
     */
    private void handleNode(final ZipInputStream zipInputStream,
                            final ByteArrayOutputStream byteArrayOutputStream,
                            final List<String> nodesFromPackage) throws IOException {

        final Map<String, Object> nodesFromArtifactFile = readYamlZipEntry(zipInputStream);
        final Map<String, Object> nodesYaml = updateNodeYml(nodesFromPackage, nodesFromArtifactFile);
        updateZipEntry(byteArrayOutputStream, nodesYaml);
    }

    private void addInnerComponentsToCache(Map<String, ImmutableTriple<String, String, Component>> componentCache,
            Component childComponent) {

        List<ComponentInstance> instances = childComponent.getComponentInstances();

        if(instances != null) {
            instances.forEach(ci -> {
                ImmutableTriple<String, String, Component> componentRecord = componentCache.get(ci.getComponentUid());
                if (componentRecord == null) {
                    // all resource must be only once!
                    Either<Resource, StorageOperationStatus> resource = toscaOperationFacade.getToscaElement(ci.getComponentUid());
                    Component componentRI = checkAndAddComponent(componentCache, ci, resource);

                    //if not atomic - insert inner components as well
                    if(!ModelConverter.isAtomicComponent(componentRI)) {
                        addInnerComponentsToCache(componentCache, componentRI);
                    }
                }
            });
        }
    }

    private Component checkAndAddComponent(Map<String, ImmutableTriple<String, String, Component>> componentCache, ComponentInstance ci, Either<Resource, StorageOperationStatus> resource) {
        if (resource.isRight()) {
            log.debug("Failed to fetch resource with id {} for instance {}", ci.getComponentUid(), ci.getName());
        }
        Component componentRI = resource.left().value();

        Map<String, ArtifactDefinition> childToscaArtifacts = componentRI.getToscaArtifacts();
        ArtifactDefinition childArtifactDefinition = childToscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
        if (childArtifactDefinition != null) {
            //add to cache
            addComponentToCache(componentCache, childArtifactDefinition.getEsId(), childArtifactDefinition.getArtifactName(), componentRI);
        }
        return componentRI;
    }

    private void addComponentToCache(Map<String, ImmutableTriple<String, String, Component>> componentCache,
            String id, String fileName, Component component) {

        String uuid = component.getInvariantUUID();
        String version = component.getVersion();

        Supplier<ImmutableTriple<String, String, Component>> sup =
            () -> new ImmutableTriple<>(id, fileName, component);

        componentCache.put(uuid, updateWith(componentCache, uuid,
            cc -> overwriteIfSameVersions(id, version, cc, sup),
            sup
        ));
    }

    private static <K, V> V updateWith(Map<K, V> kvs, K k, Function<V, V> f, Supplier<V> orElse) {
        return Optional.ofNullable(kvs.get(k)).map(f).orElseGet(orElse);
    }

    private ImmutableTriple<String, String, Component> overwriteIfSameVersions(
        String id, String version,
        ImmutableTriple<String, String, Component> cc,
        Supplier<ImmutableTriple<String, String, Component>> newValue
    ) {
        if (CommonBeUtils.compareAsdcComponentVersions(version, cc.getRight().getVersion())) {
            log.warn("Overwriting component invariantID {} of version {} with a newer version {}", id,
                cc.getRight().getVersion(),
                version);
            return newValue.get();
        }
        return cc;
    }

    private Either<ZipOutputStream, ResponseFormat> writeComponentInterface(
        Component component,
        ZipOutputStream zip,
        String fileName,
        boolean isAssociatedComponent
    ) {
        try {
            Either<String, ToscaError> mainYaml = toscaExportUtils
                .exportComponentInterface(component, isAssociatedComponent)
                .left().map(ToscaRepresentation::getMainYaml);

            // TODO: This should be done outside this function to keep this testable.
            // We can probably achieve this once the other refactorings related to SDC-2812 are merged
            String interfaceFileName = DEFINITIONS_PATH + ToscaExportHandler.getInterfaceFilename(fileName);
            zip.putNextEntry(new ZipEntry(interfaceFileName));
            zip.write(mainYaml.left().value().getBytes());

        } catch (Exception e) {
            log.error("#writeComponentInterface - zip writing failed with error: ", e);
            return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
        }
        return Either.left(zip);
    }

    private Either<byte[], ActionStatus> getEntryData(String cassandraId, Component childComponent) {
        if (cassandraId == null || cassandraId.isEmpty()) {
            return toscaExportUtils.exportComponent(childComponent)
                .right().map(toscaErrorToActionStatus(childComponent))
                .left().map(toscaRepresentation -> toscaRepresentation.getMainYaml().getBytes());
        } else {
            return getFromCassandra(cassandraId);
        }
    }

    private F<ToscaError, ActionStatus> toscaErrorToActionStatus(Component childComponent) {
        return toscaError -> {
            log.debug("Failed to export tosca template for child component {} error {}",
                childComponent.getUniqueId(), toscaError);
            return componentsUtils.convertFromToscaError(toscaError);
        };
    }

    private Either<byte[], ResponseFormat> getLatestSchemaFilesFromCassandra() {
        String fto = getVersionFirstThreeOctets();
        return sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(fto, CONFORMANCE_LEVEL)
            .right().map(schemaFilesFetchDBError(fto))
            .left().bind(iff(
                List::isEmpty,
                () -> schemaFileFetchError(fto),
                s -> Either.left(s.iterator().next().getPayloadAsArray())
                )
            );
    }

    private static <A, B> F<A, B> iff(Predicate<A> p, Supplier<B> s, Function<A, B> orElse) {
        return a -> p.test(a) ? s.get() : orElse.apply(a);
    }

    private F<CassandraOperationStatus, ResponseFormat> schemaFilesFetchDBError(String firstThreeOctets) {
        return cos -> {
            log.debug(
                "Failed to get the schema files SDC-Version: {} Conformance-Level {}. Please fix DB table accordingly.",
                firstThreeOctets, CONFORMANCE_LEVEL);
            StorageOperationStatus sos = DaoStatusConverter.convertCassandraStatusToStorageStatus(cos);
            return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(sos));
        };
    }

    private Either<byte[], ResponseFormat> schemaFileFetchError(String firstThreeOctets) {
        log.debug("Failed to get the schema files SDC-Version: {} Conformance-Level {}",
            firstThreeOctets, CONFORMANCE_LEVEL);
        return Either.right(
            componentsUtils.getResponseFormat(
                ActionStatus.TOSCA_SCHEMA_FILES_NOT_FOUND, firstThreeOctets, CONFORMANCE_LEVEL
            )
        );
    }

    private Either<byte[], ActionStatus> getFromCassandra(String cassandraId) {
        return artifactCassandraDao.getArtifact(cassandraId)
            .right().map(cos -> {
                log.debug("Failed to fetch artifact from Cassandra by id {} error {} ", cassandraId, cos);
                StorageOperationStatus storageStatus = DaoStatusConverter.convertCassandraStatusToStorageStatus(cos);
                return componentsUtils.convertFromStorageResponse(storageStatus);
            }).left().map(DAOArtifactData::getDataAsArray);
    }

    private String createCsarBlock0(String metaFileVersion, String toscaConformanceLevel) {
        return String.format(BLOCK_0_TEMPLATE, metaFileVersion, toscaConformanceLevel);
    }

    private String createToscaBlock0(String metaFileVersion, String csarVersion, String createdBy, String entryDef) {
        final String block0template = "TOSCA-Meta-File-Version: %s\nCSAR-Version: %s\nCreated-By: %s\nEntry-Definitions: Definitions/%s\n\nName: csar.meta\nContent-Type: text/plain\n";
        return String.format(block0template, metaFileVersion, csarVersion, createdBy, entryDef);
    }

    /**
     * Extracts artifacts of VFCs from CSAR
     *
     * @param csar
     * @return Map of <String, List<ArtifactDefinition>> the contains Lists of artifacts according vfcToscaNamespace
     */
    public static Map<String, List<ArtifactDefinition>> extractVfcsArtifactsFromCsar(Map<String, byte[]> csar) {

        Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
        if (csar != null) {
            log.debug("************* Going to extract VFCs artifacts from Csar. ");
            Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
            csar.entrySet().stream()
                // filter CSAR entry by node type artifact path
                .filter(e -> Pattern.compile(VFC_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
                // extract ArtifactDefinition from CSAR entry for each entry with matching artifact path
                .forEach(e ->
                    extractVfcArtifact(e, collectedWarningMessages).ifPresent(ip ->
                        addExtractedVfcArtifact(ip, artifacts)
                    )
                );
            // add counter suffix to artifact labels
            handleWarningMessages(collectedWarningMessages);

        }
        return artifacts;
    }

    /**
     * Print warnings to log
     *
     * @param collectedWarningMessages
     */
    public static void handleWarningMessages(Map<String, Set<List<String>>> collectedWarningMessages) {
        collectedWarningMessages.entrySet().stream()
                                // for each vfc
                                .forEach(e -> e.getValue().stream()
                                               // add each warning message to log
                                               .forEach(args -> log.warn(e.getKey(), args.toArray())));

    }

    private static void addExtractedVfcArtifact(
        ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact,
        Map<String, List<ArtifactDefinition>> artifacts
    ) {
        String vfcToscaNamespace = extractedVfcArtifact.getKey();

        artifacts.computeIfAbsent(vfcToscaNamespace, k -> new ArrayList<>());
        artifacts.get(vfcToscaNamespace).add(extractedVfcArtifact.getValue());
    }

    private static Optional<ImmutablePair<String, ArtifactDefinition>> extractVfcArtifact(
        Entry<String, byte[]> entry,
        Map<String, Set<List<String>>> collectedWarningMessages
    ) {
        String[] parsedCsarArtifactPath = entry.getKey().split(PATH_DELIMITER);
        String groupType = parsedCsarArtifactPath[2].toUpperCase();

        return detectArtifactGroupType(groupType, collectedWarningMessages)
            .left()
            .map(buildArtifactDefinitionFromCsarArtifactPath(entry, collectedWarningMessages, parsedCsarArtifactPath))
            .either(
                ad -> Optional.of(new ImmutablePair<>(parsedCsarArtifactPath[1], ad)),
                b -> Optional.empty()
            );
    }

    private static Either<ArtifactGroupTypeEnum, Boolean> detectArtifactGroupType(String groupType, Map<String, Set<List<String>>> collectedWarningMessages) {
        Either<ArtifactGroupTypeEnum, Boolean> result;
        try {
            ArtifactGroupTypeEnum artifactGroupType = ArtifactGroupTypeEnum.findType(groupType.toUpperCase());
            if (artifactGroupType == null || (artifactGroupType != ArtifactGroupTypeEnum.INFORMATIONAL && artifactGroupType != ArtifactGroupTypeEnum.DEPLOYMENT)) {
                String warningMessage = "Warning - unrecognized artifact group type {} was received.";
                List<String> messageArguments = new ArrayList<>();
                messageArguments.add(groupType);
                if (!collectedWarningMessages.containsKey(warningMessage)) {
                    Set<List<String>> messageArgumentLists = new HashSet<>();
                    messageArgumentLists.add(messageArguments);
                    collectedWarningMessages.put(warningMessage, messageArgumentLists);
                } else {
                    collectedWarningMessages.get(warningMessage).add(messageArguments);
                }

                result = Either.right(false);
            } else {

                result = Either.left(artifactGroupType);
            }
        } catch (Exception e) {
            log.debug("detectArtifactGroupType failed with exception", e);
            result = Either.right(false);
        }
        return result;
    }

    private static F<ArtifactGroupTypeEnum, ArtifactDefinition> buildArtifactDefinitionFromCsarArtifactPath(
        Entry<String, byte[]> entry,
        Map<String, Set<List<String>>> collectedWarningMessages,
        String[] parsedCsarArtifactPath
    ) {
        return artifactGroupType -> {
            ArtifactDefinition artifact;
            artifact = new ArtifactDefinition();
            artifact.setArtifactGroupType(artifactGroupType);
            artifact.setArtifactType(
                detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1],
                    collectedWarningMessages));
            artifact.setArtifactName(
                ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1]));
            artifact.setPayloadData(Base64.encodeBase64String(entry.getValue()));
            artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName()
                .substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName());
            artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName()));
            artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR);
            artifact.setIsFromCsar(true);
            artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue()));
            return artifact;
        };
    }

    @Getter
    public static final class NonMetaArtifactInfo {
        @Setter
        private String artifactUniqueId;
        private final String path;
        private final String artifactName;
        private final String displayName;
        private final String artifactLabel;
        private final String artifactType;
        private final ArtifactGroupTypeEnum artifactGroupType;
        private final String payloadData;
        private final String artifactChecksum;
        private final boolean isFromCsar;

        public NonMetaArtifactInfo(final String artifactName, final String path, final String artifactType,
                                   final ArtifactGroupTypeEnum artifactGroupType, final byte[] payloadData,
                                   final String artifactUniqueId, final boolean isFromCsar) {
            super();
            this.path = path;
            this.isFromCsar = isFromCsar;
            this.artifactName = ValidationUtils.normalizeFileName(artifactName);
            this.artifactType = artifactType;
            this.artifactGroupType = artifactGroupType;
            final int pointIndex = artifactName.lastIndexOf('.');
            if (pointIndex > 0) {
                displayName = artifactName.substring(0, pointIndex);
            } else {
                displayName = artifactName;
            }
            this.artifactLabel = ValidationUtils.normalizeArtifactLabel(artifactName);
            if (payloadData == null) {
                this.payloadData = null;
                this.artifactChecksum = null;
            } else {
                this.payloadData = Base64.encodeBase64String(payloadData);
                this.artifactChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(payloadData);
            }
            this.artifactUniqueId = artifactUniqueId;
        }

    }

    /**
     * This method checks the artifact GroupType & Artifact Type. <br>
     * if there is any problem warning messages are added to collectedWarningMessages
     *
     * @param artifactPath
     * @param collectedWarningMessages
     * @return
     */
    public static Either<NonMetaArtifactInfo, Boolean> validateNonMetaArtifact(
        String artifactPath,
        byte[] payloadData,
        Map<String, Set<List<String>>> collectedWarningMessages
    ) {
        try {
            String[] parsedArtifactPath = artifactPath.split(PATH_DELIMITER);
            String groupType = parsedArtifactPath[1];
            String receivedTypeName = parsedArtifactPath[2];
            String artifactFileNameType = parsedArtifactPath[3];

            return detectArtifactGroupType(groupType, collectedWarningMessages)
                .left().bind(artifactGroupType -> {
                    String artifactType =
                        detectArtifactTypeVF(artifactGroupType, receivedTypeName, collectedWarningMessages);

                    return Either.left(new NonMetaArtifactInfo(
                        artifactFileNameType, artifactPath, artifactType,
                        artifactGroupType, payloadData, null, true
                    ));
                });
        } catch (Exception e) {
            log.debug("detectArtifactGroupType failed with exception", e);
            return Either.right(false);
        }
    }

    private static String detectArtifactTypeVFC(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, String parentVfName, Map<String, Set<List<String>>> collectedWarningMessages) {
        String warningMessage = "Warning - artifact type {} that was provided for VFC {} is not recognized.";
        return detectArtifactType(artifactGroupType, receivedTypeName, warningMessage, collectedWarningMessages, parentVfName);
    }

    private static String detectArtifactTypeVF(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, Map<String, Set<List<String>>> collectedWarningMessages) {
        String warningMessage = "Warning - artifact type {} that was provided for VF is not recognized.";
        return detectArtifactType(artifactGroupType, receivedTypeName, warningMessage, collectedWarningMessages);
    }

    private static String detectArtifactType(final ArtifactGroupTypeEnum artifactGroupType,
                                             final String receivedTypeName, final String warningMessage,
                                             final Map<String, Set<List<String>>> collectedWarningMessages,
                                             final String... arguments) {
        final ArtifactConfiguration artifactConfiguration =
            ArtifactConfigManager.getInstance()
                .find(receivedTypeName, artifactGroupType, ComponentType.RESOURCE)
                .orElse(null);

        if (artifactConfiguration == null) {
            final List<String> messageArguments = new ArrayList<>();
            messageArguments.add(receivedTypeName);
            messageArguments.addAll(Arrays.asList(arguments));
            if (!collectedWarningMessages.containsKey(warningMessage)) {
                final Set<List<String>> messageArgumentLists = new HashSet<>();
                messageArgumentLists.add(messageArguments);
                collectedWarningMessages.put(warningMessage, messageArgumentLists);
            } else {
                collectedWarningMessages.get(warningMessage).add(messageArguments);
            }
        }

        return artifactConfiguration == null ? ArtifactTypeEnum.OTHER.getType() : receivedTypeName;
    }

    private Either<ZipOutputStream, ResponseFormat> writeAllFilesToCsar(Component mainComponent, CsarDefinition csarDefinition, ZipOutputStream zipstream, boolean isInCertificationRequest) throws IOException{
        ComponentArtifacts componentArtifacts = csarDefinition.getComponentArtifacts();

        Either<ZipOutputStream, ResponseFormat> writeComponentArtifactsToSpecifiedPath = writeComponentArtifactsToSpecifiedPath(mainComponent, componentArtifacts, zipstream, ARTIFACTS_PATH, isInCertificationRequest);

        if(writeComponentArtifactsToSpecifiedPath.isRight()){
            return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
        }

        ComponentTypeArtifacts mainTypeAndCIArtifacts = componentArtifacts.getMainTypeAndCIArtifacts();
        writeComponentArtifactsToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, mainTypeAndCIArtifacts.getComponentArtifacts(), zipstream, ARTIFACTS_PATH, isInCertificationRequest);

        if(writeComponentArtifactsToSpecifiedPath.isRight()){
            return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
        }

        Map<String, ArtifactsInfo> componentInstancesArtifacts = mainTypeAndCIArtifacts.getComponentInstancesArtifacts();
        Set<String> keySet = componentInstancesArtifacts.keySet();

        String currentPath = ARTIFACTS_PATH + RESOURCES_PATH;
        for (String keyAssetName : keySet) {
            ArtifactsInfo artifactsInfo = componentInstancesArtifacts.get(keyAssetName);
            String pathWithAssetName = currentPath + keyAssetName + PATH_DELIMITER;
            writeComponentArtifactsToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, artifactsInfo, zipstream, pathWithAssetName, isInCertificationRequest);

            if(writeComponentArtifactsToSpecifiedPath.isRight()){
                return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
            }
        }
        writeComponentArtifactsToSpecifiedPath = writeOperationsArtifactsToCsar(mainComponent, zipstream);

        if (writeComponentArtifactsToSpecifiedPath.isRight()) {
            return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
        }
        return Either.left(zipstream);
    }

    private Either<ZipOutputStream, ResponseFormat> writeOperationsArtifactsToCsar(Component component,
            ZipOutputStream zipstream) {
        if (checkComponentBeforeOperation(component)) return Either.left(zipstream);
        final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
        for (Map.Entry<String, InterfaceDefinition> interfaceEntry : interfaces.entrySet()) {
            for (OperationDataDefinition operation : interfaceEntry.getValue().getOperations().values()) {
                try {
                    if (checkComponentBeforeWrite(component, interfaceEntry, operation)) continue;
                    final String artifactUUID = operation.getImplementation().getArtifactUUID();
                    final Either<byte[], ActionStatus> artifactFromCassandra = getFromCassandra(artifactUUID);
                    final String artifactName = operation.getImplementation().getArtifactName();
                    if (artifactFromCassandra.isRight()) {
                        log.error(ARTIFACT_NAME_UNIQUE_ID, artifactName, artifactUUID);
                        log.error("Failed to get {} payload from DB reason: {}", artifactName,
                                artifactFromCassandra.right().value());
                        return Either.right(componentsUtils.getResponseFormat(
                                ActionStatus.ARTIFACT_PAYLOAD_NOT_FOUND_DURING_CSAR_CREATION, "Resource",
                                component.getUniqueId(), artifactName, artifactUUID));
                    }
                    final byte[] payloadData = artifactFromCassandra.left().value();
                    zipstream.putNextEntry(new ZipEntry(OperationArtifactUtil.createOperationArtifactPath(
                            component, null, operation,true)));
                    zipstream.write(payloadData);
                } catch (IOException e) {
                    log.error("Component Name {},  Interface Name {}, Operation Name {}", component.getNormalizedName(),
                            interfaceEntry.getKey(), operation.getName());
                    log.error("Error while writing the operation's artifacts to the CSAR " + "{}", e);
                    return Either.right(componentsUtils
                                                .getResponseFormat(ActionStatus.ERROR_DURING_CSAR_CREATION, "Resource",
                                                        component.getUniqueId()));
                }
            }
        }
        return Either.left(zipstream);
    }

    private boolean checkComponentBeforeWrite(Component component, Entry<String, InterfaceDefinition> interfaceEntry, OperationDataDefinition operation) {
        if (Objects.isNull(operation.getImplementation())) {
            log.debug("Component Name {}, Interface Id {}, Operation Name {} - no Operation Implementation found",
                    component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
                    operation.getName());
            return true;
        }
        if (Objects.isNull(operation.getImplementation().getArtifactName())) {
            log.debug("Component Name {}, Interface Id {}, Operation Name {} - no artifact found",
                    component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
                    operation.getName());
            return true;
        }
        if (operation.getImplementation().getArtifactName().startsWith(Constants.ESCAPED_DOUBLE_QUOTE) && operation.getImplementation().getArtifactName().endsWith(Constants.ESCAPED_DOUBLE_QUOTE)) {
            log.debug("Component Name {}, Interface Id {}, Operation Name {} - artifact name is a literal value rather than an SDC artifact",
                    component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
                    operation.getName());
            return true;
        }
        return false;
    }

    private boolean checkComponentBeforeOperation(Component component) {
        if (component instanceof Service) {
            return true;
        }
        if (Objects.isNull(((Resource) component).getInterfaces())) {
            log.debug("Component Name {}- no interfaces found", component.getNormalizedName());
            return true;
        }
        return false;
    }

    private Either<ZipOutputStream, ResponseFormat> writeComponentArtifactsToSpecifiedPath(Component mainComponent, ComponentArtifacts componentArtifacts, ZipOutputStream zipstream,
            String currentPath, boolean isInCertificationRequest) throws IOException {
        Map<String, ComponentTypeArtifacts> componentTypeArtifacts = componentArtifacts.getComponentTypeArtifacts();
        //Keys are defined:
        //<Inner Asset TOSCA name (e.g. VFC name)> folder name: <Inner Asset TOSCA name (e.g. VFC name)>_v<version>.
        //E.g. "org.openecomp.resource.vf.vipr_atm_v1.0"
        Set<String> componentTypeArtifactsKeys = componentTypeArtifacts.keySet();
        for (String keyAssetName : componentTypeArtifactsKeys) {
            ComponentTypeArtifacts componentInstanceArtifacts = componentTypeArtifacts.get(keyAssetName);
            ArtifactsInfo componentArtifacts2 = componentInstanceArtifacts.getComponentArtifacts();
            String pathWithAssetName = currentPath + keyAssetName + PATH_DELIMITER;
            Either<ZipOutputStream, ResponseFormat> writeArtifactsInfoToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, componentArtifacts2, zipstream, pathWithAssetName, isInCertificationRequest);

            if(writeArtifactsInfoToSpecifiedPath.isRight()){
                return writeArtifactsInfoToSpecifiedPath;
            }
        }

        return Either.left(zipstream);
    }

    private Either<ZipOutputStream, ResponseFormat> writeArtifactsInfoToSpecifiedPath(final Component mainComponent,
        final ArtifactsInfo currArtifactsInfo,
        final ZipOutputStream zip,
        final String path,
        final boolean isInCertificationRequest) throws IOException {

        final Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> artifactsInfo =
            currArtifactsInfo.getArtifactsInfo();
        for (final ArtifactGroupTypeEnum artifactGroupTypeEnum : artifactsInfo.keySet()) {
            final String groupTypeFolder =
                path + WordUtils.capitalizeFully(artifactGroupTypeEnum.getType()) + PATH_DELIMITER;

            final Map<String, List<ArtifactDefinition>> artifactTypesMap = artifactsInfo.get(artifactGroupTypeEnum);

            for (final String artifactType : artifactTypesMap.keySet()) {
                final List<ArtifactDefinition> artifactDefinitionList = artifactTypesMap.get(artifactType);
                String artifactTypeFolder = groupTypeFolder + artifactType + PATH_DELIMITER;

                if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType) && path
                    .contains(ARTIFACTS_PATH + RESOURCES_PATH)) {
                    // Ignore this packaging as BPMN artifacts needs to be packaged in different manner
                    continue;
                }
                if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType)) {
                    artifactTypeFolder += OperationArtifactUtil.BPMN_ARTIFACT_PATH + File.separator;
                }

                // TODO: We should not do this but in order to keep this refactoring small enough,
                // we'll leave this as is for now
                List<ArtifactDefinition> collect =
                    filterArtifactDefinitionToZip(mainComponent, artifactDefinitionList, isInCertificationRequest)
                        .collect(Collectors.toList());
                for (ArtifactDefinition ad : collect) {
                    zip.putNextEntry(new ZipEntry(artifactTypeFolder + ad.getArtifactName()));
                    zip.write(ad.getPayloadData());
                }
            }
        }
        return Either.left(zip);
    }

    private Stream<ArtifactDefinition> filterArtifactDefinitionToZip(Component mainComponent,
        List<ArtifactDefinition> artifactDefinitionList, boolean isInCertificationRequest) {
        return artifactDefinitionList
            .stream()
            .filter(shouldBeInZip(isInCertificationRequest, mainComponent))
            .map(this::fetchPayLoadData)
            .filter(Either::isLeft)
            .map(e -> e.left().value());
    }

    private Predicate<ArtifactDefinition> shouldBeInZip(boolean isInCertificationRequest, Component component) {
        return artifactDefinition ->
            !(!isInCertificationRequest
                && component.isService()
                && artifactDefinition.isHeatEnvType()
                || artifactDefinition.hasNoMandatoryEsId());
    }

    private Either<ArtifactDefinition, ActionStatus> fetchPayLoadData(ArtifactDefinition ad) {
        byte[] payloadData = ad.getPayloadData();
        if(payloadData == null) {
            return getFromCassandra(ad.getEsId())
                .left().map(pd -> {
                    ad.setPayload(pd);
                    return ad;
                }).right().map(as -> {
                    log.debug(ARTIFACT_NAME_UNIQUE_ID, ad.getArtifactName(), ad.getUniqueId());
                    log.debug("Failed to get {} payload from DB reason: {}", ad.getArtifactName(), as);
                    return as;
                });
        } else {
            return Either.left(ad);
        }
    }

    /************************************ Artifacts Structure ******************************************************************/
    /**
     * The artifacts Definition saved by their structure
     */
    private class ArtifactsInfo {
        //Key is the type of artifacts(Informational/Deployment)
        //Value is a map between an artifact type and a list of all artifacts of this type
        private Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> artifactsInfoField;

        public ArtifactsInfo() {
            this.artifactsInfoField = new EnumMap<>(ArtifactGroupTypeEnum.class);
        }

        public Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> getArtifactsInfo() {
            return artifactsInfoField;
        }

        public void addArtifactsToGroup(ArtifactGroupTypeEnum artifactGroup,
                                        Map<String, List<ArtifactDefinition>> artifactsDefinition) {
			if (artifactsInfoField.get(artifactGroup) == null) {
				artifactsInfoField.put(artifactGroup, artifactsDefinition);
			} else {
				Map<String, List<ArtifactDefinition>> artifactTypeEnumListMap =
						artifactsInfoField.get(artifactGroup);
				artifactTypeEnumListMap.putAll(artifactsDefinition);
				artifactsInfoField.put(artifactGroup, artifactTypeEnumListMap);
			}

        }

        public boolean isEmpty() {
            return artifactsInfoField.isEmpty();
        }

        public boolean isNotEmpty() {
            return !isEmpty();
        }

    }

    /**
     * The artifacts of the component and of all its composed instances
     *
     */
    private class ComponentTypeArtifacts {
        private ArtifactsInfo componentArtifacts;    //component artifacts (describes the Informational Deployment folders)
        private Map<String, ArtifactsInfo> componentInstancesArtifacts;        //artifacts of the composed instances mapped by the resourceInstance normalized name (describes the Resources folder)

        public ComponentTypeArtifacts() {
            componentArtifacts = new ArtifactsInfo();
            componentInstancesArtifacts = new HashMap<>();
        }

        public ArtifactsInfo getComponentArtifacts() {
            return componentArtifacts;
        }
        public void setComponentArtifacts(ArtifactsInfo artifactsInfo) {
            this.componentArtifacts = artifactsInfo;
        }
        public Map<String, ArtifactsInfo> getComponentInstancesArtifacts() {
            return componentInstancesArtifacts;
        }
        public void setComponentInstancesArtifacts(Map<String, ArtifactsInfo> componentInstancesArtifacts) {
            this.componentInstancesArtifacts = componentInstancesArtifacts;
        }

        public void addComponentInstancesArtifacts(String normalizedName, ArtifactsInfo artifactsInfo) {
            componentInstancesArtifacts.put(normalizedName, artifactsInfo);
        }

    }

    private class ComponentArtifacts {
        //artifacts of the component and CI's artifacts contained in it's composition (represents Informational, Deployment & Resource folders of main component)
        private ComponentTypeArtifacts mainTypeAndCIArtifacts;
        //artifacts of all component types mapped by their tosca name
        private Map<String, ComponentTypeArtifacts> componentTypeArtifacts;

        public ComponentArtifacts(){
            mainTypeAndCIArtifacts = new ComponentTypeArtifacts();
            componentTypeArtifacts = new HashMap<>();
        }

        public ComponentTypeArtifacts getMainTypeAndCIArtifacts() {
            return mainTypeAndCIArtifacts;
        }

        public void setMainTypeAndCIArtifacts(ComponentTypeArtifacts componentInstanceArtifacts) {
            this.mainTypeAndCIArtifacts = componentInstanceArtifacts;
        }

        public Map<String, ComponentTypeArtifacts> getComponentTypeArtifacts() {
            return componentTypeArtifacts;
        }

        public void setComponentTypeArtifacts(Map<String, ComponentTypeArtifacts> componentTypeArtifacts) {
            this.componentTypeArtifacts = componentTypeArtifacts;
        }
    }

    private class CsarDefinition {
        private ComponentArtifacts componentArtifacts;

        // add list of tosca artifacts and meta describes CSAR zip root

        public CsarDefinition(ComponentArtifacts componentArtifacts) {
            this.componentArtifacts = componentArtifacts;
        }

        public ComponentArtifacts getComponentArtifacts() {
            return componentArtifacts;
        }
    }

    /************************************ Artifacts Structure END******************************************************************/

    private Either<CsarDefinition,ResponseFormat> collectComponentCsarDefinition(Component component){
        ComponentArtifacts componentArtifacts = new ComponentArtifacts();
        Component updatedComponent = component;

        //get service to receive the AII artifacts uploaded to the service
        if (updatedComponent.getComponentType() == ComponentTypeEnum.SERVICE) {
            Either<Service, StorageOperationStatus> getServiceResponse = toscaOperationFacade.getToscaElement(updatedComponent.getUniqueId());

            if(getServiceResponse.isRight()){
                ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getServiceResponse.right().value());
                return Either.right(componentsUtils.getResponseFormat(actionStatus));
            }

            updatedComponent = getServiceResponse.left().value();
        }

        //find the artifacts of the main component, it would have its composed instances artifacts in a separate folder
        ComponentTypeArtifacts componentInstanceArtifacts = new ComponentTypeArtifacts();
        ArtifactsInfo artifactsInfo = collectComponentArtifacts(updatedComponent);
        componentInstanceArtifacts.setComponentArtifacts(artifactsInfo);
        componentArtifacts.setMainTypeAndCIArtifacts(componentInstanceArtifacts);

        Map<String,ComponentTypeArtifacts> resourceTypeArtifacts = componentArtifacts.getComponentTypeArtifacts();    //artifacts mapped by the component type(tosca name+version)
        //get the component instances
        List<ComponentInstance> componentInstances = updatedComponent.getComponentInstances();
        if (componentInstances!=null){
            for (ComponentInstance componentInstance:componentInstances){
                //call recursive to find artifacts for all the path
                Either<Boolean, ResponseFormat> collectComponentInstanceArtifacts = collectComponentInstanceArtifacts(
                        updatedComponent, componentInstance, resourceTypeArtifacts, componentInstanceArtifacts);
                if (collectComponentInstanceArtifacts.isRight()){
                    return Either.right(collectComponentInstanceArtifacts.right().value());
                }
            }
        }

        if(log.isDebugEnabled()){
            printResult(componentArtifacts,updatedComponent.getName());
        }

        return Either.left(new CsarDefinition(componentArtifacts));
    }

    private void printResult(ComponentArtifacts componentArtifacts, String name) {
        StringBuilder result = new StringBuilder();
        result.append("Artifacts of main component " + name + "\n");
        ComponentTypeArtifacts componentInstanceArtifacts = componentArtifacts.getMainTypeAndCIArtifacts();
        printArtifacts(componentInstanceArtifacts);
        result.append("Type Artifacts\n");
        for (Map.Entry<String, ComponentTypeArtifacts> typeArtifacts:componentArtifacts.getComponentTypeArtifacts().entrySet()){
            result.append("Folder " + typeArtifacts.getKey() + "\n");
            result.append(printArtifacts(typeArtifacts.getValue()));
        }

        if(log.isDebugEnabled()){
            log.debug(result.toString());
        }
    }

    private String printArtifacts(ComponentTypeArtifacts componentInstanceArtifacts) {
        StringBuilder result = new StringBuilder();
        ArtifactsInfo artifactsInfo = componentInstanceArtifacts.getComponentArtifacts();
        Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> componentArtifacts = artifactsInfo.getArtifactsInfo();
        printArtifacts(componentArtifacts);
        result = result.append("Resources\n");
        for (Map.Entry<String, ArtifactsInfo> resourceInstance:componentInstanceArtifacts.getComponentInstancesArtifacts().entrySet()){
            result.append("Folder" + resourceInstance.getKey() + "\n");
            result.append(printArtifacts(resourceInstance.getValue().getArtifactsInfo()));
        }

        return result.toString();
    }

    private String printArtifacts(Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> componetArtifacts) {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> artifactGroup:componetArtifacts.entrySet()){
            result.append("    " + artifactGroup.getKey().getType());
            for (Map.Entry<String, List<ArtifactDefinition>> groupArtifacts:artifactGroup.getValue().entrySet()){
                result.append("        " + groupArtifacts.getKey());
                for (ArtifactDefinition artifact:groupArtifacts.getValue()){
                    result.append("            " + artifact.getArtifactDisplayName());
                }
            }
        }

        return result.toString();
    }

    private ComponentTypeArtifacts collectComponentTypeArtifacts(
        Component fetchedComponent
    ) {
        ArtifactsInfo componentArtifacts = collectComponentArtifacts(fetchedComponent);
        ComponentTypeArtifacts componentArtifactsInfo = new ComponentTypeArtifacts();
        if (componentArtifacts.isNotEmpty()) {
            componentArtifactsInfo.setComponentArtifacts(componentArtifacts);
        }
        return componentArtifactsInfo;
    }

    private Either<Boolean, ResponseFormat> collectComponentInstanceArtifacts(Component parentComponent,ComponentInstance componentInstance,
            Map<String, ComponentTypeArtifacts> resourcesTypeArtifacts,ComponentTypeArtifacts instanceArtifactsLocation) {
        //1. get the component instance component
        String componentUid;
        if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
			componentUid = componentInstance.getSourceModelUid();
		}
		else {
			componentUid = componentInstance.getComponentUid();
		}
        Either<Component, StorageOperationStatus> component = toscaOperationFacade.getToscaElement(componentUid);
		if (component.isRight()) {
            log.error("Failed to fetch resource with id {} for instance {}",componentUid, parentComponent.getUUID());
            return Either.right(componentsUtils.getResponseFormat(ActionStatus.ASSET_NOT_FOUND_DURING_CSAR_CREATION,
                    parentComponent.getComponentType().getValue(), parentComponent.getUUID(),
                    componentInstance.getOriginType().getComponentType().getValue(), componentUid));
        }
		Component fetchedComponent = component.left().value();

        //2. fill the artifacts for the current component parent type
        String toscaComponentName =
            componentInstance.getToscaComponentName() + "_v" + componentInstance.getComponentVersion();

        // if there are no artifacts for this component type we need to fetch and build them
        ComponentTypeArtifacts componentParentArtifacts = Optional
            .ofNullable(resourcesTypeArtifacts.get(toscaComponentName))
            .orElseGet(() -> collectComponentTypeArtifacts(fetchedComponent));

        if (componentParentArtifacts.getComponentArtifacts().isNotEmpty()) {
            resourcesTypeArtifacts.put(toscaComponentName, componentParentArtifacts);
        }

        //3. find the artifacts specific to the instance
        Map<String, List<ArtifactDefinition>> componentInstanceSpecificInformationalArtifacts =
                getComponentInstanceSpecificArtifacts(componentInstance.getArtifacts(),
                        componentParentArtifacts.getComponentArtifacts().getArtifactsInfo(), ArtifactGroupTypeEnum.INFORMATIONAL);
        Map<String, List<ArtifactDefinition>> componentInstanceSpecificDeploymentArtifacts =
                getComponentInstanceSpecificArtifacts(componentInstance.getDeploymentArtifacts(),
                        componentParentArtifacts.getComponentArtifacts().getArtifactsInfo(), ArtifactGroupTypeEnum.DEPLOYMENT);

        //4. add the instances artifacts to the component type
        ArtifactsInfo artifactsInfo = new ArtifactsInfo();
        if (!componentInstanceSpecificInformationalArtifacts.isEmpty()){
            artifactsInfo.addArtifactsToGroup(ArtifactGroupTypeEnum.INFORMATIONAL, componentInstanceSpecificInformationalArtifacts);
        }
        if (!componentInstanceSpecificDeploymentArtifacts.isEmpty()){
            artifactsInfo.addArtifactsToGroup(ArtifactGroupTypeEnum.DEPLOYMENT, componentInstanceSpecificDeploymentArtifacts);
        }
        if (!artifactsInfo.isEmpty()){
            instanceArtifactsLocation.addComponentInstancesArtifacts(componentInstance.getNormalizedName(), artifactsInfo);
        }

        //5. do the same for all the component instances
        List<ComponentInstance> componentInstances = fetchedComponent.getComponentInstances();
        if (componentInstances!=null){
            for (ComponentInstance childComponentInstance:componentInstances){
                Either<Boolean, ResponseFormat> collectComponentInstanceArtifacts = collectComponentInstanceArtifacts(
                        fetchedComponent, childComponentInstance, resourcesTypeArtifacts, componentParentArtifacts);
                if (collectComponentInstanceArtifacts.isRight()){
                    return collectComponentInstanceArtifacts;
                }
            }
        }

        return Either.left(true);
    }

    public String getVersionFirstThreeOctets() {
        return versionFirstThreeOctets;
    }

    public void setVersionFirstThreeOctets(String versionFirstThreeOctetes) {
        this.versionFirstThreeOctets = versionFirstThreeOctetes;
    }
    private Map<String, List<ArtifactDefinition>> getComponentInstanceSpecificArtifacts(Map<String, ArtifactDefinition> componentArtifacts,
            Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> componentTypeArtifacts, ArtifactGroupTypeEnum artifactGroupTypeEnum) {
        Map<String, List<ArtifactDefinition>> parentArtifacts = componentTypeArtifacts.get(artifactGroupTypeEnum);    //the artfiacts of the component itself and not the instance

        Map<String, List<ArtifactDefinition>> artifactsByTypeOfComponentInstance = new HashMap<>();
        if (componentArtifacts!=null){
            for (ArtifactDefinition artifact:componentArtifacts.values()){
                List<ArtifactDefinition> parentArtifactsByType = null;
                if (parentArtifacts!=null){
                    parentArtifactsByType = parentArtifacts.get(artifact.getArtifactType());
                }
                //the artifact is of instance
                if (parentArtifactsByType == null || !parentArtifactsByType.contains(artifact)){
                    List<ArtifactDefinition> typeArtifacts = artifactsByTypeOfComponentInstance.get(artifact.getArtifactType());
                    if (typeArtifacts == null){
                        typeArtifacts = new ArrayList<>();
                        artifactsByTypeOfComponentInstance.put(artifact.getArtifactType(), typeArtifacts);
                    }
                    typeArtifacts.add(artifact);
                }
            }
        }

        return artifactsByTypeOfComponentInstance;
    }

    private ArtifactsInfo collectComponentArtifacts(Component component) {
        Map<String, ArtifactDefinition> informationalArtifacts = component.getArtifacts();
        Map<String, List<ArtifactDefinition>> informationalArtifactsByType = collectGroupArtifacts(informationalArtifacts);
        Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
        Map<String, List<ArtifactDefinition>> deploymentArtifactsByType = collectGroupArtifacts(deploymentArtifacts);
		Map<String, ArtifactDefinition> interfaceOperationArtifacts =
				OperationArtifactUtil.getDistinctInterfaceOperationArtifactsByName(component);
		Map<String, List<ArtifactDefinition>> interfaceOperationArtifactsByType = collectGroupArtifacts(
				interfaceOperationArtifacts);
        ArtifactsInfo artifactsInfo = new ArtifactsInfo();
        if (!informationalArtifactsByType.isEmpty()){
            artifactsInfo.addArtifactsToGroup(ArtifactGroupTypeEnum.INFORMATIONAL, informationalArtifactsByType);
        }
        if (!deploymentArtifactsByType.isEmpty() ){
            artifactsInfo.addArtifactsToGroup(ArtifactGroupTypeEnum.DEPLOYMENT, deploymentArtifactsByType);
		}
		//Add component interface operation artifacts
		if(MapUtils.isNotEmpty(interfaceOperationArtifacts)) {
			artifactsInfo.addArtifactsToGroup(ArtifactGroupTypeEnum.DEPLOYMENT, interfaceOperationArtifactsByType);
        }

        return artifactsInfo;
    }

    private Map<String, List<ArtifactDefinition>> collectGroupArtifacts(
            final Map<String, ArtifactDefinition> componentArtifacts) {
        final Map<String, List<ArtifactDefinition>> artifactsByType = new HashMap<>();
        for (final ArtifactDefinition artifact : componentArtifacts.values()) {
            if (artifact.getArtifactUUID() != null) {
                artifactsByType.putIfAbsent(artifact.getArtifactType(), new ArrayList<>());
                final List<ArtifactDefinition> typeArtifacts = artifactsByType.get(artifact.getArtifactType());
                typeArtifacts.add(artifact);
            }
        }
        return artifactsByType;
    }
}