1 /*******************************************************************************
2 * Copyright (c) 2013 Pascal Hirmer.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Pascal Hirmer - initial API and implementation
11 *******************************************************************************/
13 package org.eclipse.winery.topologymodeler.addons.topologycompleter.helper;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.UUID;
19 import javax.xml.namespace.QName;
21 import org.eclipse.winery.model.tosca.TCapabilityDefinition;
22 import org.eclipse.winery.model.tosca.TNodeTemplate;
23 import org.eclipse.winery.model.tosca.TNodeType;
24 import org.eclipse.winery.model.tosca.TRequirement;
25 import org.eclipse.winery.model.tosca.TRequirementType;
26 import org.eclipse.winery.model.tosca.TTopologyTemplate;
27 import org.eclipse.winery.topologymodeler.addons.topologycompleter.analyzer.TOSCAAnalyzer;
28 import org.slf4j.LoggerFactory;
31 * Contains methods to match requirements and capabilities and find elements in the {@link TTopologyTemplate}
35 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Utils.class.getName());
38 * This method searches {@link TNodeType}s in the repository that match a requirement.
41 * the requirement to be matched
42 * @param toscaAnalyzer
43 * the {@link TOSCAAnalyzer} object to access the data model
45 * @return a list of the matched {@link TNodeType}s
47 public static List<TNodeType> matchRequirementAndCapability(TRequirement requirement, TOSCAAnalyzer toscaAnalyzer) {
49 List<TNodeType> possibleNodeTypes = new ArrayList<TNodeType>();
51 // find all matching Node Types for a requirement by the "requiredCapabilityType" attribute of its type
52 for (TRequirementType requirementType : toscaAnalyzer.getRequirementTypes()) {
53 if (requirementType.getName().equals(requirement.getType().getLocalPart())) {
55 QName requiredCapabilityType = requirementType.getRequiredCapabilityType();
56 for (TNodeType nodeType : toscaAnalyzer.getNodeTypes()) {
57 if (nodeType.getCapabilityDefinitions() != null) {
58 for (TCapabilityDefinition cd : nodeType.getCapabilityDefinitions().getCapabilityDefinition()) {
59 if (cd.getCapabilityType().getLocalPart().equals(requiredCapabilityType.getLocalPart())) {
60 possibleNodeTypes.add(nodeType);
68 return possibleNodeTypes;
72 * Generates a random {@link UUID} exclusively used by the {@link TemplateBuilder}.
74 * @return the generated {@link UUID} id
76 public static String createRandomID() {
77 return UUID.randomUUID().toString();
81 * Returns a {@link TNodeTemplate} for a given Id.
83 * @param nodeTemplates
84 * all the {@link TNodeTemplate} in the {@link TTopologyTemplate}
86 * the id of the {@link TNodeTemplate} to be found
88 * @return the found {@link TNodeTemplate} or null if not found
90 public static TNodeTemplate getNodeTemplateForId(List<TNodeTemplate> nodeTemplates, String id) {
92 for (TNodeTemplate nt : nodeTemplates) {
94 if (nt.getId().equals(id)) {
99 logger.error("No NodeTemplate with " + id + " exists");
106 * Returns a {@link TNodeType} for a given Id.
109 * All the {@link TNodeType} in the {@link TTopologyTemplate}
111 * The id of the {@link TNodeType} to be searched
112 * @return the {@link TNodeType} or null if not found
114 public static TNodeType getNodeTypeForId(List<TNodeType> nodeTypes, QName id) {
116 for (TNodeType nodeType : nodeTypes) {
117 if (nodeType.getName().equals(id.getLocalPart()) && nodeType.getTargetNamespace().equals(id.getNamespaceURI())) {
122 logger.error("No NodeType with " + id + " exists");
124 // no type could be found for the given ID, this case cannot occur if the topology was modelled in the Winery Topology Modeler