aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/export/TOSCAExportUtil.java
blob: 765b07a92560b2af65f4b814b55b2ff468f24518 (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
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Kálmán Képes - initial API and implementation and/or initial documentation
 *     Oliver Kopp - adapted to new storage model and to TOSCA v1.0
 *******************************************************************************/
package org.eclipse.winery.repository.export;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.winery.common.ModelUtilities;
import org.eclipse.winery.common.RepositoryFileReference;
import org.eclipse.winery.common.Util;
import org.eclipse.winery.common.constants.QNames;
import org.eclipse.winery.common.ids.definitions.ArtifactTemplateId;
import org.eclipse.winery.common.ids.definitions.ArtifactTypeId;
import org.eclipse.winery.common.ids.definitions.CapabilityTypeId;
import org.eclipse.winery.common.ids.definitions.EntityTypeId;
import org.eclipse.winery.common.ids.definitions.NodeTypeId;
import org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId;
import org.eclipse.winery.common.ids.definitions.PolicyTemplateId;
import org.eclipse.winery.common.ids.definitions.PolicyTypeId;
import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
import org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId;
import org.eclipse.winery.common.ids.definitions.RequirementTypeId;
import org.eclipse.winery.common.ids.definitions.ServiceTemplateId;
import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
import org.eclipse.winery.common.ids.definitions.TopologyGraphElementEntityTypeId;
import org.eclipse.winery.common.ids.definitions.imports.GenericImportId;
import org.eclipse.winery.common.ids.elements.PlanId;
import org.eclipse.winery.common.ids.elements.PlansId;
import org.eclipse.winery.common.propertydefinitionkv.WinerysPropertiesDefinition;
import org.eclipse.winery.model.tosca.Definitions;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Policies;
import org.eclipse.winery.model.tosca.TCapability;
import org.eclipse.winery.model.tosca.TCapabilityDefinition;
import org.eclipse.winery.model.tosca.TDeploymentArtifact;
import org.eclipse.winery.model.tosca.TDeploymentArtifacts;
import org.eclipse.winery.model.tosca.TEntityTemplate;
import org.eclipse.winery.model.tosca.TEntityType;
import org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition;
import org.eclipse.winery.model.tosca.TImplementationArtifact;
import org.eclipse.winery.model.tosca.TImplementationArtifacts;
import org.eclipse.winery.model.tosca.TImport;
import org.eclipse.winery.model.tosca.TNodeTemplate;
import org.eclipse.winery.model.tosca.TNodeTemplate.Capabilities;
import org.eclipse.winery.model.tosca.TNodeTemplate.Requirements;
import org.eclipse.winery.model.tosca.TNodeType;
import org.eclipse.winery.model.tosca.TNodeType.CapabilityDefinitions;
import org.eclipse.winery.model.tosca.TNodeType.RequirementDefinitions;
import org.eclipse.winery.model.tosca.TPolicy;
import org.eclipse.winery.model.tosca.TRelationshipTemplate;
import org.eclipse.winery.model.tosca.TRelationshipType;
import org.eclipse.winery.model.tosca.TRelationshipType.ValidSource;
import org.eclipse.winery.model.tosca.TRelationshipType.ValidTarget;
import org.eclipse.winery.model.tosca.TRequirement;
import org.eclipse.winery.model.tosca.TRequirementDefinition;
import org.eclipse.winery.repository.JAXBSupport;
import org.eclipse.winery.repository.Utils;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.backend.Repository;
import org.eclipse.winery.repository.backend.constants.Filename;
import org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateDirectoryId;
import org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId;
import org.eclipse.winery.repository.resources.AbstractComponentInstanceResource;
import org.eclipse.winery.repository.resources.AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal;
import org.eclipse.winery.repository.resources.AbstractComponentsResource;
import org.eclipse.winery.repository.resources.EntityTypeResource;
import org.eclipse.winery.repository.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource;
import org.eclipse.winery.repository.resources.entitytemplates.policytemplates.PolicyTemplateResource;
import org.eclipse.winery.repository.resources.entitytypeimplementations.nodetypeimplementations.NodeTypeImplementationResource;
import org.eclipse.winery.repository.resources.entitytypeimplementations.relationshiptypeimplementations.RelationshipTypeImplementationResource;
import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
import org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.RelationshipTypeResource;
import org.eclipse.winery.repository.resources.entitytypes.requirementtypes.RequirementTypeResource;
import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
import org.w3c.dom.Document;

public class TOSCAExportUtil {
	
	private static final XLogger logger = XLoggerFactory.getXLogger(TOSCAExportUtil.class);
	
	/*
	 * these two are GLOBAL VARIABLES leading to the fact that this class has to
	 * be constructed for each export
	 */
	
	// collects the references to be put in the CSAR and the assigned path in
	// the CSAR MANIFEST
	// this allows to use other paths in the CSAR than on the local storage
	private Map<RepositoryFileReference, String> referencesToPathInCSARMap = null;
	
	/**
	 * Currently a very simple approach to configure the export
	 */
	private Map<String, Object> exportConfiguration;
	
	
	public enum ExportProperties {
		INCLUDEXYCOORDINATES, REPOSITORY_URI
	};
	
	
	/**
	 * Writes the <em>complete</em> tosca xml into the given outputstream
	 * 
	 * @param id the id of the TOSCA component instance to export
	 * @param out outputstream to write to
	 * @param addRelatedComponents true: all referenced components
	 *            (artifactTemplates, artifactTypes, ...) are added, false: only
	 *            the XML belonging to the id is exported. If XML types are
	 *            generated by Winery (e.g., the properties XSD for node types),
	 *            these XML types are also exported
	 * @param exportConfiguration the configuration map for the export. Uses
	 * @param exportedState exportedState object to modify. ExportProperties
	 *            provides the possible keys
	 * @return a collection of TOSCAcomponentIds referenced by the given
	 *         component
	 * @throws JAXBException
	 */
	public Collection<TOSCAComponentId> exportTOSCA(TOSCAComponentId id, OutputStream out, Map<String, Object> exportConfiguration) throws IOException, JAXBException {
		this.exportConfiguration = exportConfiguration;
		this.initializeExport();
		return this.writeDefinitionsElement(id, out);
	}
	
	private void initializeExport() {
		this.setDefaultExportConfiguration();
		// quick hack to avoid NPE
		if (this.referencesToPathInCSARMap == null) {
			this.referencesToPathInCSARMap = new HashMap<>();
		}
	}
	
	/**
	 * Quick hack to set defaults. Typically, a configuration builder or similar
	 * is used
	 */
	private void setDefaultExportConfiguration() {
		this.checkConfig(ExportProperties.INCLUDEXYCOORDINATES, Boolean.FALSE);
	}
	
	private void checkConfig(ExportProperties propKey, Boolean bo) {
		if (!this.exportConfiguration.containsKey(propKey.toString())) {
			this.exportConfiguration.put(propKey.toString(), bo);
		}
	}
	
	/**
	 * Writes the <em>complete</em> TOSCA XML into the given outputstream.
	 * Additionally, a the artifactMap is filled to enable the CSAR exporter to
	 * create necessary entries in TOSCA-Meta and to add them to the CSAR itself
	 * 
	 * @param id the component instance to export
	 * @param out outputstream to write to
	 * @param exportConfiguration Configures the exporter
	 * @param referencesToPathInCSARMap collects the references to export. It is
	 *            updated during the export
	 * @return a collection of TOSCAcomponentIds referenced by the given
	 *         component
	 * @throws JAXBException
	 */
	protected Collection<TOSCAComponentId> exportTOSCA(TOSCAComponentId id, OutputStream out, Map<RepositoryFileReference, String> referencesToPathInCSARMap, Map<String, Object> exportConfiguration) throws IOException, JAXBException {
		this.referencesToPathInCSARMap = referencesToPathInCSARMap;
		return this.exportTOSCA(id, out, exportConfiguration);
	}
	
	/**
	 * Called when the entry resource is definitions backed
	 * 
	 * @throws JAXBException
	 */
	private void writeDefinitionsElement(Definitions entryDefinitions, OutputStream out) throws JAXBException {
		Marshaller m = JAXBSupport.createMarshaller(true);
		m.marshal(entryDefinitions, out);
	}
	
	/**
	 * Writes the Definitions belonging to the given TOSCA component to the
	 * outputstream
	 * 
	 * @return a collection of TOSCAcomponentIds referenced by the given
	 *         component
	 * 
	 * @throws IOException
	 * @throws JAXBException
	 * @throws IllegalStateException if tcId does not exist
	 */
	private Collection<TOSCAComponentId> writeDefinitionsElement(TOSCAComponentId tcId, OutputStream out) throws IOException, JAXBException {
		if (!Repository.INSTANCE.exists(tcId)) {
			String error = "Component instance " + tcId.toString() + " does not exist.";
			TOSCAExportUtil.logger.error(error);
			throw new IllegalStateException(error);
		}
		
		AbstractComponentInstanceResource res = AbstractComponentsResource.getComponentInstaceResource(tcId);
		Definitions entryDefinitions = res.getDefinitions();
		
		// BEGIN: Definitions modification
		// the "imports" collection contains the imports of Definitions, not of other definitions
		// the other definitions are stored in entryDefinitions.getImport()
		// we modify the internal definitions object directly. It is not written back to the storage. Therefore, we do not need to clone it
		
		// the imports (pointing to not-definitions (xsd, wsdl, ...)) already have a correct relative URL. (quick hack)
		URI uri = (URI) this.exportConfiguration.get(TOSCAExportUtil.ExportProperties.REPOSITORY_URI.toString());
		if (uri != null) {
			// we are in the plain-XML mode, the URLs of the imports have to be adjusted
			for (TImport i : entryDefinitions.getImport()) {
				String loc = i.getLocation();
				assert (loc.startsWith("../"));
				loc = loc.substring(3);
				loc = uri + loc;
				// now the location is an absolute URL
				i.setLocation(loc);
			}
		}
		
		// files of imports have to be added to the CSAR, too
		for (TImport i : entryDefinitions.getImport()) {
			String loc = i.getLocation();
			if (Util.isRelativeURI(loc)) {
				// locally stored, add to CSAR
				GenericImportId iid = new GenericImportId(i);
				String fileName = Util.getLastURIPart(loc);
				fileName = Util.URLdecode(fileName);
				RepositoryFileReference ref = new RepositoryFileReference(iid, fileName);
				this.putRefAsReferencedItemInCSAR(ref);
			}
		}
		
		// adjust imports: add imports of definitions to it
		Collection<TOSCAComponentId> referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds(tcId);
		Collection<TImport> imports = new ArrayList<>();
		for (TOSCAComponentId id : referencedTOSCAComponentIds) {
			this.addToImports(id, imports);
		}
		entryDefinitions.getImport().addAll(imports);
		
		if (res.getElement() instanceof TEntityType) {
			// we have an entity type with a possible properties definition
			EntityTypeResource entityTypeRes = (EntityTypeResource) res;
			WinerysPropertiesDefinition wpd = ModelUtilities.getWinerysPropertiesDefinition(entityTypeRes.getEntityType());
			if (wpd != null) {
				if (wpd.getIsDerivedFromXSD() == null) {
					// Write WPD only to file if it exists and is NOT derived from an XSD (which may happen during import)
					
					String wrapperElementNamespace = wpd.getNamespace();
					String wrapperElementLocalName = wpd.getElementName();
					
					// BEGIN: add import and put into CSAR
					
					TImport imp = new TImport();
					entryDefinitions.getImport().add(imp);
					
					// fill known import values
					imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
					imp.setNamespace(wrapperElementNamespace);
					// add "winerysPropertiesDefinition" flag to import tag to support
					Map<QName, String> otherAttributes = imp.getOtherAttributes();
					otherAttributes.put(QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE, "true");
					
					// Determine location
					String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD((EntityTypeId) tcId, uri, wrapperElementLocalName);
					if (uri == null) {
						TOSCAExportUtil.logger.trace("CSAR Export mode. Putting XSD into CSAR");
						// CSAR Export mode
						// XSD has to be put into the CSAR
						Document document = ModelUtilities.getWinerysPropertiesDefinitionXSDAsDocument(wpd);
						
						// loc in import is URLencoded, loc on filesystem isn't
						String locInCSAR = Util.URLdecode(loc);
						// furthermore, the path has to start from the root of the CSAR; currently, it starts from Definitions/
						locInCSAR = locInCSAR.substring(3);
						TOSCAExportUtil.logger.trace("Location in CSAR: {}", locInCSAR);
						this.referencesToPathInCSARMap.put(new DummyRepositoryFileReferenceForGeneratedXSD(document), locInCSAR);
					}
					imp.setLocation(loc);
					
					// END: add import and put into CSAR
					
					// BEGIN: generate TOSCA conforming PropertiesDefinition
					
					TEntityType entityType = entityTypeRes.getEntityType();
					PropertiesDefinition propertiesDefinition = new PropertiesDefinition();
					propertiesDefinition.setType(new QName(wrapperElementNamespace, wrapperElementLocalName));
					entityType.setPropertiesDefinition(propertiesDefinition);
					
					// END: generate TOSCA conforming PropertiesDefinition
				} else {
					// otherwise WPD exists, but is derived from XSD
					// we DO NOT have to remove the winery properties definition from the output to allow "debugging" of the CSAR
				}
			}
		}
		
		// END: Definitions modification
		
		this.writeDefinitionsElement(entryDefinitions, out);
		
		return referencedTOSCAComponentIds;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(EntityTypeId id) {
		return this.getReferencedTOSCAComponentIdOfParentForAnAbstractComponentsWithTypeReferenceResource(id);
	}
	
	/**
	 * There is now equivalent id class for
	 * AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal,
	 * therefore we take the super type and hope that the caller knows what he
	 * does.
	 */
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIdOfParentForAnAbstractComponentsWithTypeReferenceResource(TOSCAComponentId id) {
		AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal res = (AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal) AbstractComponentsResource.getComponentInstaceResource(id);
		String derivedFrom = res.getInheritanceManagement().getDerivedFrom();
		if (StringUtils.isEmpty(derivedFrom)) {
			return Collections.emptySet();
		} else {
			// Instantiate an id with the same class as the current id
			TOSCAComponentId parentId;
			QName qname = QName.valueOf(derivedFrom);
			
			Constructor<? extends TOSCAComponentId> constructor;
			try {
				constructor = id.getClass().getConstructor(QName.class);
			} catch (NoSuchMethodException | SecurityException e1) {
				throw new IllegalStateException("Could get constructor to instantiate parent id", e1);
			}
			try {
				parentId = constructor.newInstance(qname);
			} catch (InstantiationException | IllegalAccessException
					| IllegalArgumentException | InvocationTargetException e) {
				throw new IllegalStateException("Could not instantiate id for parent", e);
			}
			
			Collection<TOSCAComponentId> result = new ArrayList<>(1);
			result.add(parentId);
			return result;
		}
	}
	
	/**
	 * This method is intended to be used by exportTOSCA. However,
	 * org.eclipse.winery.repository.client requires an XML representation of a
	 * component instances without a surrounding definitions element.
	 * 
	 * We name this method differently to prevent wrong calling due to
	 * inheritance
	 * 
	 * @param definitionsElement the parent XML element
	 */
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(TOSCAComponentId id) {
		Collection<TOSCAComponentId> referencedTOSCAComponentIds;
		
		// first of all, handle the concrete elements
		if (id instanceof ServiceTemplateId) {
			referencedTOSCAComponentIds = this.prepareForExport((ServiceTemplateId) id);
		} else if (id instanceof NodeTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((NodeTypeId) id);
		} else if (id instanceof NodeTypeImplementationId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((NodeTypeImplementationId) id);
		} else if (id instanceof RelationshipTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((RelationshipTypeId) id);
		} else if (id instanceof RelationshipTypeImplementationId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((RelationshipTypeImplementationId) id);
		} else if (id instanceof RequirementTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((RequirementTypeId) id);
		} else if (id instanceof CapabilityTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((CapabilityTypeId) id);
		} else if (id instanceof ArtifactTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((ArtifactTypeId) id);
		} else if (id instanceof ArtifactTemplateId) {
			referencedTOSCAComponentIds = this.prepareForExport((ArtifactTemplateId) id);
		} else if (id instanceof PolicyTypeId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((PolicyTypeId) id);
		} else if (id instanceof PolicyTemplateId) {
			referencedTOSCAComponentIds = this.getReferencedTOSCAComponentIds((PolicyTemplateId) id);
		} else if (id instanceof GenericImportId) {
			// in case of imports, there are no other ids referenced
			referencedTOSCAComponentIds = Collections.emptyList();
		} else {
			throw new IllegalStateException("Unhandled id class " + id.getClass());
		}
		
		// Then, handle the super classes, which support inheritance
		// Currently, it is EntityType and EntityTypeImplementation only
		// Since the latter does not exist in the TOSCA MetaModel, we just handle EntityType here
		if (id instanceof EntityTypeId) {
			Collection<TOSCAComponentId> additionalRefs = this.getReferencedTOSCAComponentIds((EntityTypeId) id);
			// the original referenceTOSCAComponentIds could be unmodifiable
			// we just create a new one...
			referencedTOSCAComponentIds = new ArrayList<>(referencedTOSCAComponentIds);
			// ...and add the new reference
			referencedTOSCAComponentIds.addAll(additionalRefs);
		}
		
		return referencedTOSCAComponentIds;
	}
	
	/**
	 * Adds the given id as import to the given imports collection
	 */
	private void addToImports(TOSCAComponentId id, Collection<TImport> imports) {
		TImport imp = new TImport();
		imp.setImportType(org.eclipse.winery.common.constants.Namespaces.TOSCA_NAMESPACE);
		imp.setNamespace(id.getNamespace().getDecoded());
		URI uri = (URI) this.exportConfiguration.get(TOSCAExportUtil.ExportProperties.REPOSITORY_URI.toString());
		if (uri == null) {
			// self-contained mode
			// all Definitions are contained in "Definitions" directory, therefore, we provide the filename only
			// references are resolved relatively from a definitions element (COS01, line 425)
			String fn = CSARExporter.getDefinitionsFileName(id);
			fn = Util.URLencode(fn);
			imp.setLocation(fn);
		} else {
			String path = Utils.getURLforPathInsideRepo(BackendUtils.getPathInsideRepo(id));
			path = path + "?definitions";
			URI absoluteURI = uri.resolve(path);
			imp.setLocation(absoluteURI.toString());
		}
		imports.add(imp);
		
		// FIXME: Currently the depended elements (such as the artifact templates linked to a node type implementation) are gathered by the corresponding "addXY" method.
		// Reason: the corresponding TDefinitions element is *not* updated if a related element is added/removed.
		// That means: The imports are not changed.
		// The current issue is that TOSCA allows imports of Definitions only and the repository has the concrete elements as main structure
		// Although during save the import can be updated (by fetching the associated resource and get the definitions of it),
		// The concrete definitions cannot be determined without
		//  a) having a complete index of all definitions in the repository
		//  b) crawling through the *complete* repository
		// Possibly the current solution, just lazily adding all dependent elements is the better solution.
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(NodeTypeImplementationId id) {
		// We have to use a HashSet to ensure that no duplicate ids are added
		// There may be multiple DAs/IAs referencing the same type
		Collection<TOSCAComponentId> ids = new HashSet<>();
		
		NodeTypeImplementationResource res = new NodeTypeImplementationResource(id);
		
		// DAs
		TDeploymentArtifacts deploymentArtifacts = res.getNTI().getDeploymentArtifacts();
		if (deploymentArtifacts != null) {
			for (TDeploymentArtifact da : deploymentArtifacts.getDeploymentArtifact()) {
				QName qname;
				if ((qname = da.getArtifactRef()) != null) {
					ids.add(new ArtifactTemplateId(qname));
				}
				ids.add(new ArtifactTypeId(da.getArtifactType()));
			}
		}
		
		// IAs
		TImplementationArtifacts implementationArtifacts = res.getNTI().getImplementationArtifacts();
		if (implementationArtifacts != null) {
			for (TImplementationArtifact ia : implementationArtifacts.getImplementationArtifact()) {
				QName qname;
				if ((qname = ia.getArtifactRef()) != null) {
					ids.add(new ArtifactTemplateId(qname));
				}
				ids.add(new ArtifactTypeId(ia.getArtifactType()));
			}
		}
		
		// inheritance
		ids.addAll(this.getReferencedTOSCAComponentIdOfParentForAnAbstractComponentsWithTypeReferenceResource(id));
		
		return ids;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(RelationshipTypeImplementationId id) {
		// We have to use a HashSet to ensure that no duplicate ids are added
		// There may be multiple IAs referencing the same type
		Collection<TOSCAComponentId> ids = new HashSet<>();
		
		RelationshipTypeImplementationResource res = new RelationshipTypeImplementationResource(id);
		
		// IAs
		for (TImplementationArtifact ia : res.getRTI().getImplementationArtifacts().getImplementationArtifact()) {
			QName qname;
			if ((qname = ia.getArtifactRef()) != null) {
				ids.add(new ArtifactTemplateId(qname));
			}
			ids.add(new ArtifactTypeId(ia.getArtifactType()));
		}
		
		// inheritance
		ids.addAll(this.getReferencedTOSCAComponentIdOfParentForAnAbstractComponentsWithTypeReferenceResource(id));
		
		return ids;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(RequirementTypeId id) {
		Collection<TOSCAComponentId> ids = new ArrayList<>(1);
		
		RequirementTypeResource res = new RequirementTypeResource(id);
		QName requiredCapabilityType = res.getRequirementType().getRequiredCapabilityType();
		if (requiredCapabilityType != null) {
			CapabilityTypeId capId = new CapabilityTypeId(requiredCapabilityType);
			ids.add(capId);
		}
		return ids;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(CapabilityTypeId id) {
		return Collections.emptyList();
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(PolicyTypeId id) {
		return Collections.emptyList();
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(PolicyTemplateId id) {
		Collection<TOSCAComponentId> ids = new ArrayList<>();
		PolicyTemplateResource res = new PolicyTemplateResource(id);
		ids.add(new PolicyTypeId(res.getType()));
		return ids;
	}
	
	/**
	 * Synchronizes the plan model references and returns the referenced TOSCA
	 * Component Ids.
	 */
	private Collection<TOSCAComponentId> prepareForExport(ServiceTemplateId id) {
		// We have to use a HashSet to ensure that no duplicate ids are added
		// E.g., there may be multiple relationship templates having the same type
		Collection<TOSCAComponentId> ids = new HashSet<>();
		ServiceTemplateResource res = new ServiceTemplateResource(id);
		
		// ensure that the plans stored locally are the same ones as stored in the definitions
		res.synchronizeReferences();
		
		// add all plans as reference in the CSAR
		// the data model is consistent with the repository
		// we crawl through the repository to as putRefAsReferencedItemInCSAR expects a repository file reference
		PlansId plansContainerId = new PlansId(id);
		SortedSet<PlanId> nestedPlans = Repository.INSTANCE.getNestedIds(plansContainerId, PlanId.class);
		for (PlanId planId : nestedPlans) {
			SortedSet<RepositoryFileReference> containedFiles = Repository.INSTANCE.getContainedFiles(planId);
			// even if we currently support only one file in the directory, we just add everything
			for (RepositoryFileReference ref : containedFiles) {
				this.putRefAsReferencedItemInCSAR(ref);
			}
		}
		
		// add included things to export queue
		
		TBoundaryDefinitions boundaryDefs;
		if ((boundaryDefs = res.getServiceTemplate().getBoundaryDefinitions()) != null) {
			Policies policies = boundaryDefs.getPolicies();
			if (policies != null) {
				for (TPolicy policy : policies.getPolicy()) {
					PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
					ids.add(policyTypeId);
				}
			}
			
			// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
		}
		
		if (res.getServiceTemplate().getTopologyTemplate() != null) {
			for (TEntityTemplate entityTemplate : res.getServiceTemplate().getTopologyTemplate().getNodeTemplateOrRelationshipTemplate()) {
				QName qname = entityTemplate.getType();
				if (entityTemplate instanceof TNodeTemplate) {
					ids.add(new NodeTypeId(qname));
					TNodeTemplate n = (TNodeTemplate) entityTemplate;
					
					// crawl through deployment artifacts
					TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
					if (deploymentArtifacts != null) {
						List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
						for (TDeploymentArtifact da : das) {
							ids.add(new ArtifactTypeId(da.getArtifactType()));
							if ((qname = da.getArtifactRef()) != null) {
								ids.add(new ArtifactTemplateId(qname));
							}
						}
					}
					
					// crawl through reqs/caps
					Requirements requirements = n.getRequirements();
					if (requirements != null) {
						for (TRequirement req : requirements.getRequirement()) {
							QName type = req.getType();
							RequirementTypeId rtId = new RequirementTypeId(type);
							ids.add(rtId);
						}
					}
					Capabilities capabilities = n.getCapabilities();
					if (capabilities != null) {
						for (TCapability cap : capabilities.getCapability()) {
							QName type = cap.getType();
							CapabilityTypeId ctId = new CapabilityTypeId(type);
							ids.add(ctId);
						}
					}
					
					// crawl through policies
					org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
					if (policies != null) {
						for (TPolicy pol : policies.getPolicy()) {
							QName type = pol.getPolicyType();
							PolicyTypeId ctId = new PolicyTypeId(type);
							ids.add(ctId);
						}
					}
				} else {
					assert (entityTemplate instanceof TRelationshipTemplate);
					ids.add(new RelationshipTypeId(qname));
				}
			}
		}
		
		return ids;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(ArtifactTypeId id) {
		// no recursive crawling needed
		return Collections.emptyList();
	}
	
	/**
	 * Determines the referenced TOSCA Component Ids and also updates the
	 * references in the Artifact Template
	 * 
	 * @return a collection of referenced TOCSA Component Ids
	 */
	private Collection<TOSCAComponentId> prepareForExport(ArtifactTemplateId id) {
		Collection<TOSCAComponentId> ids = new ArrayList<>();
		
		ArtifactTemplateResource res = new ArtifactTemplateResource(id);
		
		// "Export" type
		QName type = res.getType();
		if (type == null) {
			throw new IllegalStateException("Type is null for " + id.toString());
		}
		ids.add(new ArtifactTypeId(type));
		
		// Export files
		
		// This method is called BEFORE the concrete definitions element is written.
		// Therefore, we adapt the content of the attached files to the really existing files
		res.synchronizeReferences();
		
		ArtifactTemplateDirectoryId fileDir = new ArtifactTemplateDirectoryId(id);
		SortedSet<RepositoryFileReference> files = Repository.INSTANCE.getContainedFiles(fileDir);
		for (RepositoryFileReference ref : files) {
			// Even if writing a TOSCA only (!this.writingCSAR),
			// we put the virtual path in the TOSCA
			// Reason: Winery is mostly used as a service and local storage
			// reference to not make sense
			// The old implementation had absolutePath.toUri().toString();
			// there, but this does not work when using a cloud blob store.
			
			this.putRefAsReferencedItemInCSAR(ref);
		}
		
		return ids;
	}
	
	/**
	 * Puts the given reference as item in the CSAR
	 * 
	 * Thereby, it uses the global variable referencesToPathInCSARMap
	 */
	private void putRefAsReferencedItemInCSAR(RepositoryFileReference ref) {
		// Determine path
		String path = BackendUtils.getPathInsideRepo(ref);
		
		// put mapping reference to path into global map
		// the path is the same as put in "synchronizeReferences"
		this.referencesToPathInCSARMap.put(ref, path);
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(RelationshipTypeId id) {
		Collection<TOSCAComponentId> ids = new ArrayList<>();
		
		// add all implementations
		Collection<RelationshipTypeImplementationId> allTypeImplementations = BackendUtils.getAllElementsRelatedWithATypeAttribute(RelationshipTypeImplementationId.class, id.getQName());
		for (RelationshipTypeImplementationId ntiId : allTypeImplementations) {
			ids.add(ntiId);
		}
		
		RelationshipTypeResource res = new RelationshipTypeResource(id);
		TRelationshipType relationshipType = (TRelationshipType) res.getElement();
		
		ValidSource validSource = relationshipType.getValidSource();
		if (validSource != null) {
			QName typeRef = validSource.getTypeRef();
			// can be a node type or a requirement type
			
			// similar code as for valid target (difference: req/cap)
			NodeTypeId ntId = new NodeTypeId(typeRef);
			if (Repository.INSTANCE.exists(ntId)) {
				ids.add(ntId);
			} else {
				RequirementTypeId rtId = new RequirementTypeId(typeRef);
				ids.add(rtId);
			}
		}
		
		ValidTarget validTarget = relationshipType.getValidTarget();
		if (validTarget != null) {
			QName typeRef = validTarget.getTypeRef();
			// can be a node type or a capability type
			
			// similar code as for valid target (difference: req/cap)
			NodeTypeId ntId = new NodeTypeId(typeRef);
			if (Repository.INSTANCE.exists(ntId)) {
				ids.add(ntId);
			} else {
				CapabilityTypeId capId = new CapabilityTypeId(typeRef);
				ids.add(capId);
			}
		}
		
		this.addVisualAppearanceToCSAR(id);
		
		return ids;
	}
	
	private Collection<TOSCAComponentId> getReferencedTOSCAComponentIds(NodeTypeId id) {
		Collection<TOSCAComponentId> ids = new ArrayList<>();
		Collection<NodeTypeImplementationId> allNodeTypeImplementations = BackendUtils.getAllElementsRelatedWithATypeAttribute(NodeTypeImplementationId.class, id.getQName());
		for (NodeTypeImplementationId ntiId : allNodeTypeImplementations) {
			ids.add(ntiId);
		}
		
		NodeTypeResource res = new NodeTypeResource(id);
		TNodeType nodeType = (TNodeType) res.getElement();
		
		// add all referenced requirement types
		RequirementDefinitions reqDefsContainer = nodeType.getRequirementDefinitions();
		if (reqDefsContainer != null) {
			List<TRequirementDefinition> reqDefs = reqDefsContainer.getRequirementDefinition();
			for (TRequirementDefinition reqDef : reqDefs) {
				RequirementTypeId reqTypeId = new RequirementTypeId(reqDef.getRequirementType());
				ids.add(reqTypeId);
			}
		}
		
		// add all referenced capability types
		CapabilityDefinitions capDefsContainer = nodeType.getCapabilityDefinitions();
		if (capDefsContainer != null) {
			List<TCapabilityDefinition> capDefs = capDefsContainer.getCapabilityDefinition();
			for (TCapabilityDefinition capDef : capDefs) {
				CapabilityTypeId capTypeId = new CapabilityTypeId(capDef.getCapabilityType());
				ids.add(capTypeId);
			}
		}
		
		this.addVisualAppearanceToCSAR(id);
		
		return ids;
	}
	
	private void addVisualAppearanceToCSAR(TopologyGraphElementEntityTypeId id) {
		VisualAppearanceId visId = new VisualAppearanceId(id);
		if (Repository.INSTANCE.exists(visId)) {
			// we do NOT check for the id, but simply check for bigIcon.png (only exists in NodeType) and smallIcon.png (exists in NodeType and RelationshipType)
			
			RepositoryFileReference ref = new RepositoryFileReference(visId, Filename.FILENAME_BIG_ICON);
			if (Repository.INSTANCE.exists(ref)) {
				this.referencesToPathInCSARMap.put(ref, BackendUtils.getPathInsideRepo(ref));
			}
			
			ref = new RepositoryFileReference(visId, Filename.FILENAME_SMALL_ICON);
			if (Repository.INSTANCE.exists(ref)) {
				this.referencesToPathInCSARMap.put(ref, BackendUtils.getPathInsideRepo(ref));
			}
		}
	}
	
}