5f9826b06198ff8687e7fe3c0fded3243aacb917
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / java / org / eclipse / winery / topologymodeler / addons / topologycompleter / helper / Utils.java
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
8  *
9  * Contributors:
10  *    Pascal Hirmer - initial API and implementation
11  *******************************************************************************/
12
13 package org.eclipse.winery.topologymodeler.addons.topologycompleter.helper;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.UUID;
18
19 import javax.xml.namespace.QName;
20
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;
29
30 /**
31  * Contains methods to match requirements and capabilities and find elements in the {@link TTopologyTemplate}
32  */
33 public class Utils {
34
35         private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Utils.class.getName());
36
37         /**
38          * This method searches {@link TNodeType}s in the repository that match a requirement.
39          *
40          * @param requirement
41          *            the requirement to be matched
42          * @param toscaAnalyzer
43          *            the {@link TOSCAAnalyzer} object to access the data model
44          *
45          * @return a list of the matched {@link TNodeType}s
46          */
47         public static List<TNodeType> matchRequirementAndCapability(TRequirement requirement, TOSCAAnalyzer toscaAnalyzer) {
48
49                 List<TNodeType> possibleNodeTypes = new ArrayList<TNodeType>();
50
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())) {
54
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);
61                                                         }
62                                                 }
63                                         }
64                                 }
65                         }
66                 }
67
68                 return possibleNodeTypes;
69         }
70
71         /**
72          * Generates a random {@link UUID} exclusively used by the {@link TemplateBuilder}.
73          *
74          * @return the generated {@link UUID} id
75          */
76         public static String createRandomID() {
77                 return UUID.randomUUID().toString();
78         }
79
80         /**
81          * Returns a {@link TNodeTemplate} for a given Id.
82          *
83          * @param nodeTemplates
84          *            all the {@link TNodeTemplate} in the {@link TTopologyTemplate}
85          * @param id
86          *            the id of the {@link TNodeTemplate} to be found
87          *
88          * @return the found {@link TNodeTemplate} or null if not found
89          */
90         public static TNodeTemplate getNodeTemplateForId(List<TNodeTemplate> nodeTemplates, String id) {
91
92                 for (TNodeTemplate nt : nodeTemplates) {
93
94                         if (nt.getId().equals(id)) {
95                                 return nt;
96                         }
97                 }
98
99                 logger.error("No NodeTemplate with " + id + " exists");
100
101                 return null;
102
103         }
104
105         /**
106          * Returns a {@link TNodeType} for a given Id.
107          *
108          * @param nodeTypes
109          *            All the {@link TNodeType} in the {@link TTopologyTemplate}
110          * @param Id
111          *            The id of the {@link TNodeType} to be searched
112          * @return the {@link TNodeType} or null if not found
113          */
114         public static TNodeType getNodeTypeForId(List<TNodeType> nodeTypes, QName id) {
115
116                 for (TNodeType nodeType : nodeTypes) {
117                         if (nodeType.getName().equals(id.getLocalPart()) && nodeType.getTargetNamespace().equals(id.getNamespaceURI())) {
118                                 return nodeType;
119                         }
120                 }
121
122                 logger.error("No NodeType with " + id + " exists");
123
124                 // no type could be found for the given ID, this case cannot occur if the topology was modelled in the Winery Topology Modeler
125                 return null;
126         }
127 }