aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.topologymodeler/src/main/java/org/eclipse/winery/topologymodeler/addons/topologycompleter/placeholderhandling/PlaceHolderHandler.java
blob: a384e911db5a802dd7b53c62ca2ff83472dbf283 (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
/*******************************************************************************
 * Copyright (c) 2013 Pascal Hirmer.
 * 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:
 *    Pascal Hirmer - initial API and implementation
 *******************************************************************************/

package org.eclipse.winery.topologymodeler.addons.topologycompleter.placeholderhandling;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.winery.model.tosca.TNodeTemplate;
import org.eclipse.winery.model.tosca.TNodeType;
import org.eclipse.winery.topologymodeler.addons.topologycompleter.analyzer.TOSCAAnalyzer;
import org.eclipse.winery.topologymodeler.addons.topologycompleter.helper.Constants;

/**
 * This class finds suitable replacement types for a place holder.
 */
public class PlaceHolderHandler {

	/**
	 * This method returns a suitable {@link TNodeType} to replace a given {@link TNodeTemplate} placeholder.
	 * A suitable Node Type to replace a placeholder is matched by its type. If the type of a NodeType equals the identifier of a placeholder
	 * it can be used to replace it.
	 *
	 * @param nodeTemplate
	 *            the placeholder to be replaced
	 * @param toscaAnalyzer
	 *            the {@link TOSCAAnalyzer} object to access the data model
	 *
	 * @return a list of {@link TNodeType}s to replace the placeholder
	 */
	public static List<TNodeType> getSuitableNodeTypes(TNodeTemplate nodeTemplate, TOSCAAnalyzer toscaAnalyzer) {

		List<TNodeType> suitableNodeTypes = new ArrayList<>();

		// TODO: matching the name without a name space is unsafe and only works assuming that no one creates generic NodeTemplates with the same name as the place holders.
		// However a NodeTemplate name does not have a name space.
		if (nodeTemplate.getName().equals(Constants.PlaceHolders.WEBSERVER.toString())) {
			for (TNodeType nodeType : toscaAnalyzer.getNodeTypes()) {
				if (nodeType.getDerivedFrom() != null && nodeType.getDerivedFrom().getTypeRef().getLocalPart().equals(Constants.PlaceHolders.WEBSERVER.toString())) {
					suitableNodeTypes.add(nodeType);
				}
			}
		} else if (nodeTemplate.getName().equals(Constants.PlaceHolders.DATABASE.toString())) {
			for (TNodeType nodeType : toscaAnalyzer.getNodeTypes()) {
				if (nodeType.getDerivedFrom() != null && nodeType.getDerivedFrom().getTypeRef().getLocalPart().equals(Constants.PlaceHolders.DATABASE.toString())) {
					suitableNodeTypes.add(nodeType);
				}
			}
		} else if (nodeTemplate.getName().equals(Constants.PlaceHolders.OPERATINGSYSTEM.toString())) {
			for (TNodeType nodeType : toscaAnalyzer.getNodeTypes()) {
				if (nodeType.getDerivedFrom() != null && nodeType.getDerivedFrom().getTypeRef().getLocalPart().equals(Constants.PlaceHolders.OPERATINGSYSTEM.toString())) {
					suitableNodeTypes.add(nodeType);
				}
			}
		} else if (nodeTemplate.getName().equals(Constants.PlaceHolders.CLOUDPROVIDER.toString())) {
			for (TNodeType nodeType : toscaAnalyzer.getNodeTypes()) {
				if (nodeType.getDerivedFrom() != null && nodeType.getDerivedFrom().getTypeRef().getLocalPart().equals(Constants.PlaceHolders.CLOUDPROVIDER.toString())) {
					suitableNodeTypes.add(nodeType);
				}
			}
		}

		return suitableNodeTypes;
	}
}